env-artifacts.js 1019 B

1234567891011121314151617181920212223242526272829
  1. const { HardhatError } = require('hardhat/internal/core/errors');
  2. function isExpectedError(e, suffix) {
  3. // HH700: Artifact not found - from https://hardhat.org/hardhat-runner/docs/errors#HH700
  4. return HardhatError.isHardhatError(e) && e.number === 700 && suffix !== '';
  5. }
  6. // Modifies the artifact require functions so that instead of X it loads the XUpgradeable contract.
  7. // This allows us to run the same test suite on both the original and the transpiled and renamed Upgradeable contracts.
  8. extendEnvironment(hre => {
  9. const suffixes = ['UpgradeableWithInit', 'Upgradeable', ''];
  10. // Ethers
  11. const originalReadArtifact = hre.artifacts.readArtifact;
  12. hre.artifacts.readArtifact = async function (name) {
  13. for (const suffix of suffixes) {
  14. try {
  15. return await originalReadArtifact.call(this, name + suffix);
  16. } catch (e) {
  17. if (isExpectedError(e, suffix)) {
  18. continue;
  19. } else {
  20. throw e;
  21. }
  22. }
  23. }
  24. throw new Error('Unreachable');
  25. };
  26. });