From eee2bb03e05efcc34ef8a06f5c3f1604f1dd5fbe Mon Sep 17 00:00:00 2001 From: William Freudenberger Date: Wed, 17 Apr 2024 21:06:04 +0200 Subject: [PATCH] fix: looser is_local_representation --- libs/types/src/tokens.rs | 20 ++++++------ pallets/token-mux/src/mock.rs | 1 + pallets/token-mux/src/tests.rs | 58 ++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 11 deletions(-) diff --git a/libs/types/src/tokens.rs b/libs/types/src/tokens.rs index b365bf72f0..495716e280 100644 --- a/libs/types/src/tokens.rs +++ b/libs/types/src/tokens.rs @@ -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) + } } } diff --git a/pallets/token-mux/src/mock.rs b/pallets/token-mux/src/mock.rs index 118cada430..9ae2542025 100644 --- a/pallets/token-mux/src/mock.rs +++ b/pallets/token-mux/src/mock.rs @@ -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) diff --git a/pallets/token-mux/src/tests.rs b/pallets/token-mux/src/tests.rs index c9cc348336..bda2bb9d1e 100644 --- a/pallets/token-mux/src/tests.rs +++ b/pallets/token-mux/src/tests.rs @@ -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!(>::is_local_representation_of(&USDC_LOCAL, &USDC_1), Ok(true)); + assert_eq!( + >::is_local_representation_of( + &USDC_1, + &USDC_LOCAL + ), + Ok(false) + ); + assert_eq!(>::is_local_representation_of(&USDC_LOCAL, &USDC_2), Ok(true)); + assert_eq!( + >::is_local_representation_of( + &USDC_2, + &USDC_LOCAL + ), + Ok(false) + ); + + assert_eq!(>::is_local_representation_of(&USDC_LOCAL, &NON_USDC), Ok(false)); + assert_eq!( + >::is_local_representation_of( + &NON_USDC, + &USDC_LOCAL + ), + Ok(false) + ); + }); + } + + #[test] + fn is_local_missing_cannot_lookup() { + new_test_ext().execute_with(|| { + assert_eq!(>::is_local_representation_of(&UNREGISTERED_ASSET, &USDC_1), Err(DispatchError::CannotLookup)); + assert_eq!(>::is_local_representation_of(&USDC_1, &UNREGISTERED_ASSET), Err(DispatchError::CannotLookup)); + + assert_eq!(>::is_local_representation_of(&USDC_LOCAL, &UNREGISTERED_ASSET), Err(DispatchError::CannotLookup)); + assert_eq!(>::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!(>::is_local_representation_of(&USDC_LOCAL, &USDC_WRONG_DECIMALS), Err(DispatchError::Other("Mismatching decimals"))); + assert_eq!(>::is_local_representation_of(&USDC_WRONG_DECIMALS, &USDC_LOCAL), Ok(false)); + }); + } +}