5

I set up a Geth node on the Rinkeby network recently. And while I can send ordinary transactions in seconds without a problem, I can't seem to get a contract creation to happen in less than an hour. When I compile a simple contract in Remix and then paste the web3.js deployment code it generates into the Geth console--it submits the transaction onto the network, but then it just sits there for over an hour without any of the authorized nodes ever accepting it into a block. Although eventually it gets accepted.

I am wondering if the issue here may be the gas price. Based on etherscan, it looks like web3.js decided to set the gas price for the transaction to 4 gwei. I have verified that the average for the network is currently only 2 gwei, but still--I'd like to eliminate this as a possibility. (The gas limit is definitely not a problem--set to 4700000 and the estimated gas for the contract creation code according to Remix is only 80924.)

If anyone reads this and suspects what I'm seeing is due to something other than the gas price being too low, then feel free to view the transaction here and make suggestions:

https://rinkeby.etherscan.io/tx/0xe134c193cead33031e3348ebb1db42115fdf156cb5e1a1d43d5c4ecfdf026413

Anyway, my specific question is: where is this default gas price of 4gwei coming from, and how can I override it with something higher?

For some reason in the documentation for web3.js there is no mention of any way to specify the gas price for a contract creation, even though it does provide a way to specify it for an ordinary transaction:

https://github.com/ethereum/wiki/wiki/JavaScript-API

Can I just pass an additional option to web3.eth.contract() of gasPrice= , or perhaps to myContract.new()?

8
  • 1
    The transaction you linked to appears to have succeeded over an hour ago.
    – user19510
    Commented Mar 2, 2018 at 0:39
  • 1
    Yeah sorry, I thought my browser was supposed to be auto-refreshing but apparently it wasn't for some reason. Anyway, it took over an hour to complete which is still way too long--I've edited it now to reflect that. Commented Mar 2, 2018 at 0:47
  • 2
    I'm not sure, but the delay may have been due to the very high gas limit. Blocks have a gas limit, and so it's harder to fit very heavy transactions into a block. (Your transaction didn't actually use that much gas, but a miner preparing a block presumably doesn't know that until they try to include your transaction.) Try specifying a lower gas limit next time.
    – user19510
    Commented Mar 2, 2018 at 0:54
  • 1
    Oh wow! I hadn't realized that could make a difference. One odd thing I notice now is that the estimated gas by remix was only 80924. But the actual consumed gas was 908733. Even though still not that much, it's way more than expected and I'm not sure why. It looks like the transaction was pending for over an hour, but got mined in less than 15 seconds once it finally got accepted. Commented Mar 2, 2018 at 0:57
  • 2
    Ah, in the "details" from the "compile" tab? I see 4700000 there too for any code. I think it's just hardcoded. For the "GASESTIMATES," I have no explanation. I'd have to see the code.
    – user19510
    Commented Mar 2, 2018 at 1:14

3 Answers 3

15

From the documentation:

var contractInstance = MyContract.new([constructorParam1] [, constructorParam2],
  {data: '0x12345...', from: myAccount, gas: 1000000});

Yes, you can just add a gasPrice to that object:

var contractInstance = MyContract.new([constructorParam1] [, constructorParam2],
 {data: '0x12345...', from: myAccount, gas: 1000000, gasPrice: web3.toWei(2, 'gwei')});
2
  • 1
    Thanks. Do you know if there's documentation for web3.js anywhere that lists all the parameters each method can accept? Maybe it's better to just look at the source code if in doubt? Commented Mar 2, 2018 at 0:53
  • 2
    I don't see any documentation at all for .new(), but I'm pretty sure that parameter is just the same one as in sendTransaction and the like: github.com/ethereum/wiki/wiki/….
    – user19510
    Commented Mar 2, 2018 at 0:56
3

web3 also has a function to estimate the gas needed, you could do something like:

var gasEstimate = web3.eth.estimateGas({data: bytecode});
var contract = web3.eth.contract(abi);
var instance = contract.new({data: bytecode, from: from, gas: gasEstimate});
var receipt = web3.eth.getTransactionReceipt(instance.transactionHash);
0

You can override the gas price in your code, like below.

    const tx = {
      from: walletAddress,
      to: contractAddress,
      gas: 4700000,
      gasPrice: 20000000000,
      data: encodedABI,
    };

Where you can use the tx variable like below (I skipped the part where you need to instantiate your HttpProvider)

....
...
smartContract = new web3.eth.Contract(abi, contractAddress);

const privateKey = '0x' + '12714D.....A3D8CC53';
const walletAddress = '0x77.....60';

// change this to whatever contract method you are trying to call, e.g. buyToken(type, title, desc)
const query = smartContract.methods.buyToken(type, title, desc);
const encodedABI = query.encodeABI();
const tx = {
  from: walletAddress,
  to: contractAddress,
  gas: 4700000,
  gasPrice: 10,
  data: encodedABI,
};

const account = web3.eth.accounts.privateKeyToAccount(privateKey);
console.log(account);
web3.eth.getBalance(walletAddress).then(console.log);

web3.eth.accounts.signTransaction(tx, privateKey).then(signed => {
  const tran = web3.eth
    .sendSignedTransaction(signed.rawTransaction)
    .on('confirmation', (confirmationNumber, receipt) => {
      console.log('=> confirmation: ' + confirmationNumber);
    })
    .on('transactionHash', hash => {
      console.log('=> hash');
      console.log(hash);

    })
    .on('receipt', receipt => {
      console.log('=> reciept');
      console.log(receipt);
    })
    .on('error', console.error);
});

Your transaction fee would be gas used x gasPrice. How to know gas used? You can only know after it's been used like viewing your transaction receipt, see image below (under transaction cost item in Remix where it shows 206993 gas).

enter image description here

P/S: I'M STILL A NEWBIE IN ETHEREUM AND JUST WANTING TO HELP. IF I MADE ANY ERROR PLEASE DONT DOWNVOTE BUT LEAVE COMMENTS AND I SHALL EDIT MY ANSWER

Your Answer

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

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