无法使用 web3.js 发送 ERC20 代币

问题描述 投票:0回答:2

在阅读了几篇文章和指南后,我仍然在努力使用 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 语句来查看它所说的哪个地址无效,并且它获取的地址是“未定义”。

blockchain ethereum web3js erc20
2个回答
1
投票

您的 tx 对象中的

data
部分不正确(也可能存在其他问题,但该部分很突出)。您需要传入方法调用的编码字符串。您实际上是在设置
transfer
时尝试调用
data
方法。

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
来自哪里。)


0
投票

当我尝试通过硬件钱包发送 erc20 代币时,是否会出现盲签名?

© www.soinside.com 2019 - 2024. All rights reserved.