[Ethereum] How to set the gas price while deploying a contract with web3.js

gas-pricego-ethereumremixrinkebyweb3js

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()?

Best Answer

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')});