Skip to content

Instantly share code, notes, and snippets.

@joaostein
Last active October 21, 2022 20:53
Show Gist options
  • Save joaostein/ae38a16536c146bb3fe2795b1be13816 to your computer and use it in GitHub Desktop.
Save joaostein/ae38a16536c146bb3fe2795b1be13816 to your computer and use it in GitHub Desktop.
example of `getGasUsed` helper function
// helper function (you can create your own `helpers.js` file and import to your tests)
const getGasUsed = async (tx) => {
const { gasUsed, effectiveGasPrice } = await tx.wait();
return gasUsed.mul(effectiveGasPrice);
};
// your tests
describe('how to calculate and consider gas costs', () => {
const oneEth = ethers.utils.parseEther('1');
// deposit `oneEth`
// then...
it('should withdraw contract funds', async () => {
const initialOwnerBalance = await owner.getBalance();
const withdrawTx = await yourContract.connect(owner).withdrawFunds();
const gasUsed = await getGasUsed(withdrawTx);
const finalOwnerBalance = await owner.getBalance();
expect(finalOwnerBalance).to.equal(initialOwnerBalance.add(oneEth).sub(gasUsed));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment