Skip to content

Commit

Permalink
feat: final tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mustermeiszer committed Mar 1, 2024
1 parent c1530b5 commit 9e34107
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 33 deletions.
111 changes: 79 additions & 32 deletions runtime/integration-tests/src/generic/cases/oracles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ mod ratio_provider {
use cfg_traits::ValueProvider;
use cfg_types::{
fixed_point::Ratio,
oracles::OracleKey,
tokens::{CrossChainTransferability, CurrencyId, CustomMetadata, LocalAssetId},
};
use frame_support::traits::OriginTrait;
use runtime_common::oracle::Feeder;
use sp_runtime::traits::One;
use sp_runtime::{traits::One, FixedPointNumber};

use crate::{
generic::{
Expand All @@ -32,14 +33,25 @@ mod ratio_provider {
test_for_runtimes,
};

pub struct LocalUSDC;
impl CurrencyInfo for LocalUSDC {
pub struct OtherLocal;
impl CurrencyInfo for OtherLocal {
fn id(&self) -> CurrencyId {
CurrencyId::LocalAsset(LocalAssetId(1))
CurrencyId::LocalAsset(LocalAssetId(101))
}

fn custom(&self) -> CustomMetadata {
CustomMetadata {
pool_currency: true,
transferability: CrossChainTransferability::None,
..CONST_DEFAULT_CUSTOM
}
}
}

fn symbol(&self) -> &'static str {
"LocalUSDC"
pub struct LocalUSDC;
impl CurrencyInfo for LocalUSDC {
fn id(&self) -> CurrencyId {
CurrencyId::LocalAsset(LocalAssetId(100))
}

fn custom(&self) -> CustomMetadata {
Expand All @@ -54,69 +66,104 @@ mod ratio_provider {
pub struct DomainUSDC;
impl CurrencyInfo for DomainUSDC {
fn id(&self) -> CurrencyId {
CurrencyId::ForeignAsset(100_0001)
}

fn symbol(&self) -> &'static str {
"DomainUSDC"
CurrencyId::ForeignAsset(200_0001)
}

fn custom(&self) -> CustomMetadata {
CustomMetadata {
pool_currency: true,
transferability: CrossChainTransferability::LiquidityPools,
local_representation: Some(LocalAssetId(1)),
..Default::default()
local_representation: Some(LocalAssetId(100)),
..CONST_DEFAULT_CUSTOM
}
}
}

fn get_rate<T: Runtime>(key: (CurrencyId, CurrencyId)) -> Ratio {
fn feeder<T: Runtime>() -> Feeder<T::RuntimeOriginExt> {
pallet_order_book::MarketFeederId::<T>::get().unwrap()
}

fn get_rate_with<T: Runtime>(
key: (CurrencyId, CurrencyId),
setup: impl FnOnce(),
) -> Option<Ratio> {
let mut env = RuntimeEnv::<T>::default();

env.parachain_state_mut(|| {
assert_eq!(
orml_asset_registry::Metadata::<T>::get(LocalUSDC.id()),
None
);
assert_eq!(
orml_asset_registry::Metadata::<T>::get(DomainUSDC.id()),
None
);

register_currency::<T>(LocalUSDC, |_| {});
register_currency::<T>(DomainUSDC, |_| {});
register_currency::<T>(OtherLocal, |_| {});

pallet_order_book::Pallet::<T>::set_market_feeder(
T::RuntimeOriginExt::root(),
Feeder::<T::RuntimeOriginExt>::root(),
)
.unwrap();

<T as pallet_order_book::Config>::RatioProvider::get(
&pallet_order_book::MarketFeederId::<T>::get().unwrap(),
&key,
)
setup();

<T as pallet_order_book::Config>::RatioProvider::get(&feeder::<T>(), &key).unwrap()
})
.ok()
.flatten()
.unwrap()
}

fn get_rate<T: Runtime>(key: (CurrencyId, CurrencyId)) -> Option<Ratio> {
get_rate_with::<T>(key, || {})
}

fn local_to_variant<T: Runtime>() {
assert_eq!(
get_rate::<T>((LocalUSDC.id(), DomainUSDC.id())),
Ratio::one()
Some(Ratio::one())
);
}

fn variant_to_local<T: Runtime>() {
assert_eq!(
get_rate::<T>((DomainUSDC.id(), LocalUSDC.id())),
Ratio::one()
Some(Ratio::one())
);
}

fn variant_to_other_local<T: Runtime>() {
assert_eq!(get_rate::<T>((DomainUSDC.id(), OtherLocal.id())), None);
}

fn other_local_to_variant<T: Runtime>() {
assert_eq!(get_rate::<T>((OtherLocal.id(), DomainUSDC.id())), None);
}

fn variant_to_local_rate_set<T: Runtime>() {
let pair = (LocalUSDC.id(), DomainUSDC.id());
assert_eq!(
get_rate_with::<T>(pair, || {
pallet_oracle_feed::Pallet::<T>::feed(
feeder::<T>().0.into(),
OracleKey::ConversionRatio(pair.1, pair.0),
Ratio::checked_from_rational(1, 5).unwrap(),
)
.unwrap();
}),
Some(Ratio::one())
);
}

fn local_to_variant_rate_set<T: Runtime>() {
let pair = (LocalUSDC.id(), DomainUSDC.id());
assert_eq!(
get_rate_with::<T>(pair, || {
pallet_oracle_feed::Pallet::<T>::feed(
feeder::<T>().0.into(),
OracleKey::ConversionRatio(pair.0, pair.1),
Ratio::checked_from_rational(1, 5).unwrap(),
)
.unwrap();
}),
Some(Ratio::one())
);
}

test_for_runtimes!(all, variant_to_local);
test_for_runtimes!(all, local_to_variant);
test_for_runtimes!(all, variant_to_other_local);
test_for_runtimes!(all, other_local_to_variant);
}
4 changes: 3 additions & 1 deletion runtime/integration-tests/src/generic/utils/currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ pub trait CurrencyInfo {
10u128.pow(self.decimals())
}

fn symbol(&self) -> &'static str;
fn symbol(&self) -> &'static str {
"TKN"
}

fn name(&self) -> &'static str {
&self.symbol()
Expand Down

0 comments on commit 9e34107

Please sign in to comment.