File tree Expand file tree Collapse file tree 5 files changed +76
-1
lines changed Expand file tree Collapse file tree 5 files changed +76
-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+ export async function decodeError < abi extends Abi . Abi > ( options : {
7+ contract : ThirdwebContract < abi > ;
8+ data : Hex ;
9+ } ) {
10+ const { contract, data } = options ;
11+ let abi = contract ?. abi ;
12+ if ( contract && ! abi ) {
13+ abi = await resolveContractAbi ( contract ) . catch ( ( ) => undefined ) ;
14+ }
15+ if ( ! abi ) {
16+ throw new Error (
17+ `No ABI found for contract ${ contract . address } on chain ${ contract . chain . id } ` ,
18+ ) ;
19+ }
20+ const abiError = AbiError . fromAbi ( abi , data ) as AbiError . AbiError ;
21+ return AbiError . decode ( abiError , data ) ;
22+ }
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+ export async function decodeFunctionData < abi extends Abi . Abi > ( options : {
6+ contract : ThirdwebContract < abi > ;
7+ data : Hex . Hex ;
8+ } ) {
9+ const { contract, data } = options ;
10+ let abi = contract ?. abi ;
11+ if ( contract && ! abi ) {
12+ abi = await resolveContractAbi ( contract ) . catch ( ( ) => undefined ) ;
13+ }
14+ if ( ! abi ) {
15+ throw new Error (
16+ `No ABI found for contract ${ contract . address } on chain ${ contract . chain . id } ` ,
17+ ) ;
18+ }
19+ const abiFunction = AbiFunction . fromAbi ( abi , data ) ;
20+ return AbiFunction . decodeData ( abiFunction , data ) ;
21+ }
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+ export async function decodeFunctionResult < abi extends Abi . Abi > ( options : {
6+ contract : ThirdwebContract < abi > ;
7+ data : Hex . Hex ;
8+ } ) {
9+ const { contract, ...rest } = options ;
10+ let abi = contract ?. abi ;
11+ if ( contract && ! abi ) {
12+ abi = await resolveContractAbi ( contract ) . catch ( ( ) => undefined ) ;
13+ }
14+ if ( ! abi ) {
15+ throw new Error (
16+ `No ABI found for contract ${ contract . address } on chain ${ contract . chain . id } ` ,
17+ ) ;
18+ }
19+ const abiFunction = AbiFunction . fromAbi ( abi , rest . data ) ;
20+ return AbiFunction . decodeResult ( abiFunction , rest . data ) ;
21+ }
You can’t perform that action at this time.
0 commit comments