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(world): dedupe functions in getWorldAbi result #3025

Merged
merged 13 commits into from
Aug 13, 2024
12 changes: 9 additions & 3 deletions packages/world/ts/functionSignatureToAbiItem.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { AbiItem, parseAbiItem } from "viem";
import { AbiFunction, parseAbiItem } from "viem";

export function functionSignatureToAbiItem(functionSignature: string): AbiItem {
export function functionSignatureToAbiItem(functionSignature: string): AbiFunction {
const formattedSignature = `function ${functionSignature}`;
return parseAbiItem(formattedSignature);
const abiItem = parseAbiItem(formattedSignature);

if (abiItem.type !== "function") {
throw new Error(`Expected function signature, got ${abiItem.type}`);
}

return abiItem;
}
51 changes: 51 additions & 0 deletions packages/world/ts/getWorldAbi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, expect, it, vi } from "vitest";
import { createTestClient, http } from "viem";
import { getWorldAbi } from "./getWorldAbi";
import { foundry } from "viem/chains";

vi.mock("./getFunctions", () => {
const mockGetFunctionsResult = [{ signature: "setNumber(bool)" }, { signature: "batchCall((bytes32,bytes)[])" }];
const getFunctions = vi.fn();
getFunctions.mockResolvedValue(mockGetFunctionsResult);

return {
getFunctions,
};
});

describe("World ABI", () => {
it("should concat base and world ABI", async () => {
const client = createTestClient({
chain: foundry,
mode: "anvil",
transport: http(),
});

const abi = await getWorldAbi({
client,
worldAddress: "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB",
fromBlock: 0n,
toBlock: 0n,
});

expect(abi).toContainEqual({
inputs: [
{
type: "bool",
},
],
name: "setNumber",
outputs: [],
stateMutability: "nonpayable",
type: "function",
});

expect(abi).not.toContainEqual({
name: "batchCall",
type: "function",
stateMutability: "nonpayable",
inputs: [{ type: "tuple[]", components: [{ type: "bytes32" }, { type: "bytes" }] }],
outputs: [],
});
});
});
11 changes: 9 additions & 2 deletions packages/world/ts/getWorldAbi.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { Client, Abi, Address, getAddress } from "viem";
import { Client, Abi, AbiItem, AbiFunction, Address, getAddress, toFunctionSelector } from "viem";
import IBaseWorldAbi from "../out/IBaseWorld.sol/IBaseWorld.abi.json";
import { functionSignatureToAbiItem } from "./functionSignatureToAbiItem";
import { getFunctions } from "./getFunctions";

function isAbiFunction(abiItem: AbiItem): abiItem is AbiFunction {
return abiItem.type === "function";
}

export async function getWorldAbi({
client,
worldAddress,
Expand All @@ -20,7 +24,10 @@ export async function getWorldAbi({
fromBlock,
toBlock,
});
const worldFunctionsAbi = worldFunctions.map((func) => functionSignatureToAbiItem(func.signature));
const baseFunctionSelectors = (IBaseWorldAbi as Abi).filter(isAbiFunction).map(toFunctionSelector);
const worldFunctionsAbi = worldFunctions
Copy link
Member

Choose a reason for hiding this comment

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

is as Abi casting necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without it, even with type predicate, typescript fails to recognize in the .map(..) that it's of type AbiFunction

.map((func) => functionSignatureToAbiItem(func.signature))
.filter((abiItem) => !baseFunctionSelectors.includes(toFunctionSelector(abiItem)));
const abi = [...IBaseWorldAbi, ...worldFunctionsAbi];

return abi;
Expand Down
Loading