From 5370b9d12eb8bde751a6b9a671f78c53ced4de86 Mon Sep 17 00:00:00 2001 From: Catalin Neagu Date: Thu, 15 Apr 2021 10:22:45 +0300 Subject: [PATCH 1/2] Additional checks for swap in case of admin mistakes --- elrond_dex_pair/src/lib.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/elrond_dex_pair/src/lib.rs b/elrond_dex_pair/src/lib.rs index 2de1831fe..e74748271 100644 --- a/elrond_dex_pair/src/lib.rs +++ b/elrond_dex_pair/src/lib.rs @@ -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(); @@ -378,6 +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(); + 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 token_out = if token_in == first_token_id { second_token_id.clone() } else if token_in == second_token_id { @@ -722,6 +733,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, From ea79bec17843acee596f6a14d7b5ffbe8653ef76 Mon Sep 17 00:00:00 2001 From: Catalin Neagu Date: Thu, 15 Apr 2021 10:29:46 +0300 Subject: [PATCH 2/2] Fix last commit --- elrond_dex_pair/src/lib.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/elrond_dex_pair/src/lib.rs b/elrond_dex_pair/src/lib.rs index e74748271..15131fc5d 100644 --- a/elrond_dex_pair/src/lib.rs +++ b/elrond_dex_pair/src/lib.rs @@ -389,13 +389,6 @@ pub trait Pair { "Invalid token out" ); - 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"); - }; let old_k = self.liquidity_pool().calculate_k_for_reserves(); let amount_out = self.liquidity_pool().swap_safe_no_fee(