Skip to content

Commit e4ba47b

Browse files
[SDK] Fix decodeFunction and decodeError functions (#7767)
1 parent 710ac32 commit e4ba47b

File tree

5 files changed

+58
-3
lines changed

5 files changed

+58
-3
lines changed

.changeset/tangy-ghosts-joke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Fix decodeFunction and decodeError functions

packages/thirdweb/src/utils/abi/decodeError.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AbiError } from "ox";
12
import type * as ox__Abi from "ox/Abi";
23
import * as ox__AbiError from "ox/AbiError";
34
import { resolveContractAbi } from "../../contract/actions/resolve-abi.js";
@@ -32,6 +33,12 @@ export async function decodeError<abi extends ox__Abi.Abi>(options: {
3233
`No ABI found for contract ${contract.address} on chain ${contract.chain.id}`,
3334
);
3435
}
35-
const abiError = ox__AbiError.fromAbi(abi, data) as ox__AbiError.AbiError;
36+
const err = abi.filter(
37+
(i) => i.type === "error" && AbiError.getSelector(i) === data.slice(0, 10),
38+
);
39+
const abiError = err[0] as ox__AbiError.AbiError;
40+
if (!abiError) {
41+
throw new Error(`No ABI function found for selector ${data.slice(0, 10)}`);
42+
}
3643
return ox__AbiError.decode(abiError, data);
3744
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { expect, it } from "vitest";
2+
import { TEST_CLIENT } from "../../../test/src/test-clients.js";
3+
import { defineChain } from "../../chains/utils.js";
4+
import { getContract } from "../../contract/contract.js";
5+
import { decodeFunctionData } from "./decodeFunctionData.js";
6+
7+
it.runIf(process.env.TW_SECRET_KEY)(
8+
"decodes function data correctly",
9+
async () => {
10+
const result = await decodeFunctionData({
11+
contract: getContract({
12+
address: "0x9480d58e14b1851a2A3C7aEFAd17D48e31D3F93b",
13+
client: TEST_CLIENT,
14+
chain: defineChain(17177),
15+
}),
16+
data: "0xd8fd8f44000000000000000000000000f117ed9dcc8960062484b781097321c8380cead30000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000002466353238316131632d363830352d343536622d623738322d38356165326165623336643800000000000000000000000000000000000000000000000000000000",
17+
});
18+
expect(result).toMatchInlineSnapshot(`
19+
[
20+
"0xf117ed9dcc8960062484b781097321c8380cead3",
21+
"0x66353238316131632d363830352d343536622d623738322d383561653261656233366438",
22+
]
23+
`);
24+
},
25+
);

packages/thirdweb/src/utils/abi/decodeFunctionData.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ export async function decodeFunctionData<abi extends ox__Abi.Abi>(options: {
3232
`No ABI found for contract ${contract.address} on chain ${contract.chain.id}`,
3333
);
3434
}
35-
const abiFunction = ox__AbiFunction.fromAbi(abi, data);
35+
const fn = abi.filter(
36+
(i) =>
37+
i.type === "function" &&
38+
ox__AbiFunction.getSelector(i) === data.slice(0, 10),
39+
);
40+
const abiFunction = fn[0] as ox__AbiFunction.AbiFunction;
41+
if (!abiFunction) {
42+
throw new Error(`No ABI function found for selector ${data.slice(0, 10)}`);
43+
}
3644
return ox__AbiFunction.decodeData(abiFunction, data);
3745
}

packages/thirdweb/src/utils/abi/decodeFunctionResult.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ export async function decodeFunctionResult<abi extends ox__Abi.Abi>(options: {
3232
`No ABI found for contract ${contract.address} on chain ${contract.chain.id}`,
3333
);
3434
}
35-
const abiFunction = ox__AbiFunction.fromAbi(abi, rest.data);
35+
const fn = abi.filter(
36+
(i) =>
37+
i.type === "function" &&
38+
ox__AbiFunction.getSelector(i) === rest.data.slice(0, 10),
39+
);
40+
const abiFunction = fn[0] as ox__AbiFunction.AbiFunction;
41+
if (!abiFunction) {
42+
throw new Error(
43+
`No ABI function found for selector ${rest.data.slice(0, 10)}`,
44+
);
45+
}
3646
return ox__AbiFunction.decodeResult(abiFunction, rest.data);
3747
}

0 commit comments

Comments
 (0)