Skip to content

Commit

Permalink
fix: looser is_local_representation (#1815)
Browse files Browse the repository at this point in the history
* fix: looser is_local_representation

* tests: fix after changing setup

* Fix: zero amount order bug (#1816)

* add tests that fail and shouldn't

* add fix

* add extra tests

---------

Co-authored-by: Luis Enrique Muñoz Martín <lemunozm@gmail.com>
  • Loading branch information
wischli and lemunozm authored Apr 18, 2024
1 parent 2eebc49 commit 430fbe9
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 12 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
60 changes: 59 additions & 1 deletion pallets/token-mux/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ pub(crate) mod try_local {
new_test_ext_invalid_assets().execute_with(|| {
assert_noop!(
TokenMux::try_local(&USDC_WRONG_DECIMALS),
Error::<Runtime>::MetadataNotFound
Error::<Runtime>::DecimalMismatch
);
})
}
Expand Down 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 430fbe9

Please sign in to comment.