Search
⌃K

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 initial xdc with 0x.
  • 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:
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;
​
/**
* @title XRC20 Token
* @dev This is the a XinFin Network Compatible XRC20 token.
*/
​
contract XRC20Token {
​
}

💵 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:
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;
​
/**
* @title XRC20 Token
* @dev This is the a XinFin Network Compatible XRC20 token.
*/
​
contract XRC20Token {
​
string public name;
string public symbol;
uint8 public decimals;
​
uint256 private _totalSupply;
mapping(address => uint) private balances;
mapping(address => mapping(address => uint)) private allowances;
// To be Continued ...
​
}

💵 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:
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;
​
/**
* @title XRC20 Token
* @dev This is the a XinFin Network Compatible XRC20 token.
*/
​
contract XRC20Token {
​
string public name;
string public symbol;
uint8 public decimals;
​
uint256 private _totalSupply;
mapping(address => uint) private balances;
mapping(address => mapping(address => uint)) private allowances;
// Notice we indexed only the ADDRESSES in Approval and Transfer since it
// would be not practical to filter transactions nor approvals by value.
event Approval(address indexed owner, address indexed spender, uint value);
event Transfer(address indexed from, address indexed to, uint value);
// To be Continued ...
​
}

💵 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:
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;
​
/**
* @title XRC20 Token
* @dev This is the a XinFin Network Compatible XRC20 token.
*/
​
contract XRC20Token {
​
string public name;
string public symbol;
uint8 public decimals;
​
uint256 private _totalSupply;
mapping(address => uint) private balances;
mapping(address => mapping(address => uint)) private allowances;
event Approval(address indexed owner, address indexed spender, uint value);
event Transfer(address indexed from, address indexed to, uint value);
constructor(string memory _name, string memory _symbol, uint8 _decimals, uint256 _initialSupply) {
name = _name;
symbol = _symbol;
decimals = _decimals;
​
_totalSupply += _initialSupply * 10 ** decimals;
balances[msg.sender] = _totalSupply;
emit Transfer(address(0), msg.sender, _totalSupply);
}
​
function totalSupply() public view virtual returns (uint256) {
return _totalSupply;
}
​
function balanceOf(address account) public view virtual returns (uint256) {
return balances[account];
}
​
function allowance(address owner, address spender) public view virtual returns (uint256) {
return allowances[owner][spender];
}
​
function transfer(address recipient, uint amount) external returns (bool) {
balances[msg.sender] -= amount;
balances[recipient] += amount;
emit Transfer(msg.sender, recipient, amount);
return true;
}
​
function approve(address spender, uint amount) external returns (bool) {
allowances[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}
​
function transferFrom(
address sender,
address recipient,
uint amount
) external returns (bool) {
allowances[sender][msg.sender] -= amount;
balances[sender] -= amount;
balances[recipient] += amount;
emit Transfer(sender, recipient, amount);
return true;
}
​
}
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:
  • Open the Solidity Compiler in the left side navigation pane
    .
  • 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.
  • After Successful Compilation, it will show
    alt
    ​
  • 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.
  • Navigate to Deploy and Transactions
    alt
    .
  • 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:
Verify 01
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!
Verify 02
If everything is correctly filled out, your contract page on the block explorer should display a new tab called Contract:
Verify 03
In this page you can read from, write to, or simply read the information tied to your XRC-20 token on the blockchain:
Verify 03

🔍 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.
Verify 04
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:
Verify 05
After clicking on Write, you must confirm the transaction on the XDCPay wallet: (note the Amount is 500000000000000000000.0000)
Verify 05
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.\