Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Blockchain by (4.1k points)

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)
    a
t 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'.

1 Answer

0 votes
by (14.4k points)

The data portion in your tx object is not correct. You need to pass the encoded string for your method call. Nut what you are doing is calling the transfer method while 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"

}

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...