Skip to content

Commit

Permalink
AssetId Storage Migration
Browse files Browse the repository at this point in the history
  • Loading branch information
JuaniRios committed Jan 16, 2025
1 parent 43e9d12 commit 28ac5db
Show file tree
Hide file tree
Showing 10 changed files with 409 additions and 38 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pallets/funding/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ impl pallet_assets::Config<ContributionTokensInstance> for TestRuntime {
type WeightInfo = ();
}

#[cfg(feature = "runtime-benchmarks")]
pub struct PalletAssetsBenchmarkHelper;
#[cfg(feature = "runtime-benchmarks")]
impl pallet_assets::BenchmarkHelper<Location> for PalletAssetsBenchmarkHelper {
fn create_asset_id_parameter(id: u32) -> Location {
(Parent, Parachain(id)).into()
Expand Down
4 changes: 1 addition & 3 deletions pallets/funding/src/runtime_api.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use crate::traits::BondingRequirementCalculation;
#[allow(clippy::wildcard_imports)]

use crate::{traits::BondingRequirementCalculation, *};
use alloc::{collections::BTreeMap, string::String};
use frame_support::traits::fungibles::{Inspect, InspectEnumerable};
use itertools::Itertools;
use parity_scale_codec::{Decode, Encode};
use polimec_common::assets::AcceptedFundingAsset;
use polimec_common::{credentials::InvestorType, ProvideAssetPrice, USD_DECIMALS};
use polimec_common::{assets::AcceptedFundingAsset, credentials::InvestorType, ProvideAssetPrice, USD_DECIMALS};
use scale_info::TypeInfo;
use sp_core::Get;
use sp_runtime::traits::Zero;
Expand Down
152 changes: 150 additions & 2 deletions pallets/funding/src/storage_migrations.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,153 @@
//! A module that is responsible for migration of storage.
use frame_support::traits::StorageVersion;
use crate::{
AccountIdOf, BiddingTicketSizes, Config, ContributingTicketSizes, CurrencyMetadata, FixedPointNumber,
ParticipantsAccountType, PriceOf, ProjectMetadataOf, StringLimitOf,
};
use core::marker::PhantomData;
use frame_support::traits::{StorageVersion, UncheckedOnRuntimeUpgrade};
use polimec_common::{assets::AcceptedFundingAsset, credentials::Cid};
use scale_info::TypeInfo;
use serde::{Deserialize, Serialize};
use sp_core::{ConstU32, Decode, Encode, Get, MaxEncodedLen, RuntimeDebug};
use sp_runtime::{BoundedVec, Percent};
extern crate alloc;
use alloc::vec::Vec;
use polimec_common::migration_types::{MigrationInfo, ParticipationType};
use xcm::v4::Location;

/// The current storage version
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(5);
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(6);
pub const LOG: &str = "runtime::funding::migration";

pub mod v5_storage_items {

use super::*;
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, MaxEncodedLen, TypeInfo, Serialize, Deserialize)]
pub struct OldProjectMetadata<BoundedString, Balance: PartialOrd + Copy, Price: FixedPointNumber, AccountId, Cid> {
/// Token Metadata
pub token_information: CurrencyMetadata<BoundedString>,
/// Mainnet Token Max Supply
pub mainnet_token_max_supply: Balance,
/// Total allocation of Contribution Tokens available for the Funding Round.
pub total_allocation_size: Balance,
/// Percentage of the total allocation of Contribution Tokens available for the Auction Round
pub auction_round_allocation_percentage: Percent,
/// The minimum price per token in USD, decimal-aware. See [`calculate_decimals_aware_price()`](crate::traits::ProvideAssetPrice::calculate_decimals_aware_price) for more information.
pub minimum_price: Price,
/// Maximum and minimum ticket sizes for auction round
pub bidding_ticket_sizes: BiddingTicketSizes<Price>,
/// Maximum and minimum ticket sizes for community/remainder rounds
pub contributing_ticket_sizes: ContributingTicketSizes<Price>,
/// Participation currencies (e.g stablecoin, DOT, KSM)
/// e.g. https://github.com/paritytech/substrate/blob/427fd09bcb193c1e79dec85b1e207c718b686c35/frame/uniques/src/types.rs#L110
/// For now is easier to handle the case where only just one Currency is accepted
pub participation_currencies:
BoundedVec<AcceptedFundingAsset, ConstU32<{ AcceptedFundingAsset::VARIANT_COUNT as u32 }>>,
pub funding_destination_account: AccountId,
/// Additional metadata
pub policy_ipfs_cid: Option<Cid>,
}

#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct OldMigrationOrigin {
pub user: Location,
pub id: u32,
pub participation_type: ParticipationType,
}
impl PartialOrd for OldMigrationOrigin {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for OldMigrationOrigin {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
if self.participation_type == other.participation_type {
self.id.cmp(&other.id)
} else {
self.participation_type.cmp(&other.participation_type)
}
}
}
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct OldMigration {
pub origin: OldMigrationOrigin,
pub info: MigrationInfo,
}
}

pub mod v6 {
use super::*;
use crate::{storage_migrations::v5_storage_items::OldMigration, MaxParticipationsPerUser};
use polimec_common::migration_types::{Migration, MigrationOrigin, MigrationStatus};

type OldProjectMetadataOf<T> = super::v5_storage_items::OldProjectMetadata<
BoundedVec<u8, StringLimitOf<T>>,
<T as pallet_balances::Config>::Balance,
PriceOf<T>,
AccountIdOf<T>,
Cid,
>;

pub struct UncheckedMigrationToV6<T: Config>(PhantomData<T>);
impl<T: Config> UncheckedOnRuntimeUpgrade for UncheckedMigrationToV6<T> {
fn on_runtime_upgrade() -> frame_support::weights::Weight {
let mut items = 0;
log::info!("Starting migration to V5");
let translate_project_details = |_key, item: OldProjectMetadataOf<T>| -> Option<ProjectMetadataOf<T>> {
items += 1;
log::info!("project_details item {:?}", items);

// let old_participation_currencies = item.participation_currencies.to_vec();
// let new_participation_currencies: BoundedVec<AcceptedFundingAsset, ConstU32<{ AcceptedFundingAsset::VARIANT_COUNT as u32 }>> = old_participation_currencies.try_into().ok()?;

Some(ProjectMetadataOf::<T> {
token_information: item.token_information,
mainnet_token_max_supply: item.mainnet_token_max_supply,
total_allocation_size: item.total_allocation_size,
auction_round_allocation_percentage: item.auction_round_allocation_percentage,
minimum_price: item.minimum_price,
bidding_ticket_sizes: item.bidding_ticket_sizes,
contributing_ticket_sizes: item.contributing_ticket_sizes,
participation_currencies: item.participation_currencies,
funding_destination_account: item.funding_destination_account,
policy_ipfs_cid: item.policy_ipfs_cid,
participants_account_type: ParticipantsAccountType::Polkadot,
})
};
crate::ProjectsMetadata::<T>::translate(translate_project_details);

let translate_migration =
|_keys,
(status, migrations): (MigrationStatus, BoundedVec<OldMigration, MaxParticipationsPerUser<T>>)|
-> Option<(MigrationStatus, BoundedVec<Migration, MaxParticipationsPerUser<T>>)> {
let old_migrations = migrations.to_vec();
let mut new_migrations = Vec::new();

for mut old_migration in old_migrations {
items += 1;
log::info!("migration items {:?}", items);
let origin_junction = old_migration.origin.user.interior.take_first().unwrap();
let new_origin = MigrationOrigin {
user: origin_junction,
id: old_migration.origin.id,
participation_type: old_migration.origin.participation_type,
};
new_migrations.push(Migration { origin: new_origin, info: old_migration.info });
}
let new_migrations = new_migrations.try_into().ok()?;
Some((status, new_migrations))
};
crate::UserMigrations::<T>::translate(translate_migration);

T::DbWeight::get().reads_writes(items, items)
}
}

pub type MigrationToV6<T> = frame_support::migrations::VersionedMigration<
5,
6,
UncheckedMigrationToV6<T>,
crate::Pallet<T>,
<T as frame_system::Config>::DbWeight,
>;
}
3 changes: 3 additions & 0 deletions pallets/proxy-bonding/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ impl pallet_balances::Config for TestRuntime {
type RuntimeHoldReason = MockRuntimeHoldReason;
}

#[cfg(feature = "runtime-benchmarks")]
pub struct PalletAssetsBenchmarkHelper;
#[cfg(feature = "runtime-benchmarks")]
impl pallet_assets::BenchmarkHelper<Location> for PalletAssetsBenchmarkHelper {
fn create_asset_id_parameter(id: u32) -> Location {
(Parent, Parachain(id)).into()
Expand All @@ -89,6 +91,7 @@ impl pallet_assets::Config for TestRuntime {
type AssetId = Location;
type AssetIdParameter = Location;
type Balance = <TestRuntime as pallet_balances::Config>::Balance;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = PalletAssetsBenchmarkHelper;
type CreateOrigin = AsEnsureOriginWithArg<frame_system::EnsureSigned<u64>>;
type Currency = Balances;
Expand Down
1 change: 1 addition & 0 deletions runtimes/polimec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ parachains-common.workspace = true
# ORML
orml-oracle.workspace = true

itertools.workspace = true
[features]
default = [ "std" ]
fast-mode = [ "shared-configuration/fast-mode" ]
Expand Down
Loading

0 comments on commit 28ac5db

Please sign in to comment.