Skip to content

Commit

Permalink
Merge remote-tracking branch ''
Browse files Browse the repository at this point in the history
# Conflicts:
#	Cargo.lock
#	pallets/route-executor/Cargo.toml
#	pallets/route-executor/src/lib.rs
#	runtime/hydradx/Cargo.toml
#	runtime/hydradx/src/lib.rs
  • Loading branch information
mrq1911 committed Mar 11, 2024
2 parents 9d70968 + 50bccc0 commit 45e4a5f
Show file tree
Hide file tree
Showing 7 changed files with 112 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
6 changes: 6 additions & 0 deletions pallets/route-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ pub mod pallet {
InsufficientAssetNotSupported,
///The route execution failed in the underlying AMM
InvalidRouteExecution,
/// Trading same assets is not allowed.
NotAllowed,
}

/// Storing routes for asset pairs
Expand Down Expand Up @@ -184,6 +186,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 @@ -273,6 +278,7 @@ pub mod pallet {
) -> 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
2 changes: 1 addition & 1 deletion pallets/stableswap/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = 'pallet-stableswap'
version = '3.4.4'
version = '3.5.0'
description = 'AMM for correlated assets'
authors = ['GalacticCouncil']
edition = '2021'
Expand Down
4 changes: 4 additions & 0 deletions pallets/stableswap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,8 @@ pub mod pallet {
) -> DispatchResult {
let who = ensure_signed(origin)?;

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

ensure!(
Self::is_asset_allowed(pool_id, asset_in, Tradability::SELL)
&& Self::is_asset_allowed(pool_id, asset_out, Tradability::BUY),
Expand Down Expand Up @@ -794,6 +796,8 @@ pub mod pallet {
) -> DispatchResult {
let who = ensure_signed(origin)?;

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

ensure!(
Self::is_asset_allowed(pool_id, asset_in, Tradability::SELL)
&& Self::is_asset_allowed(pool_id, asset_out, Tradability::BUY),
Expand Down
86 changes: 86 additions & 0 deletions pallets/stableswap/src/tests/trades.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,3 +666,89 @@ fn buy_should_work_when_assets_have_different_decimals() {
assert_balance!(pool_account, asset_b, to_precision!(70, dec_b));
});
}

#[test]
fn sell_should_fail_when_trading_same_assets() {
let asset_a: AssetId = 1;
let asset_b: AssetId = 2;
ExtBuilder::default()
.with_endowed_accounts(vec![(BOB, 1, 200 * ONE), (ALICE, 1, 200 * ONE), (ALICE, 2, 200 * ONE)])
.with_registered_asset("one".as_bytes().to_vec(), 1, 12)
.with_registered_asset("two".as_bytes().to_vec(), 2, 12)
.with_pool(
ALICE,
PoolInfo::<AssetId, u64> {
assets: vec![asset_a, asset_b].try_into().unwrap(),
initial_amplification: NonZeroU16::new(100).unwrap(),
final_amplification: NonZeroU16::new(100).unwrap(),
initial_block: 0,
final_block: 0,
fee: Permill::from_percent(0),
},
InitialLiquidity {
account: ALICE,
assets: vec![
AssetAmount::new(asset_a, 100 * ONE),
AssetAmount::new(asset_b, 100 * ONE),
],
},
)
.build()
.execute_with(|| {
let pool_id = get_pool_id_at(0);
assert_noop!(
Stableswap::sell(
RuntimeOrigin::signed(BOB),
pool_id,
asset_a,
asset_a,
30 * ONE,
25 * ONE,
),
Error::<Test>::NotAllowed
);
});
}

#[test]
fn buy_should_fail_when_trading_same_assets() {
let asset_a: AssetId = 1;
let asset_b: AssetId = 2;
ExtBuilder::default()
.with_endowed_accounts(vec![(BOB, 1, 200 * ONE), (ALICE, 1, 200 * ONE), (ALICE, 2, 200 * ONE)])
.with_registered_asset("one".as_bytes().to_vec(), 1, 12)
.with_registered_asset("two".as_bytes().to_vec(), 2, 12)
.with_pool(
ALICE,
PoolInfo::<AssetId, u64> {
assets: vec![asset_a, asset_b].try_into().unwrap(),
initial_amplification: NonZeroU16::new(100).unwrap(),
final_amplification: NonZeroU16::new(100).unwrap(),
initial_block: 0,
final_block: 0,
fee: Permill::from_percent(0),
},
InitialLiquidity {
account: ALICE,
assets: vec![
AssetAmount::new(asset_a, 100 * ONE),
AssetAmount::new(asset_b, 100 * ONE),
],
},
)
.build()
.execute_with(|| {
let pool_id = get_pool_id_at(0);
assert_noop!(
Stableswap::buy(
RuntimeOrigin::signed(BOB),
pool_id,
asset_a,
asset_a,
30 * ONE,
25 * ONE,
),
Error::<Test>::NotAllowed
);
});
}

0 comments on commit 45e4a5f

Please sign in to comment.