Skip to content

Commit

Permalink
Guard the initial migration logic
Browse files Browse the repository at this point in the history
  • Loading branch information
0xrjman committed Dec 4, 2024
1 parent 1edd389 commit d20927e
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions pallets/dummy/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub use pallet::*;

#[frame_support::pallet]
pub mod pallet {
use frame_support::{pallet_prelude::*, traits::Currency};
use frame_system::pallet_prelude::*;
use pallet_balances::{self as balances};

use sp_runtime::traits::UniqueSaturatedInto;
#[pallet::pallet]
// #[pallet::generate_store(pub(super) trait Store)]
#[pallet::without_storage_info]
Expand All @@ -17,7 +16,7 @@ pub mod pallet {
#[pallet::generate_deposit(pub (crate) fn deposit_event)]
pub enum Event<T: Config> {
// Sudo account has been migrated
SudoMigrated,
SudoMigrated(T::AccountId, T::Balance),
}

#[pallet::config]
Expand All @@ -28,10 +27,16 @@ pub mod pallet {
#[pallet::call]
impl<T: Config> Pallet<T> {}

#[pallet::storage]
#[pallet::getter(fn migration_completed)]
pub type MigrationCompleted<T> = StorageValue<_, bool, ValueQuery>;

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(_n: T::BlockNumber) -> Weight {
use sp_runtime::traits::UniqueSaturatedInto;
if Self::migration_completed() {
return Weight::zero();
}

let sudo_account = T::AccountId::decode(
&mut &[
Expand All @@ -40,9 +45,9 @@ pub mod pallet {
][..],
)
.unwrap();
let amount_to_add: T::Balance = 10_000_000_000_000_000u128.unique_saturated_into();

{
let amount_to_add: T::Balance = 10_000_000_000_000_000u128.unique_saturated_into();
let imbalance =
balances::Pallet::<T>::deposit_creating(&sudo_account, amount_to_add);
drop(imbalance);
Expand All @@ -58,9 +63,11 @@ pub mod pallet {

unhashed::put(&storage_key, &encoded_sudo_key);
}
Self::deposit_event(Event::SudoMigrated);
Self::deposit_event(Event::SudoMigrated(sudo_account, amount_to_add.into()));

MigrationCompleted::<T>::put(true);

Weight::zero()
T::DbWeight::get().reads_writes(1, 3)
}
}
}

0 comments on commit d20927e

Please sign in to comment.