Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Max limit on unbond locked tokens #1002

Merged
merged 5 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/reproducible-build.yml@v4.2.2
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
42 changes: 41 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,43 @@ 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 mut balance_after_half_unlocks = rust_biguint!(USER_BALANCE as u128 / 2 * 2000 / 10000); // 80% fee on half balance
balance_after_half_unlocks += &rust_biguint!(20u64); // rounding difference
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
Loading