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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//! # Farming Module
//! Root can create reward schedules paying incentives periodically to users
//! staking certain tokens.
//!
//! A reward schedule consists of two items:
//! 1. The number of periods set globally as a configuration for all pools. This number is ultimately measured in
//!    blocks; e.g., if a period is defined as 10 blocks, then a period count of 10 means 100 blocks.
//! 2. The amount of reward tokens paid in that period.
//!
//! Users are only paid a share of the rewards in the period if they have
//! staked tokens for a reward schedule that distributed more than 0 tokens.
//!
//! The following design decisions have been made:
//! - The reward schedule is configured as a matrix such that a staked token (e.g., an AMM LP token) and an incentive
//!   token (e.g., INTR or DOT) represent one reward schedule. This enables adding multiple reward currencies per staked
//!   token.
//! - Rewards can be increased but not decreased unless the schedule is explicitly removed.
//! - The rewards period cannot change without a migration.
//! - Only constant rewards per period are paid. To implement more complex reward schemes, the farming pallet relies on
//!   the scheduler pallet. This allows a creator to configure different constant payouts by scheduling
//!   `update_reward_schedule` in the future.

// #![deny(warnings)]
#![cfg_attr(test, feature(proc_macro_hygiene))]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

mod default_weights;
pub use default_weights::WeightInfo;

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{dispatch::DispatchResult, traits::Get, transactional, weights::Weight, PalletId, RuntimeDebug};
use orml_traits::{MultiCurrency, MultiReservableCurrency};
use primitives::CurrencyId;
use reward::RewardsApi;
use scale_info::TypeInfo;
use sp_runtime::{
    traits::{AccountIdConversion, AtLeast32Bit, CheckedDiv, Saturating, Zero},
    ArithmeticError, DispatchError,
};
use sp_std::vec::Vec;

pub use pallet::*;

#[derive(Clone, Default, Encode, Decode, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct RewardSchedule<Balance: MaxEncodedLen> {
    /// Number of periods remaining
    pub period_count: u32,
    /// Amount of tokens to release
    #[codec(compact)]
    pub per_period: Balance,
}

impl<Balance: AtLeast32Bit + MaxEncodedLen + Copy> RewardSchedule<Balance> {
    /// Returns total amount to distribute, `None` if calculation overflows
    pub fn total(&self) -> Option<Balance> {
        self.per_period.checked_mul(&self.period_count.into())
    }

    /// Take the next reward and decrement the period count
    pub fn take(&mut self) -> Option<Balance> {
        if self.period_count.gt(&0) {
            self.period_count.saturating_dec();
            Some(self.per_period)
        } else {
            None
        }
    }
}

#[frame_support::pallet]
pub mod pallet {
    use super::*;
    use frame_support::pallet_prelude::*;
    use frame_system::{ensure_root, ensure_signed, pallet_prelude::*};

    pub(crate) type AccountIdOf<T> = <T as frame_system::Config>::AccountId;

    pub(crate) type CurrencyIdOf<T> =
        <<T as Config>::MultiCurrency as MultiCurrency<<T as frame_system::Config>::AccountId>>::CurrencyId;

    pub(crate) type BalanceOf<T> = <<T as Config>::MultiCurrency as MultiCurrency<AccountIdOf<T>>>::Balance;

    pub(crate) type RewardScheduleOf<T> = RewardSchedule<BalanceOf<T>>;

    /// ## Configuration
    /// The pallet's configuration trait.
    #[pallet::config]
    pub trait Config: frame_system::Config {
        /// The overarching event type.
        type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

        /// The farming pallet id, used for deriving pool accounts.
        #[pallet::constant]
        type FarmingPalletId: Get<PalletId>;

        /// The treasury account id for funding pools.
        #[pallet::constant]
        type TreasuryAccountId: Get<Self::AccountId>;

        /// The period to accrue rewards.
        #[pallet::constant]
        type RewardPeriod: Get<BlockNumberFor<Self>>;

        /// Reward pools to track stake.
        type RewardPools: RewardsApi<
            CurrencyIdOf<Self>, // pool id is the lp token
            AccountIdOf<Self>,
            BalanceOf<Self>,
            CurrencyId = CurrencyIdOf<Self>,
        >;

        /// Currency handler to transfer tokens.
        type MultiCurrency: MultiReservableCurrency<AccountIdOf<Self>, CurrencyId = CurrencyId>;

        /// Weight information for the extrinsics.
        type WeightInfo: WeightInfo;
    }

    // The pallet's events
    #[pallet::event]
    #[pallet::generate_deposit(pub(crate) fn deposit_event)]
    pub enum Event<T: Config> {
        RewardScheduleUpdated {
            pool_currency_id: CurrencyIdOf<T>,
            reward_currency_id: CurrencyIdOf<T>,
            period_count: u32,
            per_period: BalanceOf<T>,
        },
        RewardDistributed {
            pool_currency_id: CurrencyIdOf<T>,
            reward_currency_id: CurrencyIdOf<T>,
            amount: BalanceOf<T>,
        },
        RewardClaimed {
            account_id: AccountIdOf<T>,
            pool_currency_id: CurrencyIdOf<T>,
            reward_currency_id: CurrencyIdOf<T>,
            amount: BalanceOf<T>,
        },
    }

    #[pallet::error]
    pub enum Error<T> {
        InsufficientStake,
        BadRewardLength,
    }

    #[pallet::hooks]
    impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
        fn on_initialize(now: BlockNumberFor<T>) -> Weight {
            if now % T::RewardPeriod::get() == Zero::zero() {
                let mut count: u32 = 0;
                // collect first to avoid modifying in-place
                let schedules = RewardSchedules::<T>::iter().collect::<Vec<_>>();
                for (pool_currency_id, reward_currency_id, mut reward_schedule) in schedules.into_iter() {
                    if let Some(amount) = reward_schedule.take() {
                        if let Ok(_) = Self::try_distribute_reward(pool_currency_id, reward_currency_id, amount) {
                            // only update the schedule if we could distribute the reward
                            RewardSchedules::<T>::insert(pool_currency_id, reward_currency_id, reward_schedule);
                            count.saturating_inc();
                            Self::deposit_event(Event::RewardDistributed {
                                pool_currency_id,
                                reward_currency_id,
                                amount,
                            });
                        }
                    } else {
                        // period count is zero
                        RewardSchedules::<T>::remove(pool_currency_id, reward_currency_id);
                        // TODO: sweep leftover rewards?
                    }
                }
                T::WeightInfo::on_initialize(count)
            } else {
                Weight::zero()
            }
        }
    }

    #[pallet::storage]
    #[pallet::getter(fn reward_schedules)]
    pub type RewardSchedules<T: Config> = StorageDoubleMap<
        _,
        Blake2_128Concat,
        CurrencyIdOf<T>, // lp token
        Blake2_128Concat,
        CurrencyIdOf<T>, // reward currency
        RewardScheduleOf<T>,
        ValueQuery,
    >;

    #[pallet::pallet]
    pub struct Pallet<T>(_);

    // The pallet's dispatchable functions.
    #[pallet::call]
    impl<T: Config> Pallet<T> {
        /// Create or overwrite the reward schedule, if a reward schedule
        /// already exists for the rewards currency the duration is added
        /// to the existing duration and the rewards per period are modified
        /// s.t. that the total (old remaining + new) rewards are distributed
        /// over the new total duration
        #[pallet::call_index(0)]
        #[pallet::weight(T::WeightInfo::update_reward_schedule())]
        #[transactional]
        pub fn update_reward_schedule(
            origin: OriginFor<T>,
            mut pool_currency_id: CurrencyIdOf<T>,
            reward_currency_id: CurrencyIdOf<T>,
            period_count: u32,
            #[pallet::compact] amount: BalanceOf<T>,
        ) -> DispatchResult {
            ensure_root(origin)?;
            pool_currency_id.sort();

            // fund the pool account from treasury
            let treasury_account_id = T::TreasuryAccountId::get();
            let pool_account_id = Self::pool_account_id(&pool_currency_id);
            T::MultiCurrency::transfer(reward_currency_id, &treasury_account_id, &pool_account_id, amount)?;

            RewardSchedules::<T>::try_mutate(pool_currency_id, reward_currency_id, |reward_schedule| {
                let total_period_count = reward_schedule
                    .period_count
                    .checked_add(period_count)
                    .ok_or(ArithmeticError::Overflow)?;
                let total_free = T::MultiCurrency::free_balance(reward_currency_id, &pool_account_id);
                let total_per_period = total_free.checked_div(&total_period_count.into()).unwrap_or_default();

                reward_schedule.period_count = total_period_count;
                reward_schedule.per_period = total_per_period;

                Self::deposit_event(Event::RewardScheduleUpdated {
                    pool_currency_id,
                    reward_currency_id,
                    period_count: total_period_count,
                    per_period: total_per_period,
                });
                Ok(().into())
            })
        }

        /// Explicitly remove a reward schedule and transfer any remaining
        /// balance to the treasury
        #[pallet::call_index(1)]
        #[pallet::weight(T::WeightInfo::remove_reward_schedule())]
        #[transactional]
        pub fn remove_reward_schedule(
            origin: OriginFor<T>,
            mut pool_currency_id: CurrencyIdOf<T>,
            reward_currency_id: CurrencyIdOf<T>,
        ) -> DispatchResultWithPostInfo {
            ensure_root(origin)?;
            pool_currency_id.sort();

            // transfer unspent rewards to treasury
            let treasury_account_id = T::TreasuryAccountId::get();
            let pool_account_id = Self::pool_account_id(&pool_currency_id);
            T::MultiCurrency::transfer(
                reward_currency_id,
                &pool_account_id,
                &treasury_account_id,
                T::MultiCurrency::free_balance(reward_currency_id, &pool_account_id),
            )?;

            RewardSchedules::<T>::remove(pool_currency_id, reward_currency_id);
            Self::deposit_event(Event::RewardScheduleUpdated {
                pool_currency_id,
                reward_currency_id,
                period_count: Zero::zero(),
                per_period: Zero::zero(),
            });

            Ok(().into())
        }

        /// Stake all pool tokens in the reward pool
        ///
        /// - `pool_currency_id`: LP token to deposit
        /// - `length_rewards`: upper bound for number of reward currencies
        #[pallet::call_index(2)]
        #[pallet::weight(T::WeightInfo::deposit(*length_rewards))]
        #[transactional]
        pub fn deposit(
            origin: OriginFor<T>,
            mut pool_currency_id: CurrencyIdOf<T>,
            length_rewards: u32,
        ) -> DispatchResult {
            let who = ensure_signed(origin)?;
            pool_currency_id.sort();
            ensure!(
                length_rewards >= T::RewardPools::reward_currencies_len(&pool_currency_id),
                Error::<T>::BadRewardLength,
            );

            // reserve lp tokens to prevent spending
            let amount = T::MultiCurrency::free_balance(pool_currency_id.clone(), &who);
            T::MultiCurrency::reserve(pool_currency_id.clone(), &who, amount)?;

            // deposit lp tokens as stake
            T::RewardPools::deposit_stake(&pool_currency_id, &who, amount)
        }

        /// Unstake the pool tokens from the reward pool
        ///
        /// - `pool_currency_id`: LP token to withdraw
        /// - `amount`: of LP token to withdraw
        /// - `length_rewards`: upper bound for number of reward currencies
        #[pallet::call_index(3)]
        #[pallet::weight(T::WeightInfo::withdraw(*length_rewards))]
        #[transactional]
        pub fn withdraw(
            origin: OriginFor<T>,
            mut pool_currency_id: CurrencyIdOf<T>,
            amount: BalanceOf<T>,
            length_rewards: u32,
        ) -> DispatchResult {
            let who = ensure_signed(origin)?;
            pool_currency_id.sort();
            ensure!(
                length_rewards >= T::RewardPools::reward_currencies_len(&pool_currency_id),
                Error::<T>::BadRewardLength,
            );

            // unreserve lp tokens to allow spending
            let remaining = T::MultiCurrency::unreserve(pool_currency_id.clone(), &who, amount);
            ensure!(remaining.is_zero(), Error::<T>::InsufficientStake);

            // withdraw lp tokens from stake
            T::RewardPools::withdraw_stake(&pool_currency_id, &who, amount)
        }

        /// Withdraw any accrued rewards from the reward pool
        #[pallet::call_index(4)]
        #[pallet::weight(T::WeightInfo::claim())]
        #[transactional]
        pub fn claim(
            origin: OriginFor<T>,
            mut pool_currency_id: CurrencyIdOf<T>,
            reward_currency_id: CurrencyIdOf<T>,
        ) -> DispatchResult {
            let who = ensure_signed(origin)?;
            pool_currency_id.sort();
            let pool_account_id = Self::pool_account_id(&pool_currency_id);

            // get reward from staking pool
            let reward = T::RewardPools::withdraw_reward(&pool_currency_id, &who, reward_currency_id)?;
            // transfer from pool to user
            T::MultiCurrency::transfer(reward_currency_id, &pool_account_id, &who, reward)?;

            Self::deposit_event(Event::RewardClaimed {
                account_id: who,
                pool_currency_id,
                reward_currency_id,
                amount: reward,
            });

            Ok(())
        }
    }
}

// "Internal" functions, callable by code.
impl<T: Config> Pallet<T> {
    pub fn pool_account_id(pool_currency_id: &CurrencyIdOf<T>) -> T::AccountId {
        T::FarmingPalletId::get().into_sub_account_truncating(pool_currency_id)
    }

    pub fn total_rewards(pool_currency_id: &CurrencyIdOf<T>, reward_currency_id: &CurrencyIdOf<T>) -> BalanceOf<T> {
        let mut pool_currency_id = pool_currency_id.clone();
        pool_currency_id.sort();
        RewardSchedules::<T>::get(pool_currency_id, reward_currency_id)
            .total()
            .unwrap_or_default()
    }

    #[transactional]
    fn try_distribute_reward(
        pool_currency_id: CurrencyIdOf<T>,
        reward_currency_id: CurrencyIdOf<T>,
        amount: BalanceOf<T>,
    ) -> Result<(), DispatchError> {
        T::RewardPools::distribute_reward(&pool_currency_id, reward_currency_id, amount)
    }
}