How to build an ERC20 Token on Rinkeby/Mainnet using Metamask, Truffle?
One of the most powerful properties of ethereum blockchain is smart contracts that give the ability to build decentralised applications on the blockchain network, in this article i will explain how to setup a RC20 smart contract on ethereum.
Ethereum blockchain is based on the use of tokens, which can be bought, sold, or traded. In this case, “tokens” represent a diverse range of digital assets or values. In this way, tokens are essentially smart contracts that make use of the Ethereum blockchain.
In order the grantee the interoperability of the tokens many stands should for this reason. a standars is made called RC-20, it defines a common list of rules that all Ethereum tokens must adhere to. Consequently, this particular token empowers developers of all types to accurately predict how new tokens will function within the larger Ethereum system.
Let’s start by create a new project with truffle,
Create new folder
mkdir rc2-token & truffle init
Install openzeppelin-solidity as below:
npm install @openzeppelin-solidity
Once done create new contract (token.sol)under contracts folder:
pragma solidity >=0.4.21 <0.7.0;import “openzeppelin-solidity/contracts/token/ERC20/ERC20.sol”;contract TOKEN_TX is ERC20{// modify token namestring public constant NAME = “TOKEN2020”;// modify token symbolstring public constant SYMBOL = “TKT”;// modify token decimalsuint8 public constant DECIMALS = 18;// modify initial token supplyuint256 public constant INITIAL_SUPPLY = 100000 * (10 ** uint256(DECIMALS)); // 100000 tokensconstructor () ERC20(NAME, SYMBOL) public{_mint(msg.sender, INITIAL_SUPPLY);}}
Install Metamsk extention on your browser, create a new wallet or import yours if you have it already.
For rinkeby you can get free ethers for test through rinkeby faucet.
For mainnet you need to buy ethers for exchange platefrom like coinbase and withdraw them to your metamask wallet address.
now let go back to our project
To deploy you token create under migration folder 2_migrate_token.js
const TOKEN_TX= artifacts.require(“TOKEN_TX”);module.exports = function(deployer) {deployer.deploy(TOKEN_TX);};
Then edit truffle_config.js:
1- edit your mnemonic
Import your menemonic from metamask and add it here
const mnemonic = ‘your mnemonic’;
2- deploy on rinkeby
Get infura key :
rinkeby: {
provider: () => new HDWalletProvider(mnemonic, `https://rinkeby.infura.io/v3/your_infura_key`),network_id: 4}
then run:
tuffle migrate --network rinkeby
2- deploy on mainnet
Get infura key :
Note: gas price not fixe you should consult the last gas price here
mainnet: {provider: () => new HDWalletProvider(mnemonic, `https://mainnet.infura.io/v3/your_infura_key`),network_id: 1,
gas: 5500000,gasPrice: 40000000000, // check https://ethgasstation.info/},
Then run:
tuffle migrate --network mainnet
Note: in the mainnet deployment you may get an issue like:
transaction was not mined within 750 seconds, please make sur transactions wa properly sent
It’s a problem with the EVM, check Etherscan and type your used account address to deploy the contract, you will find that your contract is successfully deployed.