forked from ComposableFi/composable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distribution.rs
190 lines (165 loc) · 5.2 KB
/
distribution.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
use frame_support::traits::tokens::Balance as BalanceT;
use sp_runtime::FixedPointOperand;
use sp_std::vec::Vec;
use crate::types::DerivativeIndex;
use sp_std::vec;
pub trait DistributionStrategy<Balance> {
fn get_bond_distributions(
bonded_amounts: Vec<(DerivativeIndex, Balance, Balance)>,
input: Balance,
cap: Balance,
min_nominator_bond: Balance,
) -> Vec<(DerivativeIndex, Balance)>;
fn get_unbond_distributions(
active_bonded_amounts: Vec<(DerivativeIndex, Balance)>,
input: Balance,
min_nominator_bond: Balance,
) -> Vec<(DerivativeIndex, Balance)>;
fn get_rebond_distributions(
unbonding_amounts: Vec<(DerivativeIndex, Balance)>,
input: Balance,
) -> Vec<(DerivativeIndex, Balance)>;
}
pub struct AverageDistribution;
impl<Balance: BalanceT + FixedPointOperand> DistributionStrategy<Balance> for AverageDistribution {
fn get_bond_distributions(
bonded_amounts: Vec<(DerivativeIndex, Balance, Balance)>,
input: Balance,
cap: Balance,
min_nominator_bond: Balance,
) -> Vec<(DerivativeIndex, Balance)> {
let length = TryInto::<Balance>::try_into(bonded_amounts.len()).unwrap_or_default();
if length.is_zero() {
return Default::default()
}
let mut distributions: Vec<(DerivativeIndex, Balance)> = vec![];
let amount = input.checked_div(&length).unwrap_or_default();
for (index, active_bonded, total_bonded) in bonded_amounts.into_iter() {
if amount.saturating_add(active_bonded) < min_nominator_bond {
continue
}
let amount = cap.saturating_sub(total_bonded).min(amount);
if amount.is_zero() {
continue
}
distributions.push((index, amount));
}
distributions
}
fn get_unbond_distributions(
active_bonded_amounts: Vec<(DerivativeIndex, Balance)>,
input: Balance,
min_nominator_bond: Balance,
) -> Vec<(DerivativeIndex, Balance)> {
let length = TryInto::<Balance>::try_into(active_bonded_amounts.len()).unwrap_or_default();
if length.is_zero() {
return Default::default()
}
let mut distributions: Vec<(DerivativeIndex, Balance)> = vec![];
let amount = input.checked_div(&length).unwrap_or_default();
for (index, bonded) in active_bonded_amounts.into_iter() {
if bonded.saturating_sub(amount) < min_nominator_bond {
continue
}
if amount.is_zero() {
continue
}
distributions.push((index, amount));
}
distributions
}
fn get_rebond_distributions(
unbonding_amounts: Vec<(DerivativeIndex, Balance)>,
input: Balance,
) -> Vec<(DerivativeIndex, Balance)> {
let length = TryInto::<Balance>::try_into(unbonding_amounts.len()).unwrap_or_default();
if length.is_zero() {
return Default::default()
}
let mut distributions: Vec<(DerivativeIndex, Balance)> = vec![];
let mut amount = input.checked_div(&length).unwrap_or_default();
for (index, unbonding) in unbonding_amounts.into_iter() {
amount = amount.min(unbonding);
if amount.is_zero() {
continue
}
distributions.push((index, amount));
}
distributions
}
}
pub struct MaxMinDistribution;
impl<Balance: BalanceT + FixedPointOperand> DistributionStrategy<Balance> for MaxMinDistribution {
fn get_bond_distributions(
mut bonded_amounts: Vec<(DerivativeIndex, Balance, Balance)>,
input: Balance,
cap: Balance,
min_nominator_bond: Balance,
) -> Vec<(DerivativeIndex, Balance)> {
// ascending sequence
bonded_amounts.sort_by(|a, b| a.1.cmp(&b.1));
let mut distributions: Vec<(DerivativeIndex, Balance)> = vec![];
let mut remain = input;
for (index, active_bonded, total_bonded) in bonded_amounts.into_iter() {
if remain.is_zero() {
break
}
let amount = cap.saturating_sub(total_bonded).min(remain);
if amount.is_zero() {
// `bonding_amounts` is an ascending sequence
// if occurs an item that exceed the cap, the items after this one must all be
// exceeded
break
}
if amount.saturating_add(active_bonded) < min_nominator_bond {
continue
}
distributions.push((index, amount));
remain = remain.saturating_sub(amount);
}
distributions
}
fn get_unbond_distributions(
mut active_bonded_amounts: Vec<(DerivativeIndex, Balance)>,
input: Balance,
min_nominator_bond: Balance,
) -> Vec<(DerivativeIndex, Balance)> {
// descending sequence
active_bonded_amounts.sort_by(|a, b| b.1.cmp(&a.1));
let mut distributions: Vec<(DerivativeIndex, Balance)> = vec![];
let mut remain = input;
for (index, bonded) in active_bonded_amounts.into_iter() {
if remain.is_zero() {
break
}
let amount = remain.min(bonded.saturating_sub(min_nominator_bond));
if amount.is_zero() {
continue
}
distributions.push((index, amount));
remain = remain.saturating_sub(amount);
}
distributions
}
fn get_rebond_distributions(
mut unbonding_amounts: Vec<(DerivativeIndex, Balance)>,
input: Balance,
) -> Vec<(DerivativeIndex, Balance)> {
// descending sequence
unbonding_amounts.sort_by(|a, b| b.1.cmp(&a.1));
let mut distributions: Vec<(DerivativeIndex, Balance)> = vec![];
let mut remain = input;
for (index, unbonding) in unbonding_amounts.into_iter() {
if remain.is_zero() {
break
}
let amount = remain.min(unbonding);
if amount.is_zero() {
continue
}
distributions.push((index, amount));
remain = remain.saturating_sub(amount);
}
distributions
}
}