File tree Expand file tree Collapse file tree 5 files changed +118
-1
lines changed Expand file tree Collapse file tree 5 files changed +118
-1
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ " thirdweb " : patch
3+ ---
4+
5+ Expose utilities to decode errors and function data
Original file line number Diff line number Diff line change @@ -138,11 +138,17 @@ export { isBytes } from "viem";
138138// abi
139139// ------------------------------------------------
140140export { encodeAbiParameters } from "../utils/abi/encodeAbiParameters.js" ;
141+ export { decodeError } from "../utils/abi/decodeError.js" ;
142+ export { decodeFunctionData } from "../utils/abi/decodeFunctionData.js" ;
143+ export { decodeFunctionResult } from "../utils/abi/decodeFunctionResult.js" ;
141144
142145/**
143146 * @utils
144147 */
145- export { encodePacked } from "viem" ;
148+ export {
149+ encodePacked ,
150+ decodeAbiParameters ,
151+ } from "viem" ;
146152
147153// Useful helpers
148154export { setThirdwebDomains } from "../utils/domains.js" ;
Original file line number Diff line number Diff line change 1+ import { type Abi , AbiError } from "ox" ;
2+ import { resolveContractAbi } from "../../contract/actions/resolve-abi.js" ;
3+ import type { ThirdwebContract } from "../../contract/contract.js" ;
4+ import type { Hex } from "../encoding/hex.js" ;
5+
6+ /**
7+ * Decodes an error.
8+ * @param options - The options object.
9+ * @returns The decoded error.
10+ * @example
11+ * ```ts
12+ * import { decodeError } from "thirdweb/utils";
13+ *
14+ * const data = "0x...";
15+ * const error = await decodeError({ contract, data });
16+ * ```
17+ *
18+ * @utils
19+ */
20+ export async function decodeError < abi extends Abi . Abi > ( options : {
21+ contract : ThirdwebContract < abi > ;
22+ data : Hex ;
23+ } ) {
24+ const { contract, data } = options ;
25+ let abi = contract ?. abi ;
26+ if ( contract && ! abi ) {
27+ abi = await resolveContractAbi ( contract ) . catch ( ( ) => undefined ) ;
28+ }
29+ if ( ! abi ) {
30+ throw new Error (
31+ `No ABI found for contract ${ contract . address } on chain ${ contract . chain . id } ` ,
32+ ) ;
33+ }
34+ const abiError = AbiError . fromAbi ( abi , data ) as AbiError . AbiError ;
35+ return AbiError . decode ( abiError , data ) ;
36+ }
Original file line number Diff line number Diff line change 1+ import { type Abi , AbiFunction , type Hex } from "ox" ;
2+ import { resolveContractAbi } from "../../contract/actions/resolve-abi.js" ;
3+ import type { ThirdwebContract } from "../../contract/contract.js" ;
4+
5+ /**
6+ * Decodes the data of a function call.
7+ * @param options - The options object.
8+ * @returns The decoded data.
9+ * @example
10+ * ```ts
11+ * import { decodeFunctionData } from "thirdweb/utils";
12+ *
13+ * const data = "0x...";
14+ * const decodedData = await decodeFunctionData({ contract, data });
15+ * ```
16+ *
17+ * @utils
18+ */
19+ export async function decodeFunctionData < abi extends Abi . Abi > ( options : {
20+ contract : ThirdwebContract < abi > ;
21+ data : Hex . Hex ;
22+ } ) {
23+ const { contract, data } = options ;
24+ let abi = contract ?. abi ;
25+ if ( contract && ! abi ) {
26+ abi = await resolveContractAbi ( contract ) . catch ( ( ) => undefined ) ;
27+ }
28+ if ( ! abi ) {
29+ throw new Error (
30+ `No ABI found for contract ${ contract . address } on chain ${ contract . chain . id } ` ,
31+ ) ;
32+ }
33+ const abiFunction = AbiFunction . fromAbi ( abi , data ) ;
34+ return AbiFunction . decodeData ( abiFunction , data ) ;
35+ }
Original file line number Diff line number Diff line change 1+ import { type Abi , AbiFunction , type Hex } from "ox" ;
2+ import { resolveContractAbi } from "../../contract/actions/resolve-abi.js" ;
3+ import type { ThirdwebContract } from "../../contract/contract.js" ;
4+
5+ /**
6+ * Decodes the result of a function call.
7+ * @param options - The options object.
8+ * @returns The decoded result.
9+ * @example
10+ * ```ts
11+ * import { decodeFunctionResult } from "thirdweb/utils";
12+ *
13+ * const data = "0x...";
14+ * const result = await decodeFunctionResult({ contract, data });
15+ * ```
16+ *
17+ * @utils
18+ */
19+ export async function decodeFunctionResult < abi extends Abi . Abi > ( options : {
20+ contract : ThirdwebContract < abi > ;
21+ data : Hex . Hex ;
22+ } ) {
23+ const { contract, ...rest } = options ;
24+ let abi = contract ?. abi ;
25+ if ( contract && ! abi ) {
26+ abi = await resolveContractAbi ( contract ) . catch ( ( ) => undefined ) ;
27+ }
28+ if ( ! abi ) {
29+ throw new Error (
30+ `No ABI found for contract ${ contract . address } on chain ${ contract . chain . id } ` ,
31+ ) ;
32+ }
33+ const abiFunction = AbiFunction . fromAbi ( abi , rest . data ) ;
34+ return AbiFunction . decodeResult ( abiFunction , rest . data ) ;
35+ }
You can’t perform that action at this time.
0 commit comments