blockchain - 无法使用web3.js发送ERC20 token

标签 blockchain ethereum web3js erc20

在阅读了几篇文章和指南之后,我仍在努力使用web3发送 token 交易。我正在使用人类标准 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源代码中,我添加了一条打印语句,以查看该地址说的是无效地址,并且获取的地址是“ undefined ”。

最佳答案

您在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的来源。)

关于blockchain - 无法使用web3.js发送ERC20 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49948844/

相关文章:

blockchain - Hyperledger Composer 相对于 Fabric 的优势和局限性?

javascript - 尝试验证签名时来自以太的无效签名错误

ethereum - 处理事务 : out of gas 时出现 VM 异常

node.js - 合约地址在控制台中返回为未定义

private-key - 如何从私钥中获取助记词钱包 key (12个单词)?

blockchain - 如何将一系列结构从 web3js 发送到 Solidity 合约?

algorithm - 如何不存储 MAC 地址,但仍保留有关它如何在城市中移动的信息?

ethereum - 公共(public)kovan测试网

ethereum - 获取/猜测未验证合约的外部方法

javascript - 为类似数据结构的区 block 链对象数组排序