Skip to content

Commit

Permalink
Adds support for write calls with defined outputs.
Browse files Browse the repository at this point in the history
  • Loading branch information
robotoer committed Oct 25, 2024
1 parent bf6b94b commit 36fa79f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
27 changes: 15 additions & 12 deletions src/compat/waffle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,21 @@ class Stub<T extends AbiFunction> 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,
abi: this.func,
inputs: this.inputs,
outputs: args,
});
if (this.func.stateMutability === "pure" || this.func.stateMutability === "view") {
this.calls.push({
kind: "read",
abi: this.func,
inputs: this.inputs,
outputs: args,
});
} else {
this.calls.push({
kind: "write",
abi: this.func,
inputs: this.inputs,
outputs: args.length === 0 ? undefined : args,
});
}

return this;
}
Expand Down
10 changes: 8 additions & 2 deletions src/mock-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type MockWriteCallExpectation<T extends AbiFunction> = {
kind: "write";
abi: T;
inputs?: AbiParametersToPrimitiveTypes<T["inputs"]>;
outputs?: AbiParametersToPrimitiveTypes<T["outputs"]>;
};
export type MockRevertExpectation<T extends AbiFunction> = {
kind: "revert";
Expand Down Expand Up @@ -118,6 +119,11 @@ export const deployMock = async (
}
case "write": {
const fnSigHash = calculateFnSigHash(call);
const encodedOutputs = call.outputs ? encodeFunctionResult({
abi: [call.abi as AbiFunction],
functionName: call.abi.name,
result: call.outputs,
}) : "0x";
// Use a mock function to return the expected return value
if (firstCall) {
await signer.writeContract({
Expand All @@ -126,7 +132,7 @@ export const deployMock = async (
account: signer.account,
abi: abi,
functionName: "__doppelganger__mockReturns",
args: [fnSigHash, "0x"],
args: [fnSigHash, encodedOutputs],
});
firstCall = false;
} else {
Expand All @@ -136,7 +142,7 @@ export const deployMock = async (
account: signer.account,
abi: abi,
functionName: "__doppelganger__queueReturn",
args: [fnSigHash, "0x"],
args: [fnSigHash, encodedOutputs],
});
}
break;
Expand Down

0 comments on commit 36fa79f

Please sign in to comment.