0
let minABI = [
        // transfer
        {
            "constant": false,
            "inputs": [
                {
                    "name": "_to",
                    "type": "address"
                },
                {
                    "name": "_value",
                    "type": "uint256"
                }
            ],
            "name": "transfer",
            "outputs": [
                {
                    "name": "",
                    "type": "bool"
                }
            ],
            "type": "function"
        }
    ];

    let tokenAddress = "0xe41d2489571d322189246dafa5ebde1f4699f498";
    let toAddress = "to_address";
    let fromAddress = "from_address";
    let privateKey = 'private_key';
    let contract = new web3Node.eth.Contract(minABI, tokenAddress, { from: fromAddress });


    // Decimal
    const decimals = web3Node.utils.toBN(18);

// Amount of token
    const tokenAmount = web3Node.utils.toBN(10000000000);

// Amount as Hex - contract.methods.transfer(toAddress, tokenAmountHex).encodeABI();
    const tokenAmountHex = '0x' + tokenAmount.mul(web3Node.utils.toBN(10).pow(decimals)).toString('hex');


    web3Node.eth.getTransactionCount(fromAddress, (err, txCount) => {
        const txObject = {
            nonce:    web3Node.utils.toHex(txCount),
            to:       tokenAddress,
            value:    "0x0",
            data:     contract.methods.transfer(toAddress, tokenAmountHex).encodeABI(),
            gasLimit: web3Node.utils.toHex(21000),
            gasPrice: web3Node.utils.toHex(web3Node.utils.toWei(10, 'gwei'))
        };

        // Sign the transaction
        const tx = new EthereumTx(txObject, { chain: 'mainnet'});
        tx.sign(privateKey);

        const serializedTx = tx.serialize();
        const raw = '0x' + serializedTx.toString('hex');

        // Broadcast the transaction
        web3Node.eth.sendSignedTransaction(raw, (err, txHash) => {
            if(txHash == null) {
                res.json({'status': 1, 'error': err.message});
            } else {
                res.json({'status': 0, 'hash': txHash});
            }
        });
    })

What is the problem?

Error: UnhandledPromiseRejectionWarning: Error: Please pass numbers as strings or BN objects to avoid precision errors.

Your Answer

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

Browse other questions tagged or ask your own question.