Skip to content

Commit

Permalink
xcm fee
Browse files Browse the repository at this point in the history
  • Loading branch information
claravanstaden committed Nov 26, 2024
1 parent 990bf47 commit 9f7829c
Show file tree
Hide file tree
Showing 8 changed files with 756 additions and 668 deletions.
7 changes: 5 additions & 2 deletions bridges/snowbridge/pallets/inbound-queue-v2/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>
//! Implements the dry-run API.
use crate::{Config, Error};
use crate::{Config, Error, Junction::AccountId32, Location};
use snowbridge_core::inbound::Proof;
use snowbridge_router_primitives::inbound::v2::{ConvertMessage, Message};
use sp_core::H256;
use xcm::latest::Xcm;

pub fn dry_run<T>(message: Message, _proof: Proof) -> Result<Xcm<()>, Error<T>>
where
T: Config,
{
let xcm = T::MessageConverter::convert(message).map_err(|e| Error::<T>::ConvertMessage(e))?;
let dummy_origin = Location::new(0, AccountId32 { id: H256::zero().into(), network: None });
let xcm = T::MessageConverter::convert(message, dummy_origin)
.map_err(|e| Error::<T>::ConvertMessage(e))?;
Ok(xcm)
}
31 changes: 20 additions & 11 deletions bridges/snowbridge/pallets/inbound-queue-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ mod mock;
#[cfg(test)]
mod test;

use alloc::boxed::Box;
use codec::{Decode, DecodeAll, Encode};
use envelope::Envelope;
use frame_support::{
Expand All @@ -50,7 +51,6 @@ use scale_info::TypeInfo;
use sp_core::H160;
use sp_std::vec;
use types::Nonce;
use alloc::boxed::Box;
use xcm::prelude::{Junction::*, Location, *};

use snowbridge_core::{
Expand Down Expand Up @@ -141,6 +141,8 @@ pub mod pallet {
InvalidEnvelope,
/// Message has an unexpected nonce.
InvalidNonce,
/// Fee provided is invalid.
InvalidFee,
/// Message has an invalid payload.
InvalidPayload,
/// Message channel is invalid
Expand Down Expand Up @@ -206,12 +208,13 @@ pub mod pallet {
let message = MessageV2::decode_all(&mut envelope.payload.as_ref())
.map_err(|_| Error::<T>::InvalidPayload)?;

let xcm =
T::MessageConverter::convert(message).map_err(|e| Error::<T>::ConvertMessage(e))?;
let origin_account_location = Self::account_to_location(who)?;

let xcm = Self::do_convert(message, origin_account_location.clone())?;

// Burn the required fees for the static XCM message part
burn_fees::<T::AssetTransactor, BalanceOf<T>>(
Self::account_to_location(who)?,
origin_account_location,
T::XcmPrologueFee::get(),
)?;

Expand Down Expand Up @@ -255,17 +258,23 @@ pub mod pallet {
Ok(Location::new(0, [AccountId32 { network: None, id: account_bytes }]))
}

pub fn send_xcm(origin: OriginFor<T>, xcm: Xcm<()>, dest_para_id: u32) -> Result<XcmHash, DispatchError> {
let versioned_dest = Box::new(VersionedLocation::V5(Location::new(
1,
[Parachain(dest_para_id)],
)));
pub fn send_xcm(
origin: OriginFor<T>,
xcm: Xcm<()>,
dest_para_id: u32,
) -> Result<XcmHash, DispatchError> {
let versioned_dest =
Box::new(VersionedLocation::V5(Location::new(1, [Parachain(dest_para_id)])));
let versioned_xcm = Box::new(VersionedXcm::V5(xcm));
Ok(T::XcmSender::send(origin, versioned_dest, versioned_xcm)?)
}

pub fn do_convert(message: MessageV2) -> Result<Xcm<()>, Error<T>> {
Ok(T::MessageConverter::convert(message, T::XcmPrologueFee::get().into()).map_err(|e| Error::<T>::ConvertMessage(e))?)
pub fn do_convert(
message: MessageV2,
origin_account_location: Location,
) -> Result<Xcm<()>, Error<T>> {
Ok(T::MessageConverter::convert(message, origin_account_location)
.map_err(|e| Error::<T>::ConvertMessage(e))?)
}
}
}
21 changes: 12 additions & 9 deletions bridges/snowbridge/pallets/inbound-queue-v2/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ use snowbridge_core::{
TokenId,
};
use snowbridge_router_primitives::inbound::v2::MessageToXcm;
use sp_core::H160;
use sp_core::{H160, H256};
use sp_runtime::{
traits::{IdentifyAccount, IdentityLookup, MaybeEquivalence, Verify},
BuildStorage, MultiSignature,
BuildStorage, DispatchError, MultiSignature,
};
use sp_std::{convert::From, default::Default};
use xcm::prelude::*;
use xcm_executor::{traits::TransactAsset, AssetsInHolding};
use xcm_builder::SendControllerWeightInfo;
use sp_runtime::DispatchError;
use sp_core::H256;
use xcm_executor::{traits::TransactAsset, AssetsInHolding};

type Block = frame_system::mocking::MockBlock<Test>;

Expand Down Expand Up @@ -113,7 +111,6 @@ impl<T: snowbridge_pallet_ethereum_client::Config> BenchmarkHelper<T> for Test {
fn initialize_storage(_: BeaconHeader, _: H256) {}
}


pub struct MockXcmSenderWeights;

impl SendControllerWeightInfo for MockXcmSenderWeights {
Expand Down Expand Up @@ -153,17 +150,23 @@ parameter_types! {
pub AssetHubLocation: InteriorLocation = Parachain(1000).into();
}

const XCM_PROLOGUE_FEE: u128 = 1_000_000_000_000;

impl inbound_queue_v2::Config for Test {
type RuntimeEvent = RuntimeEvent;
type Verifier = MockVerifier;
type XcmSender = MockXcmSender;
type WeightInfo = ();
type GatewayAddress = GatewayAddress;
type AssetHubParaId = ConstU32<1000>;
type MessageConverter =
MessageToXcm<EthereumNetwork, InboundQueuePalletInstance, MockTokenIdConvert>;
type MessageConverter = MessageToXcm<
EthereumNetwork,
InboundQueuePalletInstance,
MockTokenIdConvert,
ConstU128<XCM_PROLOGUE_FEE>,
>;
type Token = Balances;
type XcmPrologueFee = ConstU128<1_000_000_000>;
type XcmPrologueFee = ConstU128<XCM_PROLOGUE_FEE>;
type AssetTransactor = SuccessfulTransactor;
#[cfg(feature = "runtime-benchmarks")]
type Helper = Test;
Expand Down
1 change: 0 additions & 1 deletion bridges/snowbridge/primitives/router/src/inbound/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,4 @@ impl<AccountId> EthereumLocationsConverterFor<AccountId> {
}
}


pub type CallIndex = [u8; 2];
Loading

0 comments on commit 9f7829c

Please sign in to comment.