Skip to content

Commit

Permalink
Merge pull request #969 from multiversx/all-xmex-fees-to-users
Browse files Browse the repository at this point in the history
All xMex fees to users
  • Loading branch information
dorin-iancu authored Nov 13, 2024
2 parents 79c6016 + 9a4f4a8 commit a2f0007
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 43 deletions.
4 changes: 3 additions & 1 deletion locked-asset/token-unstake/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use common_structs::Epoch;

use crate::tokens_per_user::UnstakePair;

multiversx_sc::imports!();
Expand All @@ -24,7 +26,7 @@ pub trait EventsModule {
&self,
#[indexed] caller: &ManagedAddress,
#[indexed] block: u64,
#[indexed] epoch: u64,
#[indexed] epoch: Epoch,
#[indexed] timestamp: u64,
data: ManagedVec<UnstakePair<Self::Api>>,
);
Expand Down
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 @@ -103,7 +113,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
17 changes: 7 additions & 10 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,19 @@ 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_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.energy_factory_address().set(energy_factory_address);
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 @@ -16,39 +18,22 @@ 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();
self.unlocked_tokens_for_user(&caller)
let new_unlocked_tokens = self
.unlocked_tokens_for_user(&caller)
.update(|user_entries| {
while !user_entries.is_empty() {
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);
}

(*user_entries).clone()
});

require!(!output_payments.is_empty(), "Nothing to unbond");
Expand All @@ -58,10 +43,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: 10
// Endpoints: 11
// Async Callback (empty): 1
// Total number of exported functions: 13
// Total number of exported functions: 14

#![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
getFeesBurnPercentage => fees_burn_percentage
Expand Down

0 comments on commit a2f0007

Please sign in to comment.