Skip to content

Commit

Permalink
Check that errors come from reverted txs
Browse files Browse the repository at this point in the history
  • Loading branch information
fvictorio committed Apr 28, 2022
1 parent 11c1a70 commit 733f7f7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/hardhat-chai-matchers/src/reverted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export function supportReverted(Assertion: Chai.AssertionStatic) {
throw new AssertionError("Expected an Error object");
}

if (!isRevertError(error)) {
return Promise.reject(error);
}

this.assert(true, null, "Expected transaction NOT to be reverted");
};

Expand Down Expand Up @@ -303,6 +307,15 @@ export function supportReverted(Assertion: Chai.AssertionStatic) {
});
}

function isRevertError(error: any): boolean {
try {
getReturnDataFromError(error);
return true;
} catch (e) {
return false;
}
}

function getReturnDataFromError(error: any): string {
const errorData = (error as any).data;

Expand Down
22 changes: 22 additions & 0 deletions packages/hardhat-chai-matchers/test/reverted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,28 @@ describe("INTEGRATION: Reverted", function () {
"Expected an Error object"
);
});

it("errors that are not related to a reverted transaction", async function () {
// use an address that almost surely doesn't have balance
const randomPrivateKey =
"0xc5c587cc6e48e9692aee0bf07474118e6d830c11905f7ec7ff32c09c99eba5f9";
const signer = new this.hre.ethers.Wallet(
randomPrivateKey,
this.hre.ethers.provider
);

// this transaction will fail because of lack of funds, not because of a
// revert
await expect(
expect(
matchers.connect(signer).revertsWithoutReasonString({
gasLimit: 1_000_000,
})
).to.not.be.reverted
).to.be.eventually.rejectedWith(
"sender doesn't have enough funds to send tx"
);
});
});
}
});

0 comments on commit 733f7f7

Please sign in to comment.