multiversx_sc_modules/
staking.rs

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
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

#[derive(TopEncode, TopDecode)]
pub struct TokenAmountPair<M: ManagedTypeApi> {
    pub token_id: TokenIdentifier<M>,
    pub amount: BigUint<M>,
}

static NOT_ENOUGH_STAKE_ERR_MSG: &[u8] = b"Not enough stake";
static MINIMUM_BOARD_MEMBERS: usize = 2;
static MAXIMUM_BOARD_MEMBERS: usize = 100;

#[multiversx_sc::module]
pub trait StakingModule {
    fn init_staking_module(
        &self,
        staking_token: &EgldOrEsdtTokenIdentifier,
        staking_amount: &BigUint,
        slash_amount: &BigUint,
        slash_quorum: usize,
        user_whitelist: &ManagedVec<ManagedAddress>,
    ) {
        for user in user_whitelist {
            let _ = self.user_whitelist().insert(user.clone());
        }

        let nr_board_members = self.user_whitelist().len();
        require!(nr_board_members > 0, "No board members");
        require!(
            nr_board_members <= MAXIMUM_BOARD_MEMBERS,
            "Too many board members"
        );
        require!(
            slash_quorum < nr_board_members,
            "Quorum higher than total possible board members"
        );
        require!(
            slash_quorum >= MINIMUM_BOARD_MEMBERS,
            "Quorum minimum board members requirement not met"
        );
        require!(
            staking_amount > &0 && slash_amount > &0,
            "Staking and slash amount cannot be 0"
        );
        require!(
            slash_amount <= staking_amount,
            "Slash amount cannot be higher than required stake"
        );

        self.staking_token().set(staking_token);
        self.required_stake_amount().set(staking_amount);
        self.slash_amount().set(slash_amount);
        self.slash_quorum().set(slash_quorum);
    }

    #[payable("*")]
    #[endpoint]
    fn stake(&self) {
        let (payment_token, payment_amount) = self.call_value().egld_or_single_fungible_esdt();
        let staking_token = self.staking_token().get();
        require!(payment_token == staking_token, "Invalid payment token");

        let caller = self.blockchain().get_caller();
        require!(
            self.user_whitelist().contains(&caller),
            "Only whitelisted members can stake"
        );

        self.staked_amount(&caller)
            .update(|amt| *amt += payment_amount);
    }

    #[endpoint]
    fn unstake(&self, unstake_amount: BigUint) {
        let caller = self.blockchain().get_caller();
        let staked_amount_mapper = self.staked_amount(&caller);
        let staked_amount = staked_amount_mapper.get();
        require!(unstake_amount <= staked_amount, NOT_ENOUGH_STAKE_ERR_MSG);

        let leftover_amount = &staked_amount - &unstake_amount;
        let required_stake_amount = self.required_stake_amount().get();
        if self.user_whitelist().contains(&caller) {
            require!(
                leftover_amount >= required_stake_amount,
                NOT_ENOUGH_STAKE_ERR_MSG
            );
        }

        staked_amount_mapper.set(&leftover_amount);

        let staking_token = self.staking_token().get();
        self.tx()
            .to(caller)
            .egld_or_single_esdt(&staking_token, 0, &unstake_amount)
            .transfer();
    }

    #[endpoint(voteSlashMember)]
    fn vote_slash_member(&self, member_to_slash: ManagedAddress) {
        require!(
            self.is_staked_board_member(&member_to_slash),
            "Voted user is not a staked board member"
        );

        let caller = self.blockchain().get_caller();
        require!(
            self.is_staked_board_member(&caller),
            NOT_ENOUGH_STAKE_ERR_MSG
        );

        let _ = self
            .slashing_proposal_voters(&member_to_slash)
            .insert(caller);
    }

    #[endpoint(cancelVoteSlashMember)]
    fn cancel_vote_slash_member(&self, member_to_slash: ManagedAddress) {
        let caller = self.blockchain().get_caller();

        let _ = self
            .slashing_proposal_voters(&member_to_slash)
            .swap_remove(&caller);
    }

    #[endpoint(slashMember)]
    fn slash_member(&self, member_to_slash: ManagedAddress) {
        let quorum = self.slash_quorum().get();
        let mut slashing_voters_mapper = self.slashing_proposal_voters(&member_to_slash);
        require!(slashing_voters_mapper.len() >= quorum, "Quorum not reached");

        let slash_amount = self.slash_amount().get();
        self.staked_amount(&member_to_slash)
            .update(|amt| *amt -= &slash_amount);
        self.total_slashed_amount()
            .update(|total| *total += slash_amount);

        slashing_voters_mapper.clear();
    }

    fn is_staked_board_member(&self, user: &ManagedAddress) -> bool {
        let required_stake = self.required_stake_amount().get();
        let user_stake = self.staked_amount(user).get();

        self.user_whitelist().contains(user) && user_stake >= required_stake
    }

    #[inline]
    fn add_board_member(&self, user: ManagedAddress) {
        let _ = self.user_whitelist().insert(user);
        let nr_board_members = self.user_whitelist().len();
        require!(
            nr_board_members <= MAXIMUM_BOARD_MEMBERS,
            "Too many board members"
        );
    }

    fn remove_board_member(&self, user: &ManagedAddress) {
        let mut whitelist_mapper = self.user_whitelist();

        let slash_quorum = self.slash_quorum().get();

        let was_whitelisted = whitelist_mapper.swap_remove(user);
        let nr_board_members = whitelist_mapper.len();
        require!(
            nr_board_members > slash_quorum,
            "remaining number of board members must be greater than the slash quorum"
        );
        if !was_whitelisted {
            return;
        }

        // remove user's votes as well
        for board_member in whitelist_mapper.iter() {
            let _ = self
                .slashing_proposal_voters(&board_member)
                .swap_remove(user);
        }
        self.slashing_proposal_voters(user).clear();
    }

    #[storage_mapper("staking_module:stakingToken")]
    fn staking_token(&self) -> SingleValueMapper<EgldOrEsdtTokenIdentifier>;

    #[storage_mapper("staking_module:requiredStakeAmount")]
    fn required_stake_amount(&self) -> SingleValueMapper<BigUint>;

    #[storage_mapper("staking_module:userWhitelist")]
    fn user_whitelist(&self) -> UnorderedSetMapper<ManagedAddress>;

    #[storage_mapper("staking_module:stakedAmount")]
    fn staked_amount(&self, user: &ManagedAddress) -> SingleValueMapper<BigUint>;

    #[storage_mapper("staking_module:slashingProposalVoters")]
    fn slashing_proposal_voters(
        &self,
        slash_address: &ManagedAddress,
    ) -> UnorderedSetMapper<ManagedAddress>;

    #[storage_mapper("staking_module:slashQuorum")]
    fn slash_quorum(&self) -> SingleValueMapper<usize>;

    #[storage_mapper("staking_module:slashAmount")]
    fn slash_amount(&self) -> SingleValueMapper<BigUint>;

    #[storage_mapper("staking_module:totalSlashedAmount")]
    fn total_slashed_amount(&self) -> SingleValueMapper<BigUint>;
}