ERC20代币批量转账实现

想了解ERC20代币批量转账的同学可以了解一下

attachments-2018-03-9MDlTgOS5aa74ab46b850.png

作者:梁雁明

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


1、编写ERC20代币合约并部署

    详细请查看:http://www.blockchainbrother.com/article/6437

2、编写ERC20代币批量转币合约

    转账合约地址查看:https://ropsten.etherscan.io/address/0xedb15fdcec8830af1fdf1a634cd35a7a1840c00b

3、编写触发合约approve方法

    3.1 npm模块依赖

"devDependencies": {
"async": "^2.4.0",
"bignumber.js": "^4.0.2",
"ethereumjs-testrpc": "^3.0.5",
"random-bytes": "^1.0.0",
"truffle": "^4.1.14",
"web3": "^0.18.4"
},
"dependencies": {
"log4js": "^0.6.38",
"truffle-contract": "^3.0.6",
"truffle-wallet-provider": "git+https://github.com/blockchain-technical-practice/truffle-wallet-provider.git"
}

    3.2 转账实现

var logger = log.getLogger("transfer");

var contract = require("truffle-contract");

var ERC20TokenData = require("../build/MarmotToken.json");
var ERC20Token = contract(ERC20TokenData);

var IONCBatchData = require("../build/MMTBatch.json");
var IONCBatch = contract(IONCBatchData);

var Web3 = require("web3");
var web3 = new Web3();


var WalletProvider = require('truffle-wallet-provider');
var provider;


var ERC20TokenAddress = "0x6147f2e4c6a363bB55b0e95af3E3a8Bec56e068A";
var ERC20TokenInstance;

var IONCBatchAddress = "0xEdB15fDCeC8830af1Fdf1A634CD35a7A1840c00B";
var IONCBatchInstance;

var senderAddress;

async function getERC20Instance() {
ERC20TokenInstance = await
ERC20Token.at(ERC20TokenAddress);
}


async function getIONCBatchInstance() {
IONCBatchInstance = await
IONCBatch.at(IONCBatchAddress);
}

// 授权给IONCBatchAddress IONC的数量
async function approve(amount) {
await getIONCBatchInstance();
await getERC20Instance();

let gasPrice = web3.eth.gasPrice;
console.info("gasPrice: " + gasPrice.toString());

var balance = await ERC20TokenInstance.balanceOf(senderAddress);
var decimal = await ERC20TokenInstance.decimals();

console.info("balanceOf: " + balance / Math.pow(10, decimal));

console.info("senderAddress: " + senderAddress);
console.info("IONCBatchAddress: " + IONCBatchAddress);
await ERC20TokenInstance.approve(IONCBatchAddress, web3.toWei(amount), {
from: senderAddress,
gas: 4463981,
gasPrice: web3.toWei(1,'gwei')
//gasPrice: gasPrice.totring()
});
}

async function batchTransfer(receives, amounts) {
await getIONCBatchInstance();
await getERC20Instance();

//estimate gasPrice
let gasPrice = web3.eth.gasPrice*1.2;
console.info("gasPrice: " + gasPrice.toString())

//estimate gas
let gas = await IONCBatchInstance.batchTransfer.estimateGas(ERC20TokenAddress, receives, amounts, {
from: senderAddress
});
gas=gas * 1.5;
console.info("gas: "+gas);
for (var i =0;ilength;i++ ) {
amounts[i] = web3.toWei(amounts[i]);
}

//5463981
//7805800
await IONCBatchInstance.batchTransfer(ERC20TokenAddress, receives, amounts, {
from: senderAddress,
gas: 4463981,
gasPrice: web3.toWei(1,'gwei')
//gasPrice: gasPrice
}).then(function (tx) {
logger.info(JSON.stringify(tx));
});

}


function init(info) {
web3.setProvider(new Web3.providers.HttpProvider(info.provider));
provider = new WalletProvider(info.privateKey, info.provider);

senderAddress = info.senderAddress;

IONCBatchAddress = info.IONCBatchAddress;
ERC20TokenAddress = info.ERC20TokenAddress;

ERC20Token.setProvider(provider);
IONCBatch.setProvider(provider);
}

exports.init = init;
exports.batchTransfer = batchTransfer;
exports.approve = approve;

    3.3调用方法(先调用approve授权合约转账,再调用batchTransfer方法)

var transfer = require('./scripts/ioncbatch_mainnet_privkey');

transfer.init({
senderAddress: "0x01b09833245bf407dfe129fe8ffa7071bee2a9ac",
privateKey: "",
IONCBatchAddress:"0xEdB15fDCeC8830af1Fdf1A634CD35a7A1840c00B",
ERC20TokenAddress:"0x6147f2e4c6a363bB55b0e95af3E3a8Bec56e068A",
provider: "https://ropsten.infura.io"
});

// 这个方法只需要执行一次,执行完后将这个方法注释
//transfer.approve(0)
//transfer.approve(5);


//这里是 接受地址 最多100个
transfer.batchTransfer(
[
"0xe9d42e59D3f8F8dC207f48a5C4C5C31444Fd7032",
],
[
1,
]);

5、验证转账结果

https://ropsten.etherscan.io/tx/0x873259bc71457a5c46d7191ce292040d84585bfe6487d245d97cbe363626f266


代码详情参见:https://github.com/longduer/ERCTOKEN

本文大部分代码参考于 :吴寿鹤 先生,谢谢大神


文章发布只为分享区块链技术内容,版权归原作者所有,观点仅代表作者本人,绝不代表区块链兄弟赞同其观点或证实其描述。

attachments-2018-02-kL1zBfXx5a7ffd0b78798.jpg

  • 发表于 2018-08-15 11:49
  • 阅读 ( 1028 )
  • 分类:以太坊

你可能感兴趣的文章

相关问题

5 条评论

请先 登录 后评论
不写代码的码农
梁雁明

区块链研究员

21 篇文章

作家榜 »

  1. 社区运营-小以 422 文章
  2. 社区运营-小链 246 文章
  3. 于中阳Mercina-zy 78 文章
  4. 涂晶 71 文章
  5. 兄弟连区块链培训 57 文章
  6. 李晓琼 45 文章
  7. 吴寿鹤 36 文章
  8. John-smith 28 文章