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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//! This is explains plasm inflation models.
//! The staking has 2 kinds.
//!
//! 1. Validator Staking
//! 2. Dapps(Operator) Staking
//!
//! About each staking, this module computes issuing new tokens.

use super::*;
use num_traits::sign::Unsigned;
use sp_arithmetic::traits::BaseArithmetic;
use sp_std::marker::PhantomData;
use traits::ComputeTotalPayout;

pub struct FirstPlasmIncentive<N: BaseArithmetic + Clone + From<u32>> {
    _phantom: PhantomData<N>,
}

impl<L, D> ComputeTotalPayout<L, D> for FirstPlasmIncentive<L>
where
    L: BaseArithmetic + Clone + From<u32>,
{
    fn compute<N, M>(
        total_tokens: N,
        era_duration: M,
        number_of_validator: L,
        _dapps_staking: D,
    ) -> (N, N)
    where
        N: BaseArithmetic + Unsigned + Clone + From<u32>,
        M: BaseArithmetic + Clone + From<u32>,
    {
        const TARGETS_NUMBER: u128 = 100;
        const MILLISECONDS_PER_YEAR: u128 = 1000 * 3600 * 24 * 36525 / 100;
        // I_0 = 2.5%.
        const I_0_DENOMINATOR: u128 = 25;
        const I_0_NUMERATOR: u128 = 1000;
        let number_of_validator_clone: u128 = number_of_validator.clone().unique_saturated_into();
        let era_duration_clone: u128 = era_duration.clone().unique_saturated_into();
        let number_of_validator: u128 = number_of_validator.unique_saturated_into();
        let portion = if TARGETS_NUMBER < number_of_validator_clone {
            // TotalForSecurityRewards
            // = TotalAmountOfIssue * I_0% * (EraDuration / 1year)

            // denominator: I_0_DENOMINATOR * EraDuration
            // numerator: 1year * I_0_NUMERATOR
            Perbill::from_rational_approximation(
                I_0_DENOMINATOR * era_duration_clone,
                MILLISECONDS_PER_YEAR * I_0_NUMERATOR,
            )
        } else {
            // TotalForSecurityRewards
            // = TotalAmountOfIssue * I_0% * (NumberOfValidators/TargetsNumber) * (EraDuration/1year)

            // denominator: I_0_DENOMINATOR * NumberOfValidators * EraDuration
            // numerator: 1year * I_0_NUMERATOR * TargetsNumber
            Perbill::from_rational_approximation(
                I_0_DENOMINATOR * number_of_validator * era_duration_clone,
                MILLISECONDS_PER_YEAR * I_0_NUMERATOR * TARGETS_NUMBER,
            )
        };
        let payout = portion * total_tokens;
        (payout, N::zero())
    }
}

pub struct CommunityRewards<N: BaseArithmetic + Clone + From<u32>> {
    _phantom: PhantomData<N>,
}

impl<L, D> ComputeTotalPayout<L, D> for CommunityRewards<L>
where
    L: BaseArithmetic + Clone + From<u32>,
{
    /// budget 2.5% of total tokens to validator staking and also 2.5% of total tokens to dapps staking
    fn compute<N, M>(
        total_tokens: N,
        era_duration: M,
        number_of_validator: L,
        _dapps_staking: D,
    ) -> (N, N)
    where
        N: BaseArithmetic + Unsigned + Clone + From<u32>,
        M: BaseArithmetic + Clone + From<u32>,
    {
        const TARGETS_NUMBER: u128 = 100;
        const MILLISECONDS_PER_YEAR: u128 = 1000 * 3600 * 24 * 36525 / 100;
        // I_0 = 2.5%.
        const I_0_DENOMINATOR: u128 = 25;
        const I_0_NUMERATOR: u128 = 1000;
        let number_of_validator_clone: u128 = number_of_validator.clone().unique_saturated_into();
        let era_duration_clone: u128 = era_duration.clone().unique_saturated_into();
        let number_of_validator: u128 = number_of_validator.unique_saturated_into();
        let portion = if TARGETS_NUMBER < number_of_validator_clone {
            // TotalForSecurityRewards
            // = TotalAmountOfIssue * I_0% * (EraDuration / 1year)

            // denominator: I_0_DENOMINATOR * EraDuration
            // numerator: 1year * I_0_NUMERATOR
            Perbill::from_rational_approximation(
                I_0_DENOMINATOR * era_duration_clone,
                MILLISECONDS_PER_YEAR * I_0_NUMERATOR,
            )
        } else {
            // TotalForSecurityRewards
            // = TotalAmountOfIssue * I_0% * (NumberOfValidators/TargetsNumber) * (EraDuration/1year)

            // denominator: I_0_DENOMINATOR * NumberOfValidators * EraDuration
            // numerator: 1year * I_0_NUMERATOR * TargetsNumber
            Perbill::from_rational_approximation(
                I_0_DENOMINATOR * number_of_validator * era_duration_clone,
                MILLISECONDS_PER_YEAR * I_0_NUMERATOR * TARGETS_NUMBER,
            )
        };
        let payout = portion * total_tokens;
        (payout.clone(), payout)
    }
}

pub struct SimpleComputeTotalPayout;

/// The total payout to all operators and validators and their nominators per era.
///
/// Testnet(Until migrate NPoS) defined as such:
///     20% of total issue tokens per a year.
///
/// `era_duration` is expressed in millisecond.
impl<V, D> ComputeTotalPayout<V, D> for SimpleComputeTotalPayout {
    fn compute<N, M>(
        total_tokens: N,
        era_duration: M,
        _validator_staking: V,
        _dapps_staking: D,
    ) -> (N, N)
    where
        N: BaseArithmetic + Unsigned + Clone + From<u32>,
        M: BaseArithmetic + Clone + From<u32>,
    {
        // Milliseconds per year for the Julian year (365.25 days).
        const MILLISECONDS_PER_YEAR: u64 = 1000 * 3600 * 24 * 36525 / 100;
        let portion = Perbill::from_rational_approximation(
            era_duration.unique_saturated_into(),
            MILLISECONDS_PER_YEAR * 5,
        );
        let payout = portion * total_tokens;
        (payout.clone(), payout)
    }
}

pub struct MaintainRatioComputeTotalPayout<Balance>
where
    Balance: BaseArithmetic + Clone + From<u32>,
{
    _phantom: PhantomData<Balance>,
}

/// The total payout to all operators and validators and their nominators per era.
///
/// Testnet(Until migrate NPoS) defined as such:
///     20% of total issue tokens per a year.
/// Maintainn is Distribute rewards while maintaining a ratio of validator and dapps-compatible staking amounts.
///
/// `era_duration` is expressed in millisecond.
impl<Balance> ComputeTotalPayout<Balance, Balance> for MaintainRatioComputeTotalPayout<Balance>
where
    Balance: BaseArithmetic + Unsigned + Clone + From<u32>,
{
    fn compute<N, M>(
        total_tokens: N,
        era_duration: M,
        validator_staking: Balance,
        dapps_staking: Balance,
    ) -> (N, N)
    where
        N: BaseArithmetic + Unsigned + Clone + From<u32>,
        M: BaseArithmetic + Clone + From<u32>,
    {
        // Milliseconds per year for the Julian year (365.25 days).
        const MILLISECONDS_PER_YEAR: u64 = 1000 * 60 * 60 * 24 * 36525 / 100;
        let portion = Perbill::from_rational_approximation(
            era_duration.unique_saturated_into(),
            MILLISECONDS_PER_YEAR * 5,
        );
        let payout = portion * total_tokens;
        if validator_staking == Balance::zero() {
            if dapps_staking == Balance::zero() {
                return (N::zero(), N::zero());
            }
            return (N::zero(), payout);
        }
        let validator_portion = Perbill::from_rational_approximation(
            validator_staking.clone(),
            validator_staking + dapps_staking,
        );
        let validator_payout = validator_portion * payout.clone();
        let dapps_payout = payout - validator_payout.clone();
        (validator_payout, dapps_payout)
    }
}

#[cfg(test)]
mod test {
    use super::*;
    fn compute_test<N>(
        total_tokens: N,
        era_duration: u64,
        validator_staking: N,
        dapps_staking: N,
    ) -> (N, N)
    where
        N: BaseArithmetic + Unsigned + Clone + From<u32>,
    {
        SimpleComputeTotalPayout::compute(
            total_tokens,
            era_duration,
            validator_staking,
            dapps_staking,
        )
    }

    fn compute_maintain_total_payout_test<N>(
        total_tokens: N,
        era_duration: u64,
        validator_staking: N,
        dapps_staking: N,
    ) -> (N, N)
    where
        N: BaseArithmetic + Unsigned + Clone + From<u32>,
    {
        MaintainRatioComputeTotalPayout::<N>::compute(
            total_tokens,
            era_duration,
            validator_staking,
            dapps_staking,
        )
    }

    fn compute_first_rewards_test<N>(
        total_tokens: N,
        era_duration: u64,
        number_of_validator: u32,
    ) -> (N, N)
    where
        N: BaseArithmetic + Unsigned + Clone + From<u32>,
    {
        FirstPlasmIncentive::<u32>::compute(total_tokens, era_duration, number_of_validator, 0)
    }

    fn compute_community_rewards_test<N>(
        total_tokens: N,
        era_duration: u64,
        number_of_validator: u32,
    ) -> (N, N)
    where
        N: BaseArithmetic + Unsigned + Clone + From<u32>,
    {
        CommunityRewards::<u32>::compute(total_tokens, era_duration, number_of_validator, 0)
    }

    #[test]
    fn test_compute_test() {
        const YEAR: u64 = 365 * 24 * 60 * 60 * 1000;
        // check maximum inflation.
        // not 10_000 due to rounding error.
        assert_eq!(compute_test(100_000_000u64, YEAR, 0, 0).0, 19_986_311);

        const DAY: u64 = 24 * 60 * 60 * 1000;
        assert_eq!(compute_test(100_000_000u64, DAY, 0, 0).0, 54_757);

        const SIX_HOURS: u64 = 6 * 60 * 60 * 1000;
        assert_eq!(compute_test(100_000_000u64, SIX_HOURS, 0, 0).0, 13_689);

        const HOUR: u64 = 60 * 60 * 1000;
        assert_eq!(compute_test(100_000_000u64, HOUR, 0, 0).0, 2_281);
    }

    #[test]
    fn test_maintain_compute_test() {
        const YEAR: u64 = 365 * 24 * 60 * 60 * 1000;
        // check maximum inflation.
        // not 10_000 due to rounding error.
        assert_eq!(
            compute_maintain_total_payout_test(100_000_000u64, YEAR, 90, 10),
            (17_987_680, 1_998_631)
        );

        assert_eq!(
            compute_maintain_total_payout_test(100_000_000u64, YEAR, 70, 30),
            (13_990_418, 599_5893)
        );

        assert_eq!(
            compute_maintain_total_payout_test(100_000_000u64, YEAR, 50, 50),
            (9_993_155, 9_993_156)
        );

        assert_eq!(
            compute_maintain_total_payout_test(100_000_000u64, YEAR, 10, 90),
            (1_998_631, 17_987_680)
        );

        assert_eq!(
            compute_maintain_total_payout_test(100_000_000u64, YEAR, 0, 100),
            (0, 19_986_311)
        );

        assert_eq!(
            compute_maintain_total_payout_test(100_000_000u64, YEAR, 100, 0),
            (19_986_311, 0)
        );

        assert_eq!(
            compute_maintain_total_payout_test(100_000_000u64, YEAR, 0, 0),
            (0, 0)
        );
    }

    #[test]
    fn test_first_rewards_compute() {
        const YEAR: u64 = 1000 * 3600 * 24 * 36525 / 100;
        assert_eq!(
            compute_first_rewards_test(100_000_000u64, YEAR, 100),
            (2_500_000, 0)
        );

        assert_eq!(
            compute_first_rewards_test(100_000_000u64, YEAR, 150),
            (2_500_000, 0)
        );

        assert_eq!(
            compute_first_rewards_test(100_000_000u64, YEAR, 50),
            (1_250_000, 0)
        );

        assert_eq!(
            compute_first_rewards_test(100_000_000u64, YEAR / 365, 100),
            (2_500_000 / 365, 0)
        );
    }

    #[test]
    fn test_community_rewards_compute() {
        const YEAR: u64 = 1000 * 3600 * 24 * 36525 / 100;
        assert_eq!(
            compute_community_rewards_test(100_000_000u64, YEAR, 100),
            (2_500_000, 2_500_000)
        );

        assert_eq!(
            compute_community_rewards_test(100_000_000u64, YEAR, 150),
            (2_500_000, 2_500_000)
        );

        assert_eq!(
            compute_community_rewards_test(100_000_000u64, YEAR, 50),
            (1_250_000, 1_250_000)
        );

        assert_eq!(
            compute_community_rewards_test(100_000_000u64, YEAR / 365, 100),
            (2_500_000 / 365, 2_500_000 / 365)
        );
    }
}