Skip to content

Commit aa785b5

Browse files
authored
fix min nominator bond check in distribution (#1705)
Signed-off-by: Cheng JIANG <[email protected]>
1 parent 34ba043 commit aa785b5

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

pallets/liquid-staking/src/distribution.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ use sp_std::vec::Vec;
77
pub struct AverageDistribution;
88
impl<Balance: BalanceT + FixedPointOperand> DistributionStrategy<Balance> for AverageDistribution {
99
fn get_bond_distributions(
10-
total_bonded_amounts: Vec<(DerivativeIndex, Balance)>,
10+
bonded_amounts: Vec<(DerivativeIndex, Balance, Balance)>,
1111
input: Balance,
1212
cap: Balance,
1313
min_nominator_bond: Balance,
1414
) -> Vec<(DerivativeIndex, Balance)> {
15-
let length = TryInto::<Balance>::try_into(total_bonded_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
}
1919

2020
let mut distributions: Vec<(DerivativeIndex, Balance)> = vec![];
2121
let amount = input.checked_div(&length).unwrap_or_default();
22-
for (index, bonded) in total_bonded_amounts.into_iter() {
23-
if amount.saturating_add(bonded) < min_nominator_bond {
22+
for (index, active_bonded, total_bonded) in bonded_amounts.into_iter() {
23+
if amount.saturating_add(active_bonded) < min_nominator_bond {
2424
continue;
2525
}
26-
let amount = cap.saturating_sub(bonded).min(amount);
26+
let amount = cap.saturating_sub(total_bonded).min(amount);
2727
if amount.is_zero() {
2828
continue;
2929
}
@@ -84,29 +84,29 @@ impl<Balance: BalanceT + FixedPointOperand> DistributionStrategy<Balance> for Av
8484
pub struct MaxMinDistribution;
8585
impl<Balance: BalanceT + FixedPointOperand> DistributionStrategy<Balance> for MaxMinDistribution {
8686
fn get_bond_distributions(
87-
mut total_bonded_amounts: Vec<(DerivativeIndex, Balance)>,
87+
mut bonded_amounts: Vec<(DerivativeIndex, Balance, Balance)>,
8888
input: Balance,
8989
cap: Balance,
9090
min_nominator_bond: Balance,
9191
) -> Vec<(DerivativeIndex, Balance)> {
9292
// ascending sequence
93-
total_bonded_amounts.sort_by(|a, b| a.1.cmp(&b.1));
93+
bonded_amounts.sort_by(|a, b| a.1.cmp(&b.1));
9494

9595
let mut distributions: Vec<(DerivativeIndex, Balance)> = vec![];
9696
let mut remain = input;
9797

98-
for (index, bonded) in total_bonded_amounts.into_iter() {
98+
for (index, active_bonded, total_bonded) in bonded_amounts.into_iter() {
9999
if remain.is_zero() {
100100
break;
101101
}
102-
let amount = cap.saturating_sub(bonded).min(remain);
102+
let amount = cap.saturating_sub(total_bonded).min(remain);
103103
if amount.is_zero() {
104104
// `bonding_amounts` is an ascending sequence
105105
// if occurs an item that exceed the cap, the items after this one must all be exceeded
106106
break;
107107
}
108108

109-
if amount.saturating_add(bonded) < min_nominator_bond {
109+
if amount.saturating_add(active_bonded) < min_nominator_bond {
110110
continue;
111111
}
112112

pallets/liquid-staking/src/lib.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,10 +1270,17 @@ pub mod pallet {
12701270
return Ok(());
12711271
}
12721272

1273-
let amounts: Vec<(DerivativeIndex, BalanceOf<T>)> = T::DerivativeIndexList::get()
1274-
.iter()
1275-
.map(|&index| (index, Self::total_bonded_of(index)))
1276-
.collect();
1273+
let amounts: Vec<(DerivativeIndex, BalanceOf<T>, BalanceOf<T>)> =
1274+
T::DerivativeIndexList::get()
1275+
.iter()
1276+
.map(|&index| {
1277+
(
1278+
index,
1279+
Self::active_bonded_of(index),
1280+
Self::total_bonded_of(index),
1281+
)
1282+
})
1283+
.collect();
12771284
let distributions = T::DistributionStrategy::get_bond_distributions(
12781285
amounts,
12791286
total_amount,

pallets/traits/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub trait ValidationDataProvider {
127127
/// Distribute liquidstaking asset to multi-accounts
128128
pub trait DistributionStrategy<Balance> {
129129
fn get_bond_distributions(
130-
total_bonded_amounts: Vec<(DerivativeIndex, Balance)>,
130+
bonded_amounts: Vec<(DerivativeIndex, Balance, Balance)>,
131131
input: Balance,
132132
cap: Balance,
133133
min_nominator_bond: Balance,

0 commit comments

Comments
 (0)