diff --git a/Cargo.lock b/Cargo.lock index 023248904..232b7e828 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4461,7 +4461,7 @@ dependencies = [ [[package]] name = "hydra-dx-math" -version = "8.0.0" +version = "8.0.1" dependencies = [ "approx", "criterion", @@ -8318,7 +8318,7 @@ dependencies = [ [[package]] name = "pallet-route-executor" -version = "2.0.2" +version = "2.0.3" dependencies = [ "frame-benchmarking", "frame-support", diff --git a/math/Cargo.toml b/math/Cargo.toml index fe2bda60e..92c6467ca 100644 --- a/math/Cargo.toml +++ b/math/Cargo.toml @@ -6,7 +6,7 @@ license = 'Apache-2.0' name = "hydra-dx-math" description = "A collection of utilities to make performing liquidity pool calculations more convenient." repository = 'https://github.com/galacticcouncil/hydradx-math' -version = "8.0.0" +version = "8.0.1" [dependencies] primitive-types = {default-features = false, version = '0.12.0'} diff --git a/math/src/stableswap/math.rs b/math/src/stableswap/math.rs index ba001673f..0dc33b3c5 100644 --- a/math/src/stableswap/math.rs +++ b/math/src/stableswap/math.rs @@ -27,6 +27,9 @@ pub fn calculate_out_given_in( amount_in: Balance, amplification: Balance, ) -> Option { + if idx_in == idx_out { + return None; + } if idx_in >= initial_reserves.len() || idx_out >= initial_reserves.len() { return None; } @@ -58,6 +61,9 @@ pub fn calculate_in_given_out( amount_out: Balance, amplification: Balance, ) -> Option { + if idx_in == idx_out { + return None; + } if idx_in >= initial_reserves.len() || idx_out >= initial_reserves.len() { return None; } @@ -88,6 +94,9 @@ pub fn calculate_out_given_in_with_fee( amplification: Balance, fee: Permill, ) -> Option<(Balance, Balance)> { + if idx_in == idx_out { + return None; + } let amount_out = calculate_out_given_in::(initial_reserves, idx_in, idx_out, amount_in, amplification)?; let fee_amount = calculate_fee_amount(amount_out, fee, Rounding::Down); let amount_out = amount_out.checked_sub(fee_amount)?; @@ -103,6 +112,9 @@ pub fn calculate_in_given_out_with_fee( amplification: Balance, fee: Permill, ) -> Option<(Balance, Balance)> { + if idx_in == idx_out { + return None; + } let amount_in = calculate_in_given_out::(initial_reserves, idx_in, idx_out, amount_out, amplification)?; let fee_amount = calculate_fee_amount(amount_in, fee, Rounding::Up); let amount_in = amount_in.checked_add(fee_amount)?; diff --git a/pallets/route-executor/Cargo.toml b/pallets/route-executor/Cargo.toml index 986f570db..5abde9b34 100644 --- a/pallets/route-executor/Cargo.toml +++ b/pallets/route-executor/Cargo.toml @@ -1,6 +1,6 @@ [package] name = 'pallet-route-executor' -version = '2.0.2' +version = '2.0.3' description = 'A pallet to execute a route containing a sequence of trades' authors = ['GalacticCouncil'] edition = '2021' diff --git a/pallets/route-executor/src/lib.rs b/pallets/route-executor/src/lib.rs index cc6fdacc9..a25f7fd49 100644 --- a/pallets/route-executor/src/lib.rs +++ b/pallets/route-executor/src/lib.rs @@ -143,6 +143,8 @@ pub mod pallet { RouteUpdateIsNotSuccessful, ///Insufficient asset is not supported for on chain routing InsufficientAssetNotSupported, + /// Trading same assets is not allowed. + NotAllowed, } /// Storing routes for asset pairs @@ -182,6 +184,9 @@ pub mod pallet { route: Vec>, ) -> DispatchResult { let who = ensure_signed(origin.clone())?; + + ensure!(asset_in != asset_out, Error::::NotAllowed); + Self::ensure_route_size(route.len())?; let asset_pair = AssetPair::new(asset_in, asset_out); @@ -251,6 +256,7 @@ pub mod pallet { max_amount_in: T::Balance, route: Vec>, ) -> DispatchResult { + ensure!(asset_in != asset_out, Error::::NotAllowed); Self::ensure_route_size(route.len())?; let asset_pair = AssetPair::new(asset_in, asset_out);