Skip to content

Commit

Permalink
fix: hardhat-chai-matchers - add check byte32 string to reverted matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
iosh authored and schaable committed Sep 12, 2024
1 parent 27721a0 commit 7964bf0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/ninety-trainers-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/hardhat-chai-matchers": patch
---

Enhanced byte32 string in `reverted` matcher check
23 changes: 22 additions & 1 deletion packages/hardhat-chai-matchers/src/internal/reverted/reverted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import type EthersT from "ethers";
import { buildAssert } from "../../utils";
import { REVERTED_MATCHER } from "../constants";
import { assertIsNotNull, preventAsyncMatcherChaining } from "../utils";
import { decodeReturnData, getReturnDataFromError } from "./utils";
import {
decodeReturnData,
getReturnDataFromError,
parseBytes32String,
} from "./utils";

export function supportReverted(
Assertion: Chai.AssertionStatic,
Expand Down Expand Up @@ -36,6 +40,14 @@ export function supportReverted(

const receipt = await getTransactionReceipt(hash);

if (receipt === null) {
// If the receipt is null, maybe the string is a bytes32 string
if (isByte32String(hash)) {
assert(false, "Expected transaction to be reverted");
return;
}
}

assertIsNotNull(receipt, "receipt");
assert(
receipt.status === 0,
Expand Down Expand Up @@ -134,3 +146,12 @@ function isTransactionReceipt(x: unknown): x is { status: number } {
function isValidTransactionHash(x: string): boolean {
return /0x[0-9a-fA-F]{64}/.test(x);
}

function isByte32String(v: string): boolean {
try {
parseBytes32String(v);
return true;
} catch {
return false;
}
}
5 changes: 5 additions & 0 deletions packages/hardhat-chai-matchers/src/internal/reverted/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,8 @@ export function resultToArray(result: EthersT.Result): any[] {
: x
);
}

export function parseBytes32String(v: string): string {
const ethers = require("ethers") as typeof EthersT;
return ethers.decodeBytes32String(v);
}
8 changes: 8 additions & 0 deletions packages/hardhat-chai-matchers/test/reverted/reverted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ describe("INTEGRATION: Reverted", function () {
"Expected a valid transaction hash, but got '0x123'"
);
});

it("promise of an byte32 string", async function () {
await expect(
Promise.resolve(
"0x3230323400000000000000000000000000000000000000000000000000000000"
)
).not.to.be.reverted;
});
});

describe("with a TxResponse as its subject", function () {
Expand Down

0 comments on commit 7964bf0

Please sign in to comment.