@@ -80,6 +80,7 @@ pub const MIN_INTEREST_CALCULATING_INTERVAL: u64 = 100; // 100 seconds
8080
8181pub const MAX_EXCHANGE_RATE : u128 = 1_000_000_000_000_000_000 ; // 1
8282pub const MIN_EXCHANGE_RATE : u128 = 20_000_000_000_000_000 ; // 0.02
83+ pub const MIN_VALID_EXCHANGE_RATE : u128 = 15_000_000_000_000_000 ; // 0.015
8384
8485type AccountIdOf < T > = <T as frame_system:: Config >:: AccountId ;
8586type AssetIdOf < T > =
@@ -1470,7 +1471,7 @@ impl<T: Config> Pallet<T> {
14701471 Self :: update_reward_supply_index ( asset_id) ?;
14711472 Self :: distribute_supplier_reward ( asset_id, who) ?;
14721473
1473- let exchange_rate = Self :: exchange_rate_stored ( asset_id) ?;
1474+ let exchange_rate: FixedU128 = Self :: exchange_rate_stored ( asset_id) ?;
14741475 let redeem_amount = Self :: calc_underlying_amount ( voucher_amount, exchange_rate) ?;
14751476
14761477 AccountDeposits :: < T > :: try_mutate_exists ( asset_id, who, |deposits| -> DispatchResult {
@@ -1583,11 +1584,16 @@ impl<T: Config> Pallet<T> {
15831584 ) -> DispatchResult {
15841585 let deposits = AccountDeposits :: < T > :: get ( asset_id, who) ;
15851586 let account_earned = AccountEarned :: < T > :: get ( asset_id, who) ;
1586- let total_earned_prior_new = exchange_rate
1587- . checked_sub ( & account_earned. exchange_rate_prior )
1588- . and_then ( |r| r. checked_mul_int ( deposits. voucher_balance ) )
1589- . and_then ( |r| r. checked_add ( account_earned. total_earned_prior ) )
1590- . ok_or ( ArithmeticError :: Overflow ) ?;
1587+
1588+ let total_earned_prior_new = if exchange_rate >= account_earned. exchange_rate_prior {
1589+ exchange_rate
1590+ . checked_sub ( & account_earned. exchange_rate_prior )
1591+ . and_then ( |delta| delta. checked_mul_int ( deposits. voucher_balance ) )
1592+ . and_then ( |result| result. checked_add ( account_earned. total_earned_prior ) )
1593+ . ok_or ( ArithmeticError :: Overflow ) ?
1594+ } else {
1595+ account_earned. total_earned_prior
1596+ } ;
15911597
15921598 AccountEarned :: < T > :: insert (
15931599 asset_id,
0 commit comments