Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
buy weight unit test
  • Loading branch information
zqhxuyuan committed Jun 8, 2022
commit 2474256161cfe78b2a459e835aaa5bf7bfe3c425
19 changes: 2 additions & 17 deletions modules/support/src/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![allow(clippy::type_complexity)]
use crate::{AddressMapping, BuyWeightRate, CurrencyId, Erc20InfoMapping, Ratio, TransactionPayment};
use crate::{AddressMapping, CurrencyId, Erc20InfoMapping, TransactionPayment};
use codec::Encode;
use frame_support::pallet_prelude::{DispatchClass, Pays, Weight};
use nutsfinance_stable_asset::{
Expand All @@ -30,12 +30,11 @@ use primitives::{
};
use sp_core::{crypto::AccountId32, H160};
use sp_io::hashing::blake2_256;
use sp_runtime::{traits::One, transaction_validity::TransactionValidityError, DispatchError, DispatchResult};
use sp_runtime::{transaction_validity::TransactionValidityError, DispatchError, DispatchResult};
use sp_std::{marker::PhantomData, vec::Vec};

#[cfg(feature = "std")]
use frame_support::traits::Imbalance;
use xcm::latest::MultiLocation;

pub struct MockAddressMapping;

Expand Down Expand Up @@ -409,17 +408,3 @@ impl<CurrencyId, Balance, AccountId, BlockNumber> StableAsset
unimplemented!()
}
}

pub struct MockNoneMinimumBalance;
impl BuyWeightRate for MockNoneMinimumBalance {
fn calculate_rate(_: MultiLocation) -> Option<Ratio> {
None
}
}

pub struct MockFixedMinimumBalance;
impl BuyWeightRate for MockFixedMinimumBalance {
fn calculate_rate(_: MultiLocation) -> Option<Ratio> {
Some(Ratio::one())
}
}
2 changes: 1 addition & 1 deletion modules/transaction-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ where
C: Convert<MultiLocation, Option<CurrencyId>>,
{
fn calculate_rate(multi_location: MultiLocation) -> Option<Ratio> {
C::convert(multi_location).and_then(|token_id| TokenExchangeRate::<T>::get(token_id))
C::convert(multi_location).and_then(TokenExchangeRate::<T>::get)
}
}

Expand Down
26 changes: 23 additions & 3 deletions modules/transaction-payment/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use super::*;
use crate::mock::{AlternativeFeeSurplus, AusdFeeSwapPath, CustomFeeSurplus, DotFeeSwapPath};
use frame_support::{
assert_noop, assert_ok, parameter_types,
assert_noop, assert_ok,
weights::{DispatchClass, DispatchInfo, Pays},
};
use mock::{
Expand All @@ -39,10 +39,9 @@ use sp_runtime::{
testing::TestXt,
traits::{One, UniqueSaturatedInto},
};
use support::{Price, TransactionPayment as TransactionPaymentT};
use support::{BuyWeightRate, Price, TransactionPayment as TransactionPaymentT};
use xcm::latest::prelude::*;
use xcm::prelude::GeneralKey;
use xcm_executor::Assets;

const CALL: <Runtime as frame_system::Config>::Call = Call::Currencies(module_currencies::Call::transfer {
dest: BOB,
Expand Down Expand Up @@ -1362,6 +1361,27 @@ impl Convert<MultiLocation, Option<CurrencyId>> for CurrencyIdConvert {
}
}

#[test]
fn buy_weight_transaction_fee_pool_works() {
builder_with_dex_and_fee_pool(true).execute_with(|| {
// Location convert return None.
let location = MultiLocation::new(1, X1(Junction::Parachain(2000)));
let rate = <BuyWeightRateOfTransactionFeePool<Runtime, CurrencyIdConvert>>::calculate_rate(location);
assert_eq!(rate, None);

// Token not in charge fee pool
let currency_id = CurrencyId::Token(TokenSymbol::LDOT);
let location = MultiLocation::new(1, X1(GeneralKey(currency_id.encode())));
let rate = <BuyWeightRateOfTransactionFeePool<Runtime, CurrencyIdConvert>>::calculate_rate(location);
assert_eq!(rate, None);

// DOT Token is in charge fee pool.
let location = MultiLocation::parent();
let rate = <BuyWeightRateOfTransactionFeePool<Runtime, CurrencyIdConvert>>::calculate_rate(location);
assert_eq!(rate, Some(Ratio::saturating_from_rational(1, 10)));
});
}

#[test]
fn swap_from_pool_not_enough_currency() {
builder_with_dex_and_fee_pool(true).execute_with(|| {
Expand Down
45 changes: 45 additions & 0 deletions runtime/common/src/xcm_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,48 @@ impl<FixedRate: Get<u128>, R: TakeRevenue, M: BuyWeightRate> Drop for FixedRateO
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::mock::new_test_ext;
use frame_support::{assert_noop, assert_ok, parameter_types};
use module_support::Ratio;
use sp_runtime::traits::One;

pub struct MockNoneBuyWeightRate;
impl BuyWeightRate for MockNoneBuyWeightRate {
fn calculate_rate(_: MultiLocation) -> Option<Ratio> {
None
}
}

pub struct MockFixedBuyWeightRate<T: Get<Ratio>>(PhantomData<T>);
impl<T: Get<Ratio>> BuyWeightRate for MockFixedBuyWeightRate<T> {
fn calculate_rate(_: MultiLocation) -> Option<Ratio> {
Some(T::get())
}
}

parameter_types! {
const FixedBasedRate: u128 = 10;
FixedRate: Ratio = Ratio::one();
}

#[test]
fn buy_weight_rate_mock_works() {
new_test_ext().execute_with(|| {
let asset: MultiAsset = (Parent, 100).into();
let assets: Assets = asset.into();
let mut trader = <FixedRateOfAsset<(), (), MockNoneBuyWeightRate>>::new();
let buy_weight = trader.buy_weight(WEIGHT_PER_SECOND, assets.clone());
assert_noop!(buy_weight, XcmError::TooExpensive);

let mut trader = <FixedRateOfAsset<FixedBasedRate, (), MockFixedBuyWeightRate<FixedRate>>>::new();
let buy_weight = trader.buy_weight(WEIGHT_PER_SECOND, assets.clone());
let asset: MultiAsset = (Parent, 90).into();
let assets: Assets = asset.into();
assert_ok!(buy_weight, assets.clone());
});
}
}