11

I'd like to set a gasPrice in my deploy script, so I could deploy and let miners pick up my transaction when gas is low. Like deploy my contract over night while I'm sleeping to save money.

How can I specify the gas price in my ethers deploy script?

   const MyContract = await ethers.getContractFactory('MyContract', signer);
   const mycontract = await MyContract.deploy();
   await mycontract.deployed();
   console.log('Deployed to:', mycontract.address);

2 Answers 2

10

You can override the options property of a transaction by adding an object as the last parameter:

 const mycontract = await MyContract.deploy({gasPrice: 50000000000}); // 50gwei

You can also specify a global gas price in your hardhat config. Something like this:

mainnet: {
   url: `https://mainnet.infura.io/v3/${InfuraKey}`, 
   gasPrice: 50000000000
}
4
  • 1
    Is it possible to intentionally suppress this error? I don't mind my transaction not being mined for hours if it's under the base fee because I will be sleeping. ``` err: max fee per gas less than block base fee ```
    – caker
    Sep 22, 2021 at 17:44
  • 1
    ohh, I guess this is tricky, because now with the base fee introduced, the provider will reject your tx if your fee is lower than the base fee. (It wont even stay in the mempool like it used to) Sep 24, 2021 at 9:04
  • Hmm, I think the only solution is to deploy as a legacy txn
    – caker
    Sep 24, 2021 at 20:06
  • I feel like you could get this error even sending legacy tx, cuz that seems to be a new rule they implemented in geth client. But I could be wrong :P Sep 25, 2021 at 3:04
4

After London Mainnet rollout, EIP-1559 changed how gas fees are calculated. Gas fees are now split into different components (block-based base fee + tip) and a transaction issuer can specify a max fee/tip that he is willing to pay.

For ethers this means calling getFeeData instead of getGasPrice and passing it as an option to the deploy method.

// The gas price (in wei)...
const feeData = await provider.getFeeData();
// {
//   gasPrice: { BigNumber: "23610503242" },
//   maxFeePerGas: { BigNumber: "46721006484" },
//   maxPriorityFeePerGas: { BigNumber: "1500000000" }
// }

const MyContract = await ethers.getContractFactory('MyContract', signer);
const mycontract = await MyContract.deploy(feeData);
await mycontract.deployed();
console.log('Deployed to:', mycontract.address);

To get more readable values you can format in gwei.

utils.formatUnits(feeData.maxFeePerGas, 'gwei');
// '46.721006484'

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.