How To Create and Deploy an XRC20 Token Using Remix
Use Remix IDE to deploy an XRC20 Token.
🧭 Table of contents
📰 Overview
Remix IDE is a blockchain development environment, which you can use to create and test smart contracts by leveraging an Ethereum Virtual Machine.
What you will learn
In this tutorial, you will learn how to set up Remix IDE and use it to build, test, and deploy a XRC20 Token on both the XDC Network mainnet and XDC Apothem testnet.
What you will do
Setup Remix IDE
Create an XRC20 token
Compile the XRC20 token
Deploy the XRC20 token
Interact with the XRC20 token
Check the deployment status on xinfin.network
📰 About XRC20 Tokens
XRC20 is a set of rules to standardize assets on the XinFin network. Every XRC20 Token must be able to execute the following methods:
totalSupply()
balanceOf(address account)
allowance(address owner, address spender)
transfer(address recipient, uint amount)
approve(address spender, uint amount)
transferFrom(address sender, address recipient, uint amount)
These are the minimum required methods that allow an asset on the XDC network to be called an XRC20 token. Also, an XRC20 token must be able to emit the following Events
on the blockchain:
Approval(address indexed tokenOwner, address indexed spender, uint tokens)
Transfer(address indexed from, address indexed to, uint tokens)
Events come in handy in the exhaustive labor of indexing state changes, and they are essential to off-chain applications to find relevant data on the blockchain. By mapping all Transfer
events, for example, we can fetch all the historic data on token transfers more easily.
Last but not least, a few contract constants that are public that are also very important to have are:
name
symbol
decimals
Without these public constants, it would be impossible to label tokens on block explorers, for example. In this tutorial, we will deploy a XRC20 token that have all the Methods
, Events
and Constants
mentioned above.
🚀 Setting up the development environment
Remix is an online Solidity IDE for compiling and deploying Solidity code to EVM compatible blockchains. To begin working on a new smart contract, you must first create a new file in the contracts folder on the left side of the view pane.
⚒️ Creating XDCPay Wallet for signing transactions
In order to get started deploying new contracts on the XDC Mainnet and/or Apothem, you need to have and XDCPay wallet to sign your transactions and store XDC tokens.
First we have to install the chrome extension of XDCPay.
Open the Chrome extension after it has been successfully installed.
Agree to the Terms of Service.
Create a new XDCPay wallet by setting up a strong password or use an existing Seed Phrase
12 or 24-Word Mnemonic Phrase
to recover your existing wallet here.
Keep the seed phrase safe. 🚨 Do not share the seed phrase with anyone or you can risk losing your assets and/or the ownership of your smart contracts! 🚨
Verify recovery phrase
Your XDCPay wallet has been successfully created.
⚒ Adding Testnet XDC to Development Wallet
Initially, your account will be empty, and you'll need some XDC tokens to initiate blockchain transactions. You will use a faucet to fill our wallet with test XDC tokens for this. These tokens are worthless in and of themselves. They are simply used to test your contracts on the testnet in order to avoid losing your real assets.
First, make a copy of your wallet address. Your wallet address would look like
xdc057ac7de8ad6f21c7bb0dcc6a389ff9161b3b943
. These account address are interchangeable with Ethereum network. We can access these accounts on Ethereum network by simply changing the initialxdc
with0x
.
Next, navigate to the XDC faucet.
Enter your XDC account address and request for Test XDC here.
If your request is approved, you will be credited with the XDC in your wallet.
If you can't see the XDC in your wallet, make sure you're connected to the XDC Apothem Testnet or the XDC Mainnet.
If you are currently connected to the XDC Mainnet, switch to the XDC Apothem Testnet.
💵 Writing our first XRC20 Token
The source code for the XRC20 token used in this tutorial is available here: XRC20 Contract Folder. But we will address all Events
, Methods
and Constants
mentioned in the section 📰 About XRC20 Tokens.
Lets start by creating the XRC20.sol
file.
And write the shell of our smart contract by writing:
💵 Constants
Inside your contract XRC20Token
, you will instantiate name
, symbol
and decimals
as public variables, and a private _totalSupply
that will be used on our totalSupply()
method later on. You'll also instantiate two mapping variables, balances
and allowances
, that are key/value variables for mapping user balances and approved spending allowances to other users:
💵 Events
As mentioned in 📰 About XRC20 Tokens, events are very important part of a smart contract logic. Events have indexed
variables that can be filtered by off-chain interfaces. You might be tempted to index all the variables that are tied to an on-chain event, but since Solidity has a maximum of 3 indexed variable limitation for events. You will write both Approval
and Transfer
events:
💵 Methods
We need to create the six methods mentioned in 📰 About XRC20 Tokens (totalSupply
, balanceOf
, allowance
, transfer
, approve
and transferFrom
) and a constructor
, which is a function called only once when the contract is deployed. It can attach information such as the token name, decimals and/or initial token supply:
Now you have implemented everything needed to make your token compliant with the XRC20 standard. Of course, there are more features we can implement to this contract, such as the SafeMath library that replace naive mathematical operations for methods that will avoid underflows
and overflows
, and supply management methods such as mint
and burn
.
💵 Compiling and Deploying
Next, try compiling the XRC20.sol
contract:
From the compiler option, select the compiler version
v0.8.16
.Choose Language as
Solidity
.Set the EVM version as the
compiler default
.Next, select
Compile XRC20.sol
.
Once your contract has been compiled, you can deploy it to the Apothem Test Network.
For deployment on the XDC Apothem Testnet. In either case, you need to have enough funds to pay for gas fees on the address that is being used for development.
Choose Injected Web3 as the Environment.
Confirm the popup to add the account to Remix IDE now.
Next, choose the account to which you want to deploy the contract.
Choose the contract you want to use.
Add Initial values regarding your token.
Press the "transact" button on the bottom right and a popup will appear, which we must confirm in order to create the transaction for contract deployment.
🔍 Verifying Contracts on the Block Explorer
Once you have successfully deployed your smart contract to the blockchain, it might be interesting to verify you contract on XinFin Block Explorer.
First lets check the address our contract is deployed. Go to your wallet and get the most recent transaction details, then copy the transaction address.
Next, navigate to the XDC Block explorer and paste the transaction hash there.
From there, we need to get the transaction details as well as the To Address
where the contract is deployed.
Once you have a XRC20
contract deployed on XDC Apothem Testnet, you can search for our newly deployed contract on Apothem Block Explorer:
Next, click in the Verify And Publish
option.
You will be redirected to the Contract verification page where we need to fill out:
Contract Name: XRC20Token
Compiler: Check your Solidity file for Compiler Version
Contract Code: Just paste everything from your
XRC20.sol
file
Once everything is filled out, press Submit!
If everything is correctly filled out, your contract page on the block explorer should display a new tab called Contract
:
In this page you can read from, write to, or simply read the information tied to your XRC-20 token on the blockchain:
🔍 Interacting with your contract on the Block Explorer
With your XDCPay wallet, it is possible to interact with verified smart contracts on the XinFin Network Block Explorer. You can read from, write to, or simply read the information tied to your Smart Contract on the blockchain.
Now head to the Contract
tab on the explorer, choose Write Contract
, and click in Connect to Web3
to connect your XDCPay wallet.
Try transfering 500 MTK
tokens that we have just created to a new wallet xdc0431d52fe37f3839895018272dfa3ba189fce07e
. Lets fill out the recipient
field with the new wallet address, and fill out the amout
field with 500 * 10^18
. Remember that our token has 18 decimals, and when we write numbers with decimals to the blockchain we have to account for the decimals as the Virtual Machine do not understand floating numbers like we humans do:
After clicking on Write
, you must confirm the transaction on the XDCPay wallet: (note the Amount is 500000000000000000000.0000)
You can check your successful transaction on the Block Explorer!
For more information about Remix IDE, Please Visit Remix IDE Documentation. For more information about XinFin Network, Please Visit XDC Network Documentation on GitBook.\
Last updated