ERC20 token batch transfer implementation and analysis

The principle of bulk transfer

We’ll look at three ways to implement airdrops, the first two of which involve redeploying an airdrop contract, and the third of which involves writing the airdrop code before the token code is deployed

There are a number of tokens that are already deployed on Ethereum and cannot be changed once they are live, so a new airdrop contract must be deployed for these tokens to be airdropped. Transfer money using airdrop contracts. This approach mainly uses two functions from the ERC20 standard

Approve: The caller of this function authorizes the given address to withdraw funds from its address. TransferFrom: This function allows the smart contract to automate the transfer process and send a given number of tokens on behalf of the owner.Copy the code

The first way

We can use the approve function to grant permission to the address of the airdrop contract, allowing it to invoke the tokens in an address, and then loop through the transferFrom in the airdrop contract to make bulk transfers

Start with a simple airdrop contract code

Pragma solidity ^ 0.4.0; contract Airdrop {function batch(address tokenAddr, address []toAddr, uint256 []value) returns (bool){

        require(toAddr.length == value.length && toAddr.length >= 1);

        bytes4 fID= bytes4(keccak256("transferFrom(address,address,uint256)"));

        for(uint256 i = 0 ; i < toAddr.length; i++){

            if(! tokenAddr.call(fID, msg.sender, toAddr[i], value[i])) { revert(); }}}}Copy the code

The Batch function is defined for airdrop, which accepts three parameters:

  1. Contract address
  2. List of accepted addresses
  3. A list of airdropped values that correspond to a list of addresses

You can see bulk transfers using the transferFrom function in the for loop

Follow the steps of the previous deployment contract and enter airdrop on the command line

If you see the image above, you have successfully deployed!

Next, the contract is used for airdrop. First, approve is used to grant certain authority to the airdrop contract

For a brief explanation of the above steps, first let’s check the number of airdrop contract addresses that can be called is 0

An amount of address tokens can be mobilized

Unlock the address, grant permission with Approve, and continue to look after mining. It is found that 20 tokens in the address eth. Accounts [0] can be called in the airdrop contract

With clearance, we’ll start the airdrop!

 airdrop.batch.sendTransaction('0x6cbde372b5d3ceeee74fd56a6681eea2c3a4e94c'['0xd1d4e4a5ea685295b22d9fbe68b0cc6a8736ecf9'.'0xa90cf1c04ad3e96f081b599db2a7c12251ada066'.'0xc7e452aa3230d3699852687f7deb3c160d6ebab0'.'0x3e9197eb3faa278d455a35d4e9f0bc529dd5732f'.'0xb2dd757d40ea4b10df06e9c91e62109b82e0420b',2,3,2,1], [1] and {gas: 300000})Copy the code

Some people may find that the above command does not execute batch method directly, but calls sendTransactoin. This is because the default gas in my command line is very low, but this transaction has a lot of data, so it will fail because gas is used up. Batch is not called directly for the final parameter GAS :300000 to set gas a little higher. Of course, you can also call batch method directly, but do not end with {gas:300000}.

You can see from above that the bulk transfer has been completed

The second way

The first method requires us to grant certain permissions to the contract address using Approve, but if we treat the contract address as an account address and transfer some tokens to it, then the airdrop contract can be transferred using the coins in our address.

Start by writing contract code that does this

Pragma solidity ^ 0.4.18; contract Ownable { address public owner;function Ownable() public {
    owner = msg.sender;
  }

  modifier onlyOwner() {
    require(msg.sender == owner);
    _;
  }
}

interface Token {
  function balanceOf(address _owner) public constant returns (uint256 );
  function transfer(address _to, uint256 _value) public ;
  event Transfer(address indexed _from, address indexed _to, uint256 _value);
}

contract Airdropper is Ownable {
    
    function AirTransfer(address[] _recipients, uint[] _values, address _tokenAddress) onlyOwner public returns (bool) {
        require(_recipients.length > 0);

        Token token = Token(_tokenAddress);
        
        for(uint j = 0; j < _recipients.length; j++){
            token.transfer(_recipients[j], _values[j]);
        }
 
        return true; }}Copy the code

It can be seen from the code that we use the token in the contract address for transaction. We deploy the token according to the previous steps

After the successful deployment, transfer some tokens to the airdrop contract, and then call the method of airdrop contract for airdrop. The implementation steps are basically the same as the first way, which will not be repeated. See the following figure for details

The third way

The first two ways are to redeploy a contract outside the contract for airdrop, and the third way is to achieve airdrop in the contract, the idea of implementation is actually the same as the previous method, but is integrated into the contract

Add the following code to the contract

function batch(address []toAddr, uint256 []value) returns (bool){
    require(toAddr.length == value.length && toAddr.length >= 1);
    for(uint256 i = 0 ; i < toAddr.length; i++){ transfer(toAddr[i], value[i]); }}Copy the code

Follow the previous steps to deploy to the GETH private chain and view the contract

It can be found that the current contract already has the Batch method, and then it can be traded

The three ways of air-dropping the contract are realized, and the principle is very simple