From 9a49b87d6e00e463e32a4c143c9efda41bb252a4 Mon Sep 17 00:00:00 2001 From: Martin Hloska Date: Sat, 14 Dec 2024 20:52:45 +0100 Subject: [PATCH] refactor: stableswap add liquidity --- Cargo.lock | 8 ++-- integration-tests/Cargo.toml | 2 +- integration-tests/src/dca.rs | 8 ++-- integration-tests/src/router.rs | 4 +- .../proptest-regressions/tests/invariants.txt | 1 + pallets/stableswap/Cargo.toml | 2 +- pallets/stableswap/src/benchmarks.rs | 28 ++++++------- pallets/stableswap/src/lib.rs | 4 +- pallets/stableswap/src/tests/add_liquidity.rs | 40 +++++++++---------- pallets/stableswap/src/tests/hooks.rs | 4 +- pallets/stableswap/src/tests/invariants.rs | 30 +++++++------- pallets/stableswap/src/tests/mock.rs | 4 +- pallets/stableswap/src/tests/price.rs | 6 +-- .../stableswap/src/tests/remove_liquidity.rs | 18 ++++----- pallets/stableswap/src/trade_execution.rs | 5 ++- runtime-mock/Cargo.toml | 2 +- runtime-mock/src/stableswap.rs | 3 +- runtime/hydradx/Cargo.toml | 2 +- runtime/hydradx/src/assets.rs | 2 +- runtime/hydradx/src/lib.rs | 2 +- 20 files changed, 89 insertions(+), 86 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0c0d80350..b9d08872d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4918,7 +4918,7 @@ dependencies = [ [[package]] name = "hydradx-runtime" -version = "273.0.0" +version = "274.0.0" dependencies = [ "cumulus-pallet-aura-ext", "cumulus-pallet-parachain-system", @@ -9137,7 +9137,7 @@ dependencies = [ [[package]] name = "pallet-stableswap" -version = "4.0.0" +version = "4.1.0" dependencies = [ "bitflags 1.3.2", "frame-benchmarking", @@ -12237,7 +12237,7 @@ dependencies = [ [[package]] name = "runtime-integration-tests" -version = "1.25.2" +version = "1.25.3" dependencies = [ "cumulus-pallet-aura-ext", "cumulus-pallet-parachain-system", @@ -12361,7 +12361,7 @@ dependencies = [ [[package]] name = "runtime-mock" -version = "1.0.1" +version = "1.0.2" dependencies = [ "frame-remote-externalities", "frame-support", diff --git a/integration-tests/Cargo.toml b/integration-tests/Cargo.toml index ea6b21627..c5faa3e92 100644 --- a/integration-tests/Cargo.toml +++ b/integration-tests/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "runtime-integration-tests" -version = "1.25.2" +version = "1.25.3" description = "Integration tests" authors = ["GalacticCouncil"] edition = "2021" diff --git a/integration-tests/src/dca.rs b/integration-tests/src/dca.rs index 8ed62338b..e8441fdbc 100644 --- a/integration-tests/src/dca.rs +++ b/integration-tests/src/dca.rs @@ -2519,10 +2519,10 @@ mod stableswap { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(ALICE.into()), pool_id, - vec![AssetAmount { + BoundedVec::truncate_from(vec![AssetAmount { asset_id: stable_asset_1, amount: amount_to_sell, - }], + }]), )); let alice_pool_id_balance = Currencies::free_balance(pool_id, &AccountId::from(ALICE)); @@ -4144,7 +4144,7 @@ pub fn init_stableswap() -> Result<(AssetId, AssetId, AssetId), DispatchError> { Stableswap::create_pool(RuntimeOrigin::root(), pool_id, asset_ids, amplification, fee)?; - Stableswap::add_liquidity(RuntimeOrigin::signed(BOB.into()), pool_id, initial)?; + Stableswap::add_liquidity(RuntimeOrigin::signed(BOB.into()), pool_id, BoundedVec::truncate_from(initial))?; Ok((pool_id, asset_in, asset_out)) } @@ -4208,7 +4208,7 @@ pub fn init_stableswap_with_three_assets_having_different_decimals( Stableswap::create_pool(RuntimeOrigin::root(), pool_id, asset_ids, amplification, fee)?; - Stableswap::add_liquidity(RuntimeOrigin::signed(BOB.into()), pool_id, initial)?; + Stableswap::add_liquidity(RuntimeOrigin::signed(BOB.into()), pool_id, BoundedVec::truncate_from(initial))?; Ok((pool_id, asset_in, asset_out)) } diff --git a/integration-tests/src/router.rs b/integration-tests/src/router.rs index e68797d06..74f356645 100644 --- a/integration-tests/src/router.rs +++ b/integration-tests/src/router.rs @@ -25,7 +25,7 @@ use hydradx_traits::router::AssetPair as Pair; use primitives::AssetId; use sp_runtime::FixedPointNumber; -use frame_support::{assert_noop, assert_ok}; +use frame_support::{assert_noop, assert_ok, BoundedVec}; use xcm_emulator::TestExt; use frame_support::storage::with_transaction; @@ -5588,7 +5588,7 @@ pub fn init_stableswap_with_details( fee, )?; - Stableswap::add_liquidity(hydradx_runtime::RuntimeOrigin::signed(BOB.into()), pool_id, initial)?; + Stableswap::add_liquidity(hydradx_runtime::RuntimeOrigin::signed(BOB.into()), pool_id, BoundedVec::truncate_from(initial))?; Ok((pool_id, asset_in, asset_out)) } diff --git a/pallets/omnipool/proptest-regressions/tests/invariants.txt b/pallets/omnipool/proptest-regressions/tests/invariants.txt index db60fd566..e9a102e67 100644 --- a/pallets/omnipool/proptest-regressions/tests/invariants.txt +++ b/pallets/omnipool/proptest-regressions/tests/invariants.txt @@ -9,3 +9,4 @@ cc 5b0408b0d514dc33832f2a374aa9e3c479d0c84e58f4b88077c24387c4c8ef70 # shrinks to cc 47648d8c0c8f9c6b40fe9eefa80179540c1d386175531a36c3a825777402ce83 # shrinks to amount = 1000, stable_price = FixedU128(0.100000000000000000), stable_reserve = 100000000000000000, native_reserve = 100000000000000000, token_1 = PoolToken { asset_id: 100, amount: 100000000000000000, price: FixedU128(0.100000000000000000) }, token_2 = PoolToken { asset_id: 200, amount: 100000000000000000, price: FixedU128(0.100000000000000000) }, token_3 = PoolToken { asset_id: 300, amount: 100000000000000000, price: FixedU128(0.100000000000000000) }, token_4 = PoolToken { asset_id: 400, amount: 100000000000467280, price: FixedU128(0.943083786844469504) }, asset_fee = 0.17%, protocol_fee = 0.025%, buy_amount = 3545063556811621, min_withdraw_fee = 100%, (price_adjustment, denom, direction) = (4, 10000, false) cc f1f5ebb8a88a3db4df5dcf4170496ef8dffffbb61bb6991226a597f95ae22ca5 # shrinks to amount = 1000, stable_price = FixedU128(0.100000000000000000), stable_reserve = 100000000000000000, native_reserve = 100000000000000000, token_1 = PoolToken { asset_id: 100, amount: 100000000000000000, price: FixedU128(0.100000000000000000) }, token_2 = PoolToken { asset_id: 200, amount: 100000000000000000, price: FixedU128(0.100000000000000000) }, token_3 = PoolToken { asset_id: 300, amount: 100000000000000000, price: FixedU128(0.100000000000000000) }, token_4 = PoolToken { asset_id: 400, amount: 100000000068367115, price: FixedU128(0.481724750982654144) }, asset_fee = 0.29%, protocol_fee = 0.03%, buy_amount = 986312481009315 cc 2591d2b97991eebcad60c25b3d7ef318ad8651bf0e1f6f064eccee110d645305 # shrinks to amount = 1000, stable_price = FixedU128(0.100000000000000000), stable_reserve = 100000000000000000, native_reserve = 100000000000000000, token_1 = PoolToken { asset_id: 100, amount: 100000000000000000, price: FixedU128(0.100000000000000000) }, token_2 = PoolToken { asset_id: 200, amount: 100000000000000000, price: FixedU128(0.100000000000000000) }, token_3 = PoolToken { asset_id: 300, amount: 100000000000000000, price: FixedU128(0.100000000000000000) }, token_4 = PoolToken { asset_id: 400, amount: 100000000001763682, price: FixedU128(0.513377270830019968) } +cc ae8021e5146d25d491b1810127c42da6bdd2ccaced93d28ecd7951a000ae9e93 # shrinks to amount = 2846898654707652, stable_price = FixedU128(0.100000000000000000), stable_reserve = 100000000000000000, native_reserve = 100000000000000000, token_1 = PoolToken { asset_id: 100, amount: 100000000000000000, price: FixedU128(0.100000000000000000) }, token_2 = PoolToken { asset_id: 200, amount: 100000000000000000, price: FixedU128(0.100000000000000000) }, token_3 = PoolToken { asset_id: 300, amount: 100000000000000000, price: FixedU128(0.100000000000000000) }, token_4 = PoolToken { asset_id: 400, amount: 100000000375617618, price: FixedU128(1.486980765625393664) }, asset_fee = 0.012%, protocol_fee = 2.5% diff --git a/pallets/stableswap/Cargo.toml b/pallets/stableswap/Cargo.toml index 58c662547..bd2ebc3bd 100644 --- a/pallets/stableswap/Cargo.toml +++ b/pallets/stableswap/Cargo.toml @@ -1,6 +1,6 @@ [package] name = 'pallet-stableswap' -version = '4.0.0' +version = '4.1.0' description = 'AMM for correlated assets' authors = ['GalacticCouncil'] edition = '2021' diff --git a/pallets/stableswap/src/benchmarks.rs b/pallets/stableswap/src/benchmarks.rs index 8ba3aa8de..d008fdbe0 100644 --- a/pallets/stableswap/src/benchmarks.rs +++ b/pallets/stableswap/src/benchmarks.rs @@ -98,9 +98,9 @@ benchmarks! { // Worst case is adding additional liquidity and not initial liquidity crate::Pallet::::add_liquidity(RawOrigin::Signed(caller).into(), pool_id, - initial, + BoundedVec::truncate_from(initial), )?; - }: _(RawOrigin::Signed(lp_provider.clone()), pool_id, added_liquidity) + }: _(RawOrigin::Signed(lp_provider.clone()), pool_id, BoundedVec::truncate_from(added_liquidity)) verify { assert!(T::Currency::free_balance(pool_id, &lp_provider) > 0u128); } @@ -139,7 +139,7 @@ benchmarks! { crate::Pallet::::add_liquidity(RawOrigin::Signed(caller).into(), pool_id, - initial, + BoundedVec::truncate_from(initial), )?; let desired_shares = 1198499641600967085948u128; }: _(RawOrigin::Signed(lp_provider.clone()), pool_id, desired_shares,asset_id, 1221886049851226) @@ -183,11 +183,11 @@ benchmarks! { // Worst case is adding additional liquidity and not initial liquidity crate::Pallet::::add_liquidity(RawOrigin::Signed(caller).into(), pool_id, - initial, + BoundedVec::truncate_from(initial), )?; crate::Pallet::::add_liquidity(RawOrigin::Signed(lp_provider.clone()).into(), pool_id, - added_liquidity + BoundedVec::truncate_from(added_liquidity) )?; // just make sure that LP provided all his liquidity of this asset @@ -236,11 +236,11 @@ benchmarks! { // Worst case is adding additional liquidity and not initial liquidity crate::Pallet::::add_liquidity(RawOrigin::Signed(caller).into(), pool_id, - initial, + BoundedVec::truncate_from(initial), )?; crate::Pallet::::add_liquidity(RawOrigin::Signed(lp_provider.clone()).into(), pool_id, - added_liquidity + BoundedVec::truncate_from(added_liquidity) )?; // just make sure that LP provided all his liquidity of this asset @@ -288,11 +288,11 @@ benchmarks! { // Worst case is adding additional liquidity and not initial liquidity crate::Pallet::::add_liquidity(RawOrigin::Signed(caller).into(), pool_id, - initial, + BoundedVec::truncate_from(initial), )?; crate::Pallet::::add_liquidity(RawOrigin::Signed(lp_provider.clone()).into(), pool_id, - added_liquidity + BoundedVec::truncate_from(added_liquidity) )?; // just make sure that LP provided all his liquidity of this asset @@ -338,7 +338,7 @@ benchmarks! { )?; crate::Pallet::::add_liquidity(RawOrigin::Signed(caller).into(), pool_id, - initial, + BoundedVec::truncate_from(initial), )?; let seller : T::AccountId = account("seller", 0, 1); @@ -392,7 +392,7 @@ benchmarks! { )?; crate::Pallet::::add_liquidity(RawOrigin::Signed(caller).into(), pool_id, - initial, + BoundedVec::truncate_from(initial), )?; let buyer: T::AccountId = account("buyer", 0, 1); @@ -570,7 +570,7 @@ benchmarks! { )?; crate::Pallet::::add_liquidity(RawOrigin::Signed(caller).into(), pool_id, - initial, + BoundedVec::truncate_from(initial), )?; let seller : T::AccountId = account("seller", 0, 1); @@ -634,7 +634,7 @@ benchmarks! { )?; crate::Pallet::::add_liquidity(RawOrigin::Signed(caller).into(), pool_id, - initial, + BoundedVec::truncate_from(initial), )?; let buyer: T::AccountId = account("buyer", 0, 1); @@ -697,7 +697,7 @@ benchmarks! { )?; crate::Pallet::::add_liquidity(RawOrigin::Signed(caller).into(), pool_id, - initial, + BoundedVec::truncate_from(initial), )?; let buyer: T::AccountId = account("buyer", 0, 1); diff --git a/pallets/stableswap/src/lib.rs b/pallets/stableswap/src/lib.rs index b262929d5..e084522c3 100644 --- a/pallets/stableswap/src/lib.rs +++ b/pallets/stableswap/src/lib.rs @@ -475,7 +475,7 @@ pub mod pallet { pub fn add_liquidity( origin: OriginFor, pool_id: T::AssetId, - assets: Vec>, + assets: BoundedVec, ConstU32>, ) -> DispatchResult { let who = ensure_signed(origin)?; @@ -485,7 +485,7 @@ pub mod pallet { pool_id, who, shares, - assets, + assets: assets.to_vec(), }); Ok(()) diff --git a/pallets/stableswap/src/tests/add_liquidity.rs b/pallets/stableswap/src/tests/add_liquidity.rs index 81fc88e48..76196d9b0 100644 --- a/pallets/stableswap/src/tests/add_liquidity.rs +++ b/pallets/stableswap/src/tests/add_liquidity.rs @@ -1,7 +1,7 @@ use crate::tests::mock::*; use crate::types::{AssetAmount, PoolInfo}; use crate::{assert_balance, to_precision, Error}; -use frame_support::{assert_noop, assert_ok}; +use frame_support::{assert_noop, assert_ok, BoundedVec}; use sp_runtime::Permill; use std::num::NonZeroU16; @@ -39,10 +39,10 @@ fn add_initial_liquidity_should_work_when_called_first_time() { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![ + BoundedVec::truncate_from(vec![ AssetAmount::new(asset_a, initial_liquidity_amount), AssetAmount::new(asset_b, initial_liquidity_amount), - ] + ]) )); assert_balance!(BOB, asset_a, 100 * ONE); @@ -88,10 +88,10 @@ fn add_initial_liquidity_should_fail_when_lp_has_insufficient_balance() { Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![ + BoundedVec::truncate_from(vec![ AssetAmount::new(asset_a, initial_liquidity_amount), AssetAmount::new(asset_b, initial_liquidity_amount), - ] + ]) ), Error::::InsufficientBalance ); @@ -146,10 +146,10 @@ fn add_liquidity_should_work_when_initial_liquidity_has_been_provided() { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![ + BoundedVec::truncate_from(vec![ AssetAmount::new(asset_a, amount_added), AssetAmount::new(asset_b, amount_added), - ] + ]) )); assert_balance!(BOB, asset_a, 100 * ONE); @@ -203,10 +203,10 @@ fn add_liquidity_should_work_when_order_is_not_sorted() { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![ + BoundedVec::truncate_from(vec![ AssetAmount::new(asset_b, amount_added), AssetAmount::new(asset_a, amount_added), - ] + ]) )); assert_balance!(BOB, asset_a, 100 * ONE); @@ -258,10 +258,10 @@ fn add_liquidity_should_fail_when_providing_insufficient_liquidity() { Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![ + BoundedVec::truncate_from(vec![ AssetAmount::new(asset_b, amount_added), AssetAmount::new(asset_a, amount_added), - ] + ]) ), Error::::InsufficientTradingAmount ); @@ -315,7 +315,7 @@ fn add_liquidity_should_work_when_providing_one_asset_only() { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![AssetAmount::new(asset_a, amount_added),] + BoundedVec::truncate_from(vec![AssetAmount::new(asset_a, amount_added)]) )); }); } @@ -371,10 +371,10 @@ fn add_liquidity_should_fail_when_providing_one_asset_not_in_pool() { Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![ + BoundedVec::truncate_from(vec![ AssetAmount::new(asset_a, amount_added), AssetAmount::new(asset_e, amount_added), - ] + ]) ), Error::::AssetNotInPool ); @@ -421,10 +421,10 @@ fn add_liquidity_should_fail_when_provided_list_contains_same_assets() { Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![ + BoundedVec::truncate_from(vec![ AssetAmount::new(asset_a, amount_added), AssetAmount::new(asset_a, amount_added), - ] + ]) ), Error::::IncorrectAssets ); @@ -467,10 +467,10 @@ fn add_initial_liquidity_should_work_when_asset_have_different_decimals() { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![ + BoundedVec::truncate_from(vec![ AssetAmount::new(asset_a, initial_liquidity_amount_a), AssetAmount::new(asset_b, initial_liquidity_amount_b), - ] + ]) )); assert_balance!(BOB, asset_a, to_precision!(100, dec_a)); @@ -525,7 +525,7 @@ fn add_liquidity_should_work_correctly() { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![AssetAmount::new(asset_a, amount),] + BoundedVec::truncate_from(vec![AssetAmount::new(asset_a, amount),]) )); let received = Tokens::free_balance(pool_id, &BOB); assert_eq!(received, 1947597621401945851); @@ -575,7 +575,7 @@ fn add_liquidity_should_work_correctly_when_fee_is_applied() { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![AssetAmount::new(asset_a, amount),] + BoundedVec::truncate_from(vec![AssetAmount::new(asset_a, amount),]) )); let received = Tokens::free_balance(pool_id, &BOB); assert_eq!(received, 1947487201901031408); diff --git a/pallets/stableswap/src/tests/hooks.rs b/pallets/stableswap/src/tests/hooks.rs index 365cc437b..eff1aafd4 100644 --- a/pallets/stableswap/src/tests/hooks.rs +++ b/pallets/stableswap/src/tests/hooks.rs @@ -1,6 +1,6 @@ use crate::tests::mock::*; use crate::types::{AssetAmount, PoolInfo, PoolState}; -use frame_support::assert_ok; +use frame_support::{assert_ok, BoundedVec}; use sp_runtime::Permill; use std::num::NonZeroU16; @@ -47,7 +47,7 @@ fn add_liquidity_should_provide_correct_values_in_the_hook() { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![AssetAmount::new(asset_a, amount)], + BoundedVec::truncate_from(vec![AssetAmount::new(asset_a, amount)]), )); let (p, state) = last_liquidity_changed_hook_state().unwrap(); assert_eq!(p, pool_id); diff --git a/pallets/stableswap/src/tests/invariants.rs b/pallets/stableswap/src/tests/invariants.rs index 04fe399fa..04fd762fa 100644 --- a/pallets/stableswap/src/tests/invariants.rs +++ b/pallets/stableswap/src/tests/invariants.rs @@ -91,9 +91,9 @@ proptest! { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![ - AssetAmount::new(asset_a, added_liquidity), - ] + BoundedVec::truncate_from(vec![ + AssetAmount::new(asset_a, added_liquidity), + ]) )); let final_shares = Tokens::total_issuance(pool_id); let delta_s = final_shares - initial_shares; @@ -164,9 +164,9 @@ proptest! { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![ - AssetAmount::new(asset_a, added_liquidity), - ] + BoundedVec::truncate_from(vec![ + AssetAmount::new(asset_a, added_liquidity), + ]) )); let final_shares = Tokens::total_issuance(pool_id); let delta_s = final_shares - initial_shares; @@ -840,9 +840,9 @@ proptest! { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![ - AssetAmount::new(asset_a, added_liquidity), - ] + BoundedVec::truncate_from(vec![ + AssetAmount::new(asset_a, added_liquidity), + ]) )); let final_shares = Tokens::total_issuance(pool_id); let delta_s = final_shares - initial_shares; @@ -942,9 +942,9 @@ proptest! { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![ - AssetAmount::new(asset_a, added_liquidity), - ] + BoundedVec::truncate_from(vec![ + AssetAmount::new(asset_a, added_liquidity), + ]) )); let final_shares = Tokens::total_issuance(pool_id); let delta_s = final_shares - initial_shares; @@ -1261,9 +1261,9 @@ proptest! { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![ - AssetAmount::new(asset_a, added_liquidity), - ] + BoundedVec::truncate_from(vec![ + AssetAmount::new(asset_a, added_liquidity), + ]) )); let final_shares = Tokens::total_issuance(pool_id); let delta_s = final_shares - initial_shares; diff --git a/pallets/stableswap/src/tests/mock.rs b/pallets/stableswap/src/tests/mock.rs index 8e71647c7..e65b59e81 100644 --- a/pallets/stableswap/src/tests/mock.rs +++ b/pallets/stableswap/src/tests/mock.rs @@ -29,7 +29,7 @@ use crate as pallet_stableswap; use crate::Config; -use frame_support::assert_ok; +use frame_support::{assert_ok, BoundedVec}; use frame_support::traits::{Contains, Everything}; use frame_support::weights::Weight; use frame_support::{ @@ -304,7 +304,7 @@ impl ExtBuilder { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(initial_liquid.account), pool_id, - initial_liquid.assets + BoundedVec::truncate_from(initial_liquid.assets) )); } } diff --git a/pallets/stableswap/src/tests/price.rs b/pallets/stableswap/src/tests/price.rs index ba78a491b..3a38b3be4 100644 --- a/pallets/stableswap/src/tests/price.rs +++ b/pallets/stableswap/src/tests/price.rs @@ -1,6 +1,6 @@ use crate::tests::*; use crate::types::{AssetAmount, PoolInfo}; -use frame_support::assert_ok; +use frame_support::{assert_ok, BoundedVec}; use sp_runtime::{FixedU128, Permill}; use std::num::NonZeroU16; @@ -181,7 +181,7 @@ fn test_share_price_in_add_remove_liquidity() { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![AssetAmount::new(asset_a, amount)], + BoundedVec::truncate_from(vec![AssetAmount::new(asset_a, amount)]), )); let final_shares = Tokens::total_issuance(pool_id); @@ -326,7 +326,7 @@ fn test_share_price_case() { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![AssetAmount::new(asset_a, amount)], + BoundedVec::truncate_from(vec![AssetAmount::new(asset_a, amount)]), )); let final_shares = Tokens::total_issuance(pool_id); diff --git a/pallets/stableswap/src/tests/remove_liquidity.rs b/pallets/stableswap/src/tests/remove_liquidity.rs index f70b6f8a7..7e9675c84 100644 --- a/pallets/stableswap/src/tests/remove_liquidity.rs +++ b/pallets/stableswap/src/tests/remove_liquidity.rs @@ -52,7 +52,7 @@ fn remove_liquidity_should_work_when_withdrawing_all_shares() { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![AssetAmount::new(asset_a, amount_added),] + BoundedVec::truncate_from(vec![AssetAmount::new(asset_a, amount_added)]) )); let shares = Tokens::free_balance(pool_id, &BOB); @@ -120,7 +120,7 @@ fn remove_liquidity_should_apply_fee_when_withdrawing_all_shares() { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![AssetAmount::new(asset_a, amount_added)] + BoundedVec::truncate_from(vec![AssetAmount::new(asset_a, amount_added)]) )); let shares = Tokens::free_balance(pool_id, &BOB); @@ -245,7 +245,7 @@ fn remove_liquidity_should_fail_when_requested_asset_not_in_pool() { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![AssetAmount::new(asset_a, amount_added)] + BoundedVec::truncate_from(vec![AssetAmount::new(asset_a, amount_added)]) )); let shares = Tokens::free_balance(pool_id, &BOB); @@ -301,7 +301,7 @@ fn remove_liquidity_should_fail_when_remaining_shares_below_min_liquidity() { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![AssetAmount::new(asset_a, amount_added)] + BoundedVec::truncate_from(vec![AssetAmount::new(asset_a, amount_added)]) )); let shares = Tokens::free_balance(pool_id, &BOB); @@ -369,7 +369,7 @@ fn verify_remove_liquidity_against_research_impl() { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![AssetAmount::new(asset_a, amount_added)] + BoundedVec::truncate_from(vec![AssetAmount::new(asset_a, amount_added)]) )); let shares = Tokens::free_balance(pool_id, &BOB); @@ -435,7 +435,7 @@ fn remove_liquidity_fail_when_desired_min_limit_is_not_reached() { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![AssetAmount::new(asset_a, amount_added)] + BoundedVec::truncate_from(vec![AssetAmount::new(asset_a, amount_added)]) )); let shares = Tokens::free_balance(pool_id, &BOB); @@ -495,7 +495,7 @@ fn scenario_add_remove_with_different_decimals() { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![AssetAmount::new(asset_c, 20 * one_c)] + BoundedVec::truncate_from(vec![AssetAmount::new(asset_c, 20 * one_c)]) )); let shares = Tokens::free_balance(pool_id, &BOB); @@ -1040,7 +1040,7 @@ fn remove_multi_asset_liquidity_should_work_when_withdrawing_some_shares() { assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![AssetAmount::new(asset_a, amount_added),] + BoundedVec::truncate_from(vec![AssetAmount::new(asset_a, amount_added)]) )); let shares = Tokens::free_balance(pool_id, &BOB); @@ -1129,7 +1129,7 @@ fn remove_multi_asset_liquidity_should_work_when_withdrawing_all_remaining_share assert_ok!(Stableswap::add_liquidity( RuntimeOrigin::signed(BOB), pool_id, - vec![AssetAmount::new(asset_a, amount_added),] + BoundedVec::truncate_from(vec![AssetAmount::new(asset_a, amount_added)]) )); let shares = Tokens::free_balance(pool_id, &BOB); diff --git a/pallets/stableswap/src/trade_execution.rs b/pallets/stableswap/src/trade_execution.rs index 53d54e25a..db20b713b 100644 --- a/pallets/stableswap/src/trade_execution.rs +++ b/pallets/stableswap/src/trade_execution.rs @@ -1,3 +1,4 @@ +use frame_support::BoundedVec; use crate::types::AssetAmount; use crate::{Balance, Config, Error, Pallet, Pools, D_ITERATIONS, Y_ITERATIONS}; use hydra_dx_math::stableswap::types::AssetReserve; @@ -152,10 +153,10 @@ where Self::add_liquidity( who, pool_id, - vec![AssetAmount { + BoundedVec::truncate_from(vec![AssetAmount { asset_id: asset_in, amount: amount_in, - }], + }]) ) .map_err(ExecutorError::Error) } else { diff --git a/runtime-mock/Cargo.toml b/runtime-mock/Cargo.toml index 5fb31cac6..41cf2bfc4 100644 --- a/runtime-mock/Cargo.toml +++ b/runtime-mock/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "runtime-mock" -version = "1.0.1" +version = "1.0.2" description = "Mock of the HydraDX Runtime for testing purposes" authors = ["GalacticCouncil"] edition = "2021" diff --git a/runtime-mock/src/stableswap.rs b/runtime-mock/src/stableswap.rs index 2b9d388b0..6f5221b4b 100644 --- a/runtime-mock/src/stableswap.rs +++ b/runtime-mock/src/stableswap.rs @@ -5,6 +5,7 @@ use serde::Deserialize; use serde::Deserializer; use sp_runtime::{FixedPointNumber, FixedU128, Permill}; use std::fs; +use sp_core::bounded_vec::BoundedVec; #[derive(Debug, Deserialize)] pub struct AssetReserve { @@ -91,7 +92,7 @@ impl Stablepools { .map(|pool| { RuntimeCall::Stableswap(pallet_stableswap::Call::add_liquidity { pool_id: pool.pool_id, - assets: pool.get_asset_amounts(), + assets: BoundedVec::truncate_from(pool.get_asset_amounts()), }) }) .collect() diff --git a/runtime/hydradx/Cargo.toml b/runtime/hydradx/Cargo.toml index ecf148df8..a64b225ba 100644 --- a/runtime/hydradx/Cargo.toml +++ b/runtime/hydradx/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hydradx-runtime" -version = "273.0.0" +version = "274.0.0" authors = ["GalacticCouncil"] edition = "2021" license = "Apache 2.0" diff --git a/runtime/hydradx/src/assets.rs b/runtime/hydradx/src/assets.rs index c2796a062..1348f2673 100644 --- a/runtime/hydradx/src/assets.rs +++ b/runtime/hydradx/src/assets.rs @@ -1645,7 +1645,7 @@ where let price = SP::spot_price(asset_to, asset_from).ok_or(pallet_referrals::Error::::PriceNotFound)?; let amount_to_receive = price.saturating_mul_int(amount); let min_expected = amount_to_receive - .saturating_sub(Permill::from_percent(1).mul_floor(amount_to_receive)) + .saturating_sub(Permill::from_percent(5).mul_floor(amount_to_receive)) .max(1); let balance = Currencies::free_balance(asset_to, &who); let r = Omnipool::sell( diff --git a/runtime/hydradx/src/lib.rs b/runtime/hydradx/src/lib.rs index c26316842..d91322eb2 100644 --- a/runtime/hydradx/src/lib.rs +++ b/runtime/hydradx/src/lib.rs @@ -112,7 +112,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("hydradx"), impl_name: create_runtime_str!("hydradx"), authoring_version: 1, - spec_version: 273, + spec_version: 274, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 1,