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

Hamid Jolany
  • 113
  • 6

3 Answers3

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?

KNK
  • 679
  • 4
  • 8
  • hi dear friend / thank for your reply / how long i have to wait? one day passed, this is my last transaction https://etherscan.io/address/0x821f83f647c8aadac221a8205d8b2c2f8c1c0571

    as you see the two first transaction resolved as soon as possible and under the 1 hour thank you again

    – Hamid Jolany May 28 '19 at 19:17
  • the transaction hash not even listed in transaction pool at all too – Hamid Jolany May 28 '19 at 19:20
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

Majd TL
  • 3,217
  • 3
  • 17
  • 36
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);
    }
})}
Hamid Jolany
  • 113
  • 6