Skip to content

Commit

Permalink
Polkadot sdk 1.9.0 (#2741)
Browse files Browse the repository at this point in the history
* upgrade polkadot-sdk 1.9.0

* fix tests

* fix tests

* /bench runtime acala *

* /bench runtime karura *

* /bench runtime mandala *

* fix tests

* fix tests

* fix tests

* fix tests

* fix tests

* add MinimalShares

* fix tests

---------

Co-authored-by: Acala Github Action Bot <hello@acala.network>
  • Loading branch information
zjb0807 and Acala Github Action Bot authored May 1, 2024
1 parent 6e1c1a4 commit da1133b
Show file tree
Hide file tree
Showing 175 changed files with 7,991 additions and 6,589 deletions.
5,109 changes: 3,151 additions & 1,958 deletions Cargo.lock

Large diffs are not rendered by default.

244 changes: 122 additions & 122 deletions Cargo.toml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ecosystem-modules/stable-asset
2 changes: 1 addition & 1 deletion evm-tests
89 changes: 45 additions & 44 deletions modules/asset-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use scale_info::prelude::format;
use sp_runtime::{traits::One, ArithmeticError, FixedPointNumber, FixedU128};
use sp_std::{boxed::Box, vec::Vec};

use xcm::{v3::prelude::*, VersionedMultiLocation};
use xcm::{v3, v4::prelude::*, VersionedLocation};

mod mock;
mod tests;
Expand Down Expand Up @@ -90,8 +90,8 @@ pub mod module {
/// The given location could not be used (e.g. because it cannot be expressed in the
/// desired version of XCM).
BadLocation,
/// MultiLocation existed
MultiLocationExisted,
/// Location existed
LocationExisted,
/// AssetId not exists
AssetIdNotExists,
/// AssetId exists
Expand All @@ -104,13 +104,13 @@ pub mod module {
/// The foreign asset registered.
ForeignAssetRegistered {
asset_id: ForeignAssetId,
asset_address: MultiLocation,
asset_address: Location,
metadata: AssetMetadata<BalanceOf<T>>,
},
/// The foreign asset updated.
ForeignAssetUpdated {
asset_id: ForeignAssetId,
asset_address: MultiLocation,
asset_address: Location,
metadata: AssetMetadata<BalanceOf<T>>,
},
/// The asset registered.
Expand Down Expand Up @@ -139,19 +139,19 @@ pub mod module {
#[pallet::getter(fn next_stable_asset_id)]
pub type NextStableAssetId<T: Config> = StorageValue<_, StableAssetPoolId, ValueQuery>;

/// The storages for MultiLocations.
/// The storages for Locations.
///
/// ForeignAssetLocations: map ForeignAssetId => Option<MultiLocation>
/// ForeignAssetLocations: map ForeignAssetId => Option<Location>
#[pallet::storage]
#[pallet::getter(fn foreign_asset_locations)]
pub type ForeignAssetLocations<T: Config> = StorageMap<_, Twox64Concat, ForeignAssetId, MultiLocation, OptionQuery>;
pub type ForeignAssetLocations<T: Config> = StorageMap<_, Twox64Concat, ForeignAssetId, v3::Location, OptionQuery>;

/// The storages for CurrencyIds.
///
/// LocationToCurrencyIds: map MultiLocation => Option<CurrencyId>
/// LocationToCurrencyIds: map Location => Option<CurrencyId>
#[pallet::storage]
#[pallet::getter(fn location_to_currency_ids)]
pub type LocationToCurrencyIds<T: Config> = StorageMap<_, Twox64Concat, MultiLocation, CurrencyId, OptionQuery>;
pub type LocationToCurrencyIds<T: Config> = StorageMap<_, Twox64Concat, v3::Location, CurrencyId, OptionQuery>;

/// The storages for EvmAddress.
///
Expand Down Expand Up @@ -201,12 +201,12 @@ pub mod module {
#[pallet::weight(T::WeightInfo::register_foreign_asset())]
pub fn register_foreign_asset(
origin: OriginFor<T>,
location: Box<VersionedMultiLocation>,
location: Box<VersionedLocation>,
metadata: Box<AssetMetadata<BalanceOf<T>>>,
) -> DispatchResult {
T::RegisterOrigin::ensure_origin(origin)?;

let location: MultiLocation = (*location).try_into().map_err(|()| Error::<T>::BadLocation)?;
let location: Location = (*location).try_into().map_err(|()| Error::<T>::BadLocation)?;
let foreign_asset_id = Self::do_register_foreign_asset(&location, &metadata)?;

Self::deposit_event(Event::<T>::ForeignAssetRegistered {
Expand All @@ -222,12 +222,12 @@ pub mod module {
pub fn update_foreign_asset(
origin: OriginFor<T>,
foreign_asset_id: ForeignAssetId,
location: Box<VersionedMultiLocation>,
location: Box<VersionedLocation>,
metadata: Box<AssetMetadata<BalanceOf<T>>>,
) -> DispatchResult {
T::RegisterOrigin::ensure_origin(origin)?;

let location: MultiLocation = (*location).try_into().map_err(|()| Error::<T>::BadLocation)?;
let location: Location = (*location).try_into().map_err(|()| Error::<T>::BadLocation)?;
Self::do_update_foreign_asset(foreign_asset_id, &location, &metadata)?;

Self::deposit_event(Event::<T>::ForeignAssetUpdated {
Expand Down Expand Up @@ -365,17 +365,18 @@ impl<T: Config> Pallet<T> {
}

fn do_register_foreign_asset(
location: &MultiLocation,
location: &Location,
metadata: &AssetMetadata<BalanceOf<T>>,
) -> Result<ForeignAssetId, DispatchError> {
let foreign_asset_id = Self::get_next_foreign_asset_id()?;
LocationToCurrencyIds::<T>::try_mutate(location, |maybe_currency_ids| -> DispatchResult {
ensure!(maybe_currency_ids.is_none(), Error::<T>::MultiLocationExisted);
let v3_location = v3::Location::try_from(location.clone()).map_err(|()| Error::<T>::BadLocation)?;
LocationToCurrencyIds::<T>::try_mutate(v3_location, |maybe_currency_ids| -> DispatchResult {
ensure!(maybe_currency_ids.is_none(), Error::<T>::LocationExisted);
*maybe_currency_ids = Some(CurrencyId::ForeignAsset(foreign_asset_id));

ForeignAssetLocations::<T>::try_mutate(foreign_asset_id, |maybe_location| -> DispatchResult {
ensure!(maybe_location.is_none(), Error::<T>::MultiLocationExisted);
*maybe_location = Some(*location);
ensure!(maybe_location.is_none(), Error::<T>::LocationExisted);
*maybe_location = Some(v3_location);

AssetMetadatas::<T>::try_mutate(
AssetIds::ForeignAssetId(foreign_asset_id),
Expand All @@ -394,28 +395,29 @@ impl<T: Config> Pallet<T> {

fn do_update_foreign_asset(
foreign_asset_id: ForeignAssetId,
location: &MultiLocation,
location: &Location,
metadata: &AssetMetadata<BalanceOf<T>>,
) -> DispatchResult {
ForeignAssetLocations::<T>::try_mutate(foreign_asset_id, |maybe_multi_locations| -> DispatchResult {
let old_multi_locations = maybe_multi_locations.as_mut().ok_or(Error::<T>::AssetIdNotExists)?;
let v3_location = v3::Location::try_from(location.clone()).map_err(|()| Error::<T>::BadLocation)?;
ForeignAssetLocations::<T>::try_mutate(foreign_asset_id, |maybe_locations| -> DispatchResult {
let old_locations = maybe_locations.as_mut().ok_or(Error::<T>::AssetIdNotExists)?;

AssetMetadatas::<T>::try_mutate(
AssetIds::ForeignAssetId(foreign_asset_id),
|maybe_asset_metadatas| -> DispatchResult {
ensure!(maybe_asset_metadatas.is_some(), Error::<T>::AssetIdNotExists);

// modify location
if location != old_multi_locations {
LocationToCurrencyIds::<T>::remove(*old_multi_locations);
LocationToCurrencyIds::<T>::try_mutate(location, |maybe_currency_ids| -> DispatchResult {
ensure!(maybe_currency_ids.is_none(), Error::<T>::MultiLocationExisted);
if v3_location != *old_locations {
LocationToCurrencyIds::<T>::remove(*old_locations);
LocationToCurrencyIds::<T>::try_mutate(v3_location, |maybe_currency_ids| -> DispatchResult {
ensure!(maybe_currency_ids.is_none(), Error::<T>::LocationExisted);
*maybe_currency_ids = Some(CurrencyId::ForeignAsset(foreign_asset_id));
Ok(())
})?;
}
*maybe_asset_metadatas = Some(metadata.clone());
*old_multi_locations = *location;
*old_locations = v3_location;
Ok(())
},
)
Expand Down Expand Up @@ -526,27 +528,25 @@ impl<T: Config> Pallet<T> {

pub struct AssetIdMaps<T>(sp_std::marker::PhantomData<T>);

impl<T: Config> AssetIdMapping<ForeignAssetId, MultiLocation, AssetMetadata<BalanceOf<T>>> for AssetIdMaps<T> {
impl<T: Config> AssetIdMapping<ForeignAssetId, Location, AssetMetadata<BalanceOf<T>>> for AssetIdMaps<T> {
fn get_asset_metadata(asset_ids: AssetIds) -> Option<AssetMetadata<BalanceOf<T>>> {
Pallet::<T>::asset_metadatas(asset_ids)
}

fn get_multi_location(foreign_asset_id: ForeignAssetId) -> Option<MultiLocation> {
Pallet::<T>::foreign_asset_locations(foreign_asset_id)
fn get_location(foreign_asset_id: ForeignAssetId) -> Option<Location> {
Pallet::<T>::foreign_asset_locations(foreign_asset_id).map(|l| l.try_into().ok())?
}

fn get_currency_id(multi_location: MultiLocation) -> Option<CurrencyId> {
Pallet::<T>::location_to_currency_ids(multi_location)
fn get_currency_id(location: Location) -> Option<CurrencyId> {
let v3_location = v3::Location::try_from(location).ok()?;
Pallet::<T>::location_to_currency_ids(v3_location)
}
}

fn key_to_currency(location: MultiLocation) -> Option<CurrencyId> {
match location {
MultiLocation {
parents: 0,
interior: X1(Junction::GeneralKey { data, length }),
} => {
let key = &data[..data.len().min(length as usize)];
fn key_to_currency(location: Location) -> Option<CurrencyId> {
match location.unpack() {
(0, [Junction::GeneralKey { data, length }]) => {
let key = &data[..data.len().min(*length as usize)];
CurrencyId::decode(&mut &*key).ok()
}
_ => None,
Expand All @@ -559,8 +559,9 @@ impl<T: Config> BuyWeightRate for BuyWeightRateOfForeignAsset<T>
where
BalanceOf<T>: Into<u128>,
{
fn calculate_rate(location: MultiLocation) -> Option<Ratio> {
if let Some(CurrencyId::ForeignAsset(foreign_asset_id)) = Pallet::<T>::location_to_currency_ids(location) {
fn calculate_rate(location: Location) -> Option<Ratio> {
let v3_location = v3::Location::try_from(location).ok()?;
if let Some(CurrencyId::ForeignAsset(foreign_asset_id)) = Pallet::<T>::location_to_currency_ids(v3_location) {
if let Some(asset_metadata) = Pallet::<T>::asset_metadatas(AssetIds::ForeignAssetId(foreign_asset_id)) {
let minimum_balance = asset_metadata.minimal_balance.into();
let rate = FixedU128::saturating_from_rational(minimum_balance, T::Currency::minimum_balance().into());
Expand All @@ -578,7 +579,7 @@ impl<T: Config> BuyWeightRate for BuyWeightRateOfLiquidCrowdloan<T>
where
BalanceOf<T>: Into<u128>,
{
fn calculate_rate(location: MultiLocation) -> Option<Ratio> {
fn calculate_rate(location: Location) -> Option<Ratio> {
let currency = key_to_currency(location);
match currency {
Some(CurrencyId::LiquidCrowdloan(lease)) => {
Expand All @@ -605,7 +606,7 @@ impl<T: Config> BuyWeightRate for BuyWeightRateOfStableAsset<T>
where
BalanceOf<T>: Into<u128>,
{
fn calculate_rate(location: MultiLocation) -> Option<Ratio> {
fn calculate_rate(location: Location) -> Option<Ratio> {
let currency = key_to_currency(location);
match currency {
Some(CurrencyId::StableAssetPoolToken(pool_id)) => {
Expand All @@ -630,7 +631,7 @@ impl<T: Config> BuyWeightRate for BuyWeightRateOfErc20<T>
where
BalanceOf<T>: Into<u128>,
{
fn calculate_rate(location: MultiLocation) -> Option<Ratio> {
fn calculate_rate(location: Location) -> Option<Ratio> {
let currency = key_to_currency(location);
match currency {
Some(CurrencyId::Erc20(address)) if !is_system_contract(&address) => {
Expand Down
1 change: 0 additions & 1 deletion modules/asset-registry/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ impl pallet_balances::Config for Runtime {
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = ();
type MaxHolds = ();
type MaxFreezes = ();
}

Expand Down
Loading

0 comments on commit da1133b

Please sign in to comment.