0

I have one issue, I can't send accurate custom ERC 20 token to an ether address.

Here is my custom token detail.

Balance: 0.000000000000000324 BucksTec

Decimals: 18

I sent 44 tokens to an ether address but it shows 0.000000000000000068 Tokens Transfered.

My question is how can I send accurately tokens, please need your best advice and experience.

Some code:

var web3 = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/xyz'));

module.exports.sendTokensTest = function(req, res){

    var toAddress = req.body.toAddress;
    var coins  = req.body.quantity;
    coins = "0x"+coins;
    var fromAddress  = req.body.fromAddress;
    var privateKey  = req.body.fromAddressPvtKey;

    try{

        var contractAddress = contractConfigTest.contractAddress;
        var contractAbi = contractConfigTest.abiArray;

        //res.send({contractAddress:contractAddress, contractAbi:contractAbi});

        var myContract = new web3.eth.Contract(contractAbi, contractAddress);

        myContract.options.from = fromAddress;

        var transactionObject = {
            from: fromAddress,
            to: contractAddress,
            value: '0x00', 
            data: myContract.methods.transfer(toAddress, coins).encodeABI(),
            chainId: 3
        };

        web3.eth.estimateGas(transactionObject, function(err1, estimateGas){
            transactionObject.gasLimit = estimateGas;

            if(err1){
                res.send({error:true, errorMsg:err1.message, errorFunction:'estimateGas' });
            }

            web3.eth.accounts.signTransaction(transactionObject, privateKey , function(err2, signed){

                if(err2){
                    res.send({error:true, errorMsg:err2.message, errorFunction:'signTransaction'});
                }

                web3.eth.sendSignedTransaction(signed.rawTransaction, function(err3, txHash){

                    if(err3){
                        res.send({error:true, errorMsg:err3.message, errorFunction:'sendSignedTransaction'});
                    }

                    res.send({txHash:txHash});
                });

            });

        });

    }catch(err){
        res.send({error:true, errorMsg:err.message, errorFunction:'sendTokens' });
    }

}

Thanks

2

0x44 is 68 in decimals. Moreover, your token has 18 decimals. This means that to send one unit of a token, you need to send 1 * 10^18. Remember, there are no decimals on ethereum, and everything is handled in the lowest possible units.

Try sending 1000000000000000000 units (no 0x before it, that will convert it to base 16, you want to convert the number to base 16 directly, not just prepend 0x) to send 1 token.

  • thanks, so if I want to send 44 tokens then I need to multiply something like this (44 * 100000000000000000) = 4400000000000000000; ? – Muhammad Shahzad Oct 1 '18 at 9:46
  • Yes, you would need to multiply by n * 10^decimals – Raghav Sood Oct 1 '18 at 10:21
  • When I sent 4 coins then it works but when I tried to send 10 coins it display me this error Error: [number-to-bn] while converting number "1.0E 19" to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported. Given value: "1.0E 19" echo (10 * pow(10, 18)); // 1.0E+19 – Muhammad Shahzad Oct 1 '18 at 11:04
  • Use a bignum lib for JS – Raghav Sood Oct 1 '18 at 11:05
  • do you konw how to convert 1.0E+19 into integer, I'm using php echo (10 * pow(10, 18)); // 1.0E+19 – Muhammad Shahzad Oct 1 '18 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.