env-artifacts.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. // Truffe (deprecated)
  11. const originalRequire = hre.artifacts.require;
  12. hre.artifacts.require = function (name) {
  13. for (const suffix of suffixes) {
  14. try {
  15. return originalRequire.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. // Ethers
  27. const originalReadArtifact = hre.artifacts.readArtifact;
  28. hre.artifacts.readArtifact = async function (name) {
  29. for (const suffix of suffixes) {
  30. try {
  31. return await originalReadArtifact.call(this, name + suffix);
  32. } catch (e) {
  33. if (isExpectedError(e, suffix)) {
  34. continue;
  35. } else {
  36. throw e;
  37. }
  38. }
  39. }
  40. throw new Error('Unreachable');
  41. };
  42. });