Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Drop tranche multiLocation #1340

Merged
merged 24 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
dcdb229
Drop Tranche location
NunoAlexandre May 10, 2023
bf5578c
fmt
NunoAlexandre May 10, 2023
7180096
Fix imports
NunoAlexandre May 11, 2023
2fef2fe
Merge branch 'main' into refactor/drop-tranche-multilocation
NunoAlexandre May 11, 2023
89019d0
centrifuge: Add TrancheLocationMigration
NunoAlexandre May 11, 2023
45c9803
centrifuge: Extend Executive with TrancheLocationMigration
NunoAlexandre May 11, 2023
7c25839
centrifuge: Add migrations module
NunoAlexandre May 11, 2023
5135d57
altair: Add migrations
NunoAlexandre May 11, 2023
c4da7b2
dev: Add migrations
NunoAlexandre May 11, 2023
19a09fe
fmt
NunoAlexandre May 11, 2023
d3b0400
Leave comment regarding the runtime version
NunoAlexandre May 11, 2023
b21b538
fixup
NunoAlexandre May 11, 2023
0bf2323
pool-system: Drop unused deps
NunoAlexandre May 12, 2023
9b2a5ba
Merge branch 'main' into refactor/drop-tranche-multilocation
NunoAlexandre May 12, 2023
0772045
Merge branch 'main' into refactor/drop-tranche-multilocation
NunoAlexandre May 15, 2023
217828f
Revert changes to migration name alias
NunoAlexandre May 15, 2023
19800e5
Fix deps
NunoAlexandre May 15, 2023
4dbda9b
fmt
NunoAlexandre May 15, 2023
71bc587
Use log instead of expect
NunoAlexandre May 15, 2023
f3e103d
Merge remote-tracking branch 'origin/main' into refactor/drop-tranche…
NunoAlexandre May 16, 2023
5c16165
fmt
NunoAlexandre May 16, 2023
0033bdb
fixup
NunoAlexandre May 16, 2023
217cce0
dev: Drop unused log dep
NunoAlexandre May 16, 2023
d0d288d
Merge branch 'main' into refactor/drop-tranche-multilocation
NunoAlexandre May 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pallets/pool-registry/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ impl pallet_pool_system::Config for Test {
type NAV = FakeNav;
type PalletId = PoolPalletId;
type PalletIndex = PoolPalletIndex;
type ParachainId = ParachainInfo;
type Permission = PermissionsMock;
type PoolCreateOrigin = EnsureSigned<u64>;
type PoolCurrency = PoolCurrency;
Expand Down
11 changes: 2 additions & 9 deletions pallets/pool-system/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,8 @@ impl<T: Config> PoolMutate<T::AccountId, T::PoolId> for Pallet<T> {
None => return Err(Error::<T>::MetadataForCurrencyNotFound.into()),
};

let parachain_id = T::ParachainId::get();

let metadata = tranche.create_asset_metadata(
decimals,
parachain_id,
T::PalletIndex::get(),
token_name.to_vec(),
token_symbol.to_vec(),
);
let metadata =
tranche.create_asset_metadata(decimals, token_name.to_vec(), token_symbol.to_vec());

T::AssetRegistry::register_asset(Some(tranche.currency.into()), metadata)
.map_err(|_| Error::<T>::FailedToRegisterTrancheMetadata)?;
Expand Down
4 changes: 0 additions & 4 deletions pallets/pool-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ use orml_traits::{
Change,
};
pub use pallet::*;
use polkadot_parachain::primitives::Id as ParachainId;
NunoAlexandre marked this conversation as resolved.
Show resolved Hide resolved
use pool_types::{
PoolChanges, PoolDepositInfo, PoolDetails, PoolEssence, PoolLocator, ScheduledUpdateDetails,
};
Expand Down Expand Up @@ -275,9 +274,6 @@ pub mod pallet {
CustomMetadata = CustomMetadata,
>;

#[pallet::constant]
type ParachainId: Get<ParachainId>;

type Currency: ReservableCurrency<Self::AccountId, Balance = Self::Balance>;

type Tokens: Mutate<Self::AccountId>
Expand Down
1 change: 0 additions & 1 deletion pallets/pool-system/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ impl Config for Runtime {
type NAV = FakeNav;
type PalletId = PoolPalletId;
type PalletIndex = PoolPalletIndex;
type ParachainId = ParachainInfo;
type Permission = Permissions;
type PoolCreateOrigin = EnsureSigned<u64>;
type PoolCurrency = PoolCurrency;
Expand Down
22 changes: 4 additions & 18 deletions pallets/pool-system/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,10 @@ use cfg_types::{
use frame_support::{assert_err, assert_noop, assert_ok};
use orml_traits::asset_registry::{AssetMetadata, Inspect};
use rand::Rng;
use sp_core::{storage::StateVersion, Encode};
use sp_core::storage::StateVersion;
use sp_runtime::{
traits::{ConstU32, One, Zero},
FixedPointNumber, Perquintill, TokenError, WeakBoundedVec,
};
use xcm::{
NunoAlexandre marked this conversation as resolved.
Show resolved Hide resolved
latest::MultiLocation,
prelude::{GeneralKey, PalletInstance, Parachain, X3},
VersionedMultiLocation,
traits::{One, Zero},
FixedPointNumber, Perquintill, TokenError,
};

use crate::{
Expand Down Expand Up @@ -2311,8 +2306,6 @@ fn create_tranche_token_metadata() {

let pool = Pool::<Runtime>::get(3).unwrap();
let tranche_currency = pool.tranches.tranches[0].currency;
let tranche_id =
WeakBoundedVec::<u8, ConstU32<32>>::force_from(tranche_currency.encode(), None);

assert_eq!(
<Runtime as Config>::AssetRegistry::metadata(&tranche_currency.into()).unwrap(),
Expand All @@ -2321,14 +2314,7 @@ fn create_tranche_token_metadata() {
name: "SuperToken".into(),
symbol: "ST".into(),
existential_deposit: 0,
location: Some(VersionedMultiLocation::V1(MultiLocation {
parents: 1,
interior: X3(
Parachain(MockParachainId::get()),
PalletInstance(PoolPalletIndex::get()),
GeneralKey(tranche_id)
),
})),
location: None,
additional: CustomMetadata {
mintable: false,
permissioned: true,
Expand Down
44 changes: 6 additions & 38 deletions pallets/pool-system/src/tranches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,20 @@ use frame_support::{
dispatch::DispatchResult,
ensure,
sp_runtime::ArithmeticError,
traits::{fungibles::Inspect, Get},
traits::{fungibles::Inspect, Get, Len},
Blake2_128, BoundedVec, Parameter, RuntimeDebug, StorageHasher,
};
use orml_traits::asset_registry::AssetMetadata;
use polkadot_parachain::primitives::Id as ParachainId;
use rev_slice::{RevSlice, SliceExt};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_arithmetic::traits::{checked_pow, BaseArithmetic, Unsigned};
use sp_runtime::{
traits::{ConstU32, Member, One, Zero},
DispatchError, FixedPointNumber, FixedPointOperand, Perquintill, WeakBoundedVec,
traits::{Member, One, Zero},
DispatchError, FixedPointNumber, FixedPointOperand, Perquintill,
};
use sp_std::{marker::PhantomData, ops::Deref, vec::Vec};
use xcm::{
latest::MultiLocation,
prelude::{GeneralKey, PalletInstance, Parachain, X3},
VersionedMultiLocation,
};

/// Type that indicates the seniority of a tranche
pub type Seniority = u32;
Expand Down Expand Up @@ -236,8 +230,6 @@ where
pub fn create_asset_metadata(
&self,
decimals: u32,
parachain_id: ParachainId,
pallet_index: u8,
token_name: Vec<u8>,
token_symbol: Vec<u8>,
) -> AssetMetadata<Balance, CustomMetadata>
Expand All @@ -246,22 +238,12 @@ where
Currency: Encode,
CustomMetadata: Parameter + Member + TypeInfo,
{
let tranche_id =
WeakBoundedVec::<u8, ConstU32<32>>::force_from(self.currency.encode(), None);

AssetMetadata {
decimals,
name: token_name,
symbol: token_symbol,
existential_deposit: Zero::zero(),
location: Some(VersionedMultiLocation::V1(MultiLocation {
parents: 1,
interior: X3(
Parachain(parachain_id.into()),
PalletInstance(pallet_index),
GeneralKey(tranche_id),
),
})),
location: None,
additional: CustomMetadata {
mintable: false,
permissioned: true,
Expand Down Expand Up @@ -1854,27 +1836,13 @@ pub mod test {
let decimals: u32 = 10;
let name: Vec<u8> = "Glimmer".into();
let symbol: Vec<u8> = "GLMR".into();
let asset_metadata = tranche.create_asset_metadata(
decimals,
// fake parachain id
ParachainId::from(42),
// fake pallet index
42u8,
name,
symbol,
);
let asset_metadata = tranche.create_asset_metadata(decimals, name, symbol);

assert_eq!(asset_metadata.existential_deposit, 0);
assert_eq!(asset_metadata.name[..], [71, 108, 105, 109, 109, 101, 114]);
assert_eq!(asset_metadata.symbol[..], [71, 76, 77, 82]);
assert_eq!(asset_metadata.decimals, decimals);
assert!(match asset_metadata.location {
Some(VersionedMultiLocation::V1(xcm::v1::MultiLocation {
parents: 1,
interior: X3(Parachain(42), PalletInstance(42), GeneralKey(_)),
})) => true,
_ => false,
})
assert_eq!(asset_metadata.location, None);
}
}

Expand Down
6 changes: 2 additions & 4 deletions runtime/altair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ use xcm_executor::XcmExecutor;
use crate::xcm::{XcmConfig, XcmOriginToTransactDispatchOrigin};

pub mod constants;
mod migrations;
mod weights;
pub mod xcm;

Expand Down Expand Up @@ -1198,7 +1199,6 @@ impl pallet_pool_system::Config for Runtime {
type NAV = Loans;
type PalletId = PoolPalletId;
type PalletIndex = PoolPalletIndex;
type ParachainId = ParachainInfo;
type Permission = Permissions;
type PoolCreateOrigin = EnsureRoot<AccountId>;
type PoolCurrency = PoolCurrency;
Expand Down Expand Up @@ -1489,16 +1489,14 @@ pub type UncheckedExtrinsic =
/// Extrinsic type that has already been checked.
pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, RuntimeCall, SignedExtra>;

type UpgradeAltair1027 = pallet_loans_ref::migrations::v1::Migration<Runtime>;

/// Executive: handles dispatch to the various modules.
pub type Executive = frame_executive::Executive<
Runtime,
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
UpgradeAltair1027,
migrations::Migrations,
NunoAlexandre marked this conversation as resolved.
Show resolved Hide resolved
>;

#[cfg(not(feature = "disable-runtime-api"))]
Expand Down
74 changes: 74 additions & 0 deletions runtime/altair/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2021 Centrifuge Foundation (centrifuge.io).
//
// This file is part of the Centrifuge chain project.
// Centrifuge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version (see http://www.gnu.org/licenses).
// Centrifuge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

use cfg_types::tokens::CurrencyId;
use frame_support::{traits::OnRuntimeUpgrade, weights::Weight};

use crate::{OrmlAssetRegistry, Runtime};

// Migrations to Altair 1027
pub type Migrations = (
pallet_loans_ref::migrations::v1::Migration<Runtime>,
TrancheLocationMigration,
);

/// This migration sets the AssetMetadata.location of all the Tranche tokens
/// registered in the AssetRegistry to `None`.
pub struct TrancheLocationMigration;

impl OnRuntimeUpgrade for TrancheLocationMigration {
fn on_runtime_upgrade() -> Weight {
for (asset_id, metadata) in orml_asset_registry::Metadata::<Runtime>::iter() {
if matches!(asset_id, CurrencyId::Tranche(_, _)) && metadata.location.is_some() {
OrmlAssetRegistry::do_update_asset(
asset_id,
// decimals
None,
// name
None,
// symbol
None,
// existential_deposit
None,
// location: we do set it to `None`
Some(None),
// additional
None,
)
.expect("TrancheLocationMigration: Failed to update tranche token");
NunoAlexandre marked this conversation as resolved.
Show resolved Hide resolved
}
}

// todo(nuno): not sure how to build this properly,
// setting it to a conservative value for now.
Weight::from_ref_time(200_000_000)
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<sp_std::vec::Vec<u8>, &'static str> {
Ok(Default::default())
NunoAlexandre marked this conversation as resolved.
Show resolved Hide resolved
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_: sp_std::vec::Vec<u8>) -> Result<(), &'static str> {
for (asset_id, metadata) in orml_asset_registry::Metadata::<Runtime>::iter() {
if matches!(asset_id, CurrencyId::Tranche(_, _)) {
frame_support::ensure!(
metadata.location.is_none(),
"A tranche token's location is not None"
)
}
}

Ok(())
}
}
6 changes: 2 additions & 4 deletions runtime/centrifuge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ use xcm_executor::XcmExecutor;

use crate::xcm::{XcmConfig, XcmOriginToTransactDispatchOrigin};

mod migrations;
mod weights;
pub mod xcm;

Expand Down Expand Up @@ -1320,7 +1321,6 @@ impl pallet_pool_system::Config for Runtime {
type NAV = Loans;
type PalletId = PoolPalletId;
type PalletIndex = PoolPalletIndex;
type ParachainId = ParachainInfo;
type Permission = Permissions;
type PoolCreateOrigin = EnsureRoot<AccountId>;
type PoolCurrency = PoolCurrency;
Expand Down Expand Up @@ -1666,16 +1666,14 @@ pub type UncheckedExtrinsic =
/// Extrinsic type that has already been checked.
pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, RuntimeCall, SignedExtra>;

type UpgradeCentrifuge1019 = pallet_loans_ref::migrations::v1::Migration<Runtime>;

/// Executive: handles dispatch to the various modules.
pub type Executive = frame_executive::Executive<
Runtime,
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
UpgradeCentrifuge1019,
migrations::Migrations,
NunoAlexandre marked this conversation as resolved.
Show resolved Hide resolved
>;

#[cfg(not(feature = "disable-runtime-api"))]
Expand Down
Loading