How to do it…

  1. Every ERC20 contract will have the same ABI structure, which is predefined by the standard. Create an application that makes use of this ABI to support all the tokens created using the standard.

  2. A basic ERC20 token specification will have the following functions:
    • totalSupply(): Returns uint
    • balanceOf(address tokenOwner): Returns uint balance
    • allowance(address tokenOwner, address spender): Returns remaining uint
    • transfer(address to, uint tokens): Returns bool success
    • approve(address spender, uint tokens): Returns bool success
    • transferFrom(address from, address to, uint tokens)Returns bool success
  3. From your application, create an instance using the ABI and address of the token contract. This will allow you to interact with the token contract:
var abi = [...]; // Contract interface
var address = "0x0..."; // Deployed ERC20 contract address

var tokenContract = new web3.eth.Contract(abi, address);
Since all ERC2o contracts follow the same function layout, you don't have to change the ABI to support multiple tokens. Make the address variable dynamic so that it is easy to switch between multiple tokens implemented using the ERC2o standard.
  1. The contract instance will have all the functions related to your token contract and use them to interact with your application.

  2. For example, use the getBalance function to retrieve the balance of a particular account:
var userAddress = "0x0..."; // Address of the user

tokenContract.methods.getBalance(userAddress)
.call()
.then(function(result) {
console.log(result);
})
  1. Invoke state-changing methods, such as transfer, using a transaction. Make sure to sign the transaction using the sender's address:
var fromAddress = "0x1.."; // Address of the sender
var toAddress = "0x2.."; // Address of the receiver
var amount = 10; // Amount to transfer

tokenContract.methods.transder(toAddress, amount)
.send({
from: fromAddress;
})
.then(function(receipt){
console.log(receipt);
});
  1. Follow a similar structure for other functions in the ERC20 standard. An application that implements all these functions will support every ERC20 token. 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.17.128