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: improve ABI resolution fallback #4753

Merged
merged 1 commit into from
Sep 23, 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
5 changes: 5 additions & 0 deletions .changeset/loud-beds-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Fix abi resolution fallback
19 changes: 12 additions & 7 deletions packages/thirdweb/src/contract/actions/resolve-abi.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { type Abi, formatAbi, parseAbi } from "abitype";
import { getInstalledModules } from "../../extensions/modules/__generated__/IModularCore/read/getInstalledModules.js";
import { download } from "../../storage/download.js";
import { extractIPFSUri } from "../../utils/bytecode/extractIPFS.js";
import { getClientFetch } from "../../utils/fetch.js";
import type { ThirdwebContract } from "../contract.js";
import { getBytecode } from "./get-bytecode.js";

const ABI_RESOLUTION_CACHE = new WeakMap<ThirdwebContract<Abi>, Promise<Abi>>();

Expand Down Expand Up @@ -117,7 +114,11 @@ export async function resolveAbiFromBytecode(
// biome-ignore lint/suspicious/noExplicitAny: library function that accepts any contract type
contract: ThirdwebContract<any>,
): Promise<Abi> {
const bytecode = await getBytecode(contract);
const [{ resolveImplementation }, { extractIPFSUri }] = await Promise.all([
import("../../utils/bytecode/resolveImplementation.js"),
import("../../utils/bytecode/extractIPFS.js"),
]);
const { bytecode } = await resolveImplementation(contract);
if (bytecode === "0x") {
const { id, name } = contract.chain;
throw new Error(
Expand Down Expand Up @@ -366,6 +367,9 @@ async function resolveModularModuleAddresses(
contract: ThirdwebContract,
): Promise<string[]> {
try {
const { getInstalledModules } = await import(
"../../extensions/modules/__generated__/IModularCore/read/getInstalledModules.js"
);
const modules = await getInstalledModules({ contract });
// if there are no plugins, return the root ABI
if (!modules.length) {
Expand Down Expand Up @@ -434,14 +438,15 @@ function joinAbis(options: JoinAbisOptions): Abi {
.filter((item) => item.type !== "constructor");

if (options.rootAbi) {
mergedPlugins = [...(options.rootAbi || []), ...mergedPlugins].filter(
Boolean,
);
mergedPlugins = [...options.rootAbi, ...mergedPlugins]
.filter((item) => item.type !== "fallback" && item.type !== "receive")
.filter(Boolean);
}

// unique by formatting every abi and then throwing them in a set
// TODO: this may not be super efficient...
const humanReadableAbi = [...new Set(formatAbi(mergedPlugins))];

// finally parse it back out
return parseAbi(humanReadableAbi);
}
Loading