在阅读了几篇文章和指南之后,我一直在努力使用 web3 发送令牌交易。我正在使用 human-standard-token-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('\n\nfailed to build');
console.log(err);
}
try {
console.log('\n\nAttempting 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 源代码中,我添加了一条打印语句,以查看它所说的地址是无效的,而它获得的地址是“未定义的”。