1
// Copyright (C) Moondance Labs Ltd.
2
// This file is part of Tanssi.
3

            
4
// Tanssi is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Tanssi is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Tanssi.  If not, see <http://www.gnu.org/licenses/>
16

            
17
use crate::{AccountIdOf, AssetIdOf, Config};
18
use frame_support::pallet_prelude::Weight;
19
use frame_support::traits::Get;
20
use parity_scale_codec::{Decode, Encode};
21

            
22
#[cfg(not(feature = "std"))]
23
use sp_std::{vec, vec::Vec};
24

            
25
#[derive(Encode, Decode, Clone)]
26
pub struct OldStream<AccountId, Unit, AssetId, Balance> {
27
    pub source: AccountId,
28
    pub target: AccountId,
29
    pub config: OldStreamConfig<Unit, AssetId, Balance>,
30
    pub deposit: Balance,
31
    pub last_time_updated: Balance,
32
    pub request_nonce: crate::RequestNonce,
33
    pub pending_request: Option<OldChangeRequest<Unit, AssetId, Balance>>,
34
    pub opening_deposit: Balance,
35
}
36

            
37
pub type OldStreamOf<T> =
38
    OldStream<AccountIdOf<T>, <T as Config>::TimeUnit, AssetIdOf<T>, <T as Config>::Balance>;
39

            
40
#[derive(Encode, Decode, Clone)]
41
pub struct OldStreamConfig<Unit, AssetId, BalanceOrDuration> {
42
    /// Unit in which time is measured using a `TimeProvider`.
43
    pub time_unit: Unit,
44
    /// Asset used for payment.
45
    pub asset_id: AssetId,
46
    /// Amount of asset / unit.
47
    pub rate: BalanceOrDuration,
48
}
49

            
50
#[derive(Encode, Decode, Clone)]
51
pub struct OldChangeRequest<Unit, AssetId, Balance> {
52
    pub requester: crate::Party,
53
    pub kind: crate::ChangeKind<Balance>,
54
    pub new_config: OldStreamConfig<Unit, AssetId, Balance>,
55
    pub deposit_change: Option<crate::DepositChange<Balance>>,
56
}
57

            
58
2
pub fn migrate_stream_payment_new_config_fields<T: Config>(_available_weight: Weight) -> Weight {
59
2
    let mut count = 0;
60
4
    crate::Streams::<T>::translate(|_key, value: OldStreamOf<T>| {
61
4
        count += 1;
62
4
        let OldStream {
63
4
            source,
64
4
            target,
65
4
            deposit,
66
4
            last_time_updated,
67
4
            request_nonce,
68
4
            pending_request,
69
4
            opening_deposit,
70
4
            config:
71
4
                OldStreamConfig {
72
4
                    time_unit,
73
4
                    asset_id,
74
4
                    rate,
75
4
                },
76
4
        } = value;
77
4

            
78
4
        let pending_request = pending_request.map(
79
4
            |OldChangeRequest {
80
                 requester,
81
                 kind,
82
                 new_config:
83
                     OldStreamConfig {
84
                         time_unit,
85
                         asset_id,
86
                         rate,
87
                     },
88
                 deposit_change,
89
2
             }| crate::ChangeRequest {
90
2
                requester,
91
2
                kind,
92
2
                deposit_change,
93
2
                new_config: crate::StreamConfig {
94
2
                    time_unit,
95
2
                    asset_id,
96
2
                    rate,
97
2
                    minimum_request_deadline_delay: 0u32.into(),
98
2
                    soft_minimum_deposit: 0u32.into(),
99
2
                },
100
4
            },
101
4
        );
102
4

            
103
4
        Some(crate::Stream {
104
4
            source,
105
4
            target,
106
4
            deposit,
107
4
            last_time_updated,
108
4
            request_nonce,
109
4
            pending_request,
110
4
            opening_deposit,
111
4
            config: crate::StreamConfig {
112
4
                time_unit,
113
4
                asset_id,
114
4
                rate,
115
4
                minimum_request_deadline_delay: 0u32.into(),
116
4
                soft_minimum_deposit: 0u32.into(),
117
4
            },
118
4
        })
119
4
    });
120
2

            
121
2
    let db_weights = T::DbWeight::get();
122
2
    db_weights.reads_writes(count, count)
123
2
}