Skip to content
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/tangy-ghosts-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Fix decodeFunction and decodeError functions
9 changes: 8 additions & 1 deletion packages/thirdweb/src/utils/abi/decodeError.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AbiError } from "ox";
import type * as ox__Abi from "ox/Abi";
import * as ox__AbiError from "ox/AbiError";
import { resolveContractAbi } from "../../contract/actions/resolve-abi.js";
Expand Down Expand Up @@ -32,6 +33,12 @@ export async function decodeError<abi extends ox__Abi.Abi>(options: {
`No ABI found for contract ${contract.address} on chain ${contract.chain.id}`,
);
}
const abiError = ox__AbiError.fromAbi(abi, data) as ox__AbiError.AbiError;
const err = abi.filter(
(i) => i.type === "error" && AbiError.getSelector(i) === data.slice(0, 10),
);
const abiError = err[0] as ox__AbiError.AbiError;
if (!abiError) {
throw new Error(`No ABI function found for selector ${data.slice(0, 10)}`);
}
Comment on lines +36 to +42
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix error message to reference "ABI error" instead of "ABI function".

The error message on line 41 incorrectly refers to "ABI function" when it should reference "ABI error" since this is the decodeError function.

-    throw new Error(`No ABI function found for selector ${data.slice(0, 10)}`);
+    throw new Error(`No ABI error found for selector ${data.slice(0, 10)}`);
🤖 Prompt for AI Agents
In packages/thirdweb/src/utils/abi/decodeError.ts around lines 36 to 42, the
error message incorrectly refers to "ABI function" instead of "ABI error".
Update the error message string on line 41 to say "No ABI error found for
selector" followed by the selector value to accurately reflect the context of
the decodeError function.

return ox__AbiError.decode(abiError, data);
}
25 changes: 25 additions & 0 deletions packages/thirdweb/src/utils/abi/decodeFunctionData.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expect, it } from "vitest";
import { TEST_CLIENT } from "../../../test/src/test-clients.js";
import { defineChain } from "../../chains/utils.js";
import { getContract } from "../../contract/contract.js";
import { decodeFunctionData } from "./decodeFunctionData.js";

it.runIf(process.env.TW_SECRET_KEY)(
"decodes function data correctly",
async () => {
const result = await decodeFunctionData({
contract: getContract({
address: "0x9480d58e14b1851a2A3C7aEFAd17D48e31D3F93b",
client: TEST_CLIENT,
chain: defineChain(17177),
}),
data: "0xd8fd8f44000000000000000000000000f117ed9dcc8960062484b781097321c8380cead30000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000002466353238316131632d363830352d343536622d623738322d38356165326165623336643800000000000000000000000000000000000000000000000000000000",
});
expect(result).toMatchInlineSnapshot(`
[
"0xf117ed9dcc8960062484b781097321c8380cead3",
"0x66353238316131632d363830352d343536622d623738322d383561653261656233366438",
]
`);
},
);
10 changes: 9 additions & 1 deletion packages/thirdweb/src/utils/abi/decodeFunctionData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
`No ABI found for contract ${contract.address} on chain ${contract.chain.id}`,
);
}
const abiFunction = ox__AbiFunction.fromAbi(abi, data);
const fn = abi.filter(
(i) =>
i.type === "function" &&
ox__AbiFunction.getSelector(i) === data.slice(0, 10),
);
const abiFunction = fn[0] as ox__AbiFunction.AbiFunction;
if (!abiFunction) {
throw new Error(`No ABI function found for selector ${data.slice(0, 10)}`);
}

Check warning on line 43 in packages/thirdweb/src/utils/abi/decodeFunctionData.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/utils/abi/decodeFunctionData.ts#L42-L43

Added lines #L42 - L43 were not covered by tests
return ox__AbiFunction.decodeData(abiFunction, data);
}
12 changes: 11 additions & 1 deletion packages/thirdweb/src/utils/abi/decodeFunctionResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ export async function decodeFunctionResult<abi extends ox__Abi.Abi>(options: {
`No ABI found for contract ${contract.address} on chain ${contract.chain.id}`,
);
}
const abiFunction = ox__AbiFunction.fromAbi(abi, rest.data);
const fn = abi.filter(
(i) =>
i.type === "function" &&
ox__AbiFunction.getSelector(i) === rest.data.slice(0, 10),
);
const abiFunction = fn[0] as ox__AbiFunction.AbiFunction;
if (!abiFunction) {
throw new Error(
`No ABI function found for selector ${rest.data.slice(0, 10)}`,
);
}
return ox__AbiFunction.decodeResult(abiFunction, rest.data);
}
Loading