Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distinguish when MinXcmFee is not registered and FeeNotEnough #753

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions traits/src/get_by_key.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use xcm::v1::MultiLocation;

/// A trait for querying a value by a key.
pub trait GetByKey<Key, Value> {
/// Return the value.
Expand Down Expand Up @@ -55,3 +57,11 @@ mod tests {
assert_eq!(Test::get(&3), 2);
}
}

// Default implementation for xTokens::MinXcmFee
pub struct DisabledParachainFee;
ghzlatarev marked this conversation as resolved.
Show resolved Hide resolved
impl GetByKey<MultiLocation, Option<u128>> for DisabledParachainFee {
fn get(_key: &MultiLocation) -> Option<u128> {
None
}
}
ghzlatarev marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 6 additions & 6 deletions xtokens/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,22 @@ Parachains should implements config `MinXcmFee` in `xtokens` module config:

```rust
parameter_type_with_key! {
pub ParachainMinFee: |location: MultiLocation| -> u128 {
pub ParachainMinFee: |location: MultiLocation| -> Option<u128> {
#[allow(clippy::match_ref_pats)] // false positive
match (location.parents, location.first_interior()) {
(1, Some(Parachain(parachains::statemine::ID))) => 4_000_000_000,
_ => u128::MAX,
(1, Some(Parachain(parachains::statemine::ID))) => Some(4_000_000_000),
_ => None,
}
};
}
```

If Parachain don't want have this case, can simply return Max value:
If Parachain don't want have this case, can simply return None:

```rust
parameter_type_with_key! {
pub ParachainMinFee: |_location: MultiLocation| -> u128 {
u128::MAX
pub ParachainMinFee: |_location: MultiLocation| -> Option<u128> {
None
};
}
```
Expand Down
6 changes: 4 additions & 2 deletions xtokens/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub mod module {
type SelfLocation: Get<MultiLocation>;

/// Minimum xcm execution fee paid on destination chain.
type MinXcmFee: GetByKey<MultiLocation, u128>;
type MinXcmFee: GetByKey<MultiLocation, Option<u128>>;
ghzlatarev marked this conversation as resolved.
Show resolved Hide resolved

/// XCM executor.
type XcmExecutor: ExecuteXcm<Self::Call>;
Expand Down Expand Up @@ -173,6 +173,8 @@ pub mod module {
FeeNotEnough,
/// Not supported MultiLocation
NotSupportedMultiLocation,
/// MinXcmFee not registered for certain reserve location
MinXcmFeeNotDefined,
}

#[pallet::hooks]
Expand Down Expand Up @@ -538,7 +540,7 @@ pub mod module {
ensure!(non_fee_reserve == dest.chain_part(), Error::<T>::InvalidAsset);

let reserve_location = non_fee_reserve.clone().ok_or(Error::<T>::AssetHasNoReserve)?;
let min_xcm_fee = T::MinXcmFee::get(&reserve_location);
let min_xcm_fee = T::MinXcmFee::get(&reserve_location).ok_or(Error::<T>::MinXcmFeeNotDefined)?;

// min xcm fee should less than user fee
let fee_to_dest: MultiAsset = (fee.id.clone(), min_xcm_fee).into();
Expand Down
6 changes: 3 additions & 3 deletions xtokens/src/mock/para.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,11 @@ match_types! {
}

parameter_type_with_key! {
pub ParachainMinFee: |location: MultiLocation| -> u128 {
pub ParachainMinFee: |location: MultiLocation| -> Option<u128> {
#[allow(clippy::match_ref_pats)] // false positive
match (location.parents, location.first_interior()) {
(1, Some(Parachain(2))) => 40,
_ => u128::MAX,
(1, Some(Parachain(2))) => Some(40),
_ => None,
}
};
}
Expand Down
6 changes: 3 additions & 3 deletions xtokens/src/mock/para_relative_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,11 @@ match_types! {
}

parameter_type_with_key! {
pub ParachainMinFee: |location: MultiLocation| -> u128 {
pub ParachainMinFee: |location: MultiLocation| -> Option<u128> {
#[allow(clippy::match_ref_pats)] // false positive
match (location.parents, location.first_interior()) {
(1, Some(Parachain(2))) => 40,
_ => u128::MAX,
(1, Some(Parachain(2))) => Some(40),
_ => None,
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion xtokens/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ fn transfer_asset_with_relay_fee_failed() {
),
40,
),
Error::<para::Runtime>::FeeNotEnough
Error::<para::Runtime>::MinXcmFeeNotDefined
);
});
}
Expand Down