在我阅读了几篇帖子和指南之后,我一直在努力使用 web3 发送代币交易。我正在使用人类标准令牌 abi 来获得 ERC20 abi。我只是想将 10 ZRX 从我的一个地址转移到另一个地址。
这是失败的函数。
var Tx = require('ethereumjs-tx');
const abi = require('human-standard-token-abi')
import * as Web3 from 'web3';
const fromAddress = '0xB03...'.toLowerCase();
const secondaryAddress = '0xF75...'.toLowerCase();
const zrxAddress = '0xe41d...';
deposit(zrxAddress, secondaryAddress, '10');
function deposit(tokenAddress:string, depositAddress:string, amount:string) {
var count = web3.eth.getTransactionCount(fromAddress);
var contract = web3.eth.contract(abi).at(tokenAddress);
console.log('Contract Address :' + contract.address);
try {
var rawTransaction = {
"from": fromAddress,
"nonce": web3.toHex(count),
"gasPrice": "0x04e3b29200",
"gasLimit": "0x7458",
"to": contract.address,
"value": "0x0",
"data": contract.transfer(depositAddress, size),
"chainId": "0x01"
}
console.log(rawTransaction);
var privKey = new Buffer(key, 'hex');
var tx = new Tx(rawTransaction);
console.log(tx);
//tx.sign(privKey);
var serializedTx = tx.serialize();
} catch (err) {
console.log('nnfailed to build');
console.log(err);
}
try {
console.log('nnAttempting to send tx');
web3.eth.sendTransaction(tx, function(err, hash) {
if(!err)
console.log(hash);
else
console.log(err);
});
} catch (err) {
console.log('nfailed to send');
console.log(err);
}
}
我目前在构建原始事务方面失败。这是错误输出。
Error: invalid address
at inputAddressFormatter (/home/jall/ZeroExTrading/node_modules/web3/lib/web3/formatters.js:279:11)
at inputTransactionFormatter (/home/jall/ZeroExTrading/node_modules/web3/lib/web3/formatters.js:101:20)
at /home/jall/ZeroExTrading/node_modules/web3/lib/web3/method.js:90:28
at Array.map (<anonymous>)
at Method.formatInput (/home/jall/ZeroExTrading/node_modules/web3/lib/web3/method.js:88:32)
at Method.toPayload (/home/jall/ZeroExTrading/node_modules/web3/lib/web3/method.js:116:23)
at Eth.send [as sendTransaction] (/home/jall/ZeroExTrading/node_modules/web3/lib/web3/method.js:141:30)
at SolidityFunction.sendTransaction (/home/jall/ZeroExTrading/node_modules/web3/lib/web3/function.js:170:26)
at SolidityFunction.execute (/home/jall/ZeroExTrading/node_modules/web3/lib/web3/function.js:256:37)
at deposit (/home/jall/ZeroExTrading/lib/Transfer.js:56:30)
它似乎拒绝了我正在输入的地址之一,但我不确定是哪一个。当我注销tokenAddress,contract.address和我的两个地址时,它们都被定义了。但是在 web3 源代码中,我添加了一个 print 语句,以查看它所说的地址无效以及它获得的地址是"未定义"的。
tx 对象中的data
部分不正确(可能还有其他问题,但该部分很突出(。您需要为方法调用传入编码字符串。您实际上是在尝试在设置 data
时调用 transfer
方法。
var rawTransaction = {
"from": fromAddress,
"nonce": web3.toHex(count),
"gasPrice": "0x04e3b29200",
"gasLimit": "0x7458",
"to": contract.address,
"value": "0x0",
"data": contract.transfer.getData(depositAddress, amount),
"chainId": "0x01"
}
(我也把size
改成了amount
。不知道size
是从哪里来的。