Conclusion: Assigning variable values inside the contract is cheaper.
Possible Reason: There may be additional deployment overhead b/c we're placing additional data in the constructor.
$ yarn ts-node scripts/define-constructor-deploy.ts
Using address 0x9E3...e03A
Wallet balance 9.978686068922924
Awaiting confirmations
Token Contract Address: 0x82e5Ec2A9F8744E87d91A2baB2663DAFE5770fF7
Wallet AFTER before deploying 9.978530472422198
9.978686068922924-9.97837653492148 Gas Cost = 3.09534e-3
$ yarn ts-node scripts/define-contract-deploy.ts
Using address 0x9E3...e03A
Wallet balance before deploying 9.978530472422198
Awaiting confirmations
Token Contract Address: 0xe01e9D582fd962084ef42cB2C33b6dDA3789588E
Wallet AFTER before deploying 9.97837653492148
9.978530472422198-9.97837653492148 Gas Cost = 1.539375e-3
const deploymentData = contract.interface.encodeDeploy([<constructor_arguments>])
Then, you could use the data to get the estimated gas limit as follows:
const estimatedGas = await ethers.provider.estimateGas({ data: deploymentData });
the above code will give you the gas limit, you would need to multiply it with the current network gas price in order to get the fee in ETH.
First, you must get the estimated gas price to use in an empty transaction:
const gasPrice = await provider.getFeeData();
Then, get the estimated gas price for your specific transaction, and multiply it by
gasPrice
:const functionGasFees = await contract.estimateGas.myFunction(<argument>); const finalGasPrice = gasPrice * functionGasFees;
Of course,
finalGasPrice
will never be exactly equal to the actual gas price you will end up paying, but it is a close enough estimate.
-
@ricmoo comment on using getFeeData() since getGasPrice() has been deprecated: ethers-io/ethers.js#2439 (reply in thread)
-
getFeeData()
ethers.js docs: https://docs.ethers.io/v5/api/providers/provider/#Provider-getFeeData -
Class 9 Example with
gasUsed()
effectiveGasPrice()
from transaction receipt
const BLOCK_GAS_LIMIT = 30000000;
//
// ..additional code
//
const sortTx = await ballotContract.sortProposals(STEP_SIZE); // Call any f(x) here
console.log("Awaiting confirmations");
const sortReceipt = await sortTx.wait();
console.log("Operation completed");
const percentUsed = sortReceipt.gasUsed
.mul(100)
.div(BLOCK_GAS_LIMIT)
.toNumber();
console.log(
`${sortReceipt.gasUsed} units of gas used at ${ethers.utils.formatUnits(
sortReceipt.effectiveGasPrice,
"gwei"
)} GWEI effective gas price, total of ${ethers.utils.formatEther(
sortReceipt.effectiveGasPrice.mul(sortReceipt.gasUsed)
)} ETH spent. This used ${percentUsed} % of the block gas limit`
);
yarn set version berry
yarn --version
yarn add --dev [email protected] #yarn 2.10.x breaks when using yarn berry
yarn hardhat
Then choose: Create an advanced sample project that uses TypeScript
yarn hardhat test
- If you get an error the following
lodash
error:
An unexpected error occurred:
Error: @typechain/hardhat tried to access lodash (a peer dependency) but it isn't provided
by your application; this makes the require call ambiguous and unsound.
Required package: lodash
Follow the instructions and install lodash:
yarn add lodash
- If
process.env
is redlined and saysCannot find name 'process'. Do you need to install type definitions for node?
Do:
yarn add dotenv
yarn install