|
1 | 1 | //! A module that is responsible for migration of storage.
|
2 |
| -use frame_support::traits::StorageVersion; |
3 |
| - |
| 2 | +use super::*; |
| 3 | +use frame_support::{ |
| 4 | + pallet_prelude::*, |
| 5 | + traits::{tokens::Balance as BalanceT, StorageVersion}, |
| 6 | +}; |
| 7 | +use serde::{Deserialize, Serialize}; |
4 | 8 | /// The current storage version
|
5 |
| -pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(3); |
| 9 | +pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(4); |
6 | 10 | pub const LOG: &str = "runtime::funding::migration";
|
| 11 | +use frame_support::traits::OnRuntimeUpgrade; |
| 12 | + |
| 13 | +pub mod v3tov4 { |
| 14 | + use super::*; |
| 15 | + #[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, MaxEncodedLen, TypeInfo)] |
| 16 | + pub struct OldProjectDetails< |
| 17 | + AccountId, |
| 18 | + Did, |
| 19 | + BlockNumber, |
| 20 | + Price: FixedPointNumber, |
| 21 | + Balance: BalanceT, |
| 22 | + EvaluationRoundInfo, |
| 23 | + > { |
| 24 | + pub issuer_account: AccountId, |
| 25 | + pub issuer_did: Did, |
| 26 | + /// Whether the project is frozen, so no `metadata` changes are allowed. |
| 27 | + pub is_frozen: bool, |
| 28 | + /// The price in USD per token decided after the Auction Round |
| 29 | + pub weighted_average_price: Option<Price>, |
| 30 | + /// The current status of the project |
| 31 | + pub status: OldProjectStatus, |
| 32 | + /// When the different project phases start and end |
| 33 | + pub phase_transition_points: PhaseTransitionPoints<BlockNumber>, |
| 34 | + /// Fundraising target amount in USD (6 decimals) |
| 35 | + pub fundraising_target_usd: Balance, |
| 36 | + /// The amount of Contribution Tokens that have not yet been sold |
| 37 | + pub remaining_contribution_tokens: Balance, |
| 38 | + /// Funding reached amount in USD (6 decimals) |
| 39 | + pub funding_amount_reached_usd: Balance, |
| 40 | + /// Information about the total amount bonded, and the outcome in regards to reward/slash/nothing |
| 41 | + pub evaluation_round_info: EvaluationRoundInfo, |
| 42 | + /// If the auction was oversubscribed, how much USD was raised across all winning bids |
| 43 | + pub usd_bid_on_oversubscription: Option<Balance>, |
| 44 | + /// When the Funding Round ends |
| 45 | + pub funding_end_block: Option<BlockNumber>, |
| 46 | + /// ParaId of project |
| 47 | + pub parachain_id: Option<ParaId>, |
| 48 | + /// Migration readiness check |
| 49 | + pub migration_readiness_check: Option<MigrationReadinessCheck>, |
| 50 | + /// HRMP Channel status |
| 51 | + pub hrmp_channel_status: HRMPChannelStatus, |
| 52 | + } |
| 53 | + |
| 54 | + #[derive(Clone, Copy, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)] |
| 55 | + pub struct MigrationReadinessCheck { |
| 56 | + pub holding_check: (xcm::v3::QueryId, CheckOutcome), |
| 57 | + pub pallet_check: (xcm::v3::QueryId, CheckOutcome), |
| 58 | + } |
| 59 | + |
| 60 | + impl MigrationReadinessCheck { |
| 61 | + pub fn is_ready(&self) -> bool { |
| 62 | + self.holding_check.1 == CheckOutcome::Passed(None) && |
| 63 | + matches!(self.pallet_check.1, CheckOutcome::Passed(Some(_))) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + pub type PalletIndex = u8; |
| 68 | + #[derive(Clone, Copy, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)] |
| 69 | + pub enum CheckOutcome { |
| 70 | + AwaitingResponse, |
| 71 | + Passed(Option<PalletIndex>), |
| 72 | + Failed, |
| 73 | + } |
| 74 | + |
| 75 | + #[derive( |
| 76 | + Default, Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen, Serialize, Deserialize, |
| 77 | + )] |
| 78 | + pub enum OldProjectStatus { |
| 79 | + #[default] |
| 80 | + Application, |
| 81 | + EvaluationRound, |
| 82 | + AuctionInitializePeriod, |
| 83 | + AuctionOpening, |
| 84 | + AuctionClosing, |
| 85 | + CalculatingWAP, |
| 86 | + CommunityRound, |
| 87 | + RemainderRound, |
| 88 | + FundingFailed, |
| 89 | + AwaitingProjectDecision, |
| 90 | + FundingSuccessful, |
| 91 | + ReadyToStartMigration, |
| 92 | + MigrationCompleted, |
| 93 | + } |
| 94 | + |
| 95 | + type OldProjectDetailsOf<T> = |
| 96 | + OldProjectDetails<AccountIdOf<T>, Did, BlockNumberFor<T>, PriceOf<T>, BalanceOf<T>, EvaluationRoundInfoOf<T>>; |
| 97 | + |
| 98 | + pub struct UncheckedMigrationToV4<T: Config>(PhantomData<T>); |
| 99 | + impl<T: Config> OnRuntimeUpgrade for UncheckedMigrationToV4<T> { |
| 100 | + fn on_runtime_upgrade() -> frame_support::weights::Weight { |
| 101 | + let mut items = 0; |
| 102 | + let mut translate = |_key, item: OldProjectDetailsOf<T>| -> Option<ProjectDetailsOf<T>> { |
| 103 | + items += 1; |
| 104 | + let new_status = match item.status { |
| 105 | + OldProjectStatus::Application => ProjectStatus::Application, |
| 106 | + OldProjectStatus::EvaluationRound => ProjectStatus::EvaluationRound, |
| 107 | + OldProjectStatus::AuctionInitializePeriod => ProjectStatus::AuctionInitializePeriod, |
| 108 | + OldProjectStatus::AuctionOpening => ProjectStatus::AuctionOpening, |
| 109 | + OldProjectStatus::AuctionClosing => ProjectStatus::AuctionClosing, |
| 110 | + OldProjectStatus::CalculatingWAP => ProjectStatus::CalculatingWAP, |
| 111 | + OldProjectStatus::CommunityRound => ProjectStatus::CommunityRound, |
| 112 | + OldProjectStatus::RemainderRound => ProjectStatus::RemainderRound, |
| 113 | + OldProjectStatus::FundingFailed => ProjectStatus::FundingFailed, |
| 114 | + OldProjectStatus::AwaitingProjectDecision => ProjectStatus::AwaitingProjectDecision, |
| 115 | + OldProjectStatus::FundingSuccessful => { |
| 116 | + debug_assert!(item.funding_end_block.is_none(), "Settlement shouldn't have started yet"); |
| 117 | + ProjectStatus::FundingSuccessful |
| 118 | + }, |
| 119 | + |
| 120 | + OldProjectStatus::ReadyToStartMigration => { |
| 121 | + debug_assert!(false, "No project should be in this state when upgrading to v4"); |
| 122 | + ProjectStatus::CTMigrationStarted |
| 123 | + }, |
| 124 | + OldProjectStatus::MigrationCompleted => { |
| 125 | + debug_assert!(false, "No project should be in this state when upgrading to v4"); |
| 126 | + ProjectStatus::CTMigrationFinished |
| 127 | + }, |
| 128 | + }; |
| 129 | + Some(ProjectDetailsOf::<T> { |
| 130 | + issuer_account: item.issuer_account, |
| 131 | + issuer_did: item.issuer_did, |
| 132 | + is_frozen: item.is_frozen, |
| 133 | + weighted_average_price: item.weighted_average_price, |
| 134 | + status: new_status, |
| 135 | + phase_transition_points: item.phase_transition_points, |
| 136 | + fundraising_target_usd: item.fundraising_target_usd, |
| 137 | + remaining_contribution_tokens: item.remaining_contribution_tokens, |
| 138 | + funding_amount_reached_usd: item.funding_amount_reached_usd, |
| 139 | + evaluation_round_info: item.evaluation_round_info, |
| 140 | + usd_bid_on_oversubscription: item.usd_bid_on_oversubscription, |
| 141 | + funding_end_block: item.funding_end_block, |
| 142 | + migration_type: None, |
| 143 | + }) |
| 144 | + }; |
| 145 | + |
| 146 | + crate::ProjectsDetails::<T>::translate(|key, object: OldProjectDetailsOf<T>| translate(key, object)); |
| 147 | + |
| 148 | + T::DbWeight::get().reads_writes(items, items) |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + pub type MigrationToV4<T> = frame_support::migrations::VersionedMigration< |
| 153 | + 3, |
| 154 | + 4, |
| 155 | + UncheckedMigrationToV4<T>, |
| 156 | + crate::Pallet<T>, |
| 157 | + <T as frame_system::Config>::DbWeight, |
| 158 | + >; |
| 159 | +} |
0 commit comments