Skip to content

Commit

Permalink
Include all fees in XCM message sent to AssetHub (#1000)
Browse files Browse the repository at this point in the history
Co-authored-by: Vincent Geddes <[email protected]>
  • Loading branch information
alistair-singh and vgeddes authored Nov 12, 2023
1 parent 618e61b commit 8895705
Show file tree
Hide file tree
Showing 9 changed files with 1,366 additions and 1,286 deletions.
26 changes: 17 additions & 9 deletions parachain/pallets/inbound-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use envelope::{Envelope, Log};
use frame_support::{
traits::{
fungible::{Inspect, Mutate},
tokens::Preservation,
tokens::{Fortitude, Precision, Preservation},
},
weights::WeightToFee,
PalletError,
Expand Down Expand Up @@ -113,7 +113,10 @@ pub mod pallet {
type GatewayAddress: Get<H160>;

/// Convert inbound message to XCM
type MessageConverter: ConvertMessage;
type MessageConverter: ConvertMessage<
AccountId = Self::AccountId,
Balance = BalanceOf<Self>,
>;

type WeightInfo: WeightInfo;

Expand Down Expand Up @@ -249,10 +252,15 @@ pub mod pallet {
)?;

// Decode message into XCM
let xcm = match inbound::VersionedMessage::decode_all(&mut envelope.payload.as_ref()) {
Ok(message) => Self::do_convert(envelope.message_id, message)?,
Err(_) => return Err(Error::<T>::InvalidPayload.into()),
};
let (xcm, fee) =
match inbound::VersionedMessage::decode_all(&mut envelope.payload.as_ref()) {
Ok(message) => Self::do_convert(envelope.message_id, message)?,
Err(_) => return Err(Error::<T>::InvalidPayload.into()),
};

// We embed fees for xcm execution inside the xcm program using teleports
// so we must burn the amount of the fee embedded into the program.
T::Token::burn_from(&sovereign_account, fee, Precision::Exact, Fortitude::Polite)?;

// Attempt to send XCM to a dest parachain
let message_id = Self::send_xcm(xcm, envelope.dest)?;
Expand Down Expand Up @@ -284,12 +292,12 @@ pub mod pallet {
pub fn do_convert(
message_id: H256,
message: inbound::VersionedMessage,
) -> Result<Xcm<()>, Error<T>> {
let mut xcm =
) -> Result<(Xcm<()>, BalanceOf<T>), Error<T>> {
let (mut xcm, fee) =
T::MessageConverter::convert(message).map_err(|e| Error::<T>::ConvertMessage(e))?;
// Append the message id as an XCM topic
xcm.inner_mut().extend(vec![SetTopic(message_id.into())]);
Ok(xcm)
Ok((xcm, fee))
}

pub fn send_xcm(xcm: Xcm<()>, dest: ParaId) -> Result<XcmHash, Error<T>> {
Expand Down
24 changes: 16 additions & 8 deletions parachain/pallets/inbound-queue/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ parameter_types! {
pub const BlockHashCount: u64 = 250;
}

type Balance = u128;

impl frame_system::Config for Test {
type BaseCallFilter = Everything;
type BlockWeights = ();
Expand Down Expand Up @@ -74,7 +76,7 @@ impl pallet_balances::Config for Test {
type MaxLocks = ();
type MaxReserves = ();
type ReserveIdentifier = [u8; 8];
type Balance = u128;
type Balance = Balance;
type RuntimeEvent = RuntimeEvent;
type DustRemoval = ();
type ExistentialDeposit = ConstU128<1>;
Expand Down Expand Up @@ -132,6 +134,7 @@ parameter_types! {
pub const GatewayAddress: H160 = H160(GATEWAY_ADDRESS);
pub const CreateAssetCall: [u8;2] = [53, 0];
pub const CreateAssetExecutionFee: u128 = 2_000_000_000;
pub const CreateAssetDeposit: u128 = 100_000_000_000;
pub const SendTokenExecutionFee: u128 = 1_000_000_000;
pub const InitialFund: u128 = 1_000_000_000_000;
}
Expand Down Expand Up @@ -177,8 +180,14 @@ impl inbound_queue::Config for Test {
type XcmSender = MockXcmSender;
type WeightInfo = ();
type GatewayAddress = GatewayAddress;
type MessageConverter =
MessageToXcm<CreateAssetCall, CreateAssetExecutionFee, SendTokenExecutionFee>;
type MessageConverter = MessageToXcm<
CreateAssetCall,
CreateAssetExecutionFee,
CreateAssetDeposit,
SendTokenExecutionFee,
AccountId,
Balance,
>;
#[cfg(feature = "runtime-benchmarks")]
type Helper = Test;
type WeightToFee = IdentityFee<u128>;
Expand Down Expand Up @@ -240,10 +249,6 @@ const BAD_OUTBOUND_QUEUE_EVENT_LOG: [u8; 254] = hex!(
"
);

const XCM_HASH: [u8; 32] = [
201, 101, 244, 67, 153, 61, 253, 203, 92, 23, 197, 172, 112, 209, 53, 248, 118, 25, 253, 110,
168, 201, 60, 156, 227, 26, 55, 145, 5, 177, 78, 189,
];
const ASSET_HUB_PARAID: u32 = 1000u32;
const TEMPLATE_PARAID: u32 = 1001u32;

Expand Down Expand Up @@ -271,7 +276,10 @@ fn test_submit_happy_path() {
expect_events(vec![InboundQueueEvent::MessageReceived {
dest: ASSET_HUB_PARAID.into(),
nonce: 1,
message_id: XCM_HASH,
message_id: [
3, 29, 43, 131, 7, 80, 47, 2, 238, 64, 45, 200, 64, 1, 46, 74, 121, 211, 8, 178,
198, 26, 230, 13, 180, 78, 164, 58, 22, 133, 206, 83,
],
}
.into()]);
});
Expand Down
Loading

0 comments on commit 8895705

Please sign in to comment.