Skip to content

Commit

Permalink
Stable asset migration (#1085)
Browse files Browse the repository at this point in the history
* feat: 🎸 add migration

* fix: 🐛 migration

* fix: 🐛 clippy
  • Loading branch information
yooml authored Dec 4, 2023
1 parent cabb9d9 commit 8f55579
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 10 deletions.
2 changes: 2 additions & 0 deletions pallets/stable-asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ mod tests;

pub mod weights;

pub mod migration;

pub use crate::traits::StableAsset;
use frame_support::{dispatch::DispatchResult, ensure, traits::Get, weights::Weight};
use frame_system::pallet_prelude::BlockNumberFor;
Expand Down
114 changes: 114 additions & 0 deletions pallets/stable-asset/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// This file is part of Bifrost.

// Copyright (C) Liebi Technologies PTE. LTD.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program 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.

// This program 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.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![cfg_attr(not(feature = "std"), no_std)]

use super::{Config, Weight, *};
use frame_support::traits::Get;

pub fn update_pallet_id<T: Config>() -> Weight {
let pool_count: u32 = PoolCount::<T>::get();

for pool_id in 0..pool_count {
Pools::<T>::try_mutate_exists(pool_id, |maybe_pool_info| -> DispatchResult {
let to: T::AccountId = T::PalletId::get().into_sub_account_truncating(pool_id);
let pool_info = maybe_pool_info.as_mut().ok_or(Error::<T>::PoolNotFound)?;

for (_, &asset_id) in pool_info.assets.iter().enumerate() {
let balance = T::Assets::free_balance(asset_id, &pool_info.account_id);
if balance == Zero::zero() {
continue;
}
T::Assets::transfer(asset_id, &pool_info.account_id, &to, balance)?;
}
let pool_asset_balance =
T::Assets::free_balance(pool_info.pool_asset, &pool_info.account_id);
T::Assets::transfer(
pool_info.pool_asset,
&pool_info.account_id,
&to,
pool_asset_balance,
)?;

pool_info.account_id = to;
Ok(())
})
.ok();
}

let count: u64 = (pool_count * 3).into();
Weight::from(T::DbWeight::get().reads_writes(count, count))
}

use frame_support::{pallet_prelude::PhantomData, traits::OnRuntimeUpgrade};
pub struct StableAssetOnRuntimeUpgrade<T>(PhantomData<T>);
impl<T: super::Config> OnRuntimeUpgrade for StableAssetOnRuntimeUpgrade<T> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<sp_std::prelude::Vec<u8>, sp_runtime::DispatchError> {
#[allow(unused_imports)]
use frame_support::PalletId;
log::info!("Bifrost `pre_upgrade`...");

Ok(vec![])
}

fn on_runtime_upgrade() -> Weight {
log::info!("Bifrost `on_runtime_upgrade`...");

let weight = super::migration::update_pallet_id::<T>();

log::info!("Bifrost `on_runtime_upgrade finished`");

weight
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_: sp_std::prelude::Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
#[allow(unused_imports)]
use frame_support::PalletId;
log::info!("Bifrost `post_upgrade`...");
let old_pallet_id: PalletId = PalletId(*b"nuts/sta");

let pool_count: u32 = PoolCount::<T>::get();
for pool_id in 0..pool_count {
if let Some(pool_info) = Pools::<T>::get(pool_id) {
let old_account_id: T::AccountId =
old_pallet_id.into_sub_account_truncating(pool_id);
for (_, &asset_id) in pool_info.assets.iter().enumerate() {
let old_balance = T::Assets::free_balance(asset_id, &old_account_id);
assert_eq!(old_balance, Zero::zero());

let balance = T::Assets::free_balance(asset_id, &pool_info.account_id);
if balance != Zero::zero() {
continue;
} else {
log::info!(
"New pool {:?} asset_id {:?} free_balance is zero.",
pool_id,
asset_id
);
}
}
} else {
log::info!("Pool {:?} not found", pool_id);
}
}

Ok(())
}
}
10 changes: 4 additions & 6 deletions runtime/bifrost-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ parameter_types! {
pub const FarmingBoostPalletId: PalletId = PalletId(*b"bf/fmbst");
pub const LendMarketPalletId: PalletId = PalletId(*b"bf/ldmkt");
pub const OraclePalletId: PalletId = PalletId(*b"bf/oracl");
pub const StableAssetPalletId: PalletId = PalletId(*b"bf/stabl");
}

impl frame_system::Config for Runtime {
Expand Down Expand Up @@ -1640,9 +1641,6 @@ impl bifrost_stable_asset::traits::ValidateAssetId<CurrencyId> for EnsurePoolAss
true
}
}
parameter_types! {
pub const StableAssetPalletId: PalletId = PalletId(*b"nuts/sta");
}

/// Configure the pallet bifrost_stable_asset in pallets/bifrost_stable_asset.
impl bifrost_stable_asset::Config for Runtime {
Expand Down Expand Up @@ -1901,11 +1899,11 @@ pub type Migrations = migrations::Unreleased;

/// The runtime migrations per release.
pub mod migrations {
#[allow(unused)]
use super::*;
use crate::Runtime;
use bifrost_stable_asset::migration::StableAssetOnRuntimeUpgrade;

/// Unreleased migrations. Add new ones here:
pub type Unreleased = ();
pub type Unreleased = StableAssetOnRuntimeUpgrade<Runtime>;
}

/// Executive: handles dispatch to the various modules.
Expand Down
10 changes: 6 additions & 4 deletions runtime/bifrost-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ parameter_types! {
pub const FarmingBoostPalletId: PalletId = PalletId(*b"bf/fmbst");
pub const LendMarketPalletId: PalletId = PalletId(*b"bf/ldmkt");
pub const OraclePalletId: PalletId = PalletId(*b"bf/oracl");
pub const StableAssetPalletId: PalletId = PalletId(*b"bf/stabl");
}

impl frame_system::Config for Runtime {
Expand Down Expand Up @@ -1311,9 +1312,6 @@ impl bifrost_stable_asset::traits::ValidateAssetId<CurrencyId> for EnsurePoolAss
true
}
}
parameter_types! {
pub const StableAssetPalletId: PalletId = PalletId(*b"nuts/sta");
}

/// Configure the pallet bifrost_stable_asset in pallets/bifrost_stable_asset.
impl bifrost_stable_asset::Config for Runtime {
Expand Down Expand Up @@ -1705,8 +1703,12 @@ pub type Migrations = migrations::Unreleased;
/// The runtime migrations per release.
pub mod migrations {
use crate::Runtime;
use bifrost_stable_asset::migration::StableAssetOnRuntimeUpgrade;
/// Unreleased migrations. Add new ones here:
pub type Unreleased = bifrost_asset_registry::migration::InsertBNCMetadata<Runtime>;
pub type Unreleased = (
bifrost_asset_registry::migration::InsertBNCMetadata<Runtime>,
StableAssetOnRuntimeUpgrade<Runtime>,
);
}

/// Executive: handles dispatch to the various modules.
Expand Down

0 comments on commit 8f55579

Please sign in to comment.