-
Notifications
You must be signed in to change notification settings - Fork 22
/
useInstantiateAndExecute.ts
143 lines (128 loc) · 3.75 KB
/
useInstantiateAndExecute.ts
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import {
DeliverTxResponse,
instantiate2Address,
} from '@cosmjs/cosmwasm-stargate'
import { toUtf8 } from '@cosmjs/encoding'
import { nanoid } from 'nanoid'
import { useCallback } from 'react'
import { useTranslation } from 'react-i18next'
import { contractQueries } from '@dao-dao/state/query'
import { Coin, UnifiedCosmosMsg, cwMsgToEncodeObject } from '@dao-dao/types'
import {
CHAIN_GAS_MULTIPLIER,
isSecretNetwork,
makeWasmMessage,
} from '@dao-dao/utils'
import { useQueryLoadingDataWithError } from './query'
import { useWallet } from './useWallet'
export type InstantiateAndExecuteOptions = {
// Instantiate message to send to the contract.
instantiate: {
admin?: string | null
funds: Coin[]
label: string
msg: Record<string, any>
}
// Wasm execute messages to execute on the contract.
executes: {
funds: Coin[]
msg: Record<string, any>
}[]
}
export type InstantiateAndExecute = ({
instantiate,
executes,
}: InstantiateAndExecuteOptions) => Promise<{
// The address of the contract that was instantiated.
contractAddress: string
// The transaction hash of the transaction.
response: DeliverTxResponse
}>
export type UseInstantiateAndExecuteResult = {
ready: boolean
instantiateAndExecute: InstantiateAndExecute
}
// This hook allows you to instantiate a contract and execute messages on it in
// a single transaction by taking advantage of `instantiate2` which can
// precompute contract addresses.
export const useInstantiateAndExecute = (
chainId: string | undefined,
codeId: number
): UseInstantiateAndExecuteResult => {
const { t } = useTranslation()
const { getSigningClient, address, chain } = useWallet({
chainId,
})
// Load checksum of the contract code.
const checksum = useQueryLoadingDataWithError(
chainId
? contractQueries.codeInfo({
chainId,
codeId,
})
: undefined,
({ dataHash }) => dataHash
)
const instantiateAndExecute: InstantiateAndExecute = useCallback(
async ({ instantiate, executes }) => {
if (checksum.loading || checksum.errored) {
throw new Error(t('error.loadingData'))
}
if (!address || !chain) {
throw new Error(t('error.logInToContinue'))
}
// Ensure active chain is not Secret Network.
if (isSecretNetwork(chain.chainId)) {
throw new Error('Secret Network does not support instantiate2.')
}
// Random salt.
const salt = nanoid()
const contractAddress = instantiate2Address(
checksum.data,
address,
toUtf8(salt),
chain.bech32Prefix
)
const messages: UnifiedCosmosMsg[] = [
// Instantiate the contract.
makeWasmMessage({
wasm: {
instantiate2: {
...instantiate,
code_id: codeId,
salt,
fix_msg: false,
},
},
}),
// Execute messages on the contract.
...executes.map((execute) =>
makeWasmMessage({
wasm: {
execute: {
contract_addr: contractAddress,
...execute,
},
},
})
),
]
const signingClient = await getSigningClient()
const response = (await signingClient.signAndBroadcast(
address,
messages.map((msg) => cwMsgToEncodeObject(chain.chainId, msg, address)),
CHAIN_GAS_MULTIPLIER
// cosmos-kit has an older version of the package. This is a workaround.
)) as DeliverTxResponse
return {
contractAddress,
response,
}
},
[address, chain, checksum, codeId, getSigningClient, t]
)
return {
ready: !checksum.loading && !checksum.errored && !!address,
instantiateAndExecute,
}
}