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

centrifuge: Anemoy pool currency migration #1566

Merged
merged 11 commits into from
Sep 25, 2023
4 changes: 2 additions & 2 deletions libs/types/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ where
)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct TrancheCurrency {
pub(crate) pool_id: PoolId,
pub(crate) tranche_id: TrancheId,
pub pool_id: PoolId,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any objections to open these fields visibility? by having them pub(crate) I would have to import he TrancheCurrencyT trait to call generate to build an instance of this type which is quite an overkill imo. let me know if I am not overseeing any requirement 👍

pub tranche_id: TrancheId,
}

impl From<TrancheCurrency> for CurrencyId {
Expand Down
89 changes: 88 additions & 1 deletion runtime/centrifuge/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,92 @@
// 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};

Check warning on line 12 in runtime/centrifuge/src/migrations.rs

View workflow job for this annotation

GitHub Actions / docs

unused imports: `Decode`, `Encode`

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

Check warning on line 14 in runtime/centrifuge/src/migrations.rs

View workflow job for this annotation

GitHub Actions / docs

unused import: `PoolSystem`

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, TrancheId};
use cfg_types::tokens::{CurrencyId, TrancheCurrency};
use frame_support::traits::OnRuntimeUpgrade;
use pallet_pool_system::PoolDetailsOf;

Check warning on line 25 in runtime/centrifuge/src/migrations.rs

View workflow job for this annotation

GitHub Actions / docs

unused import: `pallet_pool_system::PoolDetailsOf`
#[cfg(feature = "try-runtime")]
use sp_std::vec::Vec;
#[cfg(feature = "try-runtime")]
use frame_support::ensure;
use cfg_types::orders::TotalOrder;

use super::*;

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

Check warning on line 35 in runtime/centrifuge/src/migrations.rs

View workflow job for this annotation

GitHub Actions / docs

constant `LP_ETH_USDC` is never used
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(())
}
}

// todo(nuno): also check that pool value is 0 and check also that Investments::InvestOrders and
// Investments::RedeemOrder have no entries from Anemoy; the latter ones seem tricky at first
// sight since they are double maps first keyed by an AccountId, meaning we need to transverse
// that first which is more costly.
NunoAlexandre marked this conversation as resolved.
Show resolved Hide resolved
fn sanity_checks(tranche_id: TrancheId) -> bool {

Check warning on line 94 in runtime/centrifuge/src/migrations.rs

View workflow job for this annotation

GitHub Actions / docs

function `sanity_checks` is never used
let tc = TrancheCurrency { pool_id: ANEMOY_POOL_ID, tranche_id};

Investments::acc_active_invest_order(tc) == TotalOrder::default()
&& Investments::acc_active_redeem_order(tc) == TotalOrder::default()
}
}
Loading