-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.ts
113 lines (97 loc) · 2.6 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
import { ApiPromise } from '@polkadot/api'
import { u8aToHex } from '@polkadot/util'
import { decodeAddress } from '@polkadot/util-crypto'
import { GenericBody, XcmVersionedMultiLocation } from './interfaces/generics'
import { MakeXcmVersionedMultiAssetsProps, MakeXcmVersionedMultiLocationProps } from './interfaces/utils-interfaces'
export const makeXcmVersionedMultiLocation = ({
parents,
target,
value,
}: MakeXcmVersionedMultiLocationProps): XcmVersionedMultiLocation => {
let interior: any = 'Here'
if (target === 'Parachain') {
interior = {
X1: {
Parachain: Number(value),
},
}
}
if (target === 'AccountId32') {
const account = String(value)
const isHex = account.startsWith('0x')
const accountId = isHex ? value : u8aToHex(decodeAddress(account))
interior = {
X1: {
AccountId32: {
network: 'Any',
id: accountId,
},
},
}
}
return {
V1: {
parents: parents ? Number(parents) : 0,
interior,
},
}
}
export const makeAsssetMultiAsset = ({
assetId,
palletInstance = 50,
amount,
parents = 0,
}: MakeXcmVersionedMultiAssetsProps) => {
const xcmVersionedMultiLocation = {
V1: [
{
id: {
Concrete: {
parents,
interior: !assetId
? 'Here'
: {
X2: [
{
PalletInstance: palletInstance,
},
{
GeneralIndex: assetId,
},
],
},
},
},
fun: {
Fungible: Number(amount),
},
},
],
}
return xcmVersionedMultiLocation
}
export const formatExtrinsicResponse = ({ res, rej, status, txHash, dispatchError, dispatchInfo }: any) => {
if (status.isInBlock || status.isFinalized) {
if (dispatchError) {
rej(dispatchError.toString())
} else if (dispatchInfo) {
res(txHash.toString())
}
}
}
export const getPallet = (api: ApiPromise, pallet?: string) => {
if (pallet) {
if (!api.tx[pallet]) throw new Error(`${pallet} unsupported`)
return pallet
}
const palletIsIncluded = Object.keys(api.tx).some((p) => ['xcmPallet', 'polkadotXcm'].includes(p))
if (!palletIsIncluded) {
throw new Error('xcmPallet or polkadotXcm unsupported')
}
return api.tx.xcmPallet ? 'xcmPallet' : 'polkadotXcm'
}
export const parseGenericBody = (body: GenericBody) => {
if (Array.isArray(body)) return body
const _body = Object.keys(body).map((key: any) => body[key])
return _body
}