0

I have already scrolled through similar questions, but the solutions provided didn't work.

As per title, I get the error insufficient funds for gas * price + value

BACKGROUND:

I am creating a webapp for a smart contract token. Once a user signs up, a new wallet is created for him in geth keystore using web3.personal.newAccount() As the newly generated address is retrieved, the miner sends 777 ETHER to this new adddress with web3.eth.sendTransaction() in order to be able to execute transactions. Afterwards, this new address sends a transaction to the smart contract contractDeployed.createWallet(). This is the transaction where sometimes I get the error insufficient funds for gas * price + value. It doesn't always happen, but 20% of the time is quite noticeable.

I can't get my head around it as I have set on the miner --gasprice "1" and all transactions are executed with predefined gasLimit and gasPrice (as shown in the attached code).

I have tried to nest the web3js calls with callbacks and with batch, no changes. I think I am waiting for the confirmation of the previous transaction before sending the new one.

Notes: I am using web3 0.20.6

CODE:

// Initialization Skipped

const bank = "0x49b36fa1772bdb4d9249f43179832ccdb3732ffc";
const defaultGasPrice = web3.toHex(4);
const defaultGasLimit = 3700000;
const topUpWei = 777;

// Sign up
router.post('/api/v1/address', function(req, res){
  const data = { name: req.body.name.replace(" ", ""), passphrase: req.body.passphrase };

  let address;

  return Promise.resolve()
    .then(function() {
      address = web3.personal.newAccount(data.passphrase);
    })
    .then(function() {
      return web3.personal.unlockAccount(bank, "");
    })
    .then(function(ret) {
      if (ret) {
        return web3.eth.sendTransaction({ from: bank, to: address, value: topUpWei, gas: defaultGasLimit, gasPrice: defaultGasPrice });
      }
      throw ({ message: 'unable to decrypt mint wallet' });
    })
    .then(function(ret) {
      if (ret) {
        return web3.personal.unlockAccount(address, data.passphrase);
      }
      throw ({ message: 'unable to send initial Ethers' });
    })
    .then(function(ret) {
      if (ret) {
        return contractDeployed.createWallet(data.name, 2, { from: address, gas: defaultGasLimit, gasPrice: defaultGasPrice });
      }
      throw ({ message: 'unable to decrypt ' + data.name + ' wallet' });
    })
    .then(function(ret) {
      if (!ret) {
        throw ({ message: 'unable to create wallet ' + data.name });
      }
    })
    .then(function() {
      return res.status(200).json({ address: address });
    })
    .catch(function(err) {
      console.log(err.message);
      return res.status(500).json({error: err.message});
    });

  return Promise.resolve(null);
});

module.exports = router;

1 Answer 1

2

I might have to look into this further...but this line:

return web3.eth.sendTransaction({ from: bank, to: address, value: topUpWei, gas: defaultGasLimit, gasPrice: defaultGasPrice })

...the promise returned is the transaction hash and all associated data with it.

My initial guess, you might run into scenarios where the transaction is not mined fast enough to complete this promise chain. That will definitely be the case if this was live on the mainnet.


Transaction hash happens before mining status is updated. For test RPC purposes, it's usually instant...but there's a chance that sometimes it is just a bit slower than your promise...which makes sense why this fails at a fairly low percentage.

Unfortunately, this will fail almost all of the time on the mainnet.

3
  • Ah! Is the transaction hash returned even when the transaction hasn't been mined yet?
    – Afe
    Mar 20, 2018 at 18:56
  • @Afe Correct! Appending the reason to current answer.
    – ReyHaynes
    Mar 20, 2018 at 19:04
  • I have already hacked the difficulty of geth and hard coded 1, but I have to find a workaround
    – Afe
    Mar 20, 2018 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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