env-contract.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Remove the default account from the accounts list used in tests, in order
  2. // to protect tests against accidentally passing due to the contract
  3. // deployer being used subsequently as function caller
  4. //
  5. // This operation affects:
  6. // - the accounts (and signersAsPromise) parameters of `contract` blocks
  7. // - the return of hre.ethers.getSigners()
  8. extendEnvironment(hre => {
  9. // TODO: replace with a mocha root hook.
  10. // (see https://github.com/sc-forks/solidity-coverage/issues/819#issuecomment-1762963679)
  11. if (!process.env.COVERAGE) {
  12. // override hre.ethers.getSigner()
  13. // note that we don't just discard the first signer, we also cache the value to improve speed.
  14. const originalGetSigners = hre.ethers.getSigners;
  15. const filteredSignersAsPromise = originalGetSigners().then(signers => signers.slice(1));
  16. hre.ethers.getSigners = () => filteredSignersAsPromise;
  17. }
  18. // override hre.contract
  19. const originalContract = hre.contract;
  20. hre.contract = function (name, body) {
  21. originalContract.call(this, name, accounts => {
  22. let snapshot;
  23. before(async function () {
  24. // reset the state of the chain in between contract test suites
  25. // TODO: this should be removed when migration to ethers is over
  26. const { takeSnapshot } = require('@nomicfoundation/hardhat-network-helpers');
  27. snapshot = await takeSnapshot();
  28. });
  29. after(async function () {
  30. // reset the state of the chain in between contract test suites
  31. // TODO: this should be removed when migration to ethers is over
  32. await snapshot.restore();
  33. });
  34. body(accounts.slice(1));
  35. });
  36. };
  37. });