|
| 1 | +use alloc::vec::Vec; |
| 2 | + |
| 3 | +use polkadot_sdk::{ |
| 4 | + frame_support::{ |
| 5 | + pallet_prelude::*, |
| 6 | + storage_alias, |
| 7 | + traits::{Currency, ExistenceRequirement, OnRuntimeUpgrade, StorageVersion}, |
| 8 | + }, |
| 9 | + frame_system, |
| 10 | + sp_weights::Weight, |
| 11 | +}; |
| 12 | + |
| 13 | +use crate::Runtime; |
| 14 | + |
| 15 | +type AccountId = <Runtime as frame_system::Config>::AccountId; |
| 16 | +type Agent = pallet_torus0::agent::Agent<Runtime>; |
| 17 | + |
| 18 | +#[storage_alias(pallet_name)] |
| 19 | +type Key = StorageValue<polkadot_sdk::pallet_sudo::Pallet<Runtime>, AccountId, OptionQuery>; |
| 20 | + |
| 21 | +/// Moves the allocator role to the current sudo key. |
| 22 | +pub struct MigrateAllocator; |
| 23 | + |
| 24 | +impl OnRuntimeUpgrade for MigrateAllocator { |
| 25 | + fn on_runtime_upgrade() -> Weight { |
| 26 | + if StorageVersion::get::<pallet_governance::Pallet<Runtime>>() == StorageVersion::new(5) { |
| 27 | + if let Some(new_allocator) = sudo_key() { |
| 28 | + migrate_allocator(new_allocator); |
| 29 | + } |
| 30 | + |
| 31 | + StorageVersion::new(6).put::<pallet_governance::Pallet<Runtime>>(); |
| 32 | + } |
| 33 | + |
| 34 | + Weight::zero() |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +fn sudo_key() -> Option<AccountId> { |
| 39 | + Key::get() |
| 40 | +} |
| 41 | + |
| 42 | +fn migrate_allocator(new_allocator: AccountId) { |
| 43 | + let old_allocators = pallet_governance::Allocators::<Runtime>::iter_keys() |
| 44 | + .filter(|allocator| allocator != &new_allocator) |
| 45 | + .collect::<Vec<_>>(); |
| 46 | + |
| 47 | + let old_agent = old_allocators |
| 48 | + .iter() |
| 49 | + .find_map(pallet_torus0::Agents::<Runtime>::get); |
| 50 | + |
| 51 | + pallet_governance::Whitelist::<Runtime>::insert(&new_allocator, ()); |
| 52 | + replace_allocators(&new_allocator); |
| 53 | + register_allocator(&new_allocator, old_agent.as_ref()); |
| 54 | + transfer_allocator_stake(&old_allocators, &new_allocator); |
| 55 | + |
| 56 | + for old_allocator in old_allocators { |
| 57 | + let _ = pallet_torus0::agent::deregister::<Runtime>(old_allocator); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +fn register_allocator(new_allocator: &AccountId, old_agent: Option<&Agent>) { |
| 62 | + if pallet_torus0::Agents::<Runtime>::contains_key(new_allocator) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + if let Some(old_agent) = old_agent { |
| 67 | + fund_registration_burn(new_allocator); |
| 68 | + let _ = pallet_torus0::agent::register::<Runtime>( |
| 69 | + new_allocator.clone(), |
| 70 | + old_agent.name.to_vec(), |
| 71 | + old_agent.url.to_vec(), |
| 72 | + old_agent.metadata.to_vec(), |
| 73 | + ); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +fn replace_allocators(new_allocator: &AccountId) { |
| 78 | + let _ = pallet_governance::Allocators::<Runtime>::clear(u32::MAX, None); |
| 79 | + pallet_governance::Allocators::<Runtime>::insert(new_allocator, ()); |
| 80 | +} |
| 81 | + |
| 82 | +fn fund_registration_burn(new_allocator: &AccountId) { |
| 83 | + let _ = <Runtime as pallet_torus0::Config>::Currency::transfer( |
| 84 | + &pallet_governance::DaoTreasuryAddress::<Runtime>::get(), |
| 85 | + new_allocator, |
| 86 | + pallet_torus0::Burn::<Runtime>::get(), |
| 87 | + ExistenceRequirement::AllowDeath, |
| 88 | + ); |
| 89 | +} |
| 90 | + |
| 91 | +fn transfer_allocator_stake(old_allocators: &[AccountId], new_allocator: &AccountId) { |
| 92 | + if !pallet_torus0::Agents::<Runtime>::contains_key(new_allocator) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + let stakes = pallet_torus0::StakingTo::<Runtime>::iter() |
| 97 | + .filter(|(_, staked, _)| old_allocators.contains(staked)) |
| 98 | + .collect::<Vec<_>>(); |
| 99 | + |
| 100 | + for (staker, old_allocator, amount) in stakes { |
| 101 | + let _ = pallet_torus0::stake::transfer_stake::<Runtime>( |
| 102 | + staker, |
| 103 | + old_allocator, |
| 104 | + new_allocator.clone(), |
| 105 | + amount, |
| 106 | + ); |
| 107 | + } |
| 108 | +} |
0 commit comments