From df2c9fc405d3035a9148bdc3e68ebe35dd89f946 Mon Sep 17 00:00:00 2001 From: Sorin Petreasca Date: Wed, 2 Oct 2024 17:31:11 +0300 Subject: [PATCH] Router multiPairSwap event --- dex/router/src/events.rs | 49 +++++++++++++++++++++++++++++++ dex/router/src/multi_pair_swap.rs | 7 +++-- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/dex/router/src/events.rs b/dex/router/src/events.rs index 8ee097a5e..6117b1867 100644 --- a/dex/router/src/events.rs +++ b/dex/router/src/events.rs @@ -22,6 +22,14 @@ pub struct UserPairSwapEnabledEvent { pair_address: ManagedAddress, } +#[derive(TypeAbi, TopEncode)] +pub struct MultiPairSwapEvent { + caller: ManagedAddress, + token_in: TokenIdentifier, + amount_in: BigUint, + payments_out: ManagedVec>, +} + #[multiversx_sc::module] pub trait EventsModule { fn emit_create_pair_event( @@ -75,6 +83,36 @@ pub trait EventsModule { ) } + fn emit_multi_pair_swap_event( + &self, + caller: ManagedAddress, + token_in: TokenIdentifier, + amount_in: BigUint, + payments_out: ManagedVec, + ) { + if payments_out.len() == 0 { + return; + } + + let epoch = self.blockchain().get_block_epoch(); + let block_nonce = self.blockchain().get_block_nonce(); + let last_payment_index = payments_out.len() - 1; + let token_out = payments_out.get(last_payment_index).token_identifier; + self.multi_pair_swap_event( + caller.clone(), + token_in.clone(), + token_out, + epoch, + block_nonce, + MultiPairSwapEvent { + caller, + token_in, + amount_in, + payments_out, + }, + ) + } + #[event("create_pair")] fn create_pair_event( self, @@ -94,4 +132,15 @@ pub trait EventsModule { #[indexed] epoch: u64, swap_enabled_event: UserPairSwapEnabledEvent, ); + + #[event("multiPairSwap")] + fn multi_pair_swap_event( + &self, + #[indexed] caller: ManagedAddress, + #[indexed] token_in: TokenIdentifier, + #[indexed] token_out: TokenIdentifier, + #[indexed] epoch: u64, + #[indexed] block_nonce: u64, + multi_pair_swap_event: MultiPairSwapEvent, + ); } diff --git a/dex/router/src/multi_pair_swap.rs b/dex/router/src/multi_pair_swap.rs index 1e47378f9..bb9bf703f 100644 --- a/dex/router/src/multi_pair_swap.rs +++ b/dex/router/src/multi_pair_swap.rs @@ -2,7 +2,7 @@ multiversx_sc::imports!(); multiversx_sc::derive_imports!(); use super::factory; -use crate::config; +use crate::{config, events}; use pair::{pair_actions::swap::ProxyTrait as _, read_pair_storage}; type SwapOperationType = @@ -17,6 +17,7 @@ pub trait MultiPairSwap: + read_pair_storage::ReadPairStorageModule + factory::FactoryModule + token_send::TokenSendModule + + events::EventsModule { #[payable("*")] #[endpoint(multiPairSwap)] @@ -39,7 +40,7 @@ pub trait MultiPairSwap: let caller = self.blockchain().get_caller(); let mut payments = ManagedVec::new(); - let mut last_payment = EsdtTokenPayment::new(token_id, nonce, amount); + let mut last_payment = EsdtTokenPayment::new(token_id.clone(), nonce, amount.clone()); for entry in swap_operations.into_iter() { let (pair_address, function, token_wanted, amount_wanted) = entry.into_tuple(); @@ -72,6 +73,8 @@ pub trait MultiPairSwap: payments.push(last_payment); self.send().direct_multi(&caller, &payments); + self.emit_multi_pair_swap_event(caller, token_id, amount, payments.clone()); + payments }