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
24 changes: 24 additions & 0 deletions packages/world/ts/deduplicateAbi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Abi, AbiItem } from "viem";

function hasNamedInputs(entry: AbiItem) {
if (!("inputs" in entry) || !entry.inputs || !entry.inputs.length) {
return false;
}
return entry.inputs.some((input) => input.name);
karooolis marked this conversation as resolved.
Show resolved Hide resolved
}

// during deduplication, favor ABI items with named inputs
export function deduplicateAbi(abi: Abi) {
karooolis marked this conversation as resolved.
Show resolved Hide resolved
const uniqueEntries = new Map();

for (const entry of abi) {
const key = "name" in entry ? `${entry.type}_${entry.name}` : entry.type;
karooolis marked this conversation as resolved.
Show resolved Hide resolved
const existingEntry = uniqueEntries.get(key);

if (!existingEntry || (hasNamedInputs(entry) && !hasNamedInputs(existingEntry))) {
uniqueEntries.set(key, entry);
}
}

return Array.from(uniqueEntries.values());
karooolis marked this conversation as resolved.
Show resolved Hide resolved
}
3 changes: 2 additions & 1 deletion packages/world/ts/getWorldAbi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Client, Abi, Address, getAddress } from "viem";
import IBaseWorldAbi from "../out/IBaseWorld.sol/IBaseWorld.abi.json";
import { functionSignatureToAbiItem } from "./functionSignatureToAbiItem";
import { getFunctions } from "./getFunctions";
import { deduplicateAbi } from "./deduplicateAbi";

export async function getWorldAbi({
client,
Expand All @@ -21,7 +22,7 @@ export async function getWorldAbi({
toBlock,
});
const worldFunctionsAbi = worldFunctions.map((func) => functionSignatureToAbiItem(func.signature));
const abi = [...IBaseWorldAbi, ...worldFunctionsAbi];
const abi = deduplicateAbi([...IBaseWorldAbi, ...worldFunctionsAbi]);

return abi;
}
Loading