利用 web3,调用 erc 20 token 合约,连接 infura 节点,进行转账
var Web3 = require("web3");
var Tx = require('ethereumjs-tx');
// 合约地址
var contractAddress = "0x86bd73cfac0205cbd95adb5cca6adbb876fdbe68";
// 创建web3对象
var web3 = new Web3();
// 连接到 ropsten 测试节点
web3.setProvider(new Web3.providers.HttpProvider("https://ropsten.infura.io/"))
// 通过ABI和地址获取已部署的合约对象
var fs = require('fs');
var jsonFile = "contract_abi.json"; // 读取合约 abi 文件
var parsed= JSON.parse(fs.readFileSync(jsonFile));
var abi = parsed.abi;
var coinContract = web3.eth.contract(abi).at(contractAddress);
var fromAddress = "0xe61ec6bbdedd0ab319cf311152fca5f487153481";
var toAddress = "0xed4048f4d627d52edb5f62c4496396a576c0433e";
var count = web3.eth.getTransactionCount(fromAddress);
var gasPrice = web3.eth.gasPrice;
var gasLimit = 90000;
// 查询余额
var tokenBalance = coinContract.balanceOf(fromAddress);
console.log(tokenBalance)
// 转账
var data = coinContract.transfer.getData(toAddress,
1000000000000000000000,
{from: fromAddress});
var rawTransaction = {
"from": fromAddress,
"nonce": web3.toHex(count),
"gasPrice": web3.toHex(gasPrice),
"gasLimit": web3.toHex(gasLimit),
"to": contractAddress,
"value": "0x0",
"data": data,
};
// 读取私钥,这里不包含‘0x’两个字符
var privKey = new Buffer.from('c20b55c9c95916a86361a......', 'hex');
var tx = new Tx(rawTransaction);
// 用私钥签名交易信息
tx.sign(privKey);
var serializedTx = tx.serialize();
// 发送交易
web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'),
function(err, hash) {
if (!err)
console.log(hash);
else
console.log(err);
});
批量转账合约,参考这里的实现,部分源码如下:
pragma solidity ^0.4.21;
.....
contract FlyDropToken is Claimable {
using SafeMath for uint256;
ERC20 public erc20tk = ERC20(0x86bd73cfac0205cbd95adb5cca6adbb876fdbe68); // 修改成你的合约地址
bytes[] internal approveRecords;
event ReceiveApproval(address _from, uint256 _value, address _token, bytes _extraData);
/**
* @dev receive approval from an ERC20 token contract, take a record
*
* @param _from address The address which you want to send tokens from
* @param _value uint256 the amounts of tokens to be sent
* @param _token address the ERC20 token address
* @param _extraData bytes the extra data for the record
*/
function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) public {
// erc20tk = ERC20(_token);
require(erc20tk.transferFrom(_from, this, _value)); // transfer tokens to this contract
approveRecords.push(_extraData);
emit ReceiveApproval(_from, _value, _token, _extraData);
}
/**
* @dev Send tokens to other multi addresses in one function
*
* @param _destAddrs address The addresses which you want to send tokens to
* @param _values uint256 the amounts of tokens to be sent
*/
function multiSend(address[] _destAddrs, uint256[] _values) onlyOwner public returns (uint256) {
require(_destAddrs.length == _values.length);
uint256 i = 0;
for (; i < _destAddrs.length; i = i.add(1)) {
if (!erc20tk.transfer(_destAddrs[i], _values[i])) {
break;
}
}
return (i);
}
function changERC20(address _token) onlyOwner public {
erc20tk = ERC20(_token);
}
/**
* @dev Send tokens to other multi addresses in one function
*
* @param _from address The address which you want to send tokens from
* @param _destAddrs address The addresses which you want to send tokens to
* @param _values uint256 the amounts of tokens to be sent
*/
function multiSendFrom(address _from, address[] _destAddrs, uint256[] _values) onlyOwner public returns (uint256) {
require(_destAddrs.length == _values.length);
uint256 i = 0;
for (; i < _destAddrs.length; i = i.add(1)) {
if (!erc20tk.transferFrom(_from, _destAddrs[i], _values[i])) {
break;
}
}
return (i);
}
}
....
调用 token 合约,授权批量合约转账
contractAddress = '0x35790593c3c92b99a8357a9a64749ca5841d6e51';
data = coinContract.approve.getData(contractAddress,10000000000000000000000,{from: fromAddress});
调用批量转账合约,进行转账
var fromAddress = "0xe61ec6bbdedd0ab319cf311152fca5f487153481"
var toAddressArr = ["0xed4048f4d627d52edb5f62c4496396a576c0433e","0xe97e126d37d79283c6c3377c28ee807dcb2f23f1"]
var amounts = [100000000000000000000, 1000000000000000000000]
var count = web3.eth.getTransactionCount(fromAddress);
var gasPrice = web3.eth.gasPrice;
var gasLimit = 90000;
var data = contract.multiSendFrom.getData(fromAddress,
toAddressArr,
amounts);
var rawTransaction = {
"from": fromAddress,
"nonce": web3.toHex(count),
"gasPrice": web3.toHex(gasPrice),
"gasLimit": web3.toHex(gasLimit),
"to": contractAddress,
"value": "0x0",
"data": data,
};
var privKey = new Buffer.from('c20b55c9c95916a86361a.....', 'hex');
var tx = new Tx(rawTransaction);
tx.sign(privKey);
var serializedTx = tx.serialize();
web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
if (!err)
console.log(hash);
else
console.log(err);
});