Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hardhat-chai-matchers - add check byte32 string to reverted matcher #5738

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 the `reverted` matcher to correctly handle `bytes32` strings (thanks @iosh!)
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 (isBytes32String(hash)) {
assert(false, "Expected transaction to be reverted");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it also can throw an error:

 throw new TypeError(
            `Expected a valid transaction hash, but got bytes32  '${hash}'`
       )

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 isBytes32String(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