@@ -8,6 +8,7 @@ import { toUint8Array } from '@/utils';
88
99import { useHandleTx } from './useHandleTx' ;
1010import { useRpcEndpoint } from '../common' ;
11+ import { useJsdQueryClient } from './useJsdQueryClient' ;
1112
1213interface ExecuteTxParams {
1314 address : string ;
@@ -32,6 +33,7 @@ export const useExecuteContractTx = (chainName: string) => {
3233 const { data : rpcEndpoint } = useRpcEndpoint ( chainName ) ;
3334 const { chain, wallet } = useChain ( chainName ) ;
3435 const handleTx = useHandleTx ( chainName ) ;
36+ const { data : jsdQueryClient } = useJsdQueryClient ( ) ;
3537
3638 const executeTx = async ( {
3739 address,
@@ -75,14 +77,70 @@ export const useExecuteContractTx = (chainName: string) => {
7577 throw new Error ( 'Keplr wallet not available' ) ;
7678 }
7779
80+ if ( ! jsdQueryClient ) {
81+ throw new Error ( 'Query client not available' ) ;
82+ }
83+
84+ // Get contract address from contract index using listContracts
85+ const contractsResponse =
86+ await jsdQueryClient . hyperweb . hvm . listContracts ( {
87+ pagination : {
88+ limit : 1000n ,
89+ reverse : true ,
90+ countTotal : false ,
91+ key : new Uint8Array ( ) ,
92+ offset : 0n ,
93+ } ,
94+ } ) ;
95+
96+ const contractInfo = contractsResponse . contracts . find (
97+ ( contract ) => contract . index . toString ( ) === contractIndex
98+ ) ;
99+
100+ if ( ! contractInfo ) {
101+ throw new Error ( `Contract not found for index ${ contractIndex } ` ) ;
102+ }
103+
104+ // Debug: Log the contract info structure
105+ console . log ( 'Contract Info:' , contractInfo ) ;
106+ console . log ( 'Contract Info Keys:' , Object . keys ( contractInfo ) ) ;
107+
108+ // Try to extract contract address from different possible fields
109+ let contractAddress ;
110+
111+ // Try common field names
112+ if ( ( contractInfo as any ) . address ) {
113+ contractAddress = ( contractInfo as any ) . address ;
114+ } else if ( ( contractInfo as any ) . contractAddress ) {
115+ contractAddress = ( contractInfo as any ) . contractAddress ;
116+ } else if (
117+ ( contractInfo as any ) . contract &&
118+ ( contractInfo as any ) . contract . address
119+ ) {
120+ contractAddress = ( contractInfo as any ) . contract . address ;
121+ } else {
122+ // If we can't find the address, let's try using the index directly
123+ // as the system might expect it
124+ contractAddress = contractIndex ;
125+ console . warn (
126+ 'Could not find contract address field, using index directly'
127+ ) ;
128+ }
129+
130+ if ( ! contractAddress ) {
131+ throw new Error (
132+ `Contract address not found for index ${ contractIndex } `
133+ ) ;
134+ }
135+
78136 // Create signing client using hyperwebjs 1.1.1
79137 const signingClient = await getSigningHyperwebClient ( {
80138 rpcEndpoint,
81139 signer : ( window as any ) . keplr . getOfflineSigner ( chainId ) ,
82140 } ) ;
83141
84142 const msg = hyperweb . hvm . MessageComposer . fromPartial . eval ( {
85- address : contractIndex , // Contract address in 1.1.1
143+ address : contractAddress , // Use the contract address from getContractByIndex
86144 creator : address ,
87145 callee : fnName ,
88146 args : [ arg ] ,
0 commit comments