0

as you see in below, i the raw transaction has been set by the gas simplify value, the returned value is an implemented transaction hashcode, but never assumed result by this transaction figure out

function sendTransaction(_privateKey,_from,_to,_value) 
{
    return new Promise(function(resolve,reject){
    try {
        web3.eth.getBlock("latest", false, (error, result) => {
            var _gasLimit = result.gasLimit;

            web3.eth.getGasPrice(function(error,result){ 
                var _gasPrice = result;

                const Tx = require('ethereumjs-tx');
                const privateKey = Buffer.from(_privateKey, 'hex')

                var _hex_gasLimit = web3.utils.toHex(_gasLimit.toString());
                var _hex_gasPrice = web3.utils.toHex(_gasPrice.toString());
                var _hex_value = web3.utils.toHex(web3.utils.toWei(_value,'ether'));
                var _trx_count = web3.eth.getTransactionCount(_from);
                var _hex_Gas = web3.utils.toHex('50000');
                const rawTx = {
                    nonce: web3.utils.toHex(web3.eth.getTransactionCount(_from)),
                    to: _to,
                    from:_from,
                    gasLimit:_hex_gasLimit,
                    gas:50000,
                    gasPrice:_hex_gasPrice,

                    value: _hex_value,
                    data: '0x00'
                } 

                const tx = new Tx(rawTx);
                tx.sign(privateKey);

                var serializedTx = '0x'+tx.serialize().toString('hex');  
                web3.eth.sendSignedTransaction(serializedTx.toString('hex'),function(err,hash){
                    if(err)
                    { 
                        resolve(err);
                    }
                    else
                    {
                        resolve('Txn Sent and hash is '+hash);

                    }
                }); 
            });
        });
    } catch (error) {
        resolve(error);
    }
})}

here is a returned transaction hash code

0xa1338065afcfa20e4062a25892d79ad7137af2b631fc15c4a37977938562396c

0

var _trx_count = web3.eth.getTransactionCount(_from); this is async and you should wait for the result or you will get a not valid nonce and your transaction will not be mined

  • it's worked for me / thank you dude. – Hamid Jolany May 29 at 18:07
  • you are welcome :), mark my answer or yours as solved, in case someone has the same problem – Majd TL May 30 at 10:01
0

The transaction is submitted and you'we got it's hash, but you need to wait for the transaction being processed - What's the proper way to wait for a transaction to be mined and get the results?

0

the final working code in below/ hope to enjoy it ;)

async function sendTransaction(_privateKey,_from,_to,_value){
return new Promise(function(resolve,reject){
    try {
        web3.eth.getBlock("latest", false, (error, result) => {
            console.log('gas limit: '+result.gasLimit)
            var _gasLimit = result.gasLimit;

            web3.eth.getGasPrice(function(error,result){ 
                console.log('gas price: '+result);
                var _gasPrice = result;

                const Tx = require('ethereumjs-tx');
                const privateKey = Buffer.from(_privateKey, 'hex')

                var _hex_gasLimit = web3.utils.toHex(_gasLimit.toString());
                var _hex_gasPrice = web3.utils.toHex(_gasPrice.toString());
                var _hex_value = web3.utils.toHex(web3.utils.toWei(_value,'ether'));
                //var _trx_count = web3.eth.getTransactionCount(_from);
                var _hex_Gas = web3.utils.toHex('50000'); //web3.utils.toWei('1', 'Gwei')

                console.log('------------------------------------------');
                web3.eth.getTransactionCount(_from).then(
                    nonce=>{

                        var _hex_nonce = web3.utils.toHex(nonce);
                        const rawTx = {
                            nonce: _hex_nonce,
                            to: _to,
                            from:_from,
                            gasLimit:_hex_gasLimit,
                            gas:_hex_Gas,
                            gasPrice:_hex_gasPrice,
                            value: _hex_value,
                            data: '0x00'
                        } 

                        const tx = new Tx(rawTx);
                        tx.sign(privateKey);

                        var serializedTx = '0x'+tx.serialize().toString('hex');  
                        web3.eth.sendSignedTransaction(serializedTx.toString('hex'),function(err,hash){
                            if(err)
                            { 
                                resolve(err);
                                console.log('------------------------------------------');
                            }
                            else
                            {
                                resolve('Txn Sent and hash is '+hash);
                                console.log('------------------------------------------');
                            }
                        }); 
                })
            });
        });
    } catch (error) {
        resolve(error);
    }
})}

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.