-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuseInstantiateTx.tsx
More file actions
75 lines (68 loc) · 1.93 KB
/
Copy pathuseInstantiateTx.tsx
File metadata and controls
75 lines (68 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { useChain } from '@interchain-kit/react';
import { useInstantiateContract } from '@interchainjs/react/cosmwasm/wasm/v1/tx.rpc.react';
import { MsgInstantiateContract } from '@interchainjs/react/cosmwasm/wasm/v1/tx';
import { Coin, DeliverTxResponse, StdFee } from '@interchainjs/react/types';
import { defaultContext } from '@tanstack/react-query';
import { toUint8Array } from '@/utils';
import { useCustomSigningClient } from '../common';
interface InstantiateTxParams {
address: string;
codeId: number;
initMsg: object;
label: string;
admin: string;
funds: Coin[];
onTxSucceed?: (txInfo: DeliverTxResponse) => void;
onTxFailed?: () => void;
}
export const useInstantiateTx = (chainName: string) => {
const { address } = useChain(chainName);
const { data: signingClient } = useCustomSigningClient();
const { mutate: instantiateContract, isLoading } = useInstantiateContract({
clientResolver: signingClient,
options: {
context: defaultContext,
},
});
const instantiateTx = async ({
address,
codeId,
initMsg,
label,
admin,
funds,
onTxSucceed = () => { },
onTxFailed = () => { },
}: InstantiateTxParams) => {
const fee: StdFee = { amount: [], gas: '300000' };
const message = MsgInstantiateContract.fromPartial({
sender: address,
codeId: BigInt(codeId),
admin,
funds,
label,
msg: toUint8Array(initMsg),
});
instantiateContract(
{
signerAddress: address,
message,
fee,
memo: 'Instantiate Contract',
},
{
onSuccess: (res) => {
if (res.code !== 0) {
throw new Error(res.rawLog || 'Failed to instantiate contract');
}
onTxSucceed(res);
},
onError: (error) => {
console.error('Failed to instantiate contract:', error);
onTxFailed();
},
}
);
};
return { instantiateTx, isLoading };
};