Skip to content

Commit 0e30f4c

Browse files
authored
Merge branch 'main' into support-solc-0.8.27
2 parents ddadd4c + 60d344b commit 0e30f4c

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

.changeset/ninety-trainers-rush.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nomicfoundation/hardhat-chai-matchers": patch
3+
---
4+
5+
Enhanced the `reverted` matcher to correctly handle `bytes32` strings (thanks @iosh!)

packages/hardhat-chai-matchers/src/internal/reverted/reverted.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import type EthersT from "ethers";
33
import { buildAssert } from "../../utils";
44
import { REVERTED_MATCHER } from "../constants";
55
import { assertIsNotNull, preventAsyncMatcherChaining } from "../utils";
6-
import { decodeReturnData, getReturnDataFromError } from "./utils";
6+
import {
7+
decodeReturnData,
8+
getReturnDataFromError,
9+
parseBytes32String,
10+
} from "./utils";
711

812
export function supportReverted(
913
Assertion: Chai.AssertionStatic,
@@ -36,6 +40,14 @@ export function supportReverted(
3640

3741
const receipt = await getTransactionReceipt(hash);
3842

43+
if (receipt === null) {
44+
// If the receipt is null, maybe the string is a bytes32 string
45+
if (isBytes32String(hash)) {
46+
assert(false, "Expected transaction to be reverted");
47+
return;
48+
}
49+
}
50+
3951
assertIsNotNull(receipt, "receipt");
4052
assert(
4153
receipt.status === 0,
@@ -134,3 +146,12 @@ function isTransactionReceipt(x: unknown): x is { status: number } {
134146
function isValidTransactionHash(x: string): boolean {
135147
return /0x[0-9a-fA-F]{64}/.test(x);
136148
}
149+
150+
function isBytes32String(v: string): boolean {
151+
try {
152+
parseBytes32String(v);
153+
return true;
154+
} catch {
155+
return false;
156+
}
157+
}

packages/hardhat-chai-matchers/src/internal/reverted/utils.ts

+5
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,8 @@ export function resultToArray(result: EthersT.Result): any[] {
125125
: x
126126
);
127127
}
128+
129+
export function parseBytes32String(v: string): string {
130+
const ethers = require("ethers") as typeof EthersT;
131+
return ethers.decodeBytes32String(v);
132+
}

packages/hardhat-chai-matchers/test/reverted/reverted.ts

+8
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ describe("INTEGRATION: Reverted", function () {
112112
"Expected a valid transaction hash, but got '0x123'"
113113
);
114114
});
115+
116+
it("promise of an byte32 string", async function () {
117+
await expect(
118+
Promise.resolve(
119+
"0x3230323400000000000000000000000000000000000000000000000000000000"
120+
)
121+
).not.to.be.reverted;
122+
});
115123
});
116124

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

0 commit comments

Comments
 (0)