以太坊Solidity如何实现海量空投代币?

1. 摘要

通证token项目启动时,短期内繁荣生态,要舍得给粉丝们打币,把利益分出去。本文聚焦在技术层面,实现如何快速完成TOKEN海量空投,既要节约时间,又要节省TOKEN费用。

2.代码分析

话不多说,直接上代码。

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[] memory _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);
        }

        return true;
    }

     function withdrawalToken(address _tokenAddress) onlyOwner public { 
        Token token = Token(_tokenAddress);
        token.transfer(owner, token.balanceOf(this));
    }

}

说明

(1)定义interface Token接口,可以适配任何支持ERC20的代币。 (2)contract Airdropper is Ownable,对应空投合约函数,包含2个核心函数: AirTransfer 空投函数:AirTransfer(address[] memory _recipients, uint _values, address _tokenAddress) 入参分别是接受地址,对应的数量,目标ERC20的合约地址。 withdrawalToken 回收函数:withdrawalToken(address _tokenAddress) 用于把未发送的token回收管理账号

3.测试

测试流程包括3步: (1) 部署空投合约; (2) 转账足够数量的ERC20代币到空投合约地址; (3) 组装空投接受地址和其他入参,执行; 例如:["0xbCBE3593561dF94Ed18c13dA1877db3857bE4402","0xEbcA76f064cd01fDb699051c291559A697812B18","0x5fa20E0A24b72A85630200A58651EB777f8d4097","0x1ed80b85C0ecAafbFCC45249922a1096Dd92460B","0x97dF0DcCd22651c20A0511422C6d995Bf937973A","0xd201D869D0A006901a70a24b91946E19b5649F73","0x219f9C9e87085084f9148B6cFB51A586D7EFDcdb","0x0bd2e92a72d9a7c71c03a3eCEd4B563ACE6405F3","0xab19520a874061f87132128E590822B051bF75b9","0xF7CfAA4201C81A0CbB92424d70Dd14031350c31e"],100000000000000000000,"0x14780cdc560680d2cea42459fe824aad277298f1" (4) 回收未打完的TOKEN回管理账号;

4.启发问题

(1)如何快速组装打币目标地址数组列表呢?

我们拿到的地址一般是XLS表格形式给出的,举例来说1000个,如何快速把这些列表组装成solidity的数组呢?["a","b","c"]样子,组装1000个也挺费时间的,有没有快速的批量方法呢?

(2)如果真的是10万级别的空投地址,如何实现呢?

如果空投地址很大,例如是10万级别的,而以太坊的GAS费是受限的,800万到顶。这时候需要切分成多次打币。建议采用WEB3J调用实现,通过程序把XLS导入目标地址进行切分后再逐步调用程序。

文章转载自:登链社区,若有违权,请联系删除

全部评论(0)
给作者留言