4

I've been struggling to send a token transaction using web3 still after I've read several posts and guides. I'm using human-standard-token-abi to get the ERC20 abi. I'm just trying to transfer 10 ZRX from one address of mine to another.

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. 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'.

jawknee530
  • 319
  • 4
  • 12
  • Refer to this [answer](https://stackoverflow.com/a/50019666/6521116) of [Send ERC20 token with web3](https://stackoverflow.com/q/48180941/6521116) – LF00 Apr 25 '18 at 12:59

1 Answers1

1

Your data portion in the tx object is not correct (there may be other issues as well, but that part stands out). You need to pass in the encoded string for your method call. You're actually trying to call the transfer method when setting 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"
}

(I also changed size to amount. Not sure where size came from.)

Adam Kipnis
  • 10,175
  • 10
  • 35
  • 48