[英]Failing to send an ERC20 token using web3.js
在閱讀了幾篇帖子和指南之后,我一直在努力使用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源代碼中,我添加了一個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
是從哪里來的。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.