Skip to content

Commit

Permalink
Merge pull request #731 from multiversx/lkmex-transfer-events
Browse files Browse the repository at this point in the history
lkmex transfer events
  • Loading branch information
psorinionut authored Apr 6, 2023
2 parents bd0806a + dfa8840 commit 30817f6
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
65 changes: 65 additions & 0 deletions locked-asset/lkmex-transfer/src/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

use crate::LockedFunds;

#[derive(TypeAbi, TopEncode)]
pub struct LkmexTransferEvent<M: ManagedTypeApi> {
sender: ManagedAddress<M>,
receiver: ManagedAddress<M>,
locked_funds: LockedFunds<M>,
}

#[multiversx_sc::module]
pub trait LkmexTransferEventsModule {
fn emit_withdraw_event(
self,
sender: ManagedAddress,
receiver: ManagedAddress,
locked_funds: LockedFunds<Self::Api>,
) {
let event_data = LkmexTransferEvent {
sender,
receiver,
locked_funds,
};
self.withdraw_event(event_data);
}

fn emit_cancel_transfer_event(
&self,
sender: ManagedAddress,
receiver: ManagedAddress,
locked_funds: LockedFunds<Self::Api>,
) {
let event_data = LkmexTransferEvent {
sender,
receiver,
locked_funds,
};
self.cancel_transfer_event(event_data);
}

fn emit_lock_funds_event(
&self,
sender: ManagedAddress,
receiver: ManagedAddress,
locked_funds: LockedFunds<Self::Api>,
) {
let event_data = LkmexTransferEvent {
sender,
receiver,
locked_funds,
};
self.lock_funds_event(event_data);
}

#[event("withdraw_event")]
fn withdraw_event(&self, event_data: LkmexTransferEvent<Self::Api>);

#[event("cancel_transfer_event")]
fn cancel_transfer_event(&self, event_data: LkmexTransferEvent<Self::Api>);

#[event("lock_funds_event")]
fn lock_funds_event(&self, event_data: LkmexTransferEvent<Self::Api>);
}
20 changes: 17 additions & 3 deletions locked-asset/lkmex-transfer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ multiversx_sc::derive_imports!();

pub mod constants;
pub mod energy_transfer;
pub mod events;

use common_structs::{Epoch, PaymentsVec};
use permissions_module::Permissions;
Expand All @@ -29,6 +30,7 @@ pub struct ScheduledTransfer<M: ManagedTypeApi> {
#[multiversx_sc::contract]
pub trait LkmexTransfer:
energy_transfer::EnergyTransferModule
+ events::LkmexTransferEventsModule
+ energy_query::EnergyQueryModule
+ utils::UtilsModule
+ legacy_token_decode_module::LegacyTokenDecodeModule
Expand Down Expand Up @@ -67,6 +69,12 @@ pub trait LkmexTransfer:

let current_epoch = self.blockchain().get_block_epoch();
receiver_last_transfer_mapper.set(current_epoch);

let locked_funds = LockedFunds {
funds,
locked_epoch: current_epoch,
};
self.emit_withdraw_event(sender, receiver, locked_funds);
}

#[endpoint(cancelTransfer)]
Expand All @@ -82,6 +90,8 @@ pub trait LkmexTransfer:

self.add_energy_to_destination(sender.clone(), &locked_funds.funds);
self.send().direct_multi(&sender, &locked_funds.funds);

self.emit_cancel_transfer_event(sender, receiver, locked_funds);
}

fn get_unlocked_funds(
Expand Down Expand Up @@ -124,12 +134,16 @@ pub trait LkmexTransfer:
self.deduct_energy_from_sender(sender.clone(), &payments);

let current_epoch = self.blockchain().get_block_epoch();
self.locked_funds(&receiver, &sender).set(LockedFunds {
let locked_funds = LockedFunds {
funds: payments,
locked_epoch: current_epoch,
});
};
self.locked_funds(&receiver, &sender)
.set(locked_funds.clone());
sender_last_transfer_mapper.set(current_epoch);
self.all_senders(&receiver).insert(sender);
self.all_senders(&receiver).insert(sender.clone());

self.emit_lock_funds_event(sender, receiver, locked_funds);
}

fn check_address_on_cooldown(&self, last_transfer_mapper: &SingleValueMapper<Epoch>) {
Expand Down

0 comments on commit 30817f6

Please sign in to comment.