ERC-721 (EVM example)
Create and update an NFT collection, and mint, through the precompiles. Reference: ERC-721 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-erc721
# export all required environments
export CALLER_PRIVATE_KEY=
# creates new collection
pnpm call:createCollection
# update collection info
pnpm call:updateCollection
# mint token(s)
pnpm call:mintSource files
src/createCollection.ts
ts
import { NFT_PRECOMPILE_ABI } from "@therootnetwork/evm";
import { filterTransactionEvents } from "@trne/utils/filterTransactionEvents";
import { getNFTContract } from "@trne/utils/getNFTContract";
import { withEthersProvider } from "@trne/utils/withEthersProvider";
import { ContractReceipt, utils } from "ethers";
/**
* Use `nft.initializeCollection` to to create a new ERC-721 compatible collection.
*
* Assumes the caller has XRP to pay for gas.
*/
withEthersProvider("porcini", async (provider, wallet, logger) => {
const nft = getNFTContract().connect(wallet);
const collectionName = utils.hexlify(utils.toUtf8Bytes("MyCollection"));
const maxIssuance = 0; // no max issuance
const collectionOwner = wallet.address;
const baseUri = utils.hexlify(utils.toUtf8Bytes("https://example.com/token/"));
const royaltyAddresses = [wallet.address];
const royaltyEntitlements = [10_000]; // one percent
logger.info(
{
parameters: {
collectionOwner,
collectionName,
maxIssuance,
baseUri,
royaltyAddresses,
royaltyEntitlements,
},
},
`create "initializeCollection" call`
);
logger.info(`dispatch transaction from wallet=${wallet.address}`);
const tx = await nft.initializeCollection(
collectionOwner,
collectionName,
maxIssuance,
baseUri,
royaltyAddresses,
royaltyEntitlements
);
const receipt = (await tx.wait()) as unknown as ContractReceipt;
const [intializeEvent] = filterTransactionEvents(NFT_PRECOMPILE_ABI, receipt.logs, [
"InitializeCollection",
]);
logger.info(
{
result: {
transactionHash: receipt.transactionHash,
blockNumber: receipt.blockNumber,
intializeEvent: {
name: intializeEvent.name,
args: intializeEvent.args,
},
},
},
"receive result"
);
});src/mint.ts
ts
import { ERC721_PRECOMPILE_ABI } from "@therootnetwork/evm";
import { filterTransactionEvents } from "@trne/utils/filterTransactionEvents";
import { getERC721Contract } from "@trne/utils/getERC721Contract";
import { withEthersProvider } from "@trne/utils/withEthersProvider";
import { ContractReceipt } from "ethers";
const COLLECTION_ID = 1124;
/**
* Use `TRN721.mint` call to mint new token(s)
*
* Assumes the caller is the owner of the collection, and has XRP to pay for gas.
*/
withEthersProvider("porcini", async (provider, wallet, logger) => {
const erc721 = getERC721Contract(COLLECTION_ID).connect(wallet);
const tokenOwner = wallet.address;
const quantity = 2;
logger.info(
{
parameters: {
contractAddress: erc721.address,
tokenOwner,
quantity,
},
},
`create "mint" call`
);
logger.info(`dispatch transaction from wallet=${wallet.address}`);
const tx = await erc721.mint(tokenOwner, quantity);
const receipt = (await tx.wait()) as unknown as ContractReceipt;
// 2 tokens minted = 2 transfers event
const [transferEvent1, transferEvent2] = filterTransactionEvents(
ERC721_PRECOMPILE_ABI,
receipt.logs,
["Transfer"]
);
logger.info(
{
result: {
transactionHash: receipt.transactionHash,
blockNumber: receipt.blockNumber,
transferEvent1: {
name: transferEvent1.name,
args: transferEvent1.args,
},
transferEvent2: {
name: transferEvent2.name,
args: transferEvent2.args,
},
},
},
"receive result"
);
});src/updateCollection.ts
ts
import { ERC721_PRECOMPILE_ABI } from "@therootnetwork/evm";
import { filterTransactionEvents } from "@trne/utils/filterTransactionEvents";
import { getERC721Contract } from "@trne/utils/getERC721Contract";
import { withEthersProvider } from "@trne/utils/withEthersProvider";
import { ContractReceipt, utils } from "ethers";
const COLLECTION_ID = 1124;
/**
* Use `TRN721.setBaseURI` call to update the collection `metadataScheme`.
*
* Similar extrinsics are available to update different properties of a collection
* - setMaxSupply
* Check out the precompile documentation (linked in README) for more details
*
* Assumes the caller is the owner of the collection, and has XRP to pay for gas.
*/
withEthersProvider("porcini", async (provider, wallet, logger) => {
const erc721 = getERC721Contract(COLLECTION_ID).connect(wallet);
const baseUri = utils.hexlify(utils.toUtf8Bytes("https://example.com/token/"));
logger.info(
{
parameters: {
contractAddress: erc721.address,
baseUri,
},
},
`create "setBaseURI" call`
);
logger.info(`dispatch transaction from wallet=${wallet.address}`);
const tx = await erc721.setBaseURI(baseUri);
const receipt = (await tx.wait()) as unknown as ContractReceipt;
const [setEvent] = filterTransactionEvents(ERC721_PRECOMPILE_ABI, receipt.logs, [
"BaseURIUpdated",
]);
logger.info(
{
result: {
transactionHash: receipt.transactionHash,
blockNumber: receipt.blockNumber,
setEvent: {
name: setEvent.name,
args: setEvent.args,
},
},
},
"receive result"
);
});package.json
json
{
"scripts": {
"call": "tsx --tsconfig ../../../tsconfig.json",
"call:createCollection": "pnpm call src/createCollection.ts",
"call:updateCollection": "pnpm call src/updateCollection.ts",
"call:mint": "pnpm call src/mint.ts"
}
}Source:
futureversecom/trn-examples, examples/evm/use-erc721 (deleted) · commit 44c9e16, 2024-10-31 · Apache-2.0, license · part of Code examples