Skip to content

Commit

Permalink
chore: post-release cleanup (#1595)
Browse files Browse the repository at this point in the history
  • Loading branch information
wischli authored Oct 26, 2023
1 parent 90d3cf9 commit 31fdef8
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 343 deletions.
4 changes: 2 additions & 2 deletions runtime/centrifuge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("centrifuge"),
impl_name: create_runtime_str!("centrifuge"),
authoring_version: 1,
spec_version: 1023,
spec_version: 1024,
impl_version: 1,
#[cfg(not(feature = "disable-runtime-api"))]
apis: RUNTIME_API_VERSIONS,
Expand Down Expand Up @@ -1953,7 +1953,7 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
migrations::UpgradeCentrifuge1022,
migrations::UpgradeCentrifuge1024,
>;

pub struct TransactionConverter;
Expand Down
336 changes: 1 addition & 335 deletions runtime/centrifuge/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,339 +9,5 @@
// 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 crate::{Runtime, Weight};

pub type UpgradeCentrifuge1022 = (
anemoy_pool::Migration<crate::Runtime>,
add_wrapped_usdc_variants::Migration<crate::Runtime>,
// Sets account codes for all precompiles
runtime_common::migrations::precompile_account_codes::Migration<crate::Runtime>,
);

/// Migrate the Anemoy Pool's currency from LpEthUSC to Circle's USDC,
/// native on Polkadot's AssetHub.
mod anemoy_pool {
use cfg_primitives::PoolId;
use cfg_traits::PoolInspect;
use cfg_types::tokens::usdc::{CURRENCY_ID_DOT_NATIVE, CURRENCY_ID_LP_ETH};
#[cfg(feature = "try-runtime")]
use codec::{Decode, Encode};
#[cfg(feature = "try-runtime")]
use frame_support::ensure;
use frame_support::traits::{fungibles::Inspect, OnRuntimeUpgrade};
#[cfg(feature = "try-runtime")]
use pallet_pool_system::PoolDetailsOf;
use sp_std::vec;
#[cfg(feature = "try-runtime")]
use sp_std::vec::Vec;

use super::*;
use crate::PoolSystem;

const ANEMOY_POOL_ID: PoolId = 4_139_607_887;

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

impl<T: frame_system::Config> OnRuntimeUpgrade for Migration<T> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
let pool_details: PoolDetailsOf<Runtime> =
PoolSystem::pool(ANEMOY_POOL_ID).ok_or("Could not find Anemoy Pool")?;

ensure!(
pool_details.currency == CURRENCY_ID_LP_ETH,
"anemoy_pool::Migration: pre_upgrade failing as Anemoy's currency should be LpEthUSDC"
);

Ok(pool_details.encode())
}

fn on_runtime_upgrade() -> Weight {
let (sanity_checks, weight) = verify_sanity_checks();
if !sanity_checks {
log::error!("anemoy_pool::Migration: Sanity checks FAILED");
return weight;
}

pallet_pool_system::Pool::<Runtime>::mutate(ANEMOY_POOL_ID, |maybe_details| {
if let Some(details) = maybe_details {
details.currency = CURRENCY_ID_DOT_NATIVE;
log::info!("anemoy_pool::Migration: currency set to USDC ✓");
} else {
log::warn!("anemoy_pool::Migration: Pool details empty, skipping migration");
}
});

weight.saturating_add(
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(1, 1),
)
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(old_state: Vec<u8>) -> Result<(), &'static str> {
let mut old_pool_details = PoolDetailsOf::<Runtime>::decode(&mut old_state.as_ref())
.map_err(|_| "Error decoding pre-upgrade state")?;

let pool_details: PoolDetailsOf<Runtime> =
PoolSystem::pool(ANEMOY_POOL_ID).ok_or("Could not find Anemoy Pool")?;

// Ensure the currency set to USDC is the only mutation performed
old_pool_details.currency = CURRENCY_ID_DOT_NATIVE;
ensure!(
old_pool_details == pool_details,
"Corrupted migration: Only the currency of the Anemoy pool should have changed"
);

log::info!("anemoy_pool::Migration: post_upgrade succeeded ✓");
Ok(())
}
}

fn verify_sanity_checks() -> (bool, Weight) {
let res =
crate::Tokens::balance(
CURRENCY_ID_LP_ETH,
&<PoolSystem as PoolInspect<_, _>>::account_for(ANEMOY_POOL_ID),
) == 0 && pallet_investments::ActiveInvestOrders::<Runtime>::iter_keys()
.filter(|investment| investment.pool_id == ANEMOY_POOL_ID)
.count() == 0 && pallet_investments::ActiveRedeemOrders::<Runtime>::iter_keys()
.filter(|investment| investment.pool_id == ANEMOY_POOL_ID)
.count() == 0 && pallet_investments::InvestOrders::<Runtime>::iter_keys()
.filter(|(_, investment)| investment.pool_id == ANEMOY_POOL_ID)
.count() == 0 && pallet_investments::RedeemOrders::<Runtime>::iter_keys()
.filter(|(_, investment)| investment.pool_id == ANEMOY_POOL_ID)
.count() == 0;

let weight = <Runtime as frame_system::Config>::DbWeight::get().reads(
vec![
1, // pool account balance read
pallet_investments::ActiveInvestOrders::<Runtime>::iter_keys().count(),
pallet_investments::ActiveRedeemOrders::<Runtime>::iter_keys().count(),
pallet_investments::InvestOrders::<Runtime>::iter_keys().count(),
pallet_investments::RedeemOrders::<Runtime>::iter_keys().count(),
]
.iter()
.fold(0u64, |acc, x| acc.saturating_add(*x as u64)),
);

(res, weight)
}
}

/// Add more LP wrapped USDC variants to the asset registry as well as
/// bidirectional trading pairs to and from DOT native USDC for these.
pub mod add_wrapped_usdc_variants {
#[cfg(feature = "try-runtime")]
use cfg_traits::TokenSwaps;
use cfg_types::tokens::{
usdc::{
lp_wrapped_usdc_metadata, CHAIN_ID_ARBITRUM_MAINNET, CHAIN_ID_BASE_MAINNET,
CHAIN_ID_CELO_MAINNET, CONTRACT_ARBITRUM, CONTRACT_BASE, CONTRACT_CELO,
CURRENCY_ID_DOT_NATIVE, CURRENCY_ID_LP_ARB, CURRENCY_ID_LP_BASE, CURRENCY_ID_LP_CELO,
CURRENCY_ID_LP_ETH, MIN_SWAP_ORDER_AMOUNT,
},
CurrencyId, CustomMetadata,
};
use frame_support::traits::OnRuntimeUpgrade;
use orml_asset_registry::AssetMetadata;
use sp_runtime::SaturatedConversion;
use sp_std::{vec, vec::Vec};

use super::*;
#[cfg(feature = "try-runtime")]
use crate::OrderBook;
use crate::{liquidity_pools::LiquidityPoolsPalletIndex, Balance, OrmlAssetRegistry, Runtime};

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

impl<T: frame_system::Config> OnRuntimeUpgrade for Migration<T> {
fn on_runtime_upgrade() -> Weight {
let mut writes = 0u64;
// Register assets
for (currency_id, metadata) in Self::get_unregistered_metadata().into_iter() {
log::info!("Registering asset {:?}", currency_id);
if OrmlAssetRegistry::metadata(currency_id).is_none() {
OrmlAssetRegistry::do_register_asset_without_asset_processor(
metadata,
currency_id,
)
.map_err(|e| {
log::error!(
"Failed to register asset {:?} due to error {:?}",
currency_id,
e
);
})
// Add trading pairs if asset was registered successfully
.map(|_| {
log::info!(
"Adding bidirectional USDC trading pair for asset {:?}",
currency_id
);
pallet_order_book::TradingPair::<Runtime>::insert(
CURRENCY_ID_DOT_NATIVE,
currency_id,
MIN_SWAP_ORDER_AMOUNT,
);
pallet_order_book::TradingPair::<Runtime>::insert(
currency_id,
CURRENCY_ID_DOT_NATIVE,
MIN_SWAP_ORDER_AMOUNT,
);
// 2 from updating metadata and location, 2 from trading pairs
writes = writes.saturating_add(4);
})
.ok();
} else {
log::warn!("Skipping registration of asset {:?}", currency_id);
}
}
// Add trading pair for already registered LpEthUsdc if it does not exist yet
if !pallet_order_book::TradingPair::<Runtime>::contains_key(
CURRENCY_ID_DOT_NATIVE,
CURRENCY_ID_LP_ETH,
) {
log::info!(
"Adding trading pair from asset {:?} to USDC",
CURRENCY_ID_LP_ETH
);
pallet_order_book::TradingPair::<Runtime>::insert(
CURRENCY_ID_DOT_NATIVE,
CURRENCY_ID_LP_ETH,
MIN_SWAP_ORDER_AMOUNT,
);
writes = writes.saturating_add(1);
} else {
log::warn!(
"Skipping adding trading pair from asset {:?} to USDC",
CURRENCY_ID_LP_ETH
);
}
if !pallet_order_book::TradingPair::<Runtime>::contains_key(
CURRENCY_ID_LP_ETH,
CURRENCY_ID_DOT_NATIVE,
) {
log::info!(
"Adding trading pair from USDC to asset {:?}",
CURRENCY_ID_LP_ETH
);
pallet_order_book::TradingPair::<Runtime>::insert(
CURRENCY_ID_LP_ETH,
CURRENCY_ID_DOT_NATIVE,
MIN_SWAP_ORDER_AMOUNT,
);
writes = writes.saturating_add(1);
} else {
log::warn!(
"Skipping adding trading pair from USDC to asset {:?}",
CURRENCY_ID_LP_ETH
);
}

log::info!("add_wrapped_usdc_variants::Migration: on_runtime_upgrade succeeded ✓");

let new_assets: u64 = Self::get_unregistered_ids().len().saturated_into();
<Runtime as frame_system::Config>::DbWeight::get()
.reads_writes(new_assets.saturating_add(2), writes)
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
assert!(
Self::get_unregistered_ids()
.into_iter()
.all(|currency_id| OrmlAssetRegistry::metadata(currency_id).is_none()),
"At least one of new the wrapped USDC variants is already registered"
);

log::info!("add_wrapped_usdc_variants::Migration: pre_upgrade succeeded ✓");
Ok(Vec::new())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_: Vec<u8>) -> Result<(), &'static str> {
assert!(
Self::get_unregistered_ids()
.into_iter()
.all(|currency_id| OrmlAssetRegistry::metadata(currency_id).is_some()),
"At least one of new the wrapped USDC variants was not registered"
);

assert!(
Self::get_tradeable_ids()
.into_iter()
.all(|wrapped_usdc_id| {
OrderBook::valid_pair(CURRENCY_ID_DOT_NATIVE, wrapped_usdc_id)
}),
"At least one of the wrapped USDC variants is not enabled as trading pair into DOT native USDC"
);

assert!(
Self::get_tradeable_ids()
.into_iter()
.all(|wrapped_usdc_id| {
OrderBook::valid_pair(wrapped_usdc_id, CURRENCY_ID_DOT_NATIVE)
}),
"At least one of the wrapped USDC variants is not enabled as trading pair from DOT native USDC"
);

log::info!("add_wrapped_usdc_variants::Migration: post_upgrade succeeded ✓");
Ok(())
}
}

impl<T> Migration<T> {
fn get_unregistered_ids() -> Vec<CurrencyId> {
vec![CURRENCY_ID_LP_BASE, CURRENCY_ID_LP_ARB, CURRENCY_ID_LP_CELO]
}

#[cfg(feature = "try-runtime")]
fn get_tradeable_ids() -> Vec<CurrencyId> {
vec![
CURRENCY_ID_LP_ETH,
CURRENCY_ID_LP_BASE,
CURRENCY_ID_LP_ARB,
CURRENCY_ID_LP_CELO,
]
}

fn get_unregistered_metadata() -> Vec<(CurrencyId, AssetMetadata<Balance, CustomMetadata>)>
{
vec![
(
CURRENCY_ID_LP_BASE,
lp_wrapped_usdc_metadata(
"LP Base Wrapped USDC".as_bytes().to_vec(),
"LpBaseUSDC".as_bytes().to_vec(),
LiquidityPoolsPalletIndex::get(),
CHAIN_ID_BASE_MAINNET,
CONTRACT_BASE,
true,
),
),
(
CURRENCY_ID_LP_ARB,
lp_wrapped_usdc_metadata(
"LP Arbitrum Wrapped USDC".as_bytes().to_vec(),
"LpArbUSDC".as_bytes().to_vec(),
LiquidityPoolsPalletIndex::get(),
CHAIN_ID_ARBITRUM_MAINNET,
CONTRACT_ARBITRUM,
true,
),
),
(
CURRENCY_ID_LP_CELO,
lp_wrapped_usdc_metadata(
"LP Celo Wrapped USDC".as_bytes().to_vec(),
"LpCeloUSDC".as_bytes().to_vec(),
LiquidityPoolsPalletIndex::get(),
CHAIN_ID_CELO_MAINNET,
CONTRACT_CELO,
true,
),
),
]
}
}
}
pub type UpgradeCentrifuge1024 = ();
4 changes: 2 additions & 2 deletions runtime/development/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("centrifuge-devel"),
impl_name: create_runtime_str!("centrifuge-devel"),
authoring_version: 1,
spec_version: 1032,
spec_version: 1033,
impl_version: 1,
#[cfg(not(feature = "disable-runtime-api"))]
apis: RUNTIME_API_VERSIONS,
Expand Down Expand Up @@ -1982,7 +1982,7 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
crate::migrations::UpgradeDevelopment1031,
crate::migrations::UpgradeDevelopment1033,
>;

impl fp_self_contained::SelfContainedCall for RuntimeCall {
Expand Down
5 changes: 1 addition & 4 deletions runtime/development/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,4 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

pub type UpgradeDevelopment1031 = (
// Sets account codes for all precompiles
runtime_common::migrations::precompile_account_codes::Migration<crate::Runtime>,
);
pub type UpgradeDevelopment1033 = ();

0 comments on commit 31fdef8

Please sign in to comment.