-
Notifications
You must be signed in to change notification settings - Fork 5
/
calculateSovereignAccountFundTransfer.ts
168 lines (148 loc) · 4.1 KB
/
calculateSovereignAccountFundTransfer.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
import { ApiPromise, WsProvider } from '@polkadot/api';
import { ParaId } from '@polkadot/types/interfaces';
import { u8aToHex } from '@polkadot/util';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
const argv = yargs(hideBin(process.argv)).options({
network: { type: 'string', demandOption: true, alias: 'n', default: 'moonbeam' },
percent: { type: 'number', demandOption: true, alias: 'p', default: 15 },
}).argv;
// Create Provider
let wsParaProvider;
let wsRelayprovider;
let paraID;
let feeAmount = BigInt(0);
let refTime = BigInt(0);
let proofSize = BigInt(0);
if (argv['network'] === 'moonbeam') {
wsParaProvider = new WsProvider('wss://wss.api.moonbeam.network');
wsRelayprovider = new WsProvider('wss://polkadot-rpc.dwellir.com');
paraID = 2004;
feeAmount = BigInt(10000000000);
refTime = BigInt(1000000000);
proofSize = BigInt(128000);
} else if (argv['network'] === 'moonriver') {
wsParaProvider = new WsProvider('wss://wss.api.moonriver.moonbeam.network');
wsRelayprovider = new WsProvider('wss://kusama-rpc.dwellir.com');
paraID = 2023;
feeAmount = BigInt(10000000000);
refTime = BigInt(1000000000);
proofSize = BigInt(128000);
} else {
console.error('Network not supported');
process.exit();
}
const main = async () => {
// Load Provider
const apiPara = await ApiPromise.create({
provider: wsParaProvider,
noInitWarn: true,
});
const apiRelay = await ApiPromise.create({
provider: wsRelayprovider,
noInitWarn: true,
});
await apiPara.isReady;
await apiRelay.isReady;
// Get Sov Account as Child and Sibling
const [relayAccount, paraAccount] = calculateSovAddress(apiRelay, paraID);
// Get Balance in Relay Chain
const balance = (await apiRelay.query.system.account(relayAccount)).toHuman();
const spendBalance =
BigInt(balance['data']['free'].replaceAll(',', '')) -
BigInt(balance['data']['reserved'].replaceAll(',', ''));
// Calculate Percent
const balanceTransfer = (spendBalance * BigInt(argv['percent'])) / BigInt(100);
// Construct Call to AH
const destRelay = {
V4: {
parents: 0,
interior: {
X1: [
{
Parachain: 1000,
},
],
},
},
};
const benefRelay = {
V4: {
parents: 0,
interior: {
X1: [
{
AccountId32: {
network: null,
id: paraAccount,
},
},
],
},
},
};
const assetRelay = {
V4: [
{
id: {
parents: 0,
interior: 'Here',
},
fun: {
Fungible: balanceTransfer,
},
},
],
};
let relayTX = await apiRelay.tx.xcmPallet.transferAssets(
destRelay,
benefRelay,
assetRelay,
0,
'Unlimited'
);
console.log(
(await relayTX.paymentInfo('5Ec4AhPZYgv9Q1KUajtv2RieJJmhPdn9cnvKxjpxuJHVoGFt')).toHuman()
);
// Build Moonbeam/Moonriver Call
const destPara = {
V4: {
parents: 1,
interior: 'Here',
},
};
let paraTx = await apiPara.tx.xcmTransactor.transactThroughSovereign(
destPara,
null,
{
currency: {
AsCurrencyId: { ForeignAsset: '42259045809535163221576417993425387648' },
},
feeAmount: feeAmount,
},
relayTX.method.toHex(),
'SovereignAccount',
{
transactRequiredWeightAtMost: { refTime: refTime, proofSize: proofSize },
overallWeight: 'Unlimited',
},
true
);
console.log(`Call to transfer ${argv['percent']}\% from ${argv['network']} to AH`);
console.log(paraTx.method.toHex());
await apiPara.disconnect();
await apiRelay.disconnect();
};
const calculateSovAddress = (api, paraID) => {
const targetParaId: ParaId = api.createType('ParaId', paraID);
const sovAddressRelay = u8aToHex(
new Uint8Array([...new TextEncoder().encode('para'), ...targetParaId.toU8a()])
).padEnd(66, '0');
const sovAddressPara = u8aToHex(
new Uint8Array([...new TextEncoder().encode('sibl'), ...targetParaId.toU8a()])
).padEnd(66, '0');
return [sovAddressRelay, sovAddressPara];
};
main()
.catch(console.error)
.finally(() => process.exit());