forked from akure/dao-dao-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contracts.ts
78 lines (68 loc) · 2.25 KB
/
contracts.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
import { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate'
import { Coin, SigningStargateClient } from '@cosmjs/stargate'
import { findAttribute, parseRawLog } from '@cosmjs/stargate/build/logs'
import { ContractVersion } from '@dao-dao/types'
import { CHAIN_GAS_MULTIPLIER } from './constants'
import { cwMsgToEncodeObject, encodeMessageAsBase64 } from './messages'
const CONTRACT_VERSIONS = Object.values(ContractVersion)
// If version is defined, returns it. Otherwise, returns `undefined`.
// Essentially just filters version by its presence in the `ContractVersion`
// enum.
export const parseContractVersion = (
version: string
): ContractVersion | undefined => CONTRACT_VERSIONS.find((v) => v === version)
export const indexToProposalModulePrefix = (index: number) => {
index += 1
let prefix = ''
while (index > 0) {
const letterIndex = (index - 1) % 26
// capital A = 65, Z = 90
prefix = String.fromCharCode(65 + letterIndex) + prefix
index = ((index - letterIndex) / 26) | 0
}
return prefix
}
// Normalize for comparisons.
export const normalizeContractName = (contractName: string) =>
contractName.replace('crates.io:', '').trim()
// Use our custom instantiate encoder since CosmJS is unreliable due to the SDK
// version (47+) change that improperly handles the optional admin field as an
// empty string. The normal signing client `instantiate` function is thus no
// longer reliable.
export const instantiateSmartContract = async (
client: SigningCosmWasmClient | SigningStargateClient,
sender: string,
codeId: number,
label: string,
msg: unknown,
funds?: Coin[],
admin?: string | null
): Promise<string> => {
const { rawLog } = await client.signAndBroadcast(
sender,
[
cwMsgToEncodeObject(
{
wasm: {
instantiate: {
code_id: codeId,
msg: encodeMessageAsBase64(msg),
funds: funds || [],
label,
// Replace empty string with undefined.
admin: admin || undefined,
},
},
},
sender
),
],
CHAIN_GAS_MULTIPLIER
)
const contractAddress = findAttribute(
parseRawLog(rawLog),
'instantiate',
'_contract_address'
).value
return contractAddress
}