Skip to content

Commit

Permalink
Merge pull request #31 from ElrondNetwork/feat/inter-pair-swap
Browse files Browse the repository at this point in the history
Additional checks for swap in case of admin mistakes
  • Loading branch information
claudiulataretu authored Apr 15, 2021
2 parents 0f3378f + ea79bec commit ddcf545
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions elrond_dex_pair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ pub trait Pair {
&self,
#[payment_token] token_in: TokenIdentifier,
#[payment] amount_in: BigUint,
token_out: TokenIdentifier,
destination_address: Address,
) -> SCResult<()> {
let caller = self.get_caller();
Expand All @@ -378,13 +379,16 @@ pub trait Pair {

let first_token_id = self.liquidity_pool().first_token_id().get();
let second_token_id = self.liquidity_pool().second_token_id().get();
let token_out = if token_in == first_token_id {
second_token_id.clone()
} else if token_in == second_token_id {
first_token_id.clone()
} else {
return sc_error!("Bad token input");
};
require!(token_in != token_out, "Cannot swap same token");
require!(
token_in == first_token_id || token_in == second_token_id,
"Invalid token in"
);
require!(
token_out == first_token_id || token_out == second_token_id,
"Invalid token out"
);

let old_k = self.liquidity_pool().calculate_k_for_reserves();

let amount_out = self.liquidity_pool().swap_safe_no_fee(
Expand Down Expand Up @@ -722,6 +726,7 @@ pub trait Pair {
);

let mut arg_buffer = ArgBuffer::new();
arg_buffer.push_argument_bytes(requested_token.as_esdt_identifier());
arg_buffer.push_argument_bytes(destination_address.as_bytes());
self.send().direct_esdt_execute(
&pair_address,
Expand Down

0 comments on commit ddcf545

Please sign in to comment.