Skip to content

Commit

Permalink
fix: looser is_local_representation
Browse files Browse the repository at this point in the history
  • Loading branch information
wischli committed Apr 17, 2024
1 parent 56de771 commit eee2bb0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
20 changes: 9 additions & 11 deletions libs/types/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,18 +431,16 @@ where
let meta_variant =
AssetInspect::metadata(variant_currency).ok_or(DispatchError::CannotLookup)?;

let local: Self = meta_variant
.additional
.local_representation
.ok_or(DispatchError::Other("Missing local representation"))?
.into();

frame_support::ensure!(
meta_local.decimals == meta_variant.decimals,
DispatchError::Other("Mismatching decimals")
);
if let Some(local) = meta_variant.additional.local_representation {
frame_support::ensure!(
meta_local.decimals == meta_variant.decimals,
DispatchError::Other("Mismatching decimals")
);

Ok(self == &local)
Ok(self == &local.into())
} else {
Ok(false)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions pallets/token-mux/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ pub fn new_test_ext_invalid_assets() -> sp_io::TestExternalities {
USDC_WRONG_DECIMALS,
asset_metadata(5, Some(USDC_LOCAL_ASSET_ID)),
),
(USDC_LOCAL, asset_metadata(6, None)),
],
}
.assimilate_storage(&mut storage)
Expand Down
58 changes: 58 additions & 0 deletions pallets/token-mux/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,3 +506,61 @@ pub(crate) mod swaps {
})
}
}

mod is_local_representation {
use cfg_traits::HasLocalAssetRepresentation;
use cfg_types::tokens::CurrencyId;
use sp_runtime::DispatchError;

use super::*;
use crate::mock::{new_test_ext_invalid_assets, MockRegistry, USDC_WRONG_DECIMALS};

#[test]
fn is_local_happy_paths() {
new_test_ext().execute_with(|| {
assert_eq!(<CurrencyId as HasLocalAssetRepresentation<MockRegistry>>::is_local_representation_of(&USDC_LOCAL, &USDC_1), Ok(true));
assert_eq!(
<CurrencyId as HasLocalAssetRepresentation<MockRegistry>>::is_local_representation_of(
&USDC_1,
&USDC_LOCAL
),
Ok(false)
);
assert_eq!(<CurrencyId as HasLocalAssetRepresentation<MockRegistry>>::is_local_representation_of(&USDC_LOCAL, &USDC_2), Ok(true));
assert_eq!(
<CurrencyId as HasLocalAssetRepresentation<MockRegistry>>::is_local_representation_of(
&USDC_2,
&USDC_LOCAL
),
Ok(false)
);

assert_eq!(<CurrencyId as HasLocalAssetRepresentation<MockRegistry>>::is_local_representation_of(&USDC_LOCAL, &NON_USDC), Ok(false));
assert_eq!(
<CurrencyId as HasLocalAssetRepresentation<MockRegistry>>::is_local_representation_of(
&NON_USDC,
&USDC_LOCAL
),
Ok(false)
);
});
}

#[test]
fn is_local_missing_cannot_lookup() {
new_test_ext().execute_with(|| {
assert_eq!(<CurrencyId as HasLocalAssetRepresentation<MockRegistry>>::is_local_representation_of(&UNREGISTERED_ASSET, &USDC_1), Err(DispatchError::CannotLookup));
assert_eq!(<CurrencyId as HasLocalAssetRepresentation<MockRegistry>>::is_local_representation_of(&USDC_1, &UNREGISTERED_ASSET), Err(DispatchError::CannotLookup));

assert_eq!(<CurrencyId as HasLocalAssetRepresentation<MockRegistry>>::is_local_representation_of(&USDC_LOCAL, &UNREGISTERED_ASSET), Err(DispatchError::CannotLookup));
assert_eq!(<CurrencyId as HasLocalAssetRepresentation<MockRegistry>>::is_local_representation_of(&UNREGISTERED_ASSET, &USDC_LOCAL), Err(DispatchError::CannotLookup));
});
}
#[test]
fn is_local_mismatching_decimals() {
new_test_ext_invalid_assets().execute_with(|| {
assert_eq!(<CurrencyId as HasLocalAssetRepresentation<MockRegistry>>::is_local_representation_of(&USDC_LOCAL, &USDC_WRONG_DECIMALS), Err(DispatchError::Other("Mismatching decimals")));
assert_eq!(<CurrencyId as HasLocalAssetRepresentation<MockRegistry>>::is_local_representation_of(&USDC_WRONG_DECIMALS, &USDC_LOCAL), Ok(false));
});
}
}

0 comments on commit eee2bb0

Please sign in to comment.