From 00a5580513995e2b6fedb50a589672191c2341a1 Mon Sep 17 00:00:00 2001 From: Robert Chu Date: Fri, 25 Oct 2024 08:47:22 -0700 Subject: [PATCH] Properly switch between read/write modes in waffle compat mode. --- src/compat/waffle.ts | 14 ++++++++++---- src/mock-contract.ts | 6 +++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/compat/waffle.ts b/src/compat/waffle.ts index 3e0e0dc..893a41c 100644 --- a/src/compat/waffle.ts +++ b/src/compat/waffle.ts @@ -131,8 +131,8 @@ class Stub implements StubInterface { argsSet = false; constructor( - private mockContract: MockContractController, - private func: T, + private readonly mockContract: MockContractController, + private readonly func: T, ) {} private err(reason: string): never { @@ -146,8 +146,14 @@ class Stub implements StubInterface { if (!this.func.outputs) this.err("Cannot mock return values from a void function"); + const kind = + this.func.stateMutability === "view" || + this.func.stateMutability === "pure" + ? "read" + : "write"; + this.calls.push({ - kind: "read", + kind, abi: this.func, inputs: this.inputs, outputs: args, @@ -229,7 +235,7 @@ function createMock( mockContractInstance: MockContractController, // wallet: WalletClient, ): MockContract["mock"] { - const functions = abi.filter((f) => f.type === "function") as AbiFunction[]; + const functions = abi.filter((f) => f.type === "function"); const mockedAbi = Object.values(functions).reduce( (acc, func) => { const stubbed = new Stub(mockContractInstance, func); diff --git a/src/mock-contract.ts b/src/mock-contract.ts index fc9434d..b81043f 100644 --- a/src/mock-contract.ts +++ b/src/mock-contract.ts @@ -51,7 +51,7 @@ export const calculateFnSigHash = ( return toFunctionSelector(call.abi); } return encodeFunctionData({ - abi: [call.abi as AbiFunction], + abi: [call.abi], args: call.inputs, functionName: call.abi.name, }); @@ -150,7 +150,7 @@ export const deployMock = async ( account: signer.account, abi: abi, functionName: "__doppelganger__mockReverts", - args: [fnSigHash, call.reason || ""], + args: [fnSigHash, call.reason ?? ""], }); firstCall = false; } else { @@ -160,7 +160,7 @@ export const deployMock = async ( account: signer.account, abi: abi, functionName: "__doppelganger__queueRevert", - args: [fnSigHash, call.reason || ""], + args: [fnSigHash, call.reason ?? ""], }); } break;