Skip to content

Commit

Permalink
fix integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NunoAlexandre committed Aug 19, 2022
1 parent eb04ffe commit 8bd1f83
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 53 deletions.
2 changes: 1 addition & 1 deletion runtime/altair/src/xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use xcm_executor::{traits::JustTry, XcmExecutor};
pub use common_types::CurrencyId;
use runtime_common::{
decimals, parachains,
xcm::{FixedConversionRateProvider, general_key},
xcm::{general_key, FixedConversionRateProvider},
xcm_fees::{default_per_second, ksm_per_second, native_per_second},
};

Expand Down
2 changes: 1 addition & 1 deletion runtime/centrifuge/src/xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ use xcm_builder::{
};
use xcm_executor::{traits::JustTry, XcmExecutor};

use runtime_common::xcm::general_key;
use runtime_common::{
decimals, parachains,
xcm::FixedConversionRateProvider,
xcm_fees::{default_per_second, native_per_second},
CurrencyId,
};
use runtime_common::xcm::general_key;

/// The main XCM config
/// This is where we configure the core of our XCM integrations: how tokens are transferred,
Expand Down
27 changes: 18 additions & 9 deletions runtime/common/src/fixed_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
use codec::{CompactAs, Decode, Encode};
use sp_arithmetic::{
Rounding,
helpers_128bit::{multiply_by_rational_with_rounding},
helpers_128bit::multiply_by_rational_with_rounding,
traits::{Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, One, Saturating, Zero},
FixedPointNumber, FixedPointOperand,
FixedPointNumber, FixedPointOperand, Rounding,
};
use sp_std::{
ops::{self},
Expand Down Expand Up @@ -256,9 +255,14 @@ macro_rules! implement_fixed {
let negative = lhs.negative != rhs.negative;

//todo(nuno): check with runtime if this rounding is fine
multiply_by_rational_with_rounding(lhs.value, Self::DIV as u128, rhs.value, Rounding::NearestPrefDown)
.and_then(|value| from_i129(I129 { value, negative }))
.map(Self)
multiply_by_rational_with_rounding(
lhs.value,
Self::DIV as u128,
rhs.value,
Rounding::NearestPrefDown,
)
.and_then(|value| from_i129(I129 { value, negative }))
.map(Self)
}
}

Expand All @@ -269,9 +273,14 @@ macro_rules! implement_fixed {
let negative = lhs.negative != rhs.negative;

//todo(nuno): check with runtime if this rounding is fine
multiply_by_rational_with_rounding(lhs.value, rhs.value, Self::DIV as u128, Rounding::NearestPrefDown)
.and_then(|value| from_i129(I129 { value, negative }))
.map(Self)
multiply_by_rational_with_rounding(
lhs.value,
rhs.value,
Self::DIV as u128,
Rounding::NearestPrefDown,
)
.and_then(|value| from_i129(I129 { value, negative }))
.map(Self)
}
}

Expand Down
7 changes: 5 additions & 2 deletions runtime/common/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,10 @@ pub mod xcm {
use crate::{xcm_fees::default_per_second, Balance, CustomMetadata};
use common_types::CurrencyId;
use frame_support::sp_std::marker::PhantomData;
use xcm::latest::MultiLocation;
use sp_runtime::traits::ConstU32;
use sp_runtime::WeakBoundedVec;
use xcm::latest::Junction::GeneralKey;
use xcm::latest::MultiLocation;

/// Our FixedConversionRateProvider, used to charge XCM-related fees for tokens registered in
/// the asset registry that were not already handled by native Trader rules.
Expand All @@ -282,7 +282,10 @@ pub mod xcm {
}

pub fn general_key(key: &[u8]) -> xcm::latest::Junction {
GeneralKey(WeakBoundedVec::<u8, ConstU32<32>>::force_from(key.into(), None))
GeneralKey(WeakBoundedVec::<u8, ConstU32<32>>::force_from(
key.into(),
None,
))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ use altair_runtime::{
use frame_support::assert_noop;
use frame_support::assert_ok;
use orml_traits::{asset_registry::AssetMetadata, FixedConversionRateProvider, MultiCurrency};
use runtime_common::xcm::general_key;
use runtime_common::xcm_fees::{default_per_second, ksm_per_second};
use runtime_common::{decimals, parachains, Balance, XcmMetadata};
use sp_runtime::traits::BadOrigin;
use xcm::latest::{Junction, Junction::*, Junctions::*, MultiLocation, NetworkId};
use xcm::prelude::{Parachain, X2};
use xcm::VersionedMultiLocation;
use xcm_emulator::TestExt;
use runtime_common::xcm::general_key;

#[test]
fn register_air_works() {
Expand Down Expand Up @@ -102,7 +102,7 @@ fn register_tranche_asset_blocked() {
existential_deposit: 1_000_000_000_000,
location: Some(VersionedMultiLocation::V1(MultiLocation::new(
1,
X2(Parachain(2000), general_key(vec![42])),
X2(Parachain(2000), general_key(&[42])),
))),
additional: CustomMetadata::default(),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,22 @@ use altair_runtime::CurrencyIdConvert;
use altair_runtime::{Balances, CustomMetadata, Origin, OrmlAssetRegistry, OrmlTokens, XTokens};
use frame_support::assert_ok;
use orml_traits::{asset_registry::AssetMetadata, FixedConversionRateProvider, MultiCurrency};
use runtime_common::xcm::general_key;
use runtime_common::xcm_fees::{default_per_second, ksm_per_second};
use runtime_common::{decimals, parachains, Balance, XcmMetadata};
use sp_runtime::traits::Convert as C2;
use xcm::latest::{Junction, Junction::*, Junctions::*, MultiLocation, NetworkId};
use xcm::VersionedMultiLocation;
use xcm_emulator::TestExt;
use xcm_executor::traits::Convert as C1;
use runtime_common::xcm::general_key;

#[test]
fn convert_air() {
assert_eq!(parachains::kusama::altair::AIR_KEY.to_vec().into(), vec![0, 1]);
assert_eq!(parachains::kusama::altair::AIR_KEY.to_vec(), vec![0, 1]);

// The way AIR is represented relative within the Altair runtime
let air_location_inner: MultiLocation = MultiLocation::new(
0,
X1(general_key(parachains::kusama::altair::AIR_KEY)),
);
let air_location_inner: MultiLocation =
MultiLocation::new(0, X1(general_key(parachains::kusama::altair::AIR_KEY)));

assert_eq!(
<CurrencyIdConvert as C1<_, _>>::convert(air_location_inner),
Expand All @@ -74,7 +72,7 @@ fn convert_air() {

#[test]
fn convert_ausd() {
assert_eq!(parachains::kusama::karura::AUSD_KEY.to_vec().into(), vec![0, 129]);
assert_eq!(parachains::kusama::karura::AUSD_KEY, &[0, 129]);

let ausd_location: MultiLocation = MultiLocation::new(
1,
Expand Down Expand Up @@ -120,7 +118,7 @@ fn convert_unkown_multilocation() {
1,
X2(
Parachain(parachains::kusama::altair::ID),
general_key([42].to_vec()),
general_key(&[42]),
),
);

Expand Down
18 changes: 9 additions & 9 deletions runtime/integration-tests/src/xcm/kusama/tests/transfers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ use crate::xcm::kusama::setup::{
use crate::xcm::kusama::test_net::{Altair, Karura, KusamaNet, Sibling, TestNet};

use altair_runtime::{Balances, CustomMetadata, Origin, OrmlAssetRegistry, OrmlTokens, XTokens};
use runtime_common::xcm::general_key;
use runtime_common::xcm_fees::{default_per_second, ksm_per_second};
use runtime_common::{decimals, parachains, Balance, XcmMetadata};
use runtime_common::xcm::general_key;

#[test]
fn transfer_air_to_sibling() {
Expand Down Expand Up @@ -122,7 +122,7 @@ fn transfer_air_to_sibling() {
assert_eq!(current_balance, transfer_amount - fee(18));

// Sanity check for the actual amount BOB ends up with
assert_eq!(current_balance, 4990676000000000000);
assert_eq!(current_balance, 4990730400000000000);
});
}

Expand Down Expand Up @@ -272,7 +272,7 @@ fn transfer_ausd_to_altair() {
// Sanity check the actual balance
assert_eq!(
OrmlTokens::free_balance(CurrencyId::AUSD, &BOB.into()),
16990676000000
16990730400000
);
});
}
Expand Down Expand Up @@ -342,7 +342,7 @@ fn transfer_foreign_sibling_to_altair() {
let alice_initial_balance = air(10);
let sibling_asset_id = CurrencyId::ForeignAsset(1);
let asset_location =
MultiLocation::new(1, X2(Parachain(PARA_ID_SIBLING), general_key(vec![0, 1])));
MultiLocation::new(1, X2(Parachain(PARA_ID_SIBLING), general_key(&[0, 1])));
let meta: AssetMetadata<Balance, CustomMetadata> = AssetMetadata {
decimals: 18,
name: "Sibling Native Token".into(),
Expand Down Expand Up @@ -423,7 +423,7 @@ fn transfer_wormhole_usdc_karura_to_altair() {
1,
X2(
Parachain(parachains::kusama::karura::ID),
general_key("0x02f3a00dd12f644daec907013b16eb6d14bf1c4cb4".into()),
general_key("0x02f3a00dd12f644daec907013b16eb6d14bf1c4cb4".as_bytes()),
),
);
let meta: AssetMetadata<Balance, CustomMetadata> = AssetMetadata {
Expand Down Expand Up @@ -496,15 +496,15 @@ fn transfer_wormhole_usdc_karura_to_altair() {
let bob_balance = OrmlTokens::free_balance(usdc_asset_id, &BOB.into());

// Sanity check to ensure the calculated is what is expected
assert_eq!(bob_balance, 11990676);
assert_eq!(bob_balance, 11990731);
});
}

#[test]
fn test_total_fee() {
assert_eq!(air_fee(), 9324000000000000);
assert_eq!(fee(decimals::AUSD), 9324000000);
assert_eq!(fee(decimals::KSM), 9324000000);
assert_eq!(air_fee(), 9269600000000000);
assert_eq!(fee(decimals::AUSD), 9269600000);
assert_eq!(fee(decimals::KSM), 9269600000);
}

fn air_fee() -> Balance {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ use centrifuge_runtime::{
};
use frame_support::{assert_noop, assert_ok, error::BadOrigin};
use orml_traits::{asset_registry::AssetMetadata, FixedConversionRateProvider, MultiCurrency};
use runtime_common::{xcm::general_key, xcm_fees::{default_per_second, ksm_per_second}};
use runtime_common::{decimals, parachains, Balance, XcmMetadata};
use runtime_common::{
xcm::general_key,
xcm_fees::{default_per_second, ksm_per_second},
};
use xcm::latest::{Junction, Junction::*, Junctions::*, MultiLocation, NetworkId};
use xcm::VersionedMultiLocation;
use xcm_emulator::TestExt;
Expand All @@ -48,9 +51,7 @@ fn register_cfg_works() {
existential_deposit: 1_000_000_000_000,
location: Some(VersionedMultiLocation::V1(MultiLocation::new(
0,
X1(general_key(
parachains::polkadot::centrifuge::CFG_KEY,
)),
X1(general_key(parachains::polkadot::centrifuge::CFG_KEY)),
))),
additional: CustomMetadata::default(),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,23 @@ use centrifuge_runtime::{
};
use frame_support::{assert_noop, assert_ok};
use orml_traits::{asset_registry::AssetMetadata, FixedConversionRateProvider, MultiCurrency};
use runtime_common::xcm::general_key;
use runtime_common::xcm_fees::{default_per_second, ksm_per_second};
use runtime_common::{decimals, parachains, Balance, XcmMetadata};
use sp_runtime::traits::Convert as C2;
use xcm::latest::{Junction, Junction::*, Junctions::*, MultiLocation, NetworkId};
use xcm::VersionedMultiLocation;
use xcm_emulator::TestExt;
use xcm_executor::traits::Convert as C1;
use runtime_common::xcm::general_key;

#[test]
fn convert_cfg() {
assert_eq!(
parachains::polkadot::centrifuge::CFG_KEY.to_vec().into(),
vec![0, 1]
);
assert_eq!(parachains::polkadot::centrifuge::CFG_KEY, &[0, 1]);

// The way CFG is represented relative within the Centrifuge runtime
let cfg_location_inner: MultiLocation = MultiLocation::new(
0,
X1(general_key(
parachains::polkadot::centrifuge::CFG_KEY,
)),
X1(general_key(parachains::polkadot::centrifuge::CFG_KEY)),
);

assert_eq!(
Expand All @@ -82,7 +77,7 @@ fn convert_cfg() {

#[test]
fn convert_ausd() {
assert_eq!(parachains::polkadot::acala::AUSD_KEY.to_vec().into(), vec![0, 1]);
assert_eq!(parachains::polkadot::acala::AUSD_KEY, &[0, 1]);

let ausd_location: MultiLocation = MultiLocation::new(
1,
Expand Down
16 changes: 8 additions & 8 deletions runtime/integration-tests/src/xcm/polkadot/tests/transfers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ use centrifuge_runtime::{
};
use frame_support::assert_ok;
use orml_traits::{asset_registry::AssetMetadata, FixedConversionRateProvider, MultiCurrency};
use runtime_common::xcm::general_key;
use runtime_common::xcm_fees::{default_per_second, ksm_per_second};
use runtime_common::{decimals, parachains, Balance, XcmMetadata};
use sp_runtime::traits::BadOrigin;
use xcm::latest::{Junction, Junction::*, Junctions::*, MultiLocation, NetworkId};
use xcm::VersionedMultiLocation;
use xcm_emulator::TestExt;
use runtime_common::xcm::general_key;

#[test]
fn transfer_cfg_to_sibling() {
Expand Down Expand Up @@ -122,7 +122,7 @@ fn transfer_cfg_to_sibling() {
assert_eq!(current_balance, transfer_amount - fee(18));

// Sanity check for the actual amount BOB ends up with
assert_eq!(current_balance, 4990676000000000000);
assert_eq!(current_balance, 4990730400000000000);
});
}

Expand Down Expand Up @@ -272,7 +272,7 @@ fn transfer_ausd_to_centrifuge() {
// Sanity check the actual balance
assert_eq!(
OrmlTokens::free_balance(CurrencyId::AUSD, &BOB.into()),
16990676000000
16990730400000
);
});
}
Expand Down Expand Up @@ -431,7 +431,7 @@ fn transfer_wormhole_usdc_acala_to_centrifuge() {
1,
X2(
Parachain(parachains::polkadot::acala::ID),
general_key("0x02f3a00dd12f644daec907013b16eb6d14bf1c4cb4".into()),
general_key("0x02f3a00dd12f644daec907013b16eb6d14bf1c4cb4".as_bytes()),
),
);
let meta: AssetMetadata<Balance, CustomMetadata> = AssetMetadata {
Expand Down Expand Up @@ -502,15 +502,15 @@ fn transfer_wormhole_usdc_acala_to_centrifuge() {
let bob_balance = OrmlTokens::free_balance(usdc_asset_id, &BOB.into());

// Sanity check to ensure the calculated is what is expected
assert_eq!(bob_balance, 11990676);
assert_eq!(bob_balance, 11990731);
});
}

#[test]
fn test_total_fee() {
assert_eq!(cfg_fee(), 9324000000000000);
assert_eq!(fee(decimals::AUSD), 9324000000);
assert_eq!(fee(decimals::KSM), 9324000000);
assert_eq!(cfg_fee(), 9269600000000000);
assert_eq!(fee(decimals::AUSD), 9269600000);
assert_eq!(fee(decimals::KSM), 9269600000);
}

fn cfg_fee() -> Balance {
Expand Down

0 comments on commit 8bd1f83

Please sign in to comment.