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

Set fees burn percent token unstake + small refactor #1018

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions dex/pair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,4 @@ pub trait Pair<ContractReader>:
self.lp_token_identifier().set(&token_identifier);
}
}

14 changes: 12 additions & 2 deletions locked-asset/token-unstake/src/fees_handler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
multiversx_sc::imports!();

pub const MAX_PENALTY_PERCENTAGE: u64 = 10_000;
pub const MAX_PENALTY_PERCENTAGE: Percent = 10_000;

use common_structs::Percent;

use crate::{events, tokens_per_user::UnstakePair};

Expand All @@ -22,6 +24,14 @@ pub trait FeesHandlerModule:
+ utils::UtilsModule
+ events::EventsModule
{
#[only_owner]
#[endpoint(setFeesBurnPercent)]
fn set_fees_burn_percent(&self, percent: Percent) {
require!(percent <= MAX_PENALTY_PERCENTAGE, "Invalid percent");

self.fees_burn_percentage().set(percent);
}

#[payable("*")]
#[endpoint(depositUserTokens)]
fn deposit_user_tokens(&self, user: ManagedAddress) {
Expand Down Expand Up @@ -116,7 +126,7 @@ pub trait FeesHandlerModule:

#[view(getFeesBurnPercentage)]
#[storage_mapper("feesBurnPercentage")]
fn fees_burn_percentage(&self) -> SingleValueMapper<u64>;
fn fees_burn_percentage(&self) -> SingleValueMapper<Percent>;

#[view(getFeesCollectorAddress)]
#[storage_mapper("feesCollectorAddress")]
Expand Down
18 changes: 7 additions & 11 deletions locked-asset/token-unstake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod fees_handler;
pub mod tokens_per_user;
pub mod unbond_tokens;

use crate::fees_handler::MAX_PENALTY_PERCENTAGE;
use common_structs::{Epoch, Percent};

#[multiversx_sc::contract]
pub trait TokenUnstakeModule:
Expand All @@ -24,22 +24,18 @@ pub trait TokenUnstakeModule:
#[init]
fn init(
&self,
unbond_epochs: u64,
unbond_epochs: Epoch,
energy_factory_address: ManagedAddress,
fees_burn_percentage: u64,
fees_burn_percentage: Percent,
fees_collector_address: ManagedAddress,
) {
self.require_sc_address(&energy_factory_address);
self.require_sc_address(&fees_collector_address);
require!(
fees_burn_percentage <= MAX_PENALTY_PERCENTAGE,
"Invalid percentage"
);

self.set_energy_factory_address(energy_factory_address);
self.set_fees_burn_percent(fees_burn_percentage);

self.unbond_epochs().set(unbond_epochs);
self.energy_factory_address().set(&energy_factory_address);
self.fees_collector_address().set(&fees_collector_address);
self.fees_burn_percentage().set(fees_burn_percentage);
self.fees_collector_address().set(fees_collector_address);
}

#[upgrade]
Expand Down
6 changes: 4 additions & 2 deletions locked-asset/token-unstake/src/tokens_per_user.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use common_structs::Epoch;

multiversx_sc::imports!();
multiversx_sc::derive_imports!();

Expand All @@ -13,7 +15,7 @@ multiversx_sc::derive_imports!();
Debug,
)]
pub struct UnstakePair<M: ManagedTypeApi> {
pub unlock_epoch: u64,
pub unlock_epoch: Epoch,
pub locked_tokens: EsdtTokenPayment<M>,
pub unlocked_tokens: EsdtTokenPayment<M>,
}
Expand All @@ -22,7 +24,7 @@ pub struct UnstakePair<M: ManagedTypeApi> {
pub trait TokensPerUserModule {
#[view(getUnbondEpochs)]
#[storage_mapper("unbondEpochs")]
fn unbond_epochs(&self) -> SingleValueMapper<u64>;
fn unbond_epochs(&self) -> SingleValueMapper<Epoch>;

#[view(getUnlockedTokensForUser)]
#[storage_mapper("unlockedTokensForUser")]
Expand Down
65 changes: 39 additions & 26 deletions locked-asset/token-unstake/src/unbond_tokens.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::events;
use common_structs::PaymentsVec;

use crate::{events, tokens_per_user::UnstakePair};

multiversx_sc::imports!();

Expand All @@ -20,41 +22,24 @@ pub trait UnbondTokensModule:
let mut penalty_tokens = ManagedVec::<Self::Api, _>::new();
let mut processed_count = 0;

self.unlocked_tokens_for_user(&caller)
let new_unlocked_tokens = self
.unlocked_tokens_for_user(&caller)
.update(|user_entries| {
while !user_entries.is_empty() && processed_count < MAX_CLAIM_UNLOCKED_TOKENS {
let entry = user_entries.get(0);
if current_epoch < entry.unlock_epoch {
break;
}

let locked_tokens = entry.locked_tokens;
let unlocked_tokens = entry.unlocked_tokens;

// we only burn the tokens that are not unlocked
// the rest are sent back as penalty
let locked_tokens_burn_amount = unlocked_tokens.amount.clone();
self.send().esdt_local_burn(
&locked_tokens.token_identifier,
locked_tokens.token_nonce,
&locked_tokens_burn_amount,
);

let penalty_amount = &locked_tokens.amount - &unlocked_tokens.amount;
if penalty_amount > 0 {
let penalty = EsdtTokenPayment::new(
locked_tokens.token_identifier,
locked_tokens.token_nonce,
penalty_amount,
);
penalty_tokens.push(penalty);
}
self.handle_single_unbond_entry(&entry, &mut penalty_tokens);

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

processed_count += 1;
}

(*user_entries).clone()
});

require!(!output_payments.is_empty(), "Nothing to unbond");
Expand All @@ -64,10 +49,38 @@ pub trait UnbondTokensModule:
}

self.send().direct_multi(&caller, &output_payments);

let new_unlocked_tokens = self.unlocked_tokens_for_user(&caller).get();
self.emit_unlocked_tokens_event(&caller, new_unlocked_tokens);

output_payments.into()
}

fn handle_single_unbond_entry(
&self,
entry: &UnstakePair<Self::Api>,
penalty_tokens: &mut PaymentsVec<Self::Api>,
) {
let locked_tokens = &entry.locked_tokens;
let unlocked_tokens = &entry.unlocked_tokens;

// we only burn the tokens that are not unlocked
// the rest are sent back as penalty
let locked_tokens_burn_amount = &unlocked_tokens.amount;
self.send().esdt_local_burn(
&locked_tokens.token_identifier,
locked_tokens.token_nonce,
locked_tokens_burn_amount,
);

let penalty_amount = &locked_tokens.amount - &unlocked_tokens.amount;
if penalty_amount == 0 {
return;
}

let penalty = EsdtTokenPayment::new(
locked_tokens.token_identifier.clone(),
locked_tokens.token_nonce,
penalty_amount,
);
penalty_tokens.push(penalty);
}
}
5 changes: 3 additions & 2 deletions locked-asset/token-unstake/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

// Init: 1
// Upgrade: 1
// Endpoints: 11
// Endpoints: 12
// Async Callback (empty): 1
// Total number of exported functions: 14
// Total number of exported functions: 15

#![no_std]

Expand All @@ -24,6 +24,7 @@ multiversx_sc_wasm_adapter::endpoints! {
getUnlockedTokensForUser => unlocked_tokens_for_user
claimUnlockedTokens => claim_unlocked_tokens
cancelUnbond => cancel_unbond
setFeesBurnPercent => set_fees_burn_percent
depositUserTokens => deposit_user_tokens
depositFees => deposit_fees
setFeesBurnPercentage => set_fees_burn_percentage
Expand Down