πŸͺ™Create Token on Chainsquare

Note: Ethereum is undoubtedly the largest smart contract-enabled blockchain around, and hosts a vast ecosystem of DApps (decentralized applications). These dApps and Blockchain projects generally run on Chainsquare based on Ethereum ERC-20 tokens.

What are ERC-20 Tokens?

Chainsquare is compatible with the Ethereum blockchain and dApps, tokens are one of the most essential features. Within the Ethereum ecosystem, ERC-20 Tokens have the ability to represent virtually anything. A few examples are lottery tickets, points on an online platform, skills for a character in a game, fiat currency, etc. This feature is quite powerful and needs to be regulated by a standard, and that’s why the ERC-20 exists.

The ERC-20 standard ensures that tokens have the same identical properties. This further means that tokens share the same type and value, making them interchangeable. As such, ERC-20 are fungible tokens, enabling us to measure the value of a token and compare it to another. But what exactly does this mean for the tokens themselves?

The ERC-20 standard implements an API for tokens with smart contracts. The API provides functionality, enabling developers to set a total token supply, get token balances from accounts, and transfer the tokens from one account to another. However, these are just some of the features of an ERC-20 token.

How to Create an ERC-20 Token

According to the introduction, the way to become a Chainsquare/Ethereum token developer, it’s possible to cut the development time down to a minimum, and make the process effortless. However, before we go into how to develop your own ERC-20 token, there are a few preparations to be made:

  1. MetaMask β€” To set up a MetaMask account - [ Follow this section ]

  2. Set up Chainsquare network β€” [Follow this section]

  3. Get Chainquare ETH β€” Third, we need to acquire tokens. Which tokens to obtain depends on the blockchain we are using to develop our ERC-20 token. We need to do so in order to pay the appropriate fees for deploying the tokens to the blockchain. In this case, we need to get Chiansquare ETH for The Gas's fee.

  4. Head over to the Ethereum Remix IDE and make a new Solidity file, for example - token.sol

5. Insert code below to generate token

pragma solidity ^0.4.24;
 
//Safe Math Interface
 
contract SafeMath {
 
    function safeAdd(uint a, uint b) public pure returns (uint c) {
        c = a + b;
        require(c >= a);
    }
 
    function safeSub(uint a, uint b) public pure returns (uint c) {
        require(b <= a);
        c = a - b;
    }
 
    function safeMul(uint a, uint b) public pure returns (uint c) {
        c = a * b;
        require(a == 0 || c / a == b);
    }
 
    function safeDiv(uint a, uint b) public pure returns (uint c) {
        require(b > 0);
        c = a / b;
    }
}
 
 
//ERC Token Standard #20 Interface
 
contract ERC20Interface {
    function totalSupply() public constant returns (uint);
    function balanceOf(address tokenOwner) public constant returns (uint balance);
    function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
    function transfer(address to, uint tokens) public returns (bool success);
    function approve(address spender, uint tokens) public returns (bool success);
    function transferFrom(address from, address to, uint tokens) public returns (bool success);
 
    event Transfer(address indexed from, address indexed to, uint tokens);
    event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}
 
 
//Contract function to receive approval and execute function in one call
 
contract ApproveAndCallFallBack {
    function receiveApproval(address from, uint256 tokens, address token, bytes data) public;
}
 
//Actual token contract
 
contract QKCToken is ERC20Interface, SafeMath {
    string public symbol;
    string public  name;
    uint8 public decimals;
    uint public _totalSupply;
 
    mapping(address => uint) balances;
    mapping(address => mapping(address => uint)) allowed;
 
    constructor() public {
        symbol = "QKC";
        name = "QuikNode Coin";
        decimals = 2;
        _totalSupply = 100000;
        balances[YOUR_METAMASK_WALLET_ADDRESS] = _totalSupply;
        emit Transfer(address(0), YOUR_METAMASK_WALLET_ADDRESS, _totalSupply);
    }
 
    function totalSupply() public constant returns (uint) {
        return _totalSupply  - balances[address(0)];
    }
 
    function balanceOf(address tokenOwner) public constant returns (uint balance) {
        return balances[tokenOwner];
    }
 
    function transfer(address to, uint tokens) public returns (bool success) {
        balances[msg.sender] = safeSub(balances[msg.sender], tokens);
        balances[to] = safeAdd(balances[to], tokens);
        emit Transfer(msg.sender, to, tokens);
        return true;
    }
 
    function approve(address spender, uint tokens) public returns (bool success) {
        allowed[msg.sender][spender] = tokens;
        emit Approval(msg.sender, spender, tokens);
        return true;
    }
 
    function transferFrom(address from, address to, uint tokens) public returns (bool success) {
        balances[from] = safeSub(balances[from], tokens);
        allowed[from][msg.sender] = safeSub(allowed[from][msg.sender], tokens);
        balances[to] = safeAdd(balances[to], tokens);
        emit Transfer(from, to, tokens);
        return true;
    }
 
    function allowance(address tokenOwner, address spender) public constant returns (uint remaining) {
        return allowed[tokenOwner][spender];
    }
 
    function approveAndCall(address spender, uint tokens, bytes data) public returns (bool success) {
        allowed[msg.sender][spender] = tokens;
        emit Approval(msg.sender, spender, tokens);
        ApproveAndCallFallBack(spender).receiveApproval(msg.sender, tokens, this, data);
        return true;
    }
 
    function () public payable {
        revert();
    }
}

6. That’s it! your token contract is now deployed on Chainsuare ETH! To get the token in Metamask, go to the β€œDeployed Contracts” section in Remix and copy the deployed contract’s address using the copy button near the contract’s name. Open Metamask and click on the Add Token button, select the Custom Token option and paste the contract’s address in the first field. Metamask will fetch the Token Symbol and decimals automatically.

7. Add custom Token by new contract's address.

8. Finally, We get the new Token

Conclusion

Congratulations on successfully creating your very own token/coin on the Chainquare network [Check on Explorer] Read more about the ERC-20 standard here.

Last updated