- This tutorial shows how to build and deploy a straightforward ERC-721 contract on the Fantom testnet.
- You’ll leverage Truffle and OpenZeppelin to set up and compile the NFT collection, which can be minted by anyone.
- Once deployed, you can verify the contract on FTMScan for easy interactions like minting or querying balances.
- You can eventually take the same approach for Fantom mainnet and list your collection on an NFT marketplace such as Artion.
Main article
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 .
- 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.
- With FTMScan, verify the deployed contract.
Step-by-step
Create a public chain project
See Create a project.Join the Fantom testnet
See Join a public network.Get your Fantom node access and credentials
See View node access and credentials.Install OpenZeppelin Contracts
See OpenZeppelin Contracts.Install Truffle Suite
See Truffle Suite: Installation.Create the contract
-
On your machine, in the contract directory, initialize Truffle:
This will generate the Truffle boilerplate structure:
-
Go to the
contracts
directory. In the directory, create your ERC-721 contractFantom721Collection.sol
.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.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: -
Edit
truffle-config.js
to add:-
HDWalletProvider
- Your Fantom node access and credentials
-
Your Fantom account that you will use to deploy the contract.
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:
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:
-
Flatten the contract.
In the
contracts
directory, run: -
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.
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 thecreateCollectible
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.
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.
- On FTMScan, on your contract, click Contract.
- Click Read Contract.
- Check the tokenCounter field.