Skip to content

Commit

Permalink
ensure ourter and math
Browse files Browse the repository at this point in the history
  • Loading branch information
enthusiastmartin committed Mar 4, 2024
1 parent 4d96dc4 commit 50bccc0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion math/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
Expand Down
12 changes: 12 additions & 0 deletions math/src/stableswap/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub fn calculate_out_given_in<const D: u8, const Y: u8>(
amount_in: Balance,
amplification: Balance,
) -> Option<Balance> {
if idx_in == idx_out {
return None;
}
if idx_in >= initial_reserves.len() || idx_out >= initial_reserves.len() {
return None;
}
Expand Down Expand Up @@ -58,6 +61,9 @@ pub fn calculate_in_given_out<const D: u8, const Y: u8>(
amount_out: Balance,
amplification: Balance,
) -> Option<Balance> {
if idx_in == idx_out {
return None;
}
if idx_in >= initial_reserves.len() || idx_out >= initial_reserves.len() {
return None;
}
Expand Down Expand Up @@ -88,6 +94,9 @@ pub fn calculate_out_given_in_with_fee<const D: u8, const Y: u8>(
amplification: Balance,
fee: Permill,
) -> Option<(Balance, Balance)> {
if idx_in == idx_out {
return None;
}
let amount_out = calculate_out_given_in::<D, Y>(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)?;
Expand All @@ -103,6 +112,9 @@ pub fn calculate_in_given_out_with_fee<const D: u8, const Y: u8>(
amplification: Balance,
fee: Permill,
) -> Option<(Balance, Balance)> {
if idx_in == idx_out {
return None;
}
let amount_in = calculate_in_given_out::<D, Y>(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)?;
Expand Down
2 changes: 1 addition & 1 deletion pallets/route-executor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
6 changes: 6 additions & 0 deletions pallets/route-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -182,6 +184,9 @@ pub mod pallet {
route: Vec<Trade<T::AssetId>>,
) -> DispatchResult {
let who = ensure_signed(origin.clone())?;

ensure!(asset_in != asset_out, Error::<T>::NotAllowed);

Self::ensure_route_size(route.len())?;

let asset_pair = AssetPair::new(asset_in, asset_out);
Expand Down Expand Up @@ -251,6 +256,7 @@ pub mod pallet {
max_amount_in: T::Balance,
route: Vec<Trade<T::AssetId>>,
) -> DispatchResult {
ensure!(asset_in != asset_out, Error::<T>::NotAllowed);
Self::ensure_route_size(route.len())?;

let asset_pair = AssetPair::new(asset_in, asset_out);
Expand Down

0 comments on commit 50bccc0

Please sign in to comment.