1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use common::generate_storage_instance;
use frame_support::dispatch::Weight;
use frame_support::pallet_prelude::{StorageMap, StorageVersion, ValueQuery};
use frame_support::traits::Get;
use frame_support::Identity;
use sp_std::collections::btree_set::BTreeSet;

use crate::aliases::{AccountIdOf, AssetIdOf};
use crate::{AccountPools, Config, Pallet};

generate_storage_instance!(PoolXYK, AccountPools);
type OldAccountPools<T> = StorageMap<
    AccountPoolsOldInstance,
    Identity,
    AccountIdOf<T>,
    BTreeSet<AssetIdOf<T>>,
    ValueQuery,
>;

pub fn migrate<T: Config>() -> Weight {
    for (account, target_assets) in OldAccountPools::<T>::drain() {
        #[cfg(feature = "std")]
        {
            println!("{account:?}, {target_assets:?}");
        }
        AccountPools::<T>::insert(account, T::GetBaseAssetId::get(), target_assets);
    }
    StorageVersion::new(2).put::<Pallet<T>>();
    T::BlockWeights::get().max_block
}

#[cfg(test)]
mod tests {
    use frame_support::traits::GetStorageVersion;
    use hex_literal::hex;
    use sp_std::collections::btree_set::BTreeSet;

    use crate::mock::*;
    use crate::{AccountPools, Pallet};

    use super::OldAccountPools;

    #[test]
    fn test() {
        ExtBuilder::default().build().execute_with(|| {
            let target_asset_a = AssetId::from_bytes(
                hex!("0200000700000000000000000000000000000000000000000000000000000000").into(),
            );
            let target_asset_b = AssetId::from_bytes(
                hex!("0200010700000000000000000000000000000000000000000000000000000000").into(),
            );
            let target_asset_c = AssetId::from_bytes(
                hex!("0200020700000000000000000000000000000000000000000000000000000000").into(),
            );
            OldAccountPools::<Runtime>::insert(
                ALICE(),
                BTreeSet::from([target_asset_a, target_asset_b, target_asset_c]),
            );

            super::migrate::<Runtime>();

            assert_eq!(
                AccountPools::<Runtime>::iter().collect::<Vec<_>>(),
                vec![(
                    ALICE(),
                    GetBaseAssetId::get(),
                    BTreeSet::from([target_asset_a, target_asset_b, target_asset_c])
                )]
            );
            assert_eq!(Pallet::<Runtime>::on_chain_storage_version(), 2);
        });
    }
}