Last active
October 21, 2022 20:53
-
-
Save joaostein/ae38a16536c146bb3fe2795b1be13816 to your computer and use it in GitHub Desktop.
example of `getGasUsed` helper function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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