diff --git a/pallets/moonbeam-foreign-assets/src/lib.rs b/pallets/moonbeam-foreign-assets/src/lib.rs index 3b5f77a629..5e826eb7a1 100644 --- a/pallets/moonbeam-foreign-assets/src/lib.rs +++ b/pallets/moonbeam-foreign-assets/src/lib.rs @@ -341,6 +341,11 @@ pub mod pallet { let previous_location = AssetsById::::get(&asset_id).ok_or(Error::::AssetDoesNotExist)?; + ensure!( + !AssetsByLocation::::contains_key(&new_xcm_location), + Error::::LocationAlreadyExists + ); + // Remove previous foreign asset info let (_asset_id, asset_status) = AssetsByLocation::::take(&previous_location) .ok_or(Error::::CorruptedStorageOrphanLocation)?; diff --git a/pallets/moonbeam-foreign-assets/src/tests.rs b/pallets/moonbeam-foreign-assets/src/tests.rs index 94f45990d8..ee7689f17b 100644 --- a/pallets/moonbeam-foreign-assets/src/tests.rs +++ b/pallets/moonbeam-foreign-assets/src/tests.rs @@ -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::::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::::LocationAlreadyExists + ); + }); +}