Skip to content

Commit

Permalink
centrifuge: Anemoy pool currency migration [MVP]
Browse files Browse the repository at this point in the history
  • Loading branch information
NunoAlexandre committed Sep 22, 2023
1 parent 1a49dd0 commit 39b4041
Showing 1 changed file with 76 additions and 1 deletion.
77 changes: 76 additions & 1 deletion runtime/centrifuge/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,80 @@
// 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 codec::{Decode, Encode};

pub type UpgradeCentrifuge1021 = ();
use crate::{PoolSystem, Runtime, Weight};

pub type UpgradeCentrifuge1021 = anemoy_pool::Migration;

/// 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_types::tokens::CurrencyId;
use frame_support::traits::OnRuntimeUpgrade;
use pallet_pool_system::PoolDetailsOf;
#[cfg(feature = "try-runtime")]
use sp_std::vec::Vec;
#[cfg(feature = "try-runtime")]
use frame_support::ensure;

use super::*;

const ANEMOY_POOL_ID: PoolId = 4_139_607_887;
const LP_ETH_USDC: CurrencyId = CurrencyId::ForeignAsset(100_001);
const DOT_NATIVE_USDC: CurrencyId = CurrencyId::ForeignAsset(6);

pub struct Migration;

impl OnRuntimeUpgrade for Migration {
#[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 == LP_ETH_USDC,
"anemoy_pool::Migration: pre_upgrade failing as Anemoy's currency should be LpEthUSDC"
);

Ok(pool_details.encode())
}

fn on_runtime_upgrade() -> Weight {
// To be executed at 1021, reject higher spec_versions
if crate::VERSION.spec_version >= 1022 {
log::info!("anemoy_pool::Migration: NOT execution since VERSION.spec_version >= 1022");
return Weight::zero();
}

pallet_pool_system::Pool::<Runtime>::mutate(ANEMOY_POOL_ID, |details| {
let details = details.as_mut().unwrap();
details.currency = DOT_NATIVE_USDC;
log::info!("anemoy_pool::Migration: Finished mutating currency to USDC");
});

<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 = DOT_NATIVE_USDC;
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(())
}
}
}

0 comments on commit 39b4041

Please sign in to comment.