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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use common::{generate_storage_instance, AssetInfoProvider};
use frame_support::dispatch::Weight;
use frame_support::pallet_prelude::{StorageValue, ValueQuery};
use frame_support::traits::Get;
use orml_tokens::{AccountData, Accounts};
use sp_runtime::traits::UniqueSaturatedInto;
use sp_std::collections::btree_set::BTreeSet;
use sp_std::vec::Vec;

use crate::{Config, PoolProviders, Properties, TotalIssuances};

generate_storage_instance!(PoolXYK, MarkerTokensIndex);
type OldMarkerTokensIndex<AssetId> =
    StorageValue<MarkerTokensIndexOldInstance, BTreeSet<AssetId>, ValueQuery>;

pub fn migrate<T: Config>() -> Weight {
    if OldMarkerTokensIndex::<T::AssetId>::exists() {
        OldMarkerTokensIndex::<T::AssetId>::kill();
    }
    let mut acc_asset_currs = Vec::new();
    Properties::<T>::translate::<(T::AccountId, T::AccountId, T::AssetId), _>(
        |_ba, _ta, (reserves_acc, fee_acc, marker_asset)| {
            let currency: <T as orml_tokens::Config>::CurrencyId = marker_asset.clone().into();
            acc_asset_currs.push((reserves_acc.clone(), marker_asset, currency));
            Some((reserves_acc, fee_acc))
        },
    );

    for (reserves_acc, asset, _) in &acc_asset_currs {
        let total_issuance = if let Ok(issuance) = assets::Pallet::<T>::total_issuance(asset) {
            issuance
        } else {
            continue;
        };
        TotalIssuances::<T>::insert(reserves_acc, total_issuance);
    }

    Accounts::<T>::translate(
        |account, currency, data: AccountData<<T as orml_tokens::Config>::Balance>| {
            if let Some((pool_acc, _, _)) = acc_asset_currs
                .iter()
                .find(|(_, _, probe_currency)| probe_currency == &currency)
            {
                let balance: u128 = data.free.unique_saturated_into();
                PoolProviders::<T>::insert(&pool_acc, account, balance);
                None
            } else {
                Some(data)
            }
        },
    );

    T::BlockWeights::get().max_block
}

#[cfg(test)]
mod tests {
    use common::{
        balance, generate_storage_instance, AssetName, AssetSymbol, DEFAULT_BALANCE_PRECISION,
    };
    use frame_support::pallet_prelude::StorageDoubleMap;
    use frame_support::Blake2_128Concat;
    use hex_literal::hex;
    use sp_std::collections::btree_set::BTreeSet;

    use crate::mock::{AccountId, AssetId, ExtBuilder, Runtime, ALICE, BOB};
    use crate::{PoolProviders, Properties, TotalIssuances};

    use super::OldMarkerTokensIndex;

    generate_storage_instance!(PoolXYK, Properties);

    type OldProperties<AccountId, AssetId> = StorageDoubleMap<
        PropertiesOldInstance,
        Blake2_128Concat,
        AssetId,
        Blake2_128Concat,
        AssetId,
        (AccountId, AccountId, AssetId),
    >;

    #[test]
    fn test() {
        ExtBuilder::default().build().execute_with(|| {
            let asset1 = AssetId::from_bytes(
                hex!("0200000700000000000000000000000000000000000000000000000000000000").into(),
            );
            let asset2 = AssetId::from_bytes(
                hex!("0200010700000000000000000000000000000000000000000000000000000000").into(),
            );
            let asset3 = AssetId::from_bytes(
                hex!("0200020700000000000000000000000000000000000000000000000000000000").into(),
            );
            let asset4 = AssetId::from_bytes(
                hex!("0200030700000000000000000000000000000000000000000000000000000000").into(),
            );
            let asset5 = AssetId::from_bytes(
                hex!("0200040700000000000000000000000000000000000000000000000000000000").into(),
            );
            let asset6 = AssetId::from_bytes(
                hex!("0200050700000000000000000000000000000000000000000000000000000000").into(),
            );
            let set: BTreeSet<AssetId> = vec![asset1, asset2, asset3].into_iter().collect();
            OldMarkerTokensIndex::<AssetId>::put(set);
            OldProperties::<AccountId, AssetId>::insert::<_, _, (AccountId, AccountId, AssetId)>(
                &asset1,
                &asset2,
                (ALICE(), ALICE(), asset3.clone()),
            );
            OldProperties::<AccountId, AssetId>::insert::<_, _, (AccountId, AccountId, AssetId)>(
                &asset4,
                &asset5,
                (BOB(), BOB(), asset6.clone()),
            );

            assets::Pallet::<Runtime>::register_asset_id(
                ALICE(),
                asset3.clone(),
                AssetSymbol(b"A".to_vec()),
                AssetName(b"B".to_vec()),
                DEFAULT_BALANCE_PRECISION,
                0,
                true,
                None,
                None,
            )
            .unwrap();

            assets::Pallet::<Runtime>::mint_to(&asset3, &ALICE(), &ALICE(), balance!(3)).unwrap();
            assets::Pallet::<Runtime>::mint_to(&asset3, &ALICE(), &BOB(), balance!(3)).unwrap();

            assets::Pallet::<Runtime>::register_asset_id(
                BOB(),
                asset6.clone(),
                AssetSymbol(b"C".to_vec()),
                AssetName(b"D".to_vec()),
                DEFAULT_BALANCE_PRECISION,
                0,
                true,
                None,
                None,
            )
            .unwrap();

            assets::Pallet::<Runtime>::mint_to(&asset6, &BOB(), &ALICE(), balance!(4)).unwrap();
            assets::Pallet::<Runtime>::mint_to(&asset6, &BOB(), &BOB(), balance!(4)).unwrap();

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

            assert!(!OldMarkerTokensIndex::<AssetId>::exists());
            assert_eq!(
                Properties::<Runtime>::iter().collect::<Vec<_>>(),
                vec![
                    (asset1, asset2, (ALICE(), ALICE())),
                    (asset4, asset5, (BOB(), BOB())),
                ]
            );
            assert_eq!(TotalIssuances::<Runtime>::get(ALICE()), Some(balance!(6)));
            assert_eq!(TotalIssuances::<Runtime>::get(BOB()), Some(balance!(8)));

            assert_eq!(
                PoolProviders::<Runtime>::get(ALICE(), ALICE()),
                Some(balance!(3))
            );
            assert_eq!(
                PoolProviders::<Runtime>::get(ALICE(), BOB()),
                Some(balance!(3))
            );

            assert_eq!(
                PoolProviders::<Runtime>::get(BOB(), ALICE()),
                Some(balance!(4))
            );
            assert_eq!(
                PoolProviders::<Runtime>::get(BOB(), BOB()),
                Some(balance!(4))
            );
        });
    }
}