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 {super::*, crate::PoolsKey};
18

            
19
6
fn pending_rewards(candicate: AccountId, delegator: AccountId) -> Balance {
20
6
    pools::ManualRewards::<Runtime>::pending_rewards(&candicate, &delegator)
21
6
        .unwrap()
22
6
        .0
23
6
}
24

            
25
#[test]
26
1
fn first_delegation_init_checkpoint() {
27
1
    ExtBuilder::default().build().execute_with(|| {
28
1
        // Set counter to simulate past rewards.
29
1
        // New delegator should not receive any rewards when joining
30
1
        // and their checkpoint should be set to the current counter.
31
1
        let counter = 10;
32
1
        crate::Pools::<Runtime>::set(
33
1
            ACCOUNT_CANDIDATE_1,
34
1
            &PoolsKey::ManualRewardsCounter,
35
1
            counter,
36
1
        );
37
1

            
38
1
        assert_eq!(pending_rewards(ACCOUNT_CANDIDATE_1, ACCOUNT_DELEGATOR_1), 0);
39

            
40
1
        let amount = 2 * SHARE_INIT;
41
1
        FullDelegation {
42
1
            candidate: ACCOUNT_CANDIDATE_1,
43
1
            delegator: ACCOUNT_DELEGATOR_1,
44
1
            request_amount: amount,
45
1
            expected_increase: amount,
46
1
            ..default()
47
1
        }
48
1
        .test::<pools::ManualRewards<Runtime>>();
49
1

            
50
1
        let checkpoint = crate::Pools::<Runtime>::get(
51
1
            ACCOUNT_CANDIDATE_1,
52
1
            &PoolsKey::ManualRewardsCheckpoint {
53
1
                delegator: ACCOUNT_DELEGATOR_1,
54
1
            },
55
1
        );
56
1
        assert_eq!(checkpoint, counter);
57
1
        assert_eq!(pending_rewards(ACCOUNT_CANDIDATE_1, ACCOUNT_DELEGATOR_1), 0);
58
1
    });
59
1
}
60

            
61
#[test]
62
1
fn second_delegation_transfer_rewards() {
63
1
    ExtBuilder::default().build().execute_with(|| {
64
1
        let amount = 2 * SHARE_INIT;
65
1
        FullDelegation {
66
1
            candidate: ACCOUNT_CANDIDATE_1,
67
1
            delegator: ACCOUNT_DELEGATOR_1,
68
1
            request_amount: amount,
69
1
            expected_increase: amount,
70
1
            ..default()
71
1
        }
72
1
        .test::<pools::ManualRewards<Runtime>>();
73
1

            
74
1
        // Set counter to simulate rewards.
75
1
        let counter = 10;
76
1
        crate::Pools::<Runtime>::set(
77
1
            ACCOUNT_CANDIDATE_1,
78
1
            &PoolsKey::ManualRewardsCounter,
79
1
            counter,
80
1
        );
81
1

            
82
1
        let expected_rewards = 20; // 10 coins (counter) * 2 shares
83
1
        assert_eq!(
84
1
            pending_rewards(ACCOUNT_CANDIDATE_1, ACCOUNT_DELEGATOR_1),
85
1
            expected_rewards
86
1
        );
87

            
88
1
        FullDelegation {
89
1
            candidate: ACCOUNT_CANDIDATE_1,
90
1
            delegator: ACCOUNT_DELEGATOR_1,
91
1
            request_amount: amount,
92
1
            expected_increase: amount,
93
1
            expected_manual_rewards: expected_rewards,
94
1
        }
95
1
        .test::<pools::ManualRewards<Runtime>>();
96
1

            
97
1
        let checkpoint = crate::Pools::<Runtime>::get(
98
1
            ACCOUNT_CANDIDATE_1,
99
1
            &PoolsKey::ManualRewardsCheckpoint {
100
1
                delegator: ACCOUNT_DELEGATOR_1,
101
1
            },
102
1
        );
103
1
        assert_eq!(checkpoint, counter);
104
1
        assert_eq!(pending_rewards(ACCOUNT_CANDIDATE_1, ACCOUNT_DELEGATOR_1), 0);
105
1
    });
106
1
}
107

            
108
#[test]
109
1
fn undelegation_transfer_rewards() {
110
1
    ExtBuilder::default().build().execute_with(|| {
111
1
        let amount = 2 * SHARE_INIT;
112
1
        FullDelegation {
113
1
            candidate: ACCOUNT_CANDIDATE_1,
114
1
            delegator: ACCOUNT_DELEGATOR_1,
115
1
            request_amount: amount,
116
1
            expected_increase: amount,
117
1
            ..default()
118
1
        }
119
1
        .test::<pools::ManualRewards<Runtime>>();
120
1

            
121
1
        // Set counter to simulate rewards.
122
1
        let counter = 10;
123
1
        crate::Pools::<Runtime>::set(
124
1
            ACCOUNT_CANDIDATE_1,
125
1
            &PoolsKey::ManualRewardsCounter,
126
1
            counter,
127
1
        );
128
1

            
129
1
        let expected_rewards = 20; // 10 coins (counter) * 2 shares
130
1
        assert_eq!(
131
1
            pending_rewards(ACCOUNT_CANDIDATE_1, ACCOUNT_DELEGATOR_1),
132
1
            expected_rewards
133
1
        );
134

            
135
1
        let final_amount = 2 * SHARE_INIT;
136
1
        let leaving_amount = round_down(final_amount, 3); // test leaving rounding
137
1

            
138
1
        RequestUndelegation {
139
1
            candidate: ACCOUNT_CANDIDATE_1,
140
1
            delegator: ACCOUNT_DELEGATOR_1,
141
1
            request_amount: SharesOrStake::Stake(final_amount),
142
1
            expected_removed: final_amount,
143
1
            expected_leaving: leaving_amount,
144
1
            expected_manual_rewards: expected_rewards,
145
1
            ..default()
146
1
        }
147
1
        .test::<pools::ManualRewards<Runtime>>();
148
1

            
149
1
        let checkpoint = crate::Pools::<Runtime>::get(
150
1
            ACCOUNT_CANDIDATE_1,
151
1
            &PoolsKey::ManualRewardsCheckpoint {
152
1
                delegator: ACCOUNT_DELEGATOR_1,
153
1
            },
154
1
        );
155
1
        assert_eq!(checkpoint, counter);
156
1
        assert_eq!(pending_rewards(ACCOUNT_CANDIDATE_1, ACCOUNT_DELEGATOR_1), 0);
157
1
    });
158
1
}