@@ -14,7 +14,7 @@ import {
1414 Input ,
1515} from "@chakra-ui/react" ;
1616import { useMutation } from "@tanstack/react-query" ;
17- import type { AbiFunction , AbiParameter } from "abitype" ;
17+ import { type AbiFunction , type AbiParameter , formatAbiItem } from "abitype" ;
1818import { TransactionButton } from "components/buttons/TransactionButton" ;
1919import { SolidityInput } from "contract-ui/components/solidity-inputs" ;
2020import { camelToTitle } from "contract-ui/components/solidity-inputs/helpers" ;
@@ -28,13 +28,12 @@ import {
2828 type ThirdwebContract ,
2929 prepareContractCall ,
3030 readContract ,
31- resolveMethod ,
3231 simulateTransaction ,
3332 toSerializableTransaction ,
3433 toWei ,
3534} from "thirdweb" ;
3635import { useActiveAccount , useSendAndConfirmTransaction } from "thirdweb/react" ;
37- import { parseAbiParams , stringify } from "thirdweb/utils" ;
36+ import { parseAbiParams , stringify , toFunctionSelector } from "thirdweb/utils" ;
3837import {
3938 Button ,
4039 Card ,
@@ -135,7 +134,12 @@ interface InteractiveAbiFunctionProps {
135134 twAccount : Account | undefined ;
136135}
137136
138- function useAsyncRead ( contract : ThirdwebContract , functionName : string ) {
137+ function useAsyncRead ( contract : ThirdwebContract , abiFunction : AbiFunction ) {
138+ const formattedAbi = formatAbiItem ( {
139+ ...abiFunction ,
140+ type : "function" ,
141+ // biome-ignore lint/suspicious/noExplicitAny: FIXME
142+ } as any ) ;
139143 return useMutation ( {
140144 mutationFn : async ( {
141145 args,
@@ -144,7 +148,8 @@ function useAsyncRead(contract: ThirdwebContract, functionName: string) {
144148 const params = parseAbiParams ( types , args ) ;
145149 return readContract ( {
146150 contract,
147- method : resolveMethod ( functionName ) ,
151+ // biome-ignore lint/suspicious/noExplicitAny: dynamic typing
152+ method : formattedAbi as any ,
148153 params,
149154 } ) ;
150155 } ,
@@ -156,21 +161,32 @@ function useSimulateTransaction() {
156161 return useMutation ( {
157162 mutationFn : async ( {
158163 contract,
159- functionName ,
164+ abiFunction ,
160165 params,
161166 value,
162167 } : {
163168 contract : ThirdwebContract ;
164- functionName : string ;
169+ abiFunction : AbiFunction ;
165170 params : unknown [ ] ;
166171 value ?: bigint ;
167172 } ) => {
168173 if ( ! from ) {
169174 return toast . error ( "No account connected" ) ;
170175 }
176+ const formattedAbi = formatAbiItem ( {
177+ ...abiFunction ,
178+ type : "function" ,
179+ // biome-ignore lint/suspicious/noExplicitAny: FIXME
180+ } as any ) ;
181+ console . log (
182+ "formattedAbi" ,
183+ formattedAbi ,
184+ toFunctionSelector ( abiFunction ) ,
185+ ) ;
171186 const transaction = prepareContractCall ( {
172187 contract,
173- method : resolveMethod ( functionName ) ,
188+ // biome-ignore lint/suspicious/noExplicitAny: dynamic typing
189+ method : formattedAbi as any ,
174190 params,
175191 value,
176192 } ) ;
@@ -179,10 +195,16 @@ function useSimulateTransaction() {
179195 simulateTransaction ( {
180196 from,
181197 transaction,
198+ } ) . catch ( ( e ) => {
199+ console . error ( "Error simulating transaction" , e ) ;
200+ throw e ;
182201 } ) ,
183202 toSerializableTransaction ( {
184203 from,
185204 transaction,
205+ } ) . catch ( ( e ) => {
206+ console . error ( "Error serializing transaction" , e ) ;
207+ throw e ;
186208 } ) ,
187209 ] ) ;
188210 return `--- ✅ Simulation succeeded ---
@@ -257,7 +279,7 @@ export const InteractiveAbiFunction: React.FC<InteractiveAbiFunctionProps> = (
257279 data : readData ,
258280 isPending : readLoading ,
259281 error : readError ,
260- } = useAsyncRead ( contract , abiFunction . name ) ;
282+ } = useAsyncRead ( contract , abiFunction ) ;
261283
262284 const txSimulation = useSimulateTransaction ( ) ;
263285
@@ -311,12 +333,18 @@ export const InteractiveAbiFunction: React.FC<InteractiveAbiFunctionProps> = (
311333 if ( ! abiFunction . name ) {
312334 return toast . error ( "Cannot detect function name" ) ;
313335 }
336+ const formattedAbi = formatAbiItem ( {
337+ ...abiFunction ,
338+ type : "function" ,
339+ // biome-ignore lint/suspicious/noExplicitAny: FIXME
340+ } as any ) ;
314341 const types = abiFunction . inputs . map ( ( o ) => o . type ) ;
315342 const formatted = formatContractCall ( d . params ) ;
316343 const params = parseAbiParams ( types , formatted ) ;
317344 const transaction = prepareContractCall ( {
318345 contract,
319- method : resolveMethod ( abiFunction . name ) ,
346+ // biome-ignore lint/suspicious/noExplicitAny: dynamic typing
347+ method : formattedAbi as any ,
320348 params,
321349 value : d . value ? toWei ( d . value ) : undefined ,
322350 } ) ;
@@ -334,7 +362,7 @@ export const InteractiveAbiFunction: React.FC<InteractiveAbiFunctionProps> = (
334362 txSimulation . mutate ( {
335363 contract,
336364 params,
337- functionName : abiFunction . name ,
365+ abiFunction,
338366 value : d . value ? toWei ( d . value ) : undefined ,
339367 } ) ;
340368 } ) ;
0 commit comments