Skip to content

Commit

Permalink
Router multiPairSwap event
Browse files Browse the repository at this point in the history
  • Loading branch information
psorinionut committed Oct 2, 2024
1 parent 004141e commit df2c9fc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
49 changes: 49 additions & 0 deletions dex/router/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ pub struct UserPairSwapEnabledEvent<M: ManagedTypeApi> {
pair_address: ManagedAddress<M>,
}

#[derive(TypeAbi, TopEncode)]
pub struct MultiPairSwapEvent<M: ManagedTypeApi> {
caller: ManagedAddress<M>,
token_in: TokenIdentifier<M>,
amount_in: BigUint<M>,
payments_out: ManagedVec<M, EsdtTokenPayment<M>>,
}

#[multiversx_sc::module]
pub trait EventsModule {
fn emit_create_pair_event(
Expand Down Expand Up @@ -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<EsdtTokenPayment>,
) {
if payments_out.len() == 0 {

Check warning on line 93 in dex/router/src/events.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] dex/router/src/events.rs#L93

warning: length comparison to zero --> dex/router/src/events.rs:93:12 | 93 | if payments_out.len() == 0 { | ^^^^^^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `payments_out.is_empty()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#len_zero = note: `#[warn(clippy::len_zero)]` on by default
Raw output
dex/router/src/events.rs:93:12:w:warning: length comparison to zero
  --> dex/router/src/events.rs:93:12
   |
93 |         if payments_out.len() == 0 {
   |            ^^^^^^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `payments_out.is_empty()`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#len_zero
   = note: `#[warn(clippy::len_zero)]` on by default


__END__

Check warning on line 93 in dex/router/src/events.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] dex/router/src/events.rs#L93

warning: length comparison to zero --> dex/router/src/events.rs:93:12 | 93 | if payments_out.len() == 0 { | ^^^^^^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `payments_out.is_empty()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#len_zero = note: `#[warn(clippy::len_zero)]` on by default
Raw output
dex/router/src/events.rs:93:12:w:warning: length comparison to zero
  --> dex/router/src/events.rs:93:12
   |
93 |         if payments_out.len() == 0 {
   |            ^^^^^^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `payments_out.is_empty()`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#len_zero
   = note: `#[warn(clippy::len_zero)]` on by default


__END__
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,
Expand All @@ -94,4 +132,15 @@ pub trait EventsModule {
#[indexed] epoch: u64,
swap_enabled_event: UserPairSwapEnabledEvent<Self::Api>,
);

#[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<Self::Api>,
);
}
7 changes: 5 additions & 2 deletions dex/router/src/multi_pair_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<M> =
Expand All @@ -17,6 +17,7 @@ pub trait MultiPairSwap:
+ read_pair_storage::ReadPairStorageModule
+ factory::FactoryModule
+ token_send::TokenSendModule
+ events::EventsModule
{
#[payable("*")]
#[endpoint(multiPairSwap)]
Expand All @@ -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();
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit df2c9fc

Please sign in to comment.