1

I have created the following script for calculating the transaction fee for deploying smart contract using Hardhat:

const contractFactory = await ethers.getContractFactory('Lock');
const deployTx = contractFactory.getDeployTransaction(unlockTime, { value: lockedAmount });
const contract = await contractFactory.deploy(unlockTime, { value: lockedAmount });

const receipt = await contract.deployTransaction.wait();
const transactionHash = await ethers.provider.getTransactionReceipt(receipt.transactionHash);


const gasUsed = transactionHash.gasUsed;
const gasPricePaid = await ethers.provider.getGasPrice();
const transactionFee = gasUsed.mul(gasPricePaid);
console.log(`Transaction fee paid for contract deployment: ${ethers.utils.formatEther(transactionFee)} wei`);

  console.log(
`Smart contract address: ${contract.address}}`
);
  
console.log(`Transaction fee estimated for contract deployment: ${deploymentPriceRBTC} wei`);

It works fine and I get results like:

Transaction fee paid for contract deployment: 0.000838054876525056 wei

However when I open the transaction on Etherscan I see that the transaction fee is different 0.00118697 for the same transaction.

Can someone explain me what is exactly happening how can I get the real value which is stored on etherscan

1
  • This - and most of the things related to testing and debugging - are easier to do with Foundry, since you are testing and deploying using Solidity itself. Consider making the switch. Commented Jan 26, 2023 at 16:47

3 Answers 3

2

Use effectiveGasPrice value directly from the receipt, instead of ethers.provider.getGasPrice

const transactionReceipt = await ethers.provider.getTransactionReceipt(receipt.transactionHash);
const gasUsed = transactionReceipt.gasUsed;
const gasPricePaid = transactionReceipt.effectiveGasPrice;
const transactionFee = gasUsed.mul(gasPricePaid);

provider.getGasPrice 🔗

— returns suggested gas price for current/next transactions. You call this before sending the transaction, to estimate the gas price.

1
  • Thank you, that makes sense now. Can you elaborate briefly what is the difference? effectiveGasPrice is the price paid after the contract is deployed and getGasPrice is just the price that is predicted before deployment .
    – Dakata
    Commented Jan 27, 2023 at 11:33
1

I always use Remix. Connect to local host -> select your smart contract -> compile -> choose from Metamask the chain -> inject Metamask in Remix -> deploy -> Metamask will pop up with the necessary gas and cost to deploy ->then cancel. This will also give info about the price of gas at that moment.

1

Firstly, there’s no way (wei) in hell you’ll pay such a little amount of wei to deploy any contract ever. That figure is in ETH.

The reason for the discrepancy took me a while to read into 😅

The thing to consider is that, as seen in this thread, effectiveGasPrice is actually the result of base fee + priority fee.

Indeed now effectiveGasPrice In the tx receipt

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.