-
Notifications
You must be signed in to change notification settings - Fork 4
/
calculateKSMAHXcmDeliveryFees.ts
130 lines (117 loc) · 3.46 KB
/
calculateKSMAHXcmDeliveryFees.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
import { ApiPromise, WsProvider } from '@polkadot/api';
import type { XcmVersionedXcm } from '@polkadot/types/lookup';
import '@moonbeam-network/api-augment';
//https://github.com/polkadot-fellows/runtimes/blob/main/system-parachains/constants/src/kusama.rs#L37-L42
const UNITS = BigInt(1000000000000);
const QUID = UNITS / BigInt(30);
const CENTS = QUID / BigInt(100);
const GRAND = QUID / BigInt(1000);
const MILLICENTS = CENTS / BigInt(1000);
// AssetHub Constants
// https://github.com/polkadot-fellows/runtimes/blob/v1.1.0/system-parachains/asset-hubs/asset-hub-kusama/src/lib.rs#L639-L647
const ksmAHToSiblingBaseDeliveryFee = BigInt(3) * CENTS;
// https://github.com/polkadot-fellows/runtimes/blob/v1.1.0/system-parachains/asset-hubs/asset-hub-kusama/src/lib.rs#L237
const ksmAHTxByteFee = MILLICENTS;
// Config:
const wsEndpoint = 'wss://statemine-rpc.dwellir.com';
// Asset MultiLocation - This is the Asset with the longest index possible
const assetML = {
parents: 1,
interior: {
x3: [
{ parachain: 1000 },
{ palletInstance: 50 },
{ generalIndex: '340282366920938463463374607431768211455' },
],
},
};
const amount = '340282366920938463463374607431768211455';
const paraID = 2023; //ParaID
// Provider
const provider = new WsProvider(wsEndpoint);
// XCM Message
const message = {
V3: [
{
ReserveAssetDeposited: [
{
id: {
Concrete: assetML,
},
fun: { Fungible: BigInt(amount) },
},
],
},
{
ClearOrigin: [],
},
{
BuyExecution: [
{
id: {
Concrete: assetML,
},
fun: { Fungible: BigInt(amount) },
},
{ Unlimited: null },
],
},
{
DepositAsset: {
assets: {
Wild: {
AllCounted: 1,
},
},
beneficiary: {
parents: 0,
interior: {
X1: {
AccountKey20: {
network: null,
key: '0x0000000000000000000000000000000000000000',
},
},
},
},
},
},
{
SetTopic:
'0x0000000000000000000000000000000000000000000000000000000000000000',
},
],
};
const main = async () => {
// Provider
const api = await ApiPromise.create({
provider: provider,
noInitWarn: true,
});
await api.isReady;
const xcm: XcmVersionedXcm = api.createType(
'XcmVersionedXcm',
message
) as any;
const xcmBytes = xcm.toU8a();
// Calculate XCM Delivery Fee
// https://github.com/paritytech/polkadot-sdk/pull/1234
// https://github.com/paritytech/polkadot-sdk/blob/b57e53dc139cc398318d42107e915e5883a77734/polkadot/runtime/common/src/xcm_sender.rs#L67-L93
// delivery_fee_factor * (base_fee + encoded_msg_len * per_byte_fee)
// Get Delivery Fee Factor
// https://github.com/paritytech/polkadot-sdk/blob/acd043bc5fc9acfa384b69c33e341f925ef250a7/cumulus/pallets/xcmp-queue/src/lib.rs#L960-L965
const deliveryFeeFactor = await api.query.xcmpQueue.deliveryFeeFactor(
BigInt(paraID)
);
await api.disconnect();
// Delivery Fee Factor is returned as a U128 but we need it as a F128
const convDeliveryFeeFactor =
BigInt(deliveryFeeFactor.toString()) / BigInt(10 ** 18);
const fee =
convDeliveryFeeFactor *
(ksmAHToSiblingBaseDeliveryFee + BigInt(xcmBytes.length) * ksmAHTxByteFee);
console.log(
`The Max AH Delivery Fee in KSM is ${Number(fee) / Number(UNITS)}`
);
};
main();