-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcalculateKusamaDeliveryFees.ts
140 lines (123 loc) · 3.82 KB
/
calculateKusamaDeliveryFees.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
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);
// CHECK Network
// Kusama Constants
// https://github.com/polkadot-fellows/runtimes/blob/18a45cc963ad473708829f1b720fbd64a11d8d52/relay/kusama/src/xcm_config.rs#L119-L120
// https://github.com/polkadot-fellows/runtimes/blob/b5ba0e91d5dd3c4020e848b27be5f2b47e16f281/relay/kusama/src/lib.rs#L416
// ExponentialPrice<FeeAssetId, BaseDeliveryFee, TransactionByteFee, Dmp>;
// pub const TransactionByteFee: Balance = 10 * MILLICENTS;
// pub const BaseDeliveryFee: u128 = CENTS.saturating_mul(3);
const ksmAHTxByteFee = BigInt(10) * MILLICENTS;
const ksmAHToSiblingBaseDeliveryFee = BigInt(3) * CENTS;
// Config:
const wsEndpoint = 'wss://kusama-rpc.dwellir.com';
// Asset MultiLocation - This is RMRK for Example
const assetML = {
parents: 0,
interior: { here: null },
};
const amount = '340282366920938463463374607431768211455';
const paraID = 2023; //ParaID
/*
fn price_for_delivery(id: Self::Id, msg: &Xcm<()>) -> Assets {
let msg_fee = (msg.encoded_size() as u128).saturating_mul(M::get());
let fee_sum = B::get().saturating_add(msg_fee);
let amount = F::get_fee_factor(id).saturating_mul_int(fee_sum);
(A::get(), amount).into()
}
*/
// 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.dmp.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 Kusama XCM Delivery Fee in KSM is ${Number(fee) / Number(UNITS)}`
);
};
main();