Skip to content

Commit

Permalink
move base bridge fee computations to primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
svyatonik committed Dec 1, 2023
1 parent f02d3ed commit 258ecfb
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 29 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 3 additions & 12 deletions system-parachains/asset-hubs/asset-hub-kusama/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,18 +722,9 @@ pub mod bridging {
parameter_types! {
/// Base price of every byte of the Kusama -> Polkadot message. Can be adjusted via
/// governance `set_storage` call.
///
/// Default value is our estimation of the:
///
/// 1) an approximate cost of XCM execution (`ExportMessage` and surroundings) at Kusama bridge Hub;
///
/// 2) the approximate cost of Kusama -> Polkadot message delivery transaction on Polkadot Bridge Hub,
/// converted into KSMs using 1:5 conversion rate;
///
/// 3) the approximate cost of Kusama -> Polkadot message confirmation transaction on Kusama Bridge Hub.
pub storage XcmBridgeHubRouterBaseFee: Balance = bp_bridge_hub_kusama::BridgeHubKusamaBaseXcmFeeInKsms::get()
.saturating_add(bp_bridge_hub_polkadot::BridgeHubPolkadotBaseDeliveryFeeInDots::get().saturating_mul(30))
.saturating_add(bp_bridge_hub_kusama::BridgeHubKusamaBaseConfirmationFeeInKsms::get());
pub storage XcmBridgeHubRouterBaseFee: Balance = bp_bridge_hub_kusama::estimate_kusama_to_polkadot_message_fee(
bp_bridge_hub_polkadot::BridgeHubPolkadotBaseDeliveryFeeInDots::get()
);
/// Price of every byte of the Kusama -> Polkadot message. Can be adjusted via
/// governance `set_storage` call.
pub storage XcmBridgeHubRouterByteFee: Balance = TransactionByteFee::get();
Expand Down
15 changes: 3 additions & 12 deletions system-parachains/asset-hubs/asset-hub-polkadot/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,18 +614,9 @@ pub mod bridging {
parameter_types! {
/// Base price of every byte of the Polkadot -> Kusama message. Can be adjusted via
/// governance `set_storage` call.
///
/// Default value is our estimation of the:
///
/// 1) an approximate cost of XCM execution (`ExportMessage` and surroundings) at Polkadot bridge hub;
///
/// 2) the approximate cost of Polkadot -> Kusama message delivery transaction on Kusama Bridge Hub,
/// converted into DOTs using 5:1 conversion rate;
///
/// 3) the approximate cost of Polkadot -> Kusama message confirmation transaction on Polkadot Bridge Hub.
pub storage XcmBridgeHubRouterBaseFee: Balance = bp_bridge_hub_polkadot::BridgeHubPolkadotBaseXcmFeeInDots::get()
.saturating_add(bp_bridge_hub_kusama::BridgeHubKusamaBaseDeliveryFeeInKsms::get().saturating_div(30))
.saturating_add(bp_bridge_hub_polkadot::BridgeHubPolkadotBaseConfirmationFeeInDots::get());
pub storage XcmBridgeHubRouterBaseFee: Balance = bp_bridge_hub_polkadot::estimate_polkadot_to_kusama_message_fee(
bp_bridge_hub_kusama::BridgeHubKusamaBaseDeliveryFeeInKsms::get()
);
/// Price of every byte of the Polkadot -> Kusama message. Can be adjusted via
/// governance `set_storage` call.
pub storage XcmBridgeHubRouterByteFee: Balance = TransactionByteFee::get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ license.workspace = true

[dependencies]

# Local
kusama-runtime-constants = { path = "../../../../relay/kusama/constants", default-features = false}
polkadot-runtime-constants = { path = "../../../../relay/polkadot/constants", default-features = false}

# Bridge Dependencies
bp-bridge-hub-cumulus = { default-features = false , version = "0.4.0" }
bp-runtime = { default-features = false , version = "0.4.0" }
Expand All @@ -27,6 +31,8 @@ std = [
"bp-messages/std",
"bp-runtime/std",
"frame-support/std",
"kusama-runtime-constants/std",
"polkadot-runtime-constants/std",
"sp-api/std",
"sp-runtime/std",
"sp-std/std",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use frame_support::{
dispatch::DispatchClass,
sp_runtime::{MultiAddress, MultiSigner},
};
use sp_runtime::RuntimeDebug;
use sp_runtime::{FixedPointNumber, FixedU128, RuntimeDebug, Saturating};

/// BridgeHubKusama parachain.
#[derive(RuntimeDebug)]
Expand Down Expand Up @@ -99,3 +99,52 @@ frame_support::parameter_types! {
/// (initially was calculated by test `BridgeHubKusama::can_calculate_fee_for_complex_message_confirmation_transaction` + `33%`)
pub const BridgeHubKusamaBaseConfirmationFeeInKsms: u128 = 61_600_495_508;
}

/// Compute the total estimated fee that needs to be paid in KSMs by the sender when sending
/// message from Kusama Bridge Hub to Polkadot Bridge Hub.
pub fn estimate_kusama_to_polkadot_message_fee(
bridge_hub_polkadot_base_delivery_fee_in_udots: Balance,
) -> Balance {
// Sender must pay:
//
// 1) an approximate cost of XCM execution (`ExportMessage` and surroundings) at Kusama bridge Hub;
//
// 2) the approximate cost of Kusama -> Polkadot message delivery transaction on Polkadot Bridge Hub,
// converted into KSMs using 1:5 conversion rate;
//
// 3) the approximate cost of Kusama -> Polkadot message confirmation transaction on Kusama Bridge Hub.
BridgeHubKusamaBaseXcmFeeInKsms::get()
.saturating_add(convert_from_udot_to_uksm(bridge_hub_polkadot_base_delivery_fee_in_udots))
.saturating_add(BridgeHubKusamaBaseConfirmationFeeInKsms::get())
}

/// Convert from uDOTs to uKSMs.
fn convert_from_udot_to_uksm(price_in_udot: Balance) -> Balance {
// assuming exchange rate is 5 DOTs for 1 KSM
let ksm_to_dot_economic_rate = FixedU128::from_rational(1, 5);
// tokens have different nominals and we need to take that into account
let nominal_ratio = FixedU128::from_rational(
kusama_runtime_constants::currency::UNITS,
polkadot_runtime_constants::currency::UNITS,
);

ksm_to_dot_economic_rate
.saturating_mul(nominal_ratio)
.saturating_mul(FixedU128::saturating_from_integer(price_in_udot))
.into_inner() / FixedU128::DIV
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn convert_from_udot_to_uksm_works() {
let price_in_udot = 77 * polkadot_runtime_constants::currency::UNITS;
let same_price_in_uksm = convert_from_udot_to_uksm(price_in_udot);

let price_in_dot = FixedU128::from_rational(price_in_udot, polkadot_runtime_constants::currency::UNITS);
let price_in_ksm = FixedU128::from_rational(same_price_in_uksm, kusama_runtime_constants::currency::UNITS);
assert_eq!(price_in_dot / FixedU128::saturating_from_integer(5), price_in_ksm);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ license.workspace = true

[dependencies]

# Local
kusama-runtime-constants = { path = "../../../../relay/kusama/constants", default-features = false}
polkadot-runtime-constants = { path = "../../../../relay/polkadot/constants", default-features = false}

# Bridge Dependencies
bp-bridge-hub-cumulus = { default-features = false , version = "0.4.0" }
bp-runtime = { default-features = false , version = "0.4.0" }
Expand All @@ -27,6 +31,8 @@ std = [
"bp-messages/std",
"bp-runtime/std",
"frame-support/std",
"kusama-runtime-constants/std",
"polkadot-runtime-constants/std",
"sp-api/std",
"sp-runtime/std",
"sp-std/std",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use bp_runtime::{
decl_bridge_finality_runtime_apis, decl_bridge_messages_runtime_apis, Chain, Parachain,
};
use frame_support::dispatch::DispatchClass;
use sp_runtime::RuntimeDebug;
use sp_runtime::{FixedPointNumber, FixedU128, RuntimeDebug, Saturating};

/// BridgeHubPolkadot parachain.
#[derive(RuntimeDebug)]
Expand Down Expand Up @@ -78,13 +78,62 @@ frame_support::parameter_types! {
/// The XCM fee that is paid for executing XCM program (with `ExportMessage` instruction) at the Polkadot
/// BridgeHub.
/// (initially was calculated by test `BridgeHubPolkadot::can_calculate_weight_for_paid_export_message_with_reserve_transfer` + `33%`)
pub const BridgeHubPolkadotBaseXcmFeeInDots: u128 = 4_858_960_000;
pub const BridgeHubPolkadotBaseXcmFeeInDots: Balance = 4_858_960_000;

/// Transaction fee that is paid at the Polkadot BridgeHub for delivering single inbound message.
/// (initially was calculated by test `BridgeHubPolkadot::can_calculate_fee_for_complex_message_delivery_transaction` + `33%`)
pub const BridgeHubPolkadotBaseDeliveryFeeInDots: u128 = 19_251_966_280;
pub const BridgeHubPolkadotBaseDeliveryFeeInDots: Balance = 19_251_966_280;

/// Transaction fee that is paid at the Polkadot BridgeHub for delivering single outbound message confirmation.
/// (initially was calculated by test `BridgeHubPolkadot::can_calculate_fee_for_complex_message_confirmation_transaction` + `33%`)
pub const BridgeHubPolkadotBaseConfirmationFeeInDots: u128 = 18_480_166_280;
pub const BridgeHubPolkadotBaseConfirmationFeeInDots: Balance = 18_480_166_280;
}

/// Compute the total estimated fee that needs to be paid in DOTs by the sender when sending
/// message from Polkadot Bridge Hub to Kusama Bridge Hub.
pub fn estimate_polkadot_to_kusama_message_fee(
bridge_hub_kusama_base_delivery_fee_in_uksms: Balance,
) -> Balance {
// Sender must pay:
//
// 1) an approximate cost of XCM execution (`ExportMessage` and surroundings) at Polkadot bridge Hub;
//
// 2) the approximate cost of Polkadot -> Kusama message delivery transaction on Kusama Bridge Hub,
// converted into KSMs using 1:5 conversion rate;
//
// 3) the approximate cost of Polkadot -> Kusama message confirmation transaction on Polkadot Bridge Hub.
BridgeHubPolkadotBaseXcmFeeInDots::get()
.saturating_add(convert_from_uksm_to_udot(bridge_hub_kusama_base_delivery_fee_in_uksms))
.saturating_add(BridgeHubPolkadotBaseConfirmationFeeInDots::get())
}

/// Convert from uKSMs to uDOTs.
fn convert_from_uksm_to_udot(price_in_uksm: Balance) -> Balance {
// assuming exchange rate is 5 DOTs for 1 KSM
let dot_to_ksm_economic_rate = FixedU128::from_rational(5, 1);
// tokens have different nominals and we need to take that into account
let nominal_ratio = FixedU128::from_rational(
polkadot_runtime_constants::currency::UNITS,
kusama_runtime_constants::currency::UNITS,
);

dot_to_ksm_economic_rate
.saturating_mul(nominal_ratio)
.saturating_mul(FixedU128::saturating_from_integer(price_in_uksm))
.into_inner() / FixedU128::DIV
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn convert_from_uksm_to_udot_works() {
let price_in_uksm = 77 * kusama_runtime_constants::currency::UNITS;
let same_price_in_udot = convert_from_uksm_to_udot(price_in_uksm);

let price_in_ksm = FixedU128::from_rational(price_in_uksm, kusama_runtime_constants::currency::UNITS);
let price_in_dot = FixedU128::from_rational(same_price_in_udot, polkadot_runtime_constants::currency::UNITS);
assert_eq!(price_in_dot / FixedU128::saturating_from_integer(5), price_in_ksm);
}
}

0 comments on commit 258ecfb

Please sign in to comment.