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
#![cfg_attr(not(feature = "std"), no_std)]
// TODO #167: fix clippy warnings
#![allow(clippy::all)]

pub mod weights;

mod benchmarking;

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

use codec::{Decode, Encode};
use common::prelude::Balance;
use frame_support::weights::Weight;
pub use weights::WeightInfo;

#[derive(Encode, Decode, Default, PartialEq, Eq, scale_info::TypeInfo)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct StakingInfo {
    /// Amount of deposited CERES
    deposited: Balance,
    /// Current rewards in CERES
    rewards: Balance,
}

pub use pallet::*;

#[frame_support::pallet]
pub mod pallet {
    use super::*;
    use common::balance;
    use common::prelude::{Balance, FixedWrapper};
    use frame_support::pallet_prelude::*;
    use frame_support::PalletId;
    use frame_system::ensure_signed;
    use frame_system::pallet_prelude::*;
    use hex_literal::hex;
    use sp_runtime::traits::{AccountIdConversion, Zero};

    const PALLET_ID: PalletId = PalletId(*b"cerstake");

    type Assets<T> = assets::Pallet<T>;
    type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
    type AssetId = common::AssetId32<common::PredefinedAssetId>;

    #[pallet::config]
    pub trait Config: frame_system::Config + assets::Config + technical::Config {
        /// One day represented in block number
        const BLOCKS_PER_ONE_DAY: BlockNumberFor<Self>;

        type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

        /// Number of Ceres distributed per day
        type CeresPerDay: Get<Balance>;

        /// Ceres asset id
        type CeresAssetId: Get<AssetId>;

        /// Maximum Ceres in staking pool
        type MaximumCeresInStakingPool: Get<Balance>;

        /// Weight information for extrinsics in this pallet.
        type WeightInfo: WeightInfo;
    }

    #[pallet::pallet]
    #[pallet::generate_store(pub (super) trait Store)]
    #[pallet::without_storage_info]
    pub struct Pallet<T>(PhantomData<T>);

    #[pallet::event]
    #[pallet::generate_deposit(pub (super) fn deposit_event)]
    pub enum Event<T: Config> {
        /// Ceres deposited. [who, amount]
        Deposited(AccountIdOf<T>, Balance),
        /// Staked Ceres and rewards withdrawn. [who, staked, rewards]
        Withdrawn(AccountIdOf<T>, Balance, Balance),
        /// Rewards changed [balance]
        RewardsChanged(Balance),
    }

    #[pallet::error]
    pub enum Error<T> {
        /// Staking pool is full
        StakingPoolIsFull,
        /// Unauthorized
        Unauthorized,
    }

    #[pallet::type_value]
    pub fn DefaultForAuthorityAccount<T: Config>() -> AccountIdOf<T> {
        let bytes = hex!("96ea3c9c0be7bbc7b0656a1983db5eed75210256891a9609012362e36815b132");
        AccountIdOf::<T>::decode(&mut &bytes[..]).unwrap()
    }

    /// Account which has permissions for changing remaining rewards
    #[pallet::storage]
    #[pallet::getter(fn authority_account)]
    pub type AuthorityAccount<T: Config> =
        StorageValue<_, AccountIdOf<T>, ValueQuery, DefaultForAuthorityAccount<T>>;

    /// AccountId -> StakingInfo
    #[pallet::storage]
    #[pallet::getter(fn stakers)]
    pub(super) type Stakers<T: Config> =
        StorageMap<_, Identity, AccountIdOf<T>, StakingInfo, ValueQuery>;

    #[pallet::storage]
    #[pallet::getter(fn total_deposited)]
    pub(super) type TotalDeposited<T: Config> = StorageValue<_, Balance, ValueQuery>;

    #[pallet::type_value]
    pub fn RewardsRemainingDefault() -> Balance {
        balance!(600)
    }

    #[pallet::storage]
    #[pallet::getter(fn rewards_remaining)]
    pub(super) type RewardsRemaining<T: Config> =
        StorageValue<_, Balance, ValueQuery, RewardsRemainingDefault>;

    #[pallet::call]
    impl<T: Config> Pallet<T> {
        #[pallet::call_index(0)]
        #[pallet::weight(<T as Config>::WeightInfo::deposit())]
        pub fn deposit(origin: OriginFor<T>, amount: Balance) -> DispatchResultWithPostInfo {
            // Check that the extrinsic was signed and get the signer.
            // This function will return an error if the extrinsic is not signed.
            let source = ensure_signed(origin)?;

            // Maximum CERES to be in staking pool equals MaximumCeresInStakingPool
            let total_deposited = TotalDeposited::<T>::get() + amount;
            ensure!(
                total_deposited <= T::MaximumCeresInStakingPool::get(),
                Error::<T>::StakingPoolIsFull
            );

            // Transfer CERES to staking
            Assets::<T>::transfer_from(
                &T::CeresAssetId::get().into(),
                &source,
                &Self::account_id(),
                amount,
            )?;

            // Update total deposited CERES amount
            TotalDeposited::<T>::put(total_deposited);

            // Get staking info of extrinsic caller
            let mut staking_info = <Stakers<T>>::get(&source);

            // Set staking info
            staking_info.deposited = staking_info.deposited + amount;

            // Put updated staking info into storage
            <Stakers<T>>::insert(&source, staking_info);

            // Emit an event
            Self::deposit_event(Event::<T>::Deposited(source, amount));

            // Return a successful DispatchResult
            Ok(().into())
        }

        #[pallet::call_index(1)]
        #[pallet::weight(<T as Config>::WeightInfo::deposit())]
        pub fn withdraw(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
            // Check that the extrinsic was signed and get the signer.
            // This function will return an error if the extrinsic is not signed.
            let source = ensure_signed(origin)?;

            // Get staking info of extrinsic caller
            let staking_info = <Stakers<T>>::get(&source);
            let withdrawing_amount = staking_info.deposited + staking_info.rewards;

            // Withdraw CERES
            Assets::<T>::transfer_from(
                &T::CeresAssetId::get().into(),
                &Self::account_id(),
                &source,
                withdrawing_amount,
            )?;

            // Update total deposited CERES amount
            let total_deposited = TotalDeposited::<T>::get() - staking_info.deposited;
            TotalDeposited::<T>::put(total_deposited);

            // Update storage
            <Stakers<T>>::remove(&source);

            // Emit an event
            Self::deposit_event(Event::<T>::Withdrawn(
                source,
                staking_info.deposited,
                staking_info.rewards,
            ));

            // Return a successful DispatchResult
            Ok(().into())
        }

        /// Change RewardsRemaining
        #[pallet::call_index(2)]
        #[pallet::weight(<T as Config>::WeightInfo::change_rewards_remaining())]
        pub fn change_rewards_remaining(
            origin: OriginFor<T>,
            rewards_remaining: Balance,
        ) -> DispatchResultWithPostInfo {
            let user = ensure_signed(origin)?;

            if user != AuthorityAccount::<T>::get() {
                return Err(Error::<T>::Unauthorized.into());
            }

            RewardsRemaining::<T>::put(rewards_remaining);

            // Emit an event
            Self::deposit_event(Event::RewardsChanged(rewards_remaining));

            Ok(().into())
        }
    }

    #[pallet::hooks]
    impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
        fn on_initialize(now: T::BlockNumber) -> Weight {
            let mut counter: u64 = 0;

            if (now % T::BLOCKS_PER_ONE_DAY).is_zero() {
                let rewards_remaining = RewardsRemaining::<T>::get();
                let ceres_per_day = T::CeresPerDay::get();

                if rewards_remaining >= ceres_per_day {
                    let total_deposited = FixedWrapper::from(TotalDeposited::<T>::get());
                    let ceres_per_day_fixed = FixedWrapper::from(ceres_per_day);

                    for staker in <Stakers<T>>::iter() {
                        let share_in_pool =
                            FixedWrapper::from(staker.1.deposited) / total_deposited.clone();
                        let reward = share_in_pool * ceres_per_day_fixed.clone();

                        let mut staking_info = <Stakers<T>>::get(&staker.0);
                        staking_info.rewards = (FixedWrapper::from(staking_info.rewards) + reward)
                            .try_into_balance()
                            .unwrap_or(staking_info.rewards);

                        <Stakers<T>>::insert(&staker.0, staking_info);
                        counter += 1;
                    }

                    RewardsRemaining::<T>::put(rewards_remaining - ceres_per_day);
                }
            }

            T::DbWeight::get()
                .reads(4)
                .saturating_add(T::DbWeight::get().writes(counter))
        }
    }

    impl<T: Config> Pallet<T> {
        /// The account ID of pallet
        fn account_id() -> T::AccountId {
            PALLET_ID.into_account_truncating()
        }
    }
}