Skip to content

Commit

Permalink
refactor: stableswap add liquidity
Browse files Browse the repository at this point in the history
  • Loading branch information
enthusiastmartin committed Dec 14, 2024
1 parent 23d16f0 commit 9a49b87
Show file tree
Hide file tree
Showing 20 changed files with 89 additions and 86 deletions.
8 changes: 4 additions & 4 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 integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "runtime-integration-tests"
version = "1.25.2"
version = "1.25.3"
description = "Integration tests"
authors = ["GalacticCouncil"]
edition = "2021"
Expand Down
8 changes: 4 additions & 4 deletions integration-tests/src/dca.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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))
}
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
}
Expand Down
1 change: 1 addition & 0 deletions pallets/omnipool/proptest-regressions/tests/invariants.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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%
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 = '4.0.0'
version = '4.1.0'
description = 'AMM for correlated assets'
authors = ['GalacticCouncil']
edition = '2021'
Expand Down
28 changes: 14 additions & 14 deletions pallets/stableswap/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ benchmarks! {
// Worst case is adding additional liquidity and not initial liquidity
crate::Pallet::<T>::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);
}
Expand Down Expand Up @@ -139,7 +139,7 @@ benchmarks! {

crate::Pallet::<T>::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)
Expand Down Expand Up @@ -183,11 +183,11 @@ benchmarks! {
// Worst case is adding additional liquidity and not initial liquidity
crate::Pallet::<T>::add_liquidity(RawOrigin::Signed(caller).into(),
pool_id,
initial,
BoundedVec::truncate_from(initial),
)?;
crate::Pallet::<T>::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
Expand Down Expand Up @@ -236,11 +236,11 @@ benchmarks! {
// Worst case is adding additional liquidity and not initial liquidity
crate::Pallet::<T>::add_liquidity(RawOrigin::Signed(caller).into(),
pool_id,
initial,
BoundedVec::truncate_from(initial),
)?;
crate::Pallet::<T>::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
Expand Down Expand Up @@ -288,11 +288,11 @@ benchmarks! {
// Worst case is adding additional liquidity and not initial liquidity
crate::Pallet::<T>::add_liquidity(RawOrigin::Signed(caller).into(),
pool_id,
initial,
BoundedVec::truncate_from(initial),
)?;
crate::Pallet::<T>::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
Expand Down Expand Up @@ -338,7 +338,7 @@ benchmarks! {
)?;
crate::Pallet::<T>::add_liquidity(RawOrigin::Signed(caller).into(),
pool_id,
initial,
BoundedVec::truncate_from(initial),
)?;

let seller : T::AccountId = account("seller", 0, 1);
Expand Down Expand Up @@ -392,7 +392,7 @@ benchmarks! {
)?;
crate::Pallet::<T>::add_liquidity(RawOrigin::Signed(caller).into(),
pool_id,
initial,
BoundedVec::truncate_from(initial),
)?;

let buyer: T::AccountId = account("buyer", 0, 1);
Expand Down Expand Up @@ -570,7 +570,7 @@ benchmarks! {
)?;
crate::Pallet::<T>::add_liquidity(RawOrigin::Signed(caller).into(),
pool_id,
initial,
BoundedVec::truncate_from(initial),
)?;

let seller : T::AccountId = account("seller", 0, 1);
Expand Down Expand Up @@ -634,7 +634,7 @@ benchmarks! {
)?;
crate::Pallet::<T>::add_liquidity(RawOrigin::Signed(caller).into(),
pool_id,
initial,
BoundedVec::truncate_from(initial),
)?;

let buyer: T::AccountId = account("buyer", 0, 1);
Expand Down Expand Up @@ -697,7 +697,7 @@ benchmarks! {
)?;
crate::Pallet::<T>::add_liquidity(RawOrigin::Signed(caller).into(),
pool_id,
initial,
BoundedVec::truncate_from(initial),
)?;

let buyer: T::AccountId = account("buyer", 0, 1);
Expand Down
4 changes: 2 additions & 2 deletions pallets/stableswap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ pub mod pallet {
pub fn add_liquidity(
origin: OriginFor<T>,
pool_id: T::AssetId,
assets: Vec<AssetAmount<T::AssetId>>,
assets: BoundedVec<AssetAmount<T::AssetId>, ConstU32<MAX_ASSETS_IN_POOL>>,
) -> DispatchResult {
let who = ensure_signed(origin)?;

Expand All @@ -485,7 +485,7 @@ pub mod pallet {
pool_id,
who,
shares,
assets,
assets: assets.to_vec(),
});

Ok(())
Expand Down
40 changes: 20 additions & 20 deletions pallets/stableswap/src/tests/add_liquidity.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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::<Test>::InsufficientBalance
);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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::<Test>::InsufficientTradingAmount
);
Expand Down Expand Up @@ -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)])
));
});
}
Expand Down Expand Up @@ -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::<Test>::AssetNotInPool
);
Expand Down Expand Up @@ -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::<Test>::IncorrectAssets
);
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions pallets/stableswap/src/tests/hooks.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 9a49b87

Please sign in to comment.