Skip to content

Commit

Permalink
Merge pull request #1002 from multiversx/max-limit-on-unbond-locked-t…
Browse files Browse the repository at this point in the history
…okens

Max limit on unbond locked tokens
  • Loading branch information
ovidiuolteanu authored Feb 26, 2025
2 parents 9df990a + 180b1ad commit 2fb3b5a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
rust-toolchain: stable
coverage-args: --ignore-filename-regex='/.cargo/git' --output ./coverage.md
secrets:
token: ${{ secrets.GITHUB_TOKEN }}
token: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ jobs:
uses: multiversx/mx-sc-actions/.github/workflows/[email protected]
with:
image_tag: v7.0.0
attach_to_existing_release: true
attach_to_existing_release: true
8 changes: 7 additions & 1 deletion locked-asset/token-unstake/src/unbond_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::events;

multiversx_sc::imports!();

pub const MAX_CLAIM_UNLOCKED_TOKENS: u64 = 20;

#[multiversx_sc::module]
pub trait UnbondTokensModule:
crate::tokens_per_user::TokensPerUserModule
Expand All @@ -16,9 +18,11 @@ pub trait UnbondTokensModule:
let current_epoch = self.blockchain().get_block_epoch();
let mut output_payments = ManagedVec::new();
let mut penalty_tokens = ManagedVec::<Self::Api, _>::new();
let mut processed_count = 0;

self.unlocked_tokens_for_user(&caller)
.update(|user_entries| {
while !user_entries.is_empty() {
while !user_entries.is_empty() && processed_count < MAX_CLAIM_UNLOCKED_TOKENS {
let entry = user_entries.get(0);
if current_epoch < entry.unlock_epoch {
break;
Expand Down Expand Up @@ -48,6 +52,8 @@ pub trait UnbondTokensModule:

output_payments.push(unlocked_tokens);
user_entries.remove(0);

processed_count += 1;
}
});

Expand Down
41 changes: 40 additions & 1 deletion locked-asset/token-unstake/tests/token_unstake_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use multiversx_sc_scenario::{
use num_bigint::ToBigInt;
use num_traits::cast::ToPrimitive;
use simple_lock::locked_token::LockedTokenAttributes;
use token_unstake::tokens_per_user::{TokensPerUserModule, UnstakePair};
use token_unstake::{
tokens_per_user::{TokensPerUserModule, UnstakePair},
unbond_tokens::MAX_CLAIM_UNLOCKED_TOKENS,
};
use token_unstake_setup::*;

pub struct ResultWrapper<EnergyFactoryBuilder, UnstakeScBuilder>
Expand Down Expand Up @@ -99,6 +102,42 @@ fn cancel_unbond_test() {
assert_eq!(user_energy, expected_energy);
}

#[test]
fn unstake_multiple_position_test() {
let mut setup =
TokenUnstakeSetup::new(energy_factory::contract_obj, token_unstake::contract_obj);
let first_user = setup.first_user.clone();

let _ = setup.lock(
&first_user,
BASE_ASSET_TOKEN_ID,
USER_BALANCE,
LOCK_OPTIONS[2],
);

let number_of_unlocks = MAX_CLAIM_UNLOCKED_TOKENS * 2;
for _ in 0..number_of_unlocks {
let _ = setup.unlock_early(&first_user, 1, USER_BALANCE / number_of_unlocks);
}

// unbond epochs pass
setup.b_mock.set_block_epoch(10 + UNBOND_EPOCHS);
// unbond ok
setup.unbond(&first_user).assert_ok();
let balance_after_half_unlocks = rust_biguint!(USER_BALANCE as u128 / 2 * 2000 / 10000); // 80% fee on half balance
setup.b_mock.check_esdt_balance(
&first_user,
BASE_ASSET_TOKEN_ID,
&balance_after_half_unlocks,
);

setup.unbond(&first_user).assert_ok();
let balance_after_all_unlocks = balance_after_half_unlocks * 2u64;
setup
.b_mock
.check_esdt_balance(&first_user, BASE_ASSET_TOKEN_ID, &balance_after_all_unlocks);
}

fn unbond_test_common<EnergyFactoryBuilder, UnstakeScBuilder>(
energy_factory_builder: EnergyFactoryBuilder,
unstake_sc_builder: UnstakeScBuilder,
Expand Down

0 comments on commit 2fb3b5a

Please sign in to comment.