2.31 ERC-20 (EVM example) At a glance What it is Transfer an asset through its ERC-20 precompile. Futureverse’s EVM example for The Root Network, with its README and source. Where it sits Part II — Building · Chapter 2.31 Covers Examples · Source files Source github.com/futureversecom/trn-examples/tree/main/examples/evm/use-erc20
| At a glance | |
|---|---|
| What it is | Transfer an asset through its ERC-20 precompile. Futureverse’s EVM example for The Root Network, with its README and source. |
| Where it sits | Part II — Building · Chapter 2.31 |
| Covers | Examples · Source files |
| Source | github.com/futureversecom/trn-examples/tree/main/examples/evm/use-erc20 |
Transfer an asset through its ERC-20 precompile. Reference: ERC-20 precompile.
Copied from Futureverse’s
futureversecom/trn-examples, deleted with its GitHub organisation, at commit44c9e16of 2024-10-31. Apache-2.0: license. Before you run it, read Running the examples.
Offline destinations
Some places this page points to did not answer when last checked:
docs-beta.therootnetwork.com— does not resolve (checked 2026-10-18)stackblitz.com/github/futureversecom— StackBlitz loads these projects from the deleted futureversecom organisation on GitHub (checked 2026-10-18)
Run in StackBlitz (repository deleted, checked 2026-10-18) · Precompile Documentation (offline, checked 2026-10-18)
IMPORTANT
Ensure the following ENV vars are available before running the examples
CALLER_PRIVATE_KEY- Private key of an account that submits the transaction. Follow this guide to create and fund an account with some test tokens on Porcini (testnet) if you don't have one yet.
2.31.1 Examples
bash
# change your working directory to this example first
cd examples/evm/use-erc20
# export all required environments
export CALLER_PRIVATE_KEY=
# creates new collection
pnpm call:transfer2.31.2 Source files
src/transfer.ts2.31.2.1
ts
import { ERC20_PRECOMPILE_ABI } from "@therootnetwork/evm";
import { filterTransactionEvents } from "@trne/utils/filterTransactionEvents";
import { getERC20Contract } from "@trne/utils/getERC20Contract";
import { ASTO_ASSET_ID } from "@trne/utils/porcini-assets";
import { withEthersProvider } from "@trne/utils/withEthersProvider";
import { ContractReceipt } from "ethers";
/**
* Use `ERC20.transfer` call to transfer token.
*
* Assumes the caller has XRP to pay for gas.
*/
withEthersProvider("porcini", async (provider, wallet, logger) => {
const erc20 = getERC20Contract(ASTO_ASSET_ID).connect(wallet);
const target = wallet.address;
const amount = 0.1 * Math.pow(10, 18); // 0.1 ASTO
logger.info(
{
parameters: {
contractAddress: erc20.address,
target,
amount,
},
},
`create "transfer" call`
);
logger.info(`dispatch transaction from wallet=${wallet.address}`);
const tx = await erc20.transfer(target, amount.toString());
const receipt = (await tx.wait()) as unknown as ContractReceipt;
const [transferEvent] = filterTransactionEvents(ERC20_PRECOMPILE_ABI, receipt.logs, ["Transfer"]);
logger.info(
{
result: {
transactionHash: receipt.transactionHash,
blockNumber: receipt.blockNumber,
transferEvent: {
name: transferEvent.name,
args: transferEvent.args,
},
},
},
"receive result"
);
});package.json2.31.2.2
json
{
"scripts": {
"call": "tsx --tsconfig ../../../tsconfig.json",
"call:transfer": "pnpm call src/transfer.ts"
}
}Source:
futureversecom/trn-examples, examples/evm/use-erc20 (deleted) · commit 44c9e16, 2024-10-31 · Apache-2.0, license · part of Code examplesAssociated docs
Where else you might go from here. Following one is optional — this chapter stands without them.
- Assets (ERC-20)
See also— The ERC-20 contract address for The Root Network native tokens uses the following format:- Back matter — read in order
See also— The whole of Back matter as one continuous page, reprinted from its chapters without change.- Code examples
See also— Futureverse’s code examples for The Root Network: scripts for the native API and the EVM, and two contract projects, each with its README and source.- Contents
See also— Every chapter in this book, each with the one line that tells you what you get by reading it.- Front matter — read in order
See also— The whole of Front matter as one continuous page, reprinted from its chapters without change.- Guides for the code examples
See also— How Futureverse’s TRN code examples get a test account: create one in MetaMask and fund it from the Porcini faucet.- Index · A
See also— 609 index terms that start with A, each linked to the pages that define or use it.- Index · B
See also— 302 index terms that start with B, each linked to the pages that define or use it.