@@ -46,6 +46,7 @@ module liquid_staking::native_pool {
4646 const E_REWARD_NOT_IN_THRESHOLD : u64 = 107 ;
4747 const E_BAD_SHARES : u64 = 108 ;
4848 const E_TOO_BIG_PERCENT : u64 = 109 ;
49+ const E_NOT_ENOUGH_BALANCE : u64 = 110 ;
4950
5051 /* Events */
5152 struct StakedEvent has copy , drop {
@@ -188,16 +189,18 @@ module liquid_staking::native_pool {
188189 *table::borrow (&self.total_staked, self.staked_update_epoch) + pending
189190 }
190191
191- // returns total staked for active epoch
192+ /// returns total staked for active epoch
193+ /// active stake can be unstaked
192194 public fun get_total_active_stake (self: &NativePool , ctx: &mut TxContext ): u64 {
193195 let last_active_epoch = self.staked_update_epoch;
194196 let current_epoch = tx_context::epoch (ctx);
195197
196198 if (last_active_epoch > current_epoch) {
197199 last_active_epoch = current_epoch;
198200 };
199-
200- *table::borrow (&self.total_staked, last_active_epoch)
201+
202+ let pending = get_pending (self);
203+ *table::borrow (&self.total_staked, last_active_epoch) + pending
201204 }
202205
203206 public fun get_total_rewards (self: &NativePool ): u64 {
@@ -235,7 +238,7 @@ module liquid_staking::native_pool {
235238 // we can allow to stake less than 1 SUI
236239 public entry fun change_min_stake (self: &mut NativePool , _owner_cap: &OwnerCap , value: u64 ) {
237240 assert_version (self);
238- assert ! (value > 0 , E_LIMIT_TOO_LOW );
241+ assert ! (value > 1000 , E_LIMIT_TOO_LOW );
239242
240243 event::emit (MinStakeChangedEvent {
241244 prev_value: self.min_stake,
@@ -474,6 +477,11 @@ module liquid_staking::native_pool {
474477 fun stake_pool (self: &mut NativePool , wrapper: &mut SuiSystemState , ctx: &mut TxContext ) {
475478 let pending_value = coin::value (&self.pending);
476479
480+ let tickets_supply = unstake_ticket::get_total_supply (&self.ticket_metadata);
481+ if (pending_value < tickets_supply) {
482+ return
483+ };
484+ pending_value = pending_value - tickets_supply;
477485 if (pending_value < ONE_SUI ) {
478486 return
479487 };
@@ -581,10 +589,12 @@ module liquid_staking::native_pool {
581589 ctx: &mut TxContext
582590 ): Coin <SUI > {
583591
592+ assert ! (vector ::length (&validators) > 0 , E_NOTHING_TO_UNSTAKE );
584593 let i = vector ::length (&validators) - 1 ;
585594
586- let total_removed_balance = balance::zero <SUI >();
587- let total_removed_value = 0 ;
595+ let total_removed_value = coin::value (&self.pending);
596+ let total_removed_balance = coin::into_balance (coin::split (&mut self.pending, total_removed_value, ctx));
597+
588598 let collectable_reward = 0 ;
589599
590600 while (total_removed_value < amount_to_unstake) {
@@ -624,6 +634,7 @@ module liquid_staking::native_pool {
624634 };
625635
626636 // extract our fees
637+ assert ! (balance::value (&total_removed_balance) >= fee + collectable_reward, E_NOT_ENOUGH_BALANCE );
627638 let fee_balance = balance::split (&mut total_removed_balance, fee + collectable_reward);
628639 coin::join (&mut self.collectable_fee, coin::from_balance (fee_balance, ctx));
629640
@@ -651,6 +662,9 @@ module liquid_staking::native_pool {
651662
652663 // unstake validators with zero priority and stake to top validator
653664 public entry fun rebalance (self: &mut NativePool , wrapper: &mut SuiSystemState , ctx: &mut TxContext ) {
665+ assert_version (self);
666+ when_not_paused (self);
667+
654668 // calculate total stake of validators
655669 let validators = validator_set::get_bad_validators (&self.validator_set);
656670 let unstaked_sui = unstake_amount_from_validators (self, wrapper, MAX_UINT_64 , 0 , validators, ctx);
0 commit comments