Skip to content

Commit

Permalink
evm foreign asset: change_asset_location should not erase already exi…
Browse files Browse the repository at this point in the history
…sting location (#2925)

* fix: change evm foreign asset location: ensure new location not exist

* rust test: cover error LocationAlreadyExists
  • Loading branch information
librelois authored Sep 3, 2024
1 parent ca05e28 commit cf01c94
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pallets/moonbeam-foreign-assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,11 @@ pub mod pallet {
let previous_location =
AssetsById::<T>::get(&asset_id).ok_or(Error::<T>::AssetDoesNotExist)?;

ensure!(
!AssetsByLocation::<T>::contains_key(&new_xcm_location),
Error::<T>::LocationAlreadyExists
);

// Remove previous foreign asset info
let (_asset_id, asset_status) = AssetsByLocation::<T>::take(&previous_location)
.ok_or(Error::<T>::CorruptedStorageOrphanLocation)?;
Expand Down
42 changes: 42 additions & 0 deletions pallets/moonbeam-foreign-assets/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,45 @@ fn test_asset_id_non_existent_error() {
);
});
}

#[test]
fn test_location_already_exist_error() {
ExtBuilder::default().build().execute_with(|| {
// Setup: create a first foreign asset taht we will try to override
assert_ok!(EvmForeignAssets::create_foreign_asset(
RuntimeOrigin::root(),
1,
Location::parent(),
18,
encode_ticker("MTT"),
encode_token_name("Mytoken"),
));

assert_noop!(
EvmForeignAssets::create_foreign_asset(
RuntimeOrigin::root(),
2,
Location::parent(),
18,
encode_ticker("MTT"),
encode_token_name("Mytoken"),
),
Error::<Test>::LocationAlreadyExists
);

// Setup: create a second foreign asset that will try to override the first one
assert_ok!(EvmForeignAssets::create_foreign_asset(
RuntimeOrigin::root(),
2,
Location::new(2, *&[]),
18,
encode_ticker("MTT"),
encode_token_name("Mytoken"),
));

assert_noop!(
EvmForeignAssets::change_xcm_location(RuntimeOrigin::root(), 2, Location::parent()),
Error::<Test>::LocationAlreadyExists
);
});
}

0 comments on commit cf01c94

Please sign in to comment.