Skip to content

Commit b9c729f

Browse files
committed
execute contract success
1 parent 4fa5e69 commit b9c729f

File tree

5 files changed

+90
-5
lines changed

5 files changed

+90
-5
lines changed

templates/chain-admin/components/contract/deploy/DeployJsContract.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { DeliverTxResponse } from 'hyperwebjs';
66
import {
77
formatTxFee,
88
getContractIndex,
9+
getContractAddress,
910
shortenAddress,
1011
readFileContent,
1112
} from '@/utils';
@@ -73,6 +74,7 @@ export const DeployJsContract = ({
7374
txResult.events.find((e) => e.type === 'tx')?.attributes[0].value ?? '';
7475

7576
const contractIndex = getContractIndex(txResult);
77+
const contractAddress = getContractAddress(txResult);
7678

7779
const infoItems: TxInfoItem[] = [
7880
{
@@ -81,6 +83,12 @@ export const DeployJsContract = ({
8183
copyValue: contractIndex,
8284
showCopy: true,
8385
},
86+
{
87+
label: 'Contract Address',
88+
value: contractAddress,
89+
copyValue: contractAddress,
90+
showCopy: true,
91+
},
8492
{
8593
label: 'Tx Hash',
8694
value: shortenAddress(txResult.transactionHash),
@@ -109,7 +117,7 @@ export const DeployJsContract = ({
109117
width="$full"
110118
variant="primary"
111119
onClick={() => {
112-
switchTab?.(contractIndex, TabLabel.Query);
120+
switchTab?.(contractAddress, TabLabel.Query);
113121
}}
114122
>
115123
Query
@@ -118,7 +126,7 @@ export const DeployJsContract = ({
118126
width="$full"
119127
variant="primary"
120128
onClick={() => {
121-
switchTab?.(contractIndex, TabLabel.Execute);
129+
switchTab?.(contractAddress, TabLabel.Execute);
122130
}}
123131
>
124132
Execute

templates/chain-admin/components/contract/execute/ExecuteJsContract.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ export const ExecuteJsContract = ({
101101
/>
102102
</InputField>
103103
<Button
104-
onClick={handleExecute}
105104
disabled={isButtonDisabled}
105+
onClick={handleExecute}
106106
isLoading={isLoading}
107107
width="100%"
108108
variant="primary"

templates/chain-admin/components/contract/execute/ExecuteTab.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useIsHyperwebChain } from '@/hooks';
22
import { ExecuteJsContract } from './ExecuteJsContract';
33
import { ExecuteWasmContract } from './ExecuteWasmContract';
44

5-
export type ExecuteTabProps = {
5+
type ExecuteTabProps = {
66
show: boolean;
77
addressValue: string;
88
onAddressInput: (input: string) => void;

templates/chain-admin/hooks/contract/useExecuteContractTx.tsx

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { toUint8Array } from '@/utils';
88

99
import { useHandleTx } from './useHandleTx';
1010
import { useRpcEndpoint } from '../common';
11+
import { useJsdQueryClient } from './useJsdQueryClient';
1112

1213
interface 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],

templates/chain-admin/utils/contract.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,25 @@ export const getContractIndex = (txResult: DeliverJsdTxResponse) => {
206206
return response.index.toString();
207207
};
208208

209+
export const getContractAddress = (txResult: DeliverJsdTxResponse) => {
210+
const response = hyperweb.hvm.MsgInstantiateResponse.fromProtoMsg(
211+
// @ts-ignore
212+
txResult.msgResponses[0]
213+
);
214+
return response.address;
215+
};
216+
217+
export const getContractInfo = (txResult: DeliverJsdTxResponse) => {
218+
const response = hyperweb.hvm.MsgInstantiateResponse.fromProtoMsg(
219+
// @ts-ignore
220+
txResult.msgResponses[0]
221+
);
222+
return {
223+
index: response.index.toString(),
224+
address: response.address,
225+
};
226+
};
227+
209228
export const readFileContent = (file: File): Promise<string> => {
210229
return new Promise((resolve, reject) => {
211230
const reader = new FileReader();

0 commit comments

Comments
 (0)