pallet_emission0/
migrations.rs

1use polkadot_sdk::frame_support::{
2    migrations::VersionedMigration, traits::UncheckedOnRuntimeUpgrade, weights::Weight,
3};
4
5use crate::{Config, Pallet};
6
7pub mod v2 {
8    use polkadot_sdk::sp_std::collections::btree_set::BTreeSet;
9
10    use pallet_emission0_api::Emission0Api;
11    use pallet_governance_api::GovernanceApi;
12    use pallet_torus0_api::Torus0Api;
13
14    use super::*;
15
16    pub type Migration<T, W> = VersionedMigration<1, 2, MigrateToV2<T>, Pallet<T>, W>;
17    pub struct MigrateToV2<T>(core::marker::PhantomData<T>);
18
19    impl<T: Config> UncheckedOnRuntimeUpgrade for MigrateToV2<T> {
20        fn on_runtime_upgrade() -> Weight {
21            let allocators: BTreeSet<_> = <T::Governance>::get_allocators().collect();
22            if let Some(allocator) = allocators.first() {
23                for agent in <T::Torus>::agent_ids() {
24                    if allocators.contains(&agent) {
25                        continue;
26                    }
27
28                    if let Err(err) =
29                        <Pallet<T> as Emission0Api<T::AccountId>>::delegate_weight_control(
30                            &agent, allocator,
31                        )
32                    {
33                        polkadot_sdk::sp_tracing::error!(
34                            "failed to delegate weight control from {agent:?} to {allocator:?}: {err:?}"
35                        );
36                    }
37                }
38            } else {
39                polkadot_sdk::sp_tracing::error!("no allocators available");
40            }
41
42            Weight::zero()
43        }
44    }
45}