@@ -7,64 +7,71 @@ use sp_std::vec::Vec;
77pub struct AverageDistribution ;
88impl < Balance : BalanceT + FixedPointOperand > DistributionStrategy < Balance > for AverageDistribution {
99 fn get_bond_distributions (
10- bonding_amounts : & mut Vec < ( DerivativeIndex , Balance ) > ,
10+ bonded_amounts : Vec < ( DerivativeIndex , Balance ) > ,
1111 input : Balance ,
1212 cap : Balance ,
1313 min_nominator_bond : Balance ,
1414 ) -> Vec < ( DerivativeIndex , Balance ) > {
15- let length = TryInto :: < Balance > :: try_into ( bonding_amounts . len ( ) ) . unwrap_or_default ( ) ;
15+ let length = TryInto :: < Balance > :: try_into ( bonded_amounts . len ( ) ) . unwrap_or_default ( ) ;
1616 if length. is_zero ( ) {
1717 return Default :: default ( ) ;
1818 }
19+
1920 let mut distributions: Vec < ( DerivativeIndex , Balance ) > = vec ! [ ] ;
2021 let amount = input. checked_div ( & length) . unwrap_or_default ( ) ;
21- for ( index, bonded) in bonding_amounts. iter ( ) {
22- if amount. saturating_add ( * bonded) < min_nominator_bond {
22+ for ( index, bonded) in bonded_amounts. into_iter ( ) {
23+ if amount. saturating_add ( bonded) < min_nominator_bond {
24+ continue ;
25+ }
26+ let amount = cap. saturating_sub ( bonded) . min ( amount) ;
27+ if amount. is_zero ( ) {
2328 continue ;
2429 }
25- let amount = cap. saturating_sub ( * bonded) . min ( amount) ;
26- distributions. push ( ( * index, amount) ) ;
30+ distributions. push ( ( index, amount) ) ;
2731 }
2832
2933 distributions
3034 }
3135
3236 fn get_unbond_distributions (
33- bonding_amounts : & mut Vec < ( DerivativeIndex , Balance ) > ,
37+ bonded_amounts : Vec < ( DerivativeIndex , Balance ) > ,
3438 input : Balance ,
3539 _cap : Balance ,
3640 min_nominator_bond : Balance ,
3741 ) -> Vec < ( DerivativeIndex , Balance ) > {
38- let length = TryInto :: < Balance > :: try_into ( bonding_amounts . len ( ) ) . unwrap_or_default ( ) ;
42+ let length = TryInto :: < Balance > :: try_into ( bonded_amounts . len ( ) ) . unwrap_or_default ( ) ;
3943 if length. is_zero ( ) {
4044 return Default :: default ( ) ;
4145 }
46+
4247 let mut distributions: Vec < ( DerivativeIndex , Balance ) > = vec ! [ ] ;
4348 let amount = input. checked_div ( & length) . unwrap_or_default ( ) ;
44- for ( index, bonded) in bonding_amounts . iter ( ) {
49+ for ( index, bonded) in bonded_amounts . into_iter ( ) {
4550 if bonded. saturating_sub ( amount) < min_nominator_bond {
4651 continue ;
4752 }
48- distributions. push ( ( * index, amount) ) ;
53+ distributions. push ( ( index, amount) ) ;
4954 }
5055
5156 distributions
5257 }
5358
5459 fn get_rebond_distributions (
55- unbonding_amounts : & mut Vec < ( DerivativeIndex , Balance , Balance ) > ,
60+ unbonding_bonded_amounts : Vec < ( DerivativeIndex , Balance , Balance ) > ,
5661 input : Balance ,
5762 _cap : Balance ,
5863 _min_nominator_bond : Balance ,
5964 ) -> Vec < ( DerivativeIndex , Balance ) > {
60- let length = TryInto :: < Balance > :: try_into ( unbonding_amounts. len ( ) ) . unwrap_or_default ( ) ;
65+ let length =
66+ TryInto :: < Balance > :: try_into ( unbonding_bonded_amounts. len ( ) ) . unwrap_or_default ( ) ;
6167 if length. is_zero ( ) {
6268 return Default :: default ( ) ;
6369 }
70+
6471 let mut distributions: Vec < ( DerivativeIndex , Balance ) > = vec ! [ ] ;
6572 let amount = input. checked_div ( & length) . unwrap_or_default ( ) ;
66- for ( index, _, _) in unbonding_amounts . iter ( ) {
67- distributions. push ( ( * index, amount) ) ;
73+ for ( index, _, _) in unbonding_bonded_amounts . into_iter ( ) {
74+ distributions. push ( ( index, amount) ) ;
6875 }
6976
7077 distributions
@@ -76,86 +83,86 @@ impl<Balance: BalanceT + FixedPointOperand> DistributionStrategy<Balance>
7683 for MaximizationDistribution
7784{
7885 fn get_bond_distributions (
79- bonding_amounts : & mut Vec < ( DerivativeIndex , Balance ) > ,
86+ mut bonded_amounts : Vec < ( DerivativeIndex , Balance ) > ,
8087 input : Balance ,
8188 cap : Balance ,
8289 min_nominator_bond : Balance ,
8390 ) -> Vec < ( DerivativeIndex , Balance ) > {
84- //ascending sequence
85- bonding_amounts . sort_by ( |a, b| a. 1 . cmp ( & b. 1 ) ) ;
91+ // ascending sequence
92+ bonded_amounts . sort_by ( |a, b| a. 1 . cmp ( & b. 1 ) ) ;
8693
8794 let mut distributions: Vec < ( DerivativeIndex , Balance ) > = vec ! [ ] ;
8895 let mut remain = input;
8996
90- for ( index, bonded) in bonding_amounts . iter ( ) {
97+ for ( index, bonded) in bonded_amounts . into_iter ( ) {
9198 if remain. is_zero ( ) {
9299 break ;
93100 }
94- let amount = cap. saturating_sub ( * bonded) . min ( remain) ;
101+ let amount = cap. saturating_sub ( bonded) . min ( remain) ;
95102 if amount. is_zero ( ) {
96103 // `bonding_amounts` is an ascending sequence
97104 // if occurs an item that exceed the cap, the items after this one must all be exceeded
98105 break ;
99106 }
100107
101- if amount. saturating_add ( * bonded) < min_nominator_bond {
108+ if amount. saturating_add ( bonded) < min_nominator_bond {
102109 continue ;
103110 }
104111
105- distributions. push ( ( * index, amount) ) ;
112+ distributions. push ( ( index, amount) ) ;
106113 remain = remain. saturating_sub ( amount) ;
107114 }
108115
109116 distributions
110117 }
111118
112119 fn get_unbond_distributions (
113- bonding_amounts : & mut Vec < ( DerivativeIndex , Balance ) > ,
120+ mut bonded_amounts : Vec < ( DerivativeIndex , Balance ) > ,
114121 input : Balance ,
115122 _cap : Balance ,
116123 min_nominator_bond : Balance ,
117124 ) -> Vec < ( DerivativeIndex , Balance ) > {
118125 // descending sequence
119- bonding_amounts . sort_by ( |a, b| b. 1 . cmp ( & a. 1 ) ) ;
126+ bonded_amounts . sort_by ( |a, b| b. 1 . cmp ( & a. 1 ) ) ;
120127
121128 let mut distributions: Vec < ( DerivativeIndex , Balance ) > = vec ! [ ] ;
122129 let mut remain = input;
123130
124- for ( index, bonded) in bonding_amounts . iter ( ) {
131+ for ( index, bonded) in bonded_amounts . into_iter ( ) {
125132 if remain. is_zero ( ) {
126133 break ;
127134 }
128135 let amount = remain. min ( bonded. saturating_sub ( min_nominator_bond) ) ;
129136 if amount. is_zero ( ) {
130137 continue ;
131138 }
132- distributions. push ( ( * index, amount) ) ;
139+ distributions. push ( ( index, amount) ) ;
133140 remain = remain. saturating_sub ( amount) ;
134141 }
135142
136143 distributions
137144 }
138145 fn get_rebond_distributions (
139- unbonding_amounts : & mut Vec < ( DerivativeIndex , Balance , Balance ) > ,
146+ mut unbonding_bonded_amounts : Vec < ( DerivativeIndex , Balance , Balance ) > ,
140147 input : Balance ,
141148 cap : Balance ,
142149 _min_nominator_bond : Balance ,
143150 ) -> Vec < ( DerivativeIndex , Balance ) > {
144151 // descending sequence
145- unbonding_amounts . sort_by ( |a, b| b. 1 . cmp ( & a. 1 ) ) ;
152+ unbonding_bonded_amounts . sort_by ( |a, b| b. 1 . cmp ( & a. 1 ) ) ;
146153
147154 let mut distributions: Vec < ( DerivativeIndex , Balance ) > = vec ! [ ] ;
148155 let mut remain = input;
149156
150- for ( index, unlocking , active ) in unbonding_amounts . iter ( ) {
157+ for ( index, unbonding , bonded ) in unbonding_bonded_amounts . into_iter ( ) {
151158 if remain. is_zero ( ) {
152159 break ;
153160 }
154- let amount = remain. min ( * unlocking ) . min ( cap. saturating_sub ( * active ) ) ;
161+ let amount = remain. min ( unbonding ) . min ( cap. saturating_sub ( bonded ) ) ;
155162 if amount. is_zero ( ) {
156163 continue ;
157164 }
158- distributions. push ( ( * index, amount) ) ;
165+ distributions. push ( ( index, amount) ) ;
159166 remain = remain. saturating_sub ( amount) ;
160167 }
161168
0 commit comments