简体   繁体   English

无法使用web3.js发送ERC20令牌

[英]Failing to send an ERC20 token using web3.js

I've been struggling to send a token transaction using web3 still after I've read several posts and guides. 在阅读了几篇帖子和指南之后,我一直在努力使用web3发送令牌交易。 I'm using human-standard-token-abi to get the ERC20 abi. 我正在使用human-standard-token-abi来获取ERC20 abi。 I'm just trying to transfer 10 ZRX from one address of mine to another. 我只是想将10个ZRX从我的一个地址转移到另一个地址。

Here's the function that is failing. 这是失败的功能。

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);
    }
}

I'm currently failing at just building the raw transaction. 我目前无法构建原始事务。 Here is the error output. 这是错误输出。

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)

It seems to be rejecting one of the addresses I'm feeding it but I'm not sure which one. 它似乎拒绝了我正在喂它的地址之一,但我不确定是哪一个。 When I log out the tokenAddress, contract.address, and my two addresses they're all defined. 当我注销tokenAddress,contract.address和我的两个地址时,它们都被定义了。 But in the web3 source code I added a print statement to see which address it was saying is invalid and the address it gets is 'undefined'. 但是在web3源代码中,我添加了一个print语句来查看它所说的地址是无效的,它得到的地址是“未定义的”。

Your data portion in the tx object is not correct (there may be other issues as well, but that part stands out). 您在tx对象中的data部分不正确(可能还有其他问题,但该部分突出)。 You need to pass in the encoded string for your method call. 您需要传入方法调用的编码字符串。 You're actually trying to call the transfer method when setting 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"
}

(I also changed size to amount . Not sure where size came from.) (我也改变sizeamount 。不知道从哪里size是从哪里来的。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM