Fantom: ERC-721 collection contract with Truffle and OpenZeppelin
ERC-721 is the non-fungible token (NFT) standard for smart contracts.
In this tutorial, you will:
- Create a simple ERC-721 collection contract that allows anyone to mint new non-fungible tokens in the collection.
- Deploy the contract on the Fantom testnet through a node deployed with Chainstack.
- Interact with the deployed contract.
- See that you can register your collection on an NFT market.
Prerequisites
- Chainstack account to deploy a Fantom node.
- Truffle Suite to create and deploy contracts.
- OpenZeppelin Contracts to use the audited ERC-721 libraries to create your ERC-721 collection contract.
Overview
To get from zero to a deployed ERC-721 contract on the Fantom testnet, do the following:
- With Chainstack, create a public chain project.
- With Chainstack, join the Fantom testnet.
- With Chainstack, access your Fantom node credentials.
- With OpenZeppelin, create an ERC-721 contract.
- With Truffle, compile and deploy the contract through your Fantom node.
Step-by-step
Create a public chain project
See Create a project.
Join the Fantom testnet
Get your Fantom node access and credentials
See View node access and credentials.
Install OpenZeppelin Contracts
Install Truffle Suite
See Truffle Suite: Installation.
Create the contract
-
On your machine, in the contract directory, initialize Truffle:
truffle init
This will generate the Truffle boilerplate structure:
. ├── contracts │ └── .gitkeep ├── migrations │ └── .gitkeep ├── test │ └── .gitkeep └── truffle-config.js
-
Go to the
contracts
directory. In the directory, create your ERC-721 contractFantom721Collection.sol
.//SPDX-License-Identifier: MIT pragma solidity ^0.8; import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; contract Fantom721Collection is ERC721URIStorage { uint256 public tokenCounter; constructor () public ERC721 ("COLLECTION_NAME", "COLLECTION_TICKER"){ tokenCounter = 0; } function createCollectible(string memory tokenURI) public returns (uint256) { uint256 newItemId = tokenCounter; _safeMint(msg.sender, newItemId); _setTokenURI(newItemId, tokenURI); tokenCounter = tokenCounter + 1; return newItemId; } }
The contract implementation is the following:
- The contract uses OpenZeppelin audited ERC-721 contract templates.
- The contract is a mintable collection. Anyone can add a token to the collection through
createCollectible
. - COLLECTION_NAME — any name to give to your collection
- COLLECTION_TICKER — any ticker for your collection
-
Create
2_deploy_contracts.js
in themigrations
directory.module.exports = function(deployer) { var Fantom721Collection = artifacts.require("./Fantom721Collection.sol"); deployer.deploy(Fantom721Collection); };
This will create the contract deployment instructions for Truffle.
Compile and deploy the contract
-
Install
HDWalletProvider
.HDWalletProvider is Truffle's separate npm package used to sign transactions.
Run:
npm install @truffle/hdwallet-provider
-
Edit
truffle-config.js
to add:-
HDWalletProvider
-
Your Fantom node access and credentials
-
Your Fantom account that you will use to deploy the contract.
const HDWalletProvider = require("@truffle/hdwallet-provider"); const private_key = 'YOUR_PRIVATE_KEY'; module.exports = { networks: { testnet: { provider: () => new HDWalletProvider(private_key, "YOUR_CHAINSTACK_ENDPOINT"), network_id: 4002 } }, compilers: { solc: { version: "0.8.9", } } };
where
testnet
— any network name that you will pass to thetruffle migrate --network
command.HDWalletProvider
— Truffle's custom provider to sign transactions.- YOUR_PRIVATE_KEY — the private key of your Fantom account that will deploy the contract. The account must have enough FTM funds to run the deployment. See also Fantom testnet faucet.
- YOUR_CHAINSTACK_ENDPOINT — your Fantom node HTTPS endpoint deployed with Chainstack. See also View node access and credentials and Tools.
network_id
— the network ID of the Fantom network: mainnet is250
, testnet is4002
.solc
— the Solidity compiler version that Truffle must use.
-
-
Run:
truffle migrate --network testnet
This will engage
2_deploy_contracts.js
and deploy the contract to the Fantom testnet as specified intruffle-config.js
.
Interact with the contract
Once your contract is deployed, you can view it online at FTMScan testnet.
For an easy way to interact with your deployed contract, verify it on FTMScan.
Flatten your contract code
Since your ERC-721 contract uses imported OpenZeppelin libraries, you must put all the imports into one .sol
file to make FTMScan be able to verify it.
-
Install Truffle Flattener.
Run:
npm install truffle-flattener
-
Flatten the contract.
In the
contracts
directory, run:npx truffle-flattener Fantom721Collection.sol > FlatFantom721Collection.sol
-
Clean up the licensing information.
The flattened contract will have the same licensing note imported from each of the files. Multiple licensing notes in one file break the FTMScan verification, so you have to leave one licensing note for the entirety of the flattened contract.
The easiest way to clean up is to search for the
SPDX
mentions in the file and remove all of them except for the very first one.
Verify the deployed contract on FTMScan
At this point, you have your flattened and cleaned-up contract ready for the FTMScan verification.
- Go to FTMScan testnet.
- Find your deployed contract. The address of your contract should have been printed by Truffle at the end of the deployment in the
contract address
field. - On the contract page on FTMScan, click Contract > Verify and Publish.
- In Compiler Type, select Solidity (Single file).
- In Compiler Version, select v0.8.9. This is the version this tutorial used to compile the contract.
- In Open Source License Type, select MIT License (MIT).
- Click Continue.
- Keep the Optimization option set to No as Truffle does not use optimization by default.
- Paste the entirety of your flattened
.sol
contract in the Enter the Solidity Contract Code below field. - Click Verify and Publish.
FTMScan will take a few seconds to compile your contract, verify, and publish it.
Interact with the contract
Now that your ERC-721 contract is verified, FTMScan is effectively a front-end instance for your contract.
Mint an NFT in the collection
You can use any account to call the createCollectible
function.
Make sure you have:
- MetaMask installed and unlocked as you will need it to call the contract. See Fantom tooling: MetaMask.
- Testnet FTM on the account to pay for the transaction. See Fantom testnet faucet.
- On FTMScan, on your contract, click Contract.
- Click Write Contract.
- Click Connect to Web3.
- Under createCollectible, in the tokenURI field, provide any string to serve as metadata for this specific NFT. See also OpenZeppelin ERC-721 for a metadata example.
- Click Write.
This will send a transaction to mint in NFT in your contract collection and distribute the token to the account called createCollectible
.
Check the balances
Check the NFT balance of an address:
- On FTMScan, on your contract, click Contract.
- Click Read Contract.
- Scroll to the balanceOf field.
- In the owner (address) field, provide the address of the account you used to deploy the contract.
- Click Query.
Check the number of minted NFTs
- On FTMScan, on your contract, click Contract.
- Click Read Contract.
- Check the tokenCounter field.
Listing on an NFT market
Having gone through the tutorial to understand the basics of creating an NFT collection, you can amend the contract to your needs and deploy it on the Fantom mainnet.
Once deployed, you can list the collection at an NFT marketplace—Artion.
See Artion: Register Collection.
Conclusion
This tutorial guided you through the basics of creating and deploying a contract in the ERC-721 non-fungible token standard.
The contract that you created is a collection that anyone on the Fantom network can interact with to add their tokens to the collection.
When you are ready, you can also deploy your own ERC-721 contract on the Fantom mainnet and list the collection on a Fantom NFT marketplace.
This tutorial uses testnet, however, the exact same instructions and sequence work on the mainnet.
About the author
Updated 9 months ago