How to create your own TRC20 token on Tron
TRON is a decentralized blockchain content delivery platform to allow creators to publish and own their uploaded content. TRON originally existed as an ERC20 token (TRX) operating on Ethereum's blockchain but now exists as its own blockchain. TRON has its own set of token standards called TRC10 and TRC20. In this guide I will walk you through creating your own TRC20 token.
Contents
Download TronLink
- TronLink is a browser based extension for interacting with the TRON blockchain. It is available for chrome based browsers like Google Chrome and Brave. Add the extension to your browser by clicking the 'Add to Chrome' button.
- Once the extension is added, click on it in the upper right hand corner. You will then be walked through some steps to create your wallet. First you are going to create a password then press 'Continue'. Then click on 'Create Wallet' like so:
- Now name your wallet and press 'Continue'. Next copy the mnemonic phrase to a safe place and press 'Continue'. The last step is to confirm the mnemonic phrase by selecting the words in order and press 'Confirm' to finish. You have now created a wallet!
Get test TRX coins
- Click the 'Settings' tab in TronLink and select 'Shasta Testnet' in the 'Node selection' box.
- Next go here and paste in your Tron address and click 'Submit'. 10,000 text TRX will then be deposited to your address.
Install TronBox
We will use TronBox to deploy our smart contract onto Tron's blockchain.
- Paste the following lines of code into your terminal to install tronbox.
npm install -g tronbox
- Make a directory on your computer for tronbox and then enter it
mkdir tron-dev
cd tron-dev
- Initialize TronBox (this can take some time)
tronbox init
Configuring TronBox
We will need to copy your private key from our Tron wallet and place it in a .env file. Go to your TronLink extension and click 'Export' and copy your private key.
Next create a '.env' file and paste in the following code with your private key.
nano .env
export PRIVATE_KEY_SHASTA=your_private_key
- Press 'control x', then 'y', and then 'enter' to save.
Now we will have to change the version compiler in tronbox.js
.
nano tronbox.js
Scroll down to where is says // version: '0.5.4'
and uncomment the line (remove the '//') and enter '0.4.24'. Tronbox only supports the following Solidity versions: 0.4.24, 0.4.25, 0.5.4, 0.5.8, 0.5.10.
- When you are done press 'control x', then 'y', and then 'enter' to save.
Modify our token's code
Smart contracts on Tron are built using Solidity, just like on Ethereum. So we will use a modified version of a ERC20 token code created by TokenFactory to create our TRC20 token.
- Copy the following code into your favorite code editor. I will be using Atom.
pragma solidity ^0.4.24;
contract Token {
/// @return total amount of tokens function totalSupply() constant returns (uint256 supply) {}
/// @param _owner The address from which the balance will be retrieved /// @return The balance function balanceOf(address _owner) constant returns (uint256 balance) {}
/// @notice send `_value` token to `_to` from `msg.sender` /// @param _to The address of the recipient /// @param _value The amount of token to be transferred /// @return Whether the transfer was successful or not function transfer(address _to, uint256 _value) returns (bool success) {}
/// @notice send `_value` token to `_to` from `_from` on the condition it is approved by `_from` /// @param _from The address of the sender /// @param _to The address of the recipient /// @param _value The amount of token to be transferred /// @return Whether the transfer was successful or not function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {}
/// @notice `msg.sender` approves `_addr` to spend `_value` tokens /// @param _spender The address of the account able to transfer the tokens /// @param _value The amount of wei to be approved for transfer /// @return Whether the approval was successful or not function approve(address _spender, uint256 _value) returns (bool success) {}
/// @param _owner The address of the account owning tokens /// @param _spender The address of the account able to transfer the tokens /// @return Amount of remaining tokens allowed to spent function allowance(address _owner, address _spender) constant returns (uint256 remaining) {}
event Transfer(address indexed _from, address indexed _to, uint256 _value); event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}
contract StandardToken is Token {
function transfer(address _to, uint256 _value) returns (bool success) {
if (balances[msg.sender] >= _value && _value > 0) { balances[msg.sender] -= _value; balances[_to] += _value; Transfer(msg.sender, _to, _value); return true; } else { return false; } }
function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {
if (balances[_from] >= _value && allowed[_from][msg.sender] >= _value && _value > 0) { balances[_to] += _value; balances[_from] -= _value; allowed[_from][msg.sender] -= _value; Transfer(_from, _to, _value); return true; } else { return false; } }
function balanceOf(address _owner) constant returns (uint256 balance) { return balances[_owner]; }
function approve(address _spender, uint256 _value) returns (bool success) { allowed[msg.sender][_spender] = _value; Approval(msg.sender, _spender, _value); return true; }
function allowance(address _owner, address _spender) constant returns (uint256 remaining) { return allowed[_owner][_spender]; }
mapping (address => uint256) balances; mapping (address => mapping (address => uint256)) allowed; uint256 public totalSupply;
}
contract CoinWiki is StandardToken {
function () { //if ether is sent to this address, send it back. throw; }
string public name; uint8 public decimals; string public symbol; string public version = 'H1.0';
function CoinWiki( ) { balances[msg.sender] = 160000000000000000000000000; // Give the creator all initial tokens (100000 for example) totalSupply = 160000000000000000000000000; // Update total supply (100000 for example) name = "CoinWiki Token"; // Set the name for display purposes decimals = 18; // Amount of decimals for display purposes symbol = "CWT"; // Set the symbol for display purposes }
// Approves and then calls the receiving contract function approveAndCall(address _spender, uint256 _value, bytes _extraData) returns (bool success) { allowed[msg.sender][_spender] = _value; Approval(msg.sender, _spender, _value);
if(!_spender.call(bytes4(bytes32(sha3("receiveApproval(address,uint256,address,bytes)"))), msg.sender, _value, this, _extraData)) { throw; } return true; }
}
- Change your token's supply on line 99 and 100
- Name your token on line 101
- Set your token's decimal on line 102 (Most tokens have 18 decimal places but you can have any number you like)
- Change your token's symbol on line 103
- Change the contract name on line 85 and the function name on line 97 to whatever you like (I just used my token's name)
Now we are going to add our code to TronBox.
- Enter contracts directory
cd tronbox/contracts
- Make new file for our token and paste in the modified code from your text editor. Name the file like so: <name_of_token>.sol
nano CoinWiki.sol
- Press 'control x', then 'y', and then 'enter' to save your file
Modify Migrations
- Modify '2_deploy_contracts.js' to match your token
cd ..
nano migrations/2_deploy_contracts.js
- Uncommit both lines that have commits and replace all instances of 'MyContract' with the name of your contract
- Press 'control x', then 'y', and then 'enter' to exit and save your file
Compile and Deploy your Token
- Input the following code into your terminal to compile and deploy your smart contract to the Tron testnet.
cd ..
tronbox compile --compile-all
source .env && tronbox migrate --reset --network shasta
- After you migrate your smart contract to the Shasta testnet you will see two variables: base58 and hex. The hex value is your 'contract address'. To see your new token, paste the value into the search bar on Shasta Tronscan.
(base58) TVEfcAw4BaGWMzR8HxRszhLZqHTRgru2rx
(hex) 41d356d6a077e97e3f24fd17978b278d285c360ee0