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
18 changes: 17 additions & 1 deletion packages/world/ts/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Hex } from "viem";
import { Hex, createTestClient, http, publicActions, walletActions } from "viem";

export type WorldFunction = {
readonly signature: string;
Expand All @@ -7,3 +7,19 @@ export type WorldFunction = {
readonly systemFunctionSignature: string;
readonly systemFunctionSelector: Hex;
};

export const anvilHost = "127.0.0.1";
export const anvilPort = 8555;

// ID of the current test worker. Used by the `@viem/anvil` proxy server.
export const poolId = Number(process.env.VITEST_POOL_ID ?? 1);

export const anvilRpcUrl = `http://${anvilHost}:${anvilPort}/${poolId}`;

export const testClient = createTestClient({
mode: "anvil",
transport: http(anvilRpcUrl),
pollingInterval: 10,
})
.extend(publicActions)
.extend(walletActions);
44 changes: 44 additions & 0 deletions packages/world/ts/getWorldAbi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, expect, it, vi } from "vitest";
import { getWorldAbi } from "./getWorldAbi";
import { testClient } from "./common";

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 abi = await getWorldAbi({
client: testClient,
karooolis marked this conversation as resolved.
Show resolved Hide resolved
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 as AbiFunction)));
const abi = [...IBaseWorldAbi, ...worldFunctionsAbi];
Copy link
Member

Choose a reason for hiding this comment

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

we could prob improve the return type of functionSignatureToAbiItem so we don't have to cast this

Copy link
Contributor Author

@karooolis karooolis Aug 13, 2024

Choose a reason for hiding this comment

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

Adjusted to return AbiFunction but in order for that to work, have to ensure that it indeed returns function ABI item, otherwise the function throws. Not sure how else to handle it


return abi;
Expand Down
Loading