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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use super::*;
use hydradx_traits::liquidity_mining::PriceAdjustment;
pub use hydradx_traits::liquidity_mining::{DepositId, GlobalFarmId, YieldFarmId};
pub type FarmId = u32;
pub type Balance = u128;
pub type FarmMultiplier = FixedU128;
pub struct DefaultPriceAdjustment;
impl<T: Config<I>, I: 'static> PriceAdjustment<GlobalFarmData<T, I>> for DefaultPriceAdjustment {
type Error = DispatchError;
type PriceAdjustment = FixedU128;
fn get(global_farm: &GlobalFarmData<T, I>) -> Result<Self::PriceAdjustment, Self::Error> {
Ok(global_farm.price_adjustment)
}
}
#[derive(Clone, Encode, Decode, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
#[codec(mel_bound())]
#[scale_info(skip_type_params(T, I))]
pub struct GlobalFarmData<T: Config<I>, I: 'static = ()> {
pub id: GlobalFarmId,
pub(super) owner: T::AccountId,
pub(super) updated_at: PeriodOf<T>,
pub(super) total_shares_z: Balance,
pub(super) accumulated_rpz: FixedU128,
pub reward_currency: T::AssetId,
pub(super) pending_rewards: Balance,
pub(super) accumulated_paid_rewards: Balance,
pub(super) yield_per_period: Perquintill,
pub(super) planned_yielding_periods: PeriodOf<T>,
pub(super) blocks_per_period: BlockNumberFor<T>,
pub incentivized_asset: T::AssetId,
pub(super) max_reward_per_period: Balance,
pub(super) min_deposit: Balance,
pub(super) live_yield_farms_count: u32,
pub(super) total_yield_farms_count: u32,
pub(super) price_adjustment: FixedU128,
pub(super) state: FarmState,
}
impl<T: Config<I>, I: 'static> GlobalFarmData<T, I> {
#[allow(clippy::too_many_arguments)]
pub fn new(
id: GlobalFarmId,
updated_at: PeriodOf<T>,
reward_currency: T::AssetId,
yield_per_period: Perquintill,
planned_yielding_periods: PeriodOf<T>,
blocks_per_period: T::BlockNumber,
owner: T::AccountId,
incentivized_asset: T::AssetId,
max_reward_per_period: Balance,
min_deposit: Balance,
price_adjustment: FixedU128,
) -> Self {
Self {
pending_rewards: Zero::zero(),
accumulated_rpz: Zero::zero(),
accumulated_paid_rewards: Zero::zero(),
total_shares_z: Zero::zero(),
live_yield_farms_count: Zero::zero(),
total_yield_farms_count: Zero::zero(),
id,
updated_at,
reward_currency,
yield_per_period,
planned_yielding_periods,
blocks_per_period,
owner,
incentivized_asset,
max_reward_per_period,
min_deposit,
price_adjustment,
state: FarmState::Active,
}
}
pub fn increase_yield_farm_counts(&mut self) -> Result<(), ArithmeticError> {
self.live_yield_farms_count = self
.live_yield_farms_count
.checked_add(1)
.ok_or(ArithmeticError::Overflow)?;
self.total_yield_farms_count = self
.total_yield_farms_count
.checked_add(1)
.ok_or(ArithmeticError::Overflow)?;
Ok(())
}
pub fn decrease_live_yield_farm_count(&mut self) -> Result<(), Error<T, I>> {
self.live_yield_farms_count = self
.live_yield_farms_count
.checked_sub(1)
.defensive_ok_or::<Error<T, I>>(InconsistentStateError::InvalidLiveYielFarmsCount.into())?;
Ok(())
}
pub fn decrease_total_yield_farm_count(&mut self) -> Result<(), Error<T, I>> {
self.total_yield_farms_count = self
.total_yield_farms_count
.checked_sub(1)
.defensive_ok_or::<Error<T, I>>(InconsistentStateError::InvalidTotalYieldFarmsCount.into())?;
Ok(())
}
pub fn has_live_farms(&self) -> bool {
!self.live_yield_farms_count.is_zero()
}
pub fn can_be_removed(&self) -> bool {
self.state == FarmState::Terminated && self.total_yield_farms_count.is_zero()
}
pub fn is_full(&self) -> bool {
self.total_yield_farms_count.ge(&<T>::MaxYieldFarmsPerGlobalFarm::get())
}
pub fn add_stake(&mut self, amount: Balance) -> Result<(), ArithmeticError> {
self.total_shares_z = self
.total_shares_z
.checked_add(amount)
.ok_or(ArithmeticError::Overflow)?;
Ok(())
}
pub fn remove_stake(&mut self, amount: Balance) -> Result<(), Error<T, I>> {
self.total_shares_z = self
.total_shares_z
.checked_sub(amount)
.defensive_ok_or::<Error<T, I>>(InconsistentStateError::InvalidTotalSharesZ.into())?;
Ok(())
}
}
#[derive(Clone, Encode, Decode, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
#[codec(mel_bound())]
#[scale_info(skip_type_params(T, I))]
pub struct YieldFarmData<T: Config<I>, I: 'static = ()> {
pub(super) id: FarmId,
pub(super) updated_at: PeriodOf<T>,
pub(super) total_shares: Balance,
pub(super) total_valued_shares: Balance,
pub(super) accumulated_rpvs: FixedU128,
pub(super) accumulated_rpz: FixedU128,
pub(super) loyalty_curve: Option<LoyaltyCurve>,
pub(super) multiplier: FarmMultiplier,
pub(super) state: FarmState,
pub(super) entries_count: u64,
pub(super) left_to_distribute: Balance,
pub(super) total_stopped: PeriodOf<T>,
pub(super) _phantom: PhantomData<I>,
}
impl<T: Config<I>, I: 'static> YieldFarmData<T, I> {
#[allow(clippy::too_many_arguments)]
pub fn new(
id: FarmId,
updated_at: PeriodOf<T>,
loyalty_curve: Option<LoyaltyCurve>,
multiplier: FarmMultiplier,
) -> Self {
Self {
id,
updated_at,
loyalty_curve,
multiplier,
accumulated_rpvs: Zero::zero(),
accumulated_rpz: Zero::zero(),
total_shares: Zero::zero(),
total_valued_shares: Zero::zero(),
state: FarmState::Active,
entries_count: Default::default(),
left_to_distribute: Default::default(),
total_stopped: Default::default(),
_phantom: PhantomData::default(),
}
}
pub fn can_be_removed(&self) -> bool {
self.state == FarmState::Terminated && self.entries_count.is_zero()
}
pub fn decrease_entries_count(&mut self) -> Result<(), Error<T, I>> {
self.entries_count = self
.entries_count
.checked_sub(1)
.defensive_ok_or::<Error<T, I>>(InconsistentStateError::InvalidYieldFarmEntriesCount.into())?;
Ok(())
}
pub fn increase_entries_count(&mut self) -> Result<(), ArithmeticError> {
self.entries_count = self.entries_count.checked_add(1).ok_or(ArithmeticError::Overflow)?;
Ok(())
}
pub fn has_entries(&self) -> bool {
!self.entries_count.is_zero()
}
}
#[derive(Clone, Encode, Decode, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(T, I))]
pub struct LoyaltyCurve {
pub initial_reward_percentage: FixedU128,
pub scale_coef: u32,
}
impl Default for LoyaltyCurve {
fn default() -> Self {
Self {
initial_reward_percentage: FixedU128::from_inner(500_000_000_000_000_000), scale_coef: 100,
}
}
}
#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq, Eq, MaxEncodedLen)]
#[codec(mel_bound())]
#[scale_info(skip_type_params(T, I))]
pub struct DepositData<T: Config<I>, I: 'static = ()> {
pub(super) shares: Balance,
pub(super) amm_pool_id: T::AmmPoolId,
pub(super) yield_farm_entries: BoundedVec<YieldFarmEntry<T, I>, T::MaxFarmEntriesPerDeposit>,
}
impl<T: Config<I>, I: 'static> DepositData<T, I> {
pub fn new(shares: Balance, amm_pool_id: T::AmmPoolId) -> Self {
Self {
shares,
amm_pool_id,
yield_farm_entries: BoundedVec::default(),
}
}
pub fn add_yield_farm_entry(&mut self, entry: YieldFarmEntry<T, I>) -> Result<(), DispatchError> {
if self.search_yield_farm_entry(entry.yield_farm_id).is_some() {
return Err(Error::<T, I>::DoubleLock.into());
}
self.yield_farm_entries
.try_push(entry)
.map_err(|_| Error::<T, I>::MaxEntriesPerDeposit)?;
Ok(())
}
pub fn remove_yield_farm_entry(&mut self, yield_farm_id: YieldFarmId) -> Result<YieldFarmEntry<T, I>, Error<T, I>> {
if let Some(idx) = self.search_yield_farm_entry(yield_farm_id) {
return Ok(self.yield_farm_entries.swap_remove(idx));
}
Err(Error::<T, I>::YieldFarmEntryNotFound)
}
pub fn get_yield_farm_entry(&mut self, yield_farm_id: YieldFarmId) -> Option<&mut YieldFarmEntry<T, I>> {
if let Some(idx) = self.search_yield_farm_entry(yield_farm_id) {
return self.yield_farm_entries.get_mut(idx);
}
None
}
pub fn search_yield_farm_entry(&self, yield_farm_id: YieldFarmId) -> Option<usize> {
self.yield_farm_entries
.iter()
.position(|e| e.yield_farm_id == yield_farm_id)
}
pub fn can_be_removed(&self) -> bool {
self.yield_farm_entries.is_empty()
}
}
#[derive(Clone, Encode, Decode, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
#[codec(mel_bound())]
#[scale_info(skip_type_params(T, I))]
pub struct YieldFarmEntry<T: Config<I>, I: 'static = ()> {
pub(super) global_farm_id: GlobalFarmId,
pub(super) yield_farm_id: YieldFarmId,
pub(super) valued_shares: Balance,
pub(super) accumulated_rpvs: FixedU128,
pub(super) accumulated_claimed_rewards: Balance,
pub(super) entered_at: PeriodOf<T>,
pub(super) updated_at: PeriodOf<T>,
pub(super) stopped_at_creation: PeriodOf<T>,
pub(super) _phantom: PhantomData<I>,
}
impl<T: Config<I>, I: 'static> YieldFarmEntry<T, I> {
pub fn new(
global_farm_id: GlobalFarmId,
yield_farm_id: YieldFarmId,
valued_shares: Balance,
accumulated_rpvs: FixedU128,
entered_at: PeriodOf<T>,
stopped_at_creation: PeriodOf<T>,
) -> Self {
Self {
global_farm_id,
yield_farm_id,
valued_shares,
accumulated_rpvs,
accumulated_claimed_rewards: Zero::zero(),
entered_at,
updated_at: entered_at,
stopped_at_creation,
_phantom: PhantomData,
}
}
}
#[derive(Clone, Encode, Decode, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub enum FarmState {
Active,
Stopped,
Terminated,
}
impl FarmState {
pub fn is_active(&self) -> bool {
*self == FarmState::Active
}
pub fn is_stopped(&self) -> bool {
*self == FarmState::Stopped
}
pub fn is_terminated(&self) -> bool {
*self == FarmState::Terminated
}
}