Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Use a const fee instead
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Jun 26, 2024
1 parent 031eceb commit 8ad87ba
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 52 deletions.
2 changes: 2 additions & 0 deletions bridges/snowbridge/Cargo.lock

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

30 changes: 5 additions & 25 deletions bridges/snowbridge/pallets/inbound-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ use sp_core::H160;
use sp_runtime::traits::Zero;
use sp_std::vec;
use xcm::prelude::{
send_xcm, validate_send, Junction::*, Location, SendError as XcmpSendError, SendXcm, Xcm,
XcmContext, XcmHash, *,
send_xcm, Junction::*, Location, SendError as XcmpSendError, SendXcm, Xcm, XcmContext, XcmHash,
*,
};
use xcm_executor::traits::TransactAsset;

Expand Down Expand Up @@ -143,7 +143,7 @@ pub mod pallet {
type AssetTransactor: TransactAsset;

/// The most expensive xcm here only used to estimate send cost
type MaxSendCostXcm: Get<Xcm<()>>;
type MaxSendCost: Get<BalanceOf<Self>>;
}

#[pallet::hooks]
Expand Down Expand Up @@ -362,33 +362,13 @@ pub mod pallet {
})?;
Ok(())
}

pub fn calculate_send_cost(
para_id: ParaId,
xcm: Xcm<()>,
) -> Result<BalanceOf<T>, Error<T>> {
let dest = Location::new(1, [Parachain(para_id.into())]);
let (_, assets) = validate_send::<T::XcmSender>(dest, xcm).map_err(Error::<T>::from)?;
ensure!(assets.len() == 1, Error::<T>::Send(SendError::Fees));
let fee = assets.get(0).unwrap();
let cost: u128 = match (*fee).fun {
Fungible(amount) => Some(amount),
_ => None,
}
.ok_or(Error::<T>::Send(SendError::Fees))?;
Ok(cost.saturated_into::<BalanceOf<T>>())
}
}

/// API for accessing the delivery cost of a message
impl<T: Config> Get<BalanceOf<T>> for Pallet<T> {
fn get() -> BalanceOf<T> {
// Cost here based on MaxMessagePayloadSize(the worst case)
let delivery_cost = Self::calculate_delivery_cost(T::MaxMessageSize::get());
// Cost here based on MaxSendCostXcm(the worst case)
let send_cost = Self::calculate_send_cost(1000_u32.into(), T::MaxSendCostXcm::get())
.unwrap_or_default();
delivery_cost.saturating_add(send_cost)
Self::calculate_delivery_cost(T::MaxMessageSize::get())
.saturating_add(T::MaxSendCost::get())
}
}
}
24 changes: 2 additions & 22 deletions bridges/snowbridge/pallets/inbound-queue/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,27 +176,7 @@ parameter_types! {
multiplier: FixedU128::from_rational(1, 1),
};
pub DefaultFee: Asset = (Parent, 1_000_000_000).into();
pub MaxSendCostXcm: Xcm<()> = vec![
ReceiveTeleportedAsset(DefaultFee::get().into()),
BuyExecution { fees: DefaultFee::get(), weight_limit: Unlimited },
DescendOrigin(PalletInstance(80).into()),
UniversalOrigin(GlobalConsensus(Ethereum { chain_id: 1 })),
ReserveAssetDeposited(DefaultFee::get().into()),
ClearOrigin,
DepositReserveAsset {
assets: Definite(DefaultFee::get().into()),
dest: Location::new(1, [Parachain(1000)]),
xcm: vec![
// Buy execution on target.
BuyExecution { fees: DefaultFee::get(), weight_limit: Unlimited },
// Deposit asset to beneficiary.
DepositAsset { assets: Definite(DefaultFee::get().into()), beneficiary: Location::default() },
// Forward message id to destination parachain.
SetTopic([0; 32]),
]
.into(),
},
].into();
pub MaxSendCost: u128 = 1;
}

pub const DOT: u128 = 10_000_000_000;
Expand Down Expand Up @@ -270,7 +250,7 @@ impl inbound_queue::Config for Test {
type LengthToFee = IdentityFee<u128>;
type MaxMessageSize = ConstU32<1024>;
type AssetTransactor = SuccessfulTransactor;
type MaxSendCostXcm = MaxSendCostXcm;
type MaxSendCost = MaxSendCost;
}

pub fn last_events(n: usize) -> Vec<RuntimeEvent> {
Expand Down
6 changes: 1 addition & 5 deletions bridges/snowbridge/runtime/test-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,11 +595,7 @@ where
<Runtime as snowbridge_pallet_inbound_queue::Config>::MaxMessageSize::get(),
);
let inbound_send_cost =
<snowbridge_pallet_inbound_queue::Pallet<Runtime>>::calculate_send_cost(
1000.into(),
<Runtime as snowbridge_pallet_inbound_queue::Config>::MaxSendCostXcm::get(),
)
.unwrap();
<Runtime as snowbridge_pallet_inbound_queue::Config>::MaxSendCost::get();
let outbound_delivery_cost =
<snowbridge_pallet_outbound_queue::Pallet<Runtime>>::calculate_local_fee();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ parameter_types! {
rewards: Rewards { local: 1 * UNITS, remote: meth(1) },
multiplier: FixedU128::from_rational(1, 1),
};
pub MaxSendCost: Balance = UNITS / 10;
}

impl snowbridge_pallet_inbound_queue::Config for Runtime {
Expand Down Expand Up @@ -90,6 +91,7 @@ impl snowbridge_pallet_inbound_queue::Config for Runtime {
type WeightInfo = crate::weights::snowbridge_pallet_inbound_queue::WeightInfo<Runtime>;
type PricingParameters = EthereumSystem;
type AssetTransactor = <xcm_config::XcmConfig as xcm_executor::Config>::AssetTransactor;
type MaxSendCost = MaxSendCost;
}

impl snowbridge_pallet_outbound_queue::Config for Runtime {
Expand Down

0 comments on commit 8ad87ba

Please sign in to comment.