ERC-20 (EVM example)
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.
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:transferSource files
src/transfer.ts
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.json
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 examples