-
Notifications
You must be signed in to change notification settings - Fork 34
/
utils.ts
188 lines (169 loc) · 5.42 KB
/
utils.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import '@polkadot/api-augment'
import { ApiPromise, WsProvider } from '@polkadot/api'
import shell from 'shelljs'
import fs from 'fs'
import { blake2AsU8a } from '@polkadot/util-crypto'
import { stringToU8a, bnToU8a, u8aConcat, u8aToHex } from '@polkadot/util'
import { decodeAddress, encodeAddress } from '@polkadot/keyring'
import { KeyringPair } from '@polkadot/keyring/types'
import { AccountId, Index } from '@polkadot/types/interfaces'
import { promisify } from 'util'
import { Vec } from '@polkadot/types'
const EMPTY_U8A_32 = new Uint8Array(32)
export const readFile = promisify(fs.readFile)
export const exec = (cmd: string): shell.ShellString => {
console.log(`$ ${cmd}`)
const res = shell.exec(cmd, { silent: true })
if (res.code !== 0) {
console.error('Error: Command failed with code', res.code)
console.log(res)
}
return res
}
export const sleep = (ms: number): Promise<void> => new Promise(resolve => setTimeout(resolve, ms))
export const chainHeight = async (api: ApiPromise): Promise<number> => {
const {
block: {
header: { number: height }
}
} = await api.rpc.chain.getBlock()
return height.toNumber()
}
export const createAddress = (id: string): string =>
encodeAddress(u8aConcat(stringToU8a(`modl${id}`), EMPTY_U8A_32).subarray(0, 32))
export const sovereignRelayOf = (paraId: number): string =>
encodeAddress(
u8aConcat(stringToU8a('para'), bnToU8a(paraId, 32, true), EMPTY_U8A_32).subarray(0, 32)
)
export const sovereignParaOf = (paraId: number): string =>
encodeAddress(
u8aConcat(stringToU8a('sibl'), bnToU8a(paraId, 32, true), EMPTY_U8A_32).subarray(0, 32)
)
export const subAccountId = (address: string, index: number): string => {
const seedBytes = stringToU8a('modlpy/utilisuba')
const whoBytes = decodeAddress(address)
const indexBytes = bnToU8a(index, 16).reverse()
const combinedBytes = new Uint8Array(seedBytes.length + whoBytes.length + indexBytes.length)
combinedBytes.set(seedBytes)
combinedBytes.set(whoBytes, seedBytes.length)
combinedBytes.set(indexBytes, seedBytes.length + whoBytes.length)
const entropy = blake2AsU8a(combinedBytes, 256)
return encodeAddress(entropy)
}
export const nextNonce = async (api: ApiPromise, signer: KeyringPair): Promise<Index> => {
return await api.rpc.system.accountNextIndex(signer.address)
}
export const createXcm = (encoded: string, refundAccount: string, originType = 'Native') => {
return {
V2: [
{
WithdrawAsset: [
{
id: {
Concrete: {
parents: 0,
interior: 'Here'
}
},
fun: {
Fungible: getDefaultXcmFee()
}
}
]
},
{
BuyExecution: {
fees: {
id: {
Concrete: {
parents: 0,
interior: 'Here'
}
},
fun: {
Fungible: getDefaultXcmFee()
}
},
weightLimit: 'Unlimited'
}
},
{
Transact: {
originType,
requireWeightAtMost: '3000000000',
call: {
encoded
}
}
},
'RefundSurplus',
{
DepositAsset: {
assets: {
Wild: {
AllOf: {
id: {
Concrete: {
parents: 0,
interior: 'Here'
}
},
fun: 'Fungible'
}
}
},
maxAssets: 1,
beneficiary: {
parents: 0,
interior: {
X1: {
AccountId32: {
network: 'Any',
id: u8aToHex(decodeAddress(refundAccount))
}
}
}
}
}
}
]
}
}
export const getApi = async (endpoint: string): Promise<ApiPromise> => {
return ApiPromise.create({
provider: new WsProvider(endpoint)
})
}
export const getRelayApi = async (endpoint: string): Promise<ApiPromise> => {
return ApiPromise.create({
provider: new WsProvider(endpoint)
})
}
export const getCouncilThreshold = async (api: ApiPromise): Promise<number> => {
const members = (await api.query.generalCouncilMembership.members()) as unknown as Vec<AccountId>
return Math.ceil(members.length / 2)
}
export const calcWeightPerSecond = (precision: number, price: number): number => {
const WEIGHT_PER_SECOND = 10 ** 12
// for fixed weigher always 600_000_000
const weight = 600_000_000
//assume we charge 0.02$ at most for each xcm reserved based transfer
const max_fee = 0.02
/// max_fee = (weight_per_second * weight)/WEIGHT_PER_SECOND/(10**precision) * price
/// so weight_per_second = max_fee*WEIGHT_PER_SECOND*(10**precision)/weight/price
const weight_per_second = (((max_fee * WEIGHT_PER_SECOND) / weight) * 10 ** precision) / price
return Math.floor(weight_per_second)
}
export const getDefaultRelayChainWsUrl = (): string => {
return process.env['RELAY_CHAIN_TYPE'] === 'kusama'
? 'wss://kusama-rpc.polkadot.io'
: 'wss://rpc.polkadot.io'
}
export const getDefaultParachainWsUrl = (): string => {
return process.env['RELAY_CHAIN_TYPE'] === 'kusama'
? 'wss://heiko-rpc.parallel.fi'
: 'wss://rpc.parallel.fi'
}
export const getDefaultXcmFee = (): number => {
return process.env['RELAY_CHAIN_TYPE'] === 'kusama' ? 10_000_000_000 : 2_500_000_000
}