diff --git a/Cargo.lock b/Cargo.lock index 238a4275e..733e286b7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5366,7 +5366,7 @@ dependencies = [ [[package]] name = "hydra-dx-math" -version = "10.4.2" +version = "10.5.0" dependencies = [ "approx", "criterion", @@ -9725,7 +9725,7 @@ dependencies = [ [[package]] name = "pallet-hsm" -version = "1.4.0" +version = "1.4.1" dependencies = [ "ethabi", "evm", @@ -10862,7 +10862,7 @@ dependencies = [ [[package]] name = "pallet-stableswap" -version = "6.0.0" +version = "5.5.0" dependencies = [ "bitflags 1.3.2", "frame-benchmarking", diff --git a/integration-tests/src/hsm.rs b/integration-tests/src/hsm.rs index b05cd27a2..a0cb346be 100644 --- a/integration-tests/src/hsm.rs +++ b/integration-tests/src/hsm.rs @@ -31,9 +31,9 @@ use primitives::{AssetId, Balance}; use sp_core::{RuntimeDebug, H256, U256}; use sp_runtime::traits::CheckedConversion; use sp_runtime::traits::One; -use sp_runtime::BoundedVec; use sp_runtime::Perbill; use sp_runtime::Permill; +use sp_runtime::{BoundedVec, FixedPointNumber}; use std::sync::Arc; use xcm_emulator::{Network, TestExt}; @@ -2077,7 +2077,9 @@ fn arb_should_repeg_continuously_when_less_hollar_in_pool() { 1_000_000_000_000_000_000, Some(state.fee), &state.pegs, - ); + ) + .unwrap() + .reciprocal(); let opp = pallet_hsm::Pallet::::find_arbitrage_opportunity(2).expect("some arb"); assert_ok!(pallet_hsm::Pallet::::simulate_arbitrage( @@ -2107,7 +2109,9 @@ fn arb_should_repeg_continuously_when_less_hollar_in_pool() { 1_000_000_000_000_000_000, Some(state.fee), &state.pegs, - ); + ) + .unwrap() + .reciprocal(); assert!(initial_spot_price < final_spot_price); }); } @@ -2230,7 +2234,9 @@ fn arb_should_repeg_continuously_when_less_hollar_in_pool_and_collateral_has_12_ 1_000_000_000_000_000_000, Some(state.fee), &state.pegs, - ); + ) + .unwrap() + .reciprocal(); let opp = pallet_hsm::Pallet::::find_arbitrage_opportunity(collateral_asset_id) .expect("some arb"); @@ -2262,7 +2268,9 @@ fn arb_should_repeg_continuously_when_less_hollar_in_pool_and_collateral_has_12_ 1_000_000_000_000_000_000, Some(state.fee), &state.pegs, - ); + ) + .unwrap() + .reciprocal(); assert!(initial_spot_price < final_spot_price); }); } @@ -2385,7 +2393,9 @@ fn arb_should_repeg_continuously_when_more_hollar_in_pool() { 1_000_000_000_000_000_000, Some(state.fee), &state.pegs, - ); + ) + .unwrap() + .reciprocal(); let mut last_spot_price = initial_spot_price; @@ -2408,7 +2418,9 @@ fn arb_should_repeg_continuously_when_more_hollar_in_pool() { 1_000_000_000_000_000_000, Some(state.fee), &state.pegs, - ); + ) + .unwrap() + .reciprocal(); println!("Block: {:?}: spot: {:?}", block_idx, spot_price); assert!(spot_price < last_spot_price); last_spot_price = spot_price; @@ -2432,7 +2444,9 @@ fn arb_should_repeg_continuously_when_more_hollar_in_pool() { 1_000_000_000_000_000_000, Some(state.fee), &state.pegs, - ); + ) + .unwrap() + .reciprocal(); assert!(initial_spot_price > final_spot_price); }); } @@ -2568,7 +2582,9 @@ fn arb_should_repeg_continuously_when_more_hollar_in_pool_and_collateral_has_12_ 1_000_000_000_000_000_000, Some(state.fee), &state.pegs, - ); + ) + .unwrap() + .reciprocal(); let mut last_spot_price = initial_spot_price; @@ -2595,7 +2611,9 @@ fn arb_should_repeg_continuously_when_more_hollar_in_pool_and_collateral_has_12_ 1_000_000_000_000_000_000, Some(state.fee), &state.pegs, - ); + ) + .unwrap() + .reciprocal(); println!("Block: {:?}: spot: {:?}", block_idx, spot_price); assert!(spot_price < last_spot_price); last_spot_price = spot_price; @@ -2619,7 +2637,9 @@ fn arb_should_repeg_continuously_when_more_hollar_in_pool_and_collateral_has_12_ 1_000_000_000_000_000_000, Some(state.fee), &state.pegs, - ); + ) + .unwrap() + .reciprocal(); assert!(initial_spot_price > final_spot_price); }); } diff --git a/math/Cargo.toml b/math/Cargo.toml index f5f835aae..d92a6f5a8 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 = "10.4.2" +version = "10.5.0" [dependencies] primitive-types = { workspace = true } diff --git a/math/src/stableswap/math.rs b/math/src/stableswap/math.rs index 37cb54ab6..2b8ede4bd 100644 --- a/math/src/stableswap/math.rs +++ b/math/src/stableswap/math.rs @@ -902,6 +902,7 @@ pub fn calculate_spot_price( fee, pegs, ) + .map(|p| p.reciprocal())? } (SHARE_ASSET, STABLE_ASSET) => { let asset_out_idx = asset_reserves.iter().position(|r| r.0 == asset_out)?; @@ -915,7 +916,7 @@ pub fn calculate_spot_price( pegs, )?; - FixedU128::checked_from_rational(shares, min_trade_amount) + FixedU128::checked_from_rational(min_trade_amount, shares) } (STABLE_ASSET, SHARE_ASSET) => { let added_asset = (asset_in, min_trade_amount); @@ -937,7 +938,7 @@ pub fn calculate_spot_price( pegs, )?; - FixedU128::checked_from_rational(min_trade_amount, shares_for_min_trade) + FixedU128::checked_from_rational(shares_for_min_trade, min_trade_amount) } _ => None, } diff --git a/pallets/hsm/Cargo.toml b/pallets/hsm/Cargo.toml index a4d2e9c5f..1c69cdb38 100644 --- a/pallets/hsm/Cargo.toml +++ b/pallets/hsm/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-hsm" -version = "1.4.0" +version = "1.4.1" edition = "2021" description = "Hollar stability module" authors = ["GalacticCouncil"] diff --git a/pallets/hsm/src/lib.rs b/pallets/hsm/src/lib.rs index d17f6103b..9929ebe93 100644 --- a/pallets/hsm/src/lib.rs +++ b/pallets/hsm/src/lib.rs @@ -1526,8 +1526,6 @@ where // just to be safe return None; } - let after_spot = FixedU128::one().div(after_spot); - if after_spot > sell_price { return Some(()); } diff --git a/pallets/hsm/src/tests/arb.rs b/pallets/hsm/src/tests/arb.rs index a91804a47..3a081dc2b 100644 --- a/pallets/hsm/src/tests/arb.rs +++ b/pallets/hsm/src/tests/arb.rs @@ -292,7 +292,6 @@ proptest! { &state.pegs, ).expect("Pool not found"); - let after_spot = FixedU128::one().div(after_spot); assert_eq_approx!(sell_price,after_spot, FixedU128::from_float(0.01), "Price should converge"); } _ => {} diff --git a/pallets/stableswap/Cargo.toml b/pallets/stableswap/Cargo.toml index d5731360d..a37e9cd33 100644 --- a/pallets/stableswap/Cargo.toml +++ b/pallets/stableswap/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pallet-stableswap" -version = "6.0.0" +version = "5.5.0" description = "AMM for correlated assets" authors = ["GalacticCouncil"] edition = "2021" diff --git a/pallets/stableswap/src/trade_execution.rs b/pallets/stableswap/src/trade_execution.rs index 12831c843..8ea27ab2b 100644 --- a/pallets/stableswap/src/trade_execution.rs +++ b/pallets/stableswap/src/trade_execution.rs @@ -6,7 +6,7 @@ use hydradx_traits::router::{ExecutorError, PoolType, TradeExecution}; use hydradx_traits::stableswap::AssetAmount; use orml_traits::MultiCurrency; use sp_core::Get; -use sp_runtime::{ArithmeticError, DispatchError, FixedU128}; +use sp_runtime::{ArithmeticError, DispatchError, FixedPointNumber, FixedU128}; use sp_std::collections::btree_map::BTreeMap; use sp_std::vec; use sp_std::vec::Vec; @@ -322,6 +322,8 @@ where Some(trade_fee), &asset_pegs, ) + .ok_or_else(|| ExecutorError::Error(ArithmeticError::Overflow.into()))? + .reciprocal() .ok_or_else(|| ExecutorError::Error(ArithmeticError::Overflow.into()))?; Ok(spot_price)