How to send ERC20 token using Web3 API?

  • I created some custom token in Ropsten testnet using this guide: https://steemit.com/ethereum/@maxnachamkin/how-to-create-your-own-ethereum-token-in-an-hour-erc20-verified

    I can send it to other accounts using MetaMask, but can't figure out how to do it in node.js using web3, ethereumjs-tx and Web3 JavaScript app API.

    My code at the moment looks like this:

    var count = web3.eth.getTransactionCount("0x26...");
    var abiArray = JSON.parse(fs.readFileSync('mycoin.json', 'utf-8'));
    var contractAddress = "0x8...";
    var contract = web3.eth.contract(abiArray).at(contractAddress);
    var rawTransaction = {
        "from": "0x26...",
        "nonce": web3.toHex(count),
        "gasPrice": "0x04e3b29200",
        "gasLimit": "0x7458",
        "to": contractAddress,
        "value": "0x0",
        "data": contract.transfer("0xCb...", 10, {from: "0x26..."}),
        "chainId": 0x03
    };
    
    var privKey = new Buffer('fc3...', 'hex');
    var tx = new Tx(rawTransaction);
    
    tx.sign(privKey);
    var serializedTx = tx.serialize();
    
    web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
        if (!err)
            console.log(hash);
        else
            console.log(err);
    });
    

    In this case code just stops at contract.transfer("0xCb...", 10, {from: "0x26..."}) part and request is just pending. Couldn't find any guide for doing similar things. Found some code here:

    Web3 sending custom tokens by using the transfer function. Need to set the from account

    And here:

    How to transfer ERC20 tokens using web3js

    But still got stucked, don't know what I'm missing.

    Well looking at the web3 documentation you can try contract.methods. transfer("0xCb...", 10).send({from : XX},function(){dostuff})instead of sending sendRawTransaction.

    You should use `getData` to generate the raw transaction data, like this `"data": contract.transfer.getData("0xCb...", 10, {from: "0x26..."}),`.

    This answer explain how to use `getData` https://ethereum.stackexchange.com/a/12932

    @TomasNavickas were you using web3.js `v0.20.x`? Can you kindly post a complete example as the answer and marked as solved. Thank you.

    @TomasNavickas This works, but it's sending wei (ether) instead of the actual token. Any idea why?

    @Viper Wei (ether) is used to pay transaction fee. Can you provide some transaction example that we could see what input data you are sending?

    What does the Buffer do in this case? Why is the var privKey not just assigned as a string containing the private key?

  • I'm using web3.js version: 0.20.1 in node.js express application. I'm running Parity in Virtualbox Ubuntu machine.

    The correct code looks like following:

    var count = web3.eth.getTransactionCount("0x26...");
    var abiArray = JSON.parse(fs.readFileSync('mycoin.json', 'utf-8'));
    var contractAddress = "0x8...";
    var contract = web3.eth.contract(abiArray).at(contractAddress);
    var rawTransaction = {
        "from": "0x26...",
        "nonce": web3.toHex(count),
        "gasPrice": "0x04e3b29200",
        "gasLimit": "0x7458",
        "to": contractAddress,
        "value": "0x0",
        "data": contract.transfer.getData("0xCb...", 10, {from: "0x26..."}),
        "chainId": 0x03
    };
    
    var privKey = new Buffer('fc3...', 'hex');
    var tx = new Tx(rawTransaction);
    
    tx.sign(privKey);
    var serializedTx = tx.serialize();
    
    web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
        if (!err)
            console.log(hash);
        else
            console.log(err);
    });
    

    Can u please share the tutorial of using web3.js for ether and token transfers

    What does the Buffer do in this case? Why is the var privKey not just assigned as a string containing the private key?

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM