Skip to content

Commit 25a9c34

Browse files
authored
update exchange rate formula & default reserve factor (#1337)
* update exchange rate formula & default reserve factor Signed-off-by: Cheng JIANG <[email protected]> * remove dbg point Signed-off-by: Cheng JIANG <[email protected]> * simplify Signed-off-by: Cheng JIANG <[email protected]>
1 parent 236947a commit 25a9c34

File tree

6 files changed

+37
-40
lines changed

6 files changed

+37
-40
lines changed

node/parallel/src/chain_spec/heiko.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ fn heiko_genesis(
260260
parachain_info: ParachainInfoConfig { parachain_id: id },
261261
liquid_staking: LiquidStakingConfig {
262262
exchange_rate: Rate::saturating_from_rational(100_u32, 100_u32), // 1
263-
reserve_factor: Zero::zero(),
263+
reserve_factor: Ratio::from_rational(125u32, 1000_000u32),
264264
},
265265
democracy: DemocracyConfig::default(),
266266
general_council: GeneralCouncilConfig::default(),

node/parallel/src/chain_spec/parallel.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ fn parallel_genesis(
272272
parachain_info: ParachainInfoConfig { parachain_id: id },
273273
liquid_staking: LiquidStakingConfig {
274274
exchange_rate: Rate::saturating_from_rational(100_u32, 100_u32), // 1
275-
reserve_factor: Zero::zero(),
275+
reserve_factor: Ratio::from_rational(5u32, 10_000u32),
276276
},
277277
democracy: DemocracyConfig::default(),
278278
general_council: GeneralCouncilConfig::default(),

node/parallel/src/chain_spec/vanilla.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ fn vanilla_genesis(
305305
parachain_info: ParachainInfoConfig { parachain_id: id },
306306
liquid_staking: LiquidStakingConfig {
307307
exchange_rate: Rate::saturating_from_rational(100u32, 100u32), // 1
308-
reserve_factor: Zero::zero(),
308+
reserve_factor: Ratio::from_rational(125u32, 1000_000u32),
309309
},
310310
democracy: DemocracyConfig::default(),
311311
general_council: GeneralCouncilConfig::default(),

pallets/liquid-staking/src/lib.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -363,21 +363,21 @@ pub mod pallet {
363363
Error::<T>::UnstakeTooSmall
364364
);
365365

366-
let asset_amount =
366+
let staking_amount =
367367
Self::liquid_to_staking(liquid_amount).ok_or(Error::<T>::InvalidExchangeRate)?;
368368

369369
let target_blocknumber = T::RelayChainBlockNumberProvider::current_block_number()
370370
.checked_add(&T::BondingDuration::get())
371371
.ok_or(ArithmeticError::Overflow)?;
372-
Self::do_push_back(&who, asset_amount, target_blocknumber)?;
372+
Self::do_push_back(&who, staking_amount, target_blocknumber)?;
373373

374374
T::Assets::burn_from(Self::liquid_currency()?, &who, liquid_amount)?;
375375

376376
MatchingPool::<T>::try_mutate(|p| -> DispatchResult {
377-
p.update_total_unstake_amount(liquid_amount, ArithmeticKind::Addition)
377+
p.update_total_unstake_amount(staking_amount, ArithmeticKind::Addition)
378378
})?;
379379

380-
Self::deposit_event(Event::<T>::Unstaked(who, liquid_amount, asset_amount));
380+
Self::deposit_event(Event::<T>::Unstaked(who, liquid_amount, staking_amount));
381381
Ok(().into())
382382
}
383383

@@ -434,7 +434,7 @@ pub mod pallet {
434434

435435
let old_matching_ledger = Self::matching_pool();
436436
let (bond_amount, rebond_amount, unbond_amount) =
437-
MatchingPool::<T>::try_mutate(|b| b.matching::<Self>(unbonding_amount))?;
437+
MatchingPool::<T>::try_mutate(|b| b.matching(unbonding_amount))?;
438438

439439
if bonding_amount.is_zero() && unbonding_amount.is_zero() {
440440
Self::do_bond(bond_amount, RewardDestination::Staked)?;
@@ -667,10 +667,7 @@ pub mod pallet {
667667
Self::notify_placeholder(),
668668
)?;
669669

670-
let liquid_amount =
671-
Self::staking_to_liquid(amount).ok_or(Error::<T>::InvalidExchangeRate)?;
672-
673-
XcmRequests::<T>::insert(query_id, XcmRequest::Unbond { liquid_amount });
670+
XcmRequests::<T>::insert(query_id, XcmRequest::Unbond { amount });
674671

675672
Self::deposit_event(Event::<T>::Unbonding(amount));
676673

@@ -682,14 +679,18 @@ pub mod pallet {
682679
if amount.is_zero() {
683680
return Ok(());
684681
}
682+
685683
let query_id = T::XCM::do_rebond(
686684
amount,
687685
Self::staking_currency()?,
688686
T::DerivativeIndex::get(),
689687
Self::notify_placeholder(),
690688
)?;
689+
691690
XcmRequests::<T>::insert(query_id, XcmRequest::Rebond { amount });
691+
692692
Self::deposit_event(Event::<T>::Rebonding(amount));
693+
693694
Ok(())
694695
}
695696

@@ -739,25 +740,26 @@ pub mod pallet {
739740
res: Option<(u32, XcmError)>,
740741
) -> DispatchResult {
741742
let executed = res.is_none();
743+
use XcmRequest::*;
742744

743745
match request {
744-
XcmRequest::Bond { amount, .. } | XcmRequest::BondExtra { amount } if executed => {
746+
Bond { amount, .. } | BondExtra { amount } if executed => {
745747
MatchingPool::<T>::try_mutate(|p| -> DispatchResult {
746748
p.update_total_stake_amount(amount, ArithmeticKind::Subtraction)
747749
})?;
748750
T::Assets::burn_from(Self::staking_currency()?, &Self::account_id(), amount)?;
749751
}
750-
XcmRequest::Unbond { liquid_amount } if executed => {
752+
Unbond { amount } if executed => {
751753
MatchingPool::<T>::try_mutate(|p| -> DispatchResult {
752-
p.update_total_unstake_amount(liquid_amount, ArithmeticKind::Subtraction)
754+
p.update_total_unstake_amount(amount, ArithmeticKind::Subtraction)
753755
})?;
754756
}
755-
XcmRequest::Rebond { amount } if executed => {
757+
Rebond { amount } if executed => {
756758
MatchingPool::<T>::try_mutate(|p| -> DispatchResult {
757759
p.update_total_stake_amount(amount, ArithmeticKind::Subtraction)
758760
})?;
759761
}
760-
XcmRequest::WithdrawUnbonded {
762+
WithdrawUnbonded {
761763
num_slashing_spans: _,
762764
amount,
763765
} if executed => {
@@ -769,6 +771,7 @@ pub mod pallet {
769771
if executed {
770772
XcmRequests::<T>::remove(&query_id);
771773
}
774+
772775
Ok(())
773776
}
774777

@@ -780,10 +783,9 @@ pub mod pallet {
780783
match Rate::checked_from_rational(
781784
bonding_amount
782785
.checked_add(old_matching_ledger.total_stake_amount)
786+
.and_then(|r| r.checked_sub(old_matching_ledger.total_unstake_amount))
783787
.ok_or(ArithmeticError::Overflow)?,
784-
T::Assets::total_issuance(Self::liquid_currency()?)
785-
.checked_add(old_matching_ledger.total_unstake_amount)
786-
.ok_or(ArithmeticError::Overflow)?,
788+
T::Assets::total_issuance(Self::liquid_currency()?),
787789
) {
788790
Some(exchange_rate) if exchange_rate != Self::exchange_rate() => {
789791
ExchangeRate::<T>::put(exchange_rate);

pallets/liquid-staking/src/tests.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,30 +77,33 @@ fn test_settlement_should_work() {
7777
use StakeOp::*;
7878
TestNet::reset();
7979
ParaA::execute_with(|| {
80-
let test_case: Vec<(Vec<StakeOp>, Balance, (Balance, Balance, Balance))> = vec![
80+
let test_case: Vec<(Vec<StakeOp>, Balance, Balance, (Balance, Balance, Balance))> = vec![
8181
(
8282
vec![Stake(ksm(5000f64)), Unstake(ksm(1000f64))],
8383
0,
84+
0,
8485
(ksm(3975f64), 0, 0),
8586
),
8687
// Calculate right here.
8788
(
8889
vec![Unstake(ksm(10f64)), Unstake(ksm(5f64)), Stake(ksm(10f64))],
90+
ksm(3975f64),
8991
0,
9092
(0, 0, ksm(5.05f64)),
9193
),
92-
(vec![], 0, (0, 0, 0)),
94+
// (vec![], 0, (0, 0, 0)),
9395
];
94-
for (i, (stake_ops, unbonding_amount, matching_result)) in test_case.into_iter().enumerate()
96+
for (i, (stake_ops, bonding_amount, unbonding_amount, matching_result)) in
97+
test_case.into_iter().enumerate()
9598
{
9699
stake_ops.into_iter().for_each(StakeOp::execute);
97100
assert_eq!(
98-
LiquidStaking::matching_pool().matching::<LiquidStaking>(unbonding_amount),
101+
LiquidStaking::matching_pool().matching(unbonding_amount),
99102
Ok(matching_result)
100103
);
101104
assert_ok!(LiquidStaking::settlement(
102105
Origin::signed(ALICE),
103-
ksm(0f64),
106+
bonding_amount,
104107
unbonding_amount,
105108
));
106109
LiquidStaking::notification_received(

pallets/liquid-staking/src/types.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use codec::{Decode, Encode};
22

33
use super::{BalanceOf, Config};
44
use frame_support::{dispatch::DispatchResult, traits::tokens::Balance as BalanceT};
5-
use primitives::{ArithmeticKind, LiquidStakingConvert};
5+
use primitives::ArithmeticKind;
66
use scale_info::TypeInfo;
77
use sp_runtime::{traits::Zero, ArithmeticError, DispatchError, FixedPointOperand, RuntimeDebug};
88
use sp_std::{cmp::Ordering, result::Result, vec::Vec};
@@ -22,26 +22,23 @@ impl<Balance: BalanceT + FixedPointOperand> MatchingLedger<Balance> {
2222
/// `unbonding_amount` is the total amount of the unbonding asset on the relaychain.
2323
///
2424
/// the returned tri-tuple is formed as `(bond_amount, rebond_amount, unbond_amount)`.
25-
pub fn matching<T: LiquidStakingConvert<Balance>>(
25+
pub fn matching(
2626
&mut self,
2727
unbonding_amount: Balance,
2828
) -> Result<(Balance, Balance, Balance), DispatchError> {
2929
use Ordering::*;
3030

31-
let unstake_asset_amout =
32-
T::liquid_to_staking(self.total_unstake_amount).ok_or(ArithmeticError::Overflow)?;
33-
3431
let (bond_amount, rebond_amount, unbond_amount) = if matches!(
35-
self.total_stake_amount.cmp(&unstake_asset_amout),
32+
self.total_stake_amount.cmp(&self.total_unstake_amount),
3633
Less | Equal
3734
) {
3835
(
3936
Zero::zero(),
4037
Zero::zero(),
41-
unstake_asset_amout - self.total_stake_amount,
38+
self.total_unstake_amount - self.total_stake_amount,
4239
)
4340
} else {
44-
let amount = self.total_stake_amount - unstake_asset_amout;
41+
let amount = self.total_stake_amount - self.total_unstake_amount;
4542
if amount < unbonding_amount {
4643
(Zero::zero(), amount, Zero::zero())
4744
} else {
@@ -52,12 +49,7 @@ impl<Balance: BalanceT + FixedPointOperand> MatchingLedger<Balance> {
5249
self.total_stake_amount = bond_amount
5350
.checked_add(&rebond_amount)
5451
.ok_or(ArithmeticError::Overflow)?;
55-
if unbond_amount.is_zero() {
56-
self.total_unstake_amount = Zero::zero();
57-
} else {
58-
self.total_unstake_amount =
59-
T::staking_to_liquid(unbond_amount).ok_or(ArithmeticError::Overflow)?;
60-
}
52+
self.total_unstake_amount = unbond_amount;
6153

6254
Ok((bond_amount, rebond_amount, unbond_amount))
6355
}
@@ -117,7 +109,7 @@ pub enum XcmRequest<T: Config> {
117109
amount: BalanceOf<T>,
118110
},
119111
Unbond {
120-
liquid_amount: BalanceOf<T>,
112+
amount: BalanceOf<T>,
121113
},
122114
Rebond {
123115
amount: BalanceOf<T>,

0 commit comments

Comments
 (0)