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

Adds support for write calls with defined outputs. #99

Merged
merged 1 commit into from
Oct 25, 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
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
Loading