5

I am trying to send ETH from one of my account to another with web3 from nodejs:

    web3.eth.personal.unlockAccount(config.mainGethAccount, config.gethPassword, function () {
        console.log("sendingTransaction: " + config.mainGethAccount);
        let obj = {
            from: config.mainGethAccount,
            to: newAccountId,
            value: web3.utils.toWei(.005, 'ether')
        };
        console.log("obj",obj);
        web3.eth.sendTransaction(obj).then(function (receipt) {
            done(newAccountId,config.userAccountGethPassword);
        });
    });

my web3 object is ok, it is able to create new account, but it crashes in .sendTransaction and ETH are actually never transferred:

sendingTransaction: 0x6c92C4201bBaFE97011C9EA9144c907Fd16F5089
[2017-08-01 03:21:46.240] [LOG]   obj { from: '0x6c92C4201bBaFE97011C9EA9144c907Fd16F5089',
  to: '0x2628fCe3675f25F94f06E1F0EE2c782be40fa6c8',
  value: '5000000000000000' }
[2017-08-01 03:22:46.040] [WARN]  Unhandled rejection Error: Transaction was not mined within 50 blocks, please make sure your transaction was properly send. Be aware that it might still be mined!
    at C:\tmp\qqq\node_modules\web3\packages\web3-core-method\src\index.js:362:45
    at tryCatcher (C:\tmp\qqq\node_modules\web3\packages\web3-core-promievent\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (C:\tmp\qqq\node_modules\web3\packages\web3-core-promievent\node_modules\bluebird\js\release\promise.js:497:31)
    at Promise._settlePromise (C:\tmp\qqq\node_modules\web3\packages\web3-core-promievent\node_modules\bluebird\js\release\promise.js:555:18)
    at Promise._settlePromise0 (C:\tmp\qqq\node_modules\web3\packages\web3-core-promievent\node_modules\bluebird\js\release\promise.js:600:10)
    at Promise._settlePromises (C:\tmp\qqq\node_modules\web3\packages\web3-core-promievent\node_modules\bluebird\js\release\promise.js:679:18)
    at Async._drainQueue (C:\tmp\qqq\node_modules\web3\packages\web3-core-promievent\node_modules\bluebird\js\release\async.js:125:16)
    at Async._drainQueues (C:\tmp\qqq\node_modules\web3\packages\web3-core-promievent\node_modules\bluebird\js\release\async.js:135:10)
    at Immediate.Async.drainQueues (C:\tmp\qqq\node_modules\web3\packages\web3-core-promievent\node_modules\bluebird\js\release\async.js:16:14)
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)

The most interesting thing is that it works from time to time. But is it possible to fix it?

  • Are you using mainnet, testnet or private testnet? I'd try increasing the gasPrice per transaction. – Ismael Aug 2 '17 at 2:38
  • I am using testnet, but I'd be thankfull if you give an idea how to do that... – Stepan Yakovenko Aug 3 '17 at 20:55
  • You have to add the gasPrice parameter like this web3.eth.sendTransaction({ from: "0xSenderAddress", to: "0xRecipientAddress", value: "YourValue", gasPrice: gasPrice }) (the docs have each parameter github.com/ethereum/wiki/wiki/…). To get the default value call web3.eth.gasPrice like this var gasPrice = web3.eth.gasPrice;. – Ismael Aug 3 '17 at 22:51
  • 1
    @StepanYakovenko - I am exactly on the same problem. What have you done to fix the issue? Any help would be highly appreciated. Cheers. – Abdul Munim Aug 22 '17 at 19:08
  • @AbdulMunim, I have migrated from the testnet to my local blockchain network and never seen this again. I propose to start a bounty for this question and wait till some exper will give us an idea... – Stepan Yakovenko Aug 22 '17 at 19:37
1

What test-net were you using at this point? If it was the Ropsten test-net around July 31st (when your question was asked), this test-net was under attack and it would not confirm any transactions.

So chances are your code is fine, it was the network. You can try using the Rinkeby or Kovan testnets to confirm this. Also, Ropsten should be working fine now, just note that is has issues from time to time.

  • no, not ropsten, local geth --syncmode 'light'. (chain 3?) trying again with a full gas client once it's fully downloaded/synchronised. – Anona112 Oct 13 '17 at 18:52
2

If it works from time to time, odds are you are setting the gas price too low. If you have the cash or on the testnet, just jack up the gas price and you'll see it fly through. For instance on ropsten, I place the gas price at 300 gWei sometimes just so I can get included instantly

1

Finally solved it: For me the source of this obscure error was that signing key belonged to the receiver! The api does NOT detect if implicit signing_key address!=sender field!

Another problem here was mixing hex and int, and a missing parseInt value: web3.toHex(parseInt(web3.utils.toWei(.005, 'ether')))

Now I still get occasional time outs but after a minute at least they show up in the transaction log.

Worst api ever

1
+50

Please correct me, if this needs to be done manually here, but try also adding the nonce:

let obj = {
            from: config.mainGethAccount,
            to: newAccountId,
            nonce: web3.toHex(web3.eth.getTransactionCount(config.mainGethAccount)),
            value: web3.utils.toWei(.005, 'ether')
        };

EDIT: As the error seemed to persist, here a more thorough explanation on sending raw transactions. I like using promises here, also not using an account stored in the node (as you're then tied to it), but signing it with a private key myself, so i'm flexible to use infura, for example.

var EthTx = require("ethereumjs-tx");
const Web3 = require("web3");

if (typeof web3 !== 'undefined') {
    web3 = new Web3(web3.currentProvider);
} else {
    // Use the provider from the config.
    web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545')); //your node url or infura 

promisedSendRawTransaction = function (serializedTx) {
    return new Promise(function (resolve, reject) {
        web3.eth.sendRawTransaction(serializedTx, function (e, data) {
            if (e !== null) {
                console.log(e);
                reject(e);
            } else {
                resolve(data);
            }
        });
    });
};

                var pKey1 = "yourPrivateKey";
                var pKey1x = new Buffer(pKey1, "hex");

                var rawTx = {
                nonce: web3.toHex(web3.eth.getTransactionCount(addr1)),  
                to: theAddress,
                value: web3.toHex(theValue),  
                gasPrice: web3.toHex(22000000000),  
                gas: web3.toHex(900000),
                gasLimit: web3.toHex(4700000)
                }

                var tx = new EthTx(rawTx);
                tx.sign(pKey1x);
                console.log("This is the raw tx: " + tx);
                var serializedTx = "0x" + tx.serialize().toString('hex');

                console.log("The tx now looks like this " + serializedTx);

                console.log("Now sending the full tx.");

                promisedSendRawTransaction(serializedTx).then(function(data){
                    console.log("Tx ID" + data);
                }
  • same problem as yesterday again. maybe I was too quick giving the bounty reward :( – Anona112 Oct 19 '17 at 21:41
  • please review my edit and try if that helps you. – Nikita Fuchs Oct 20 '17 at 23:52
  • 1
    thx. problem was that signing key address != sender or signing key== receiver – Anona112 Oct 21 '17 at 11:12
-1

Include a try/catch block like this:

 try {
           web3.eth.sendTransaction(obj).then(function (receipt) {
             done(newAccountId,config.userAccountGethPassword);
          });
        } catch (err) {
            if (err.includes('not mined within 50 blocks')) {
                const handle = setInterval(() => {
                    web3.eth.getTransactionReceipt(err.response.transactionHash).then((resp)
=> {
                        if (resp != null && resp.blockNumber > 0) {
                            clearInterval(handle);
                            web3.eth.sendTransaction(obj).then(function (receipt) {
                            done(newAccountId,config.userAccountGethPassword);
                            continue;
                        }
                    });
                },3000);
            } else {
                console.error(err.response);
            }
        }

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.