-
Notifications
You must be signed in to change notification settings - Fork 4
/
calculateDOTAHXcmDeliveryFees.ts
126 lines (113 loc) · 3.35 KB
/
calculateDOTAHXcmDeliveryFees.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
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/polkadot.rs
const UNITS = BigInt(10000000000);
const DOLLARS = UNITS;
const GRAND = DOLLARS * BigInt(1000);
const CENTS = DOLLARS / BigInt(100);
const MILLICENTS = CENTS / BigInt(1000);
// AssetHub Constants
// https://github.com/polkadot-fellows/runtimes/blob/v1.1.0/system-parachains/asset-hubs/asset-hub-polkadot/src/lib.rs#L577
const dotAHToSiblingBaseDeliveryFee = BigInt(3) * CENTS;
// https://github.com/polkadot-fellows/runtimes/blob/v1.1.0/system-parachains/asset-hubs/asset-hub-polkadot/src/lib.rs#L247
const dotAHTxByteFee = MILLICENTS;
// Config:
const wsEndpoint = 'wss://statemint-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: '1337' }],
},
};
const amount = '1000000';
const paraID = 2004; //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 *
(dotAHToSiblingBaseDeliveryFee + BigInt(xcmBytes.length) * dotAHTxByteFee);
console.log(
`The Max AH Delivery Fee in DOT is ${Number(fee) / Number(UNITS)}`
);
};
main();