customError.js 726 B

123456789101112131415161718192021222324
  1. const { config } = require('hardhat');
  2. const optimizationsEnabled = config.solidity.compilers.some(c => c.settings.optimizer.enabled);
  3. /** Revert handler that supports custom errors. */
  4. async function expectRevertCustomError(promise, reason) {
  5. try {
  6. await promise;
  7. expect.fail("Expected promise to throw but it didn't");
  8. } catch (revert) {
  9. if (reason) {
  10. if (optimizationsEnabled) {
  11. // Optimizations currently mess with Hardhat's decoding of custom errors
  12. expect(revert.message).to.include.oneOf([reason, 'unrecognized return data or custom error']);
  13. } else {
  14. expect(revert.message).to.include(reason);
  15. }
  16. }
  17. }
  18. }
  19. module.exports = {
  20. expectRevertCustomError,
  21. };