Skip to content

Commit

Permalink
use bounded vec for stableswap extrinsic
Browse files Browse the repository at this point in the history
  • Loading branch information
dmoka committed Dec 1, 2024
1 parent 41b2448 commit e4517bb
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 42 deletions.
6 changes: 3 additions & 3 deletions pallets/stableswap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ extern crate core;
use frame_support::pallet_prelude::{DispatchResult, Get};
use frame_support::{ensure, require_transactional, transactional, PalletId};
use frame_system::pallet_prelude::{BlockNumberFor, OriginFor};
use hydradx_traits::{registry::Inspect, stableswap::StableswapAddLiquidity, AMMAddLiquidity, AccountIdFor};
use hydradx_traits::{registry::Inspect, stableswap::StableswapAddLiquidity, AccountIdFor};
pub use pallet::*;
use sp_runtime::traits::{AccountIdConversion, BlockNumberProvider, Zero};
use sp_runtime::{ArithmeticError, DispatchError, Permill, SaturatedConversion};
Expand Down Expand Up @@ -476,7 +476,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 Down Expand Up @@ -1186,7 +1186,7 @@ impl<T: Config> Pallet<T> {
pool_id,
who: who.clone(),
shares: share_amount,
assets: assets.clone().to_vec(),
assets: assets.to_vec(),
});

Ok(share_amount)
Expand Down
22 changes: 11 additions & 11 deletions pallets/stableswap/src/tests/add_liquidity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn add_initial_liquidity_should_work_when_called_first_time() {
vec![
AssetAmount::new(asset_a, initial_liquidity_amount),
AssetAmount::new(asset_b, initial_liquidity_amount),
]
].try_into().unwrap()
));

assert_balance!(BOB, asset_a, 100 * ONE);
Expand Down Expand Up @@ -92,7 +92,7 @@ fn add_initial_liquidity_should_fail_when_lp_has_insufficient_balance() {
vec![
AssetAmount::new(asset_a, initial_liquidity_amount),
AssetAmount::new(asset_b, initial_liquidity_amount),
]
].try_into().unwrap()
),
Error::<Test>::InsufficientBalance
);
Expand Down Expand Up @@ -150,7 +150,7 @@ fn add_liquidity_should_work_when_initial_liquidity_has_been_provided() {
vec![
AssetAmount::new(asset_a, amount_added),
AssetAmount::new(asset_b, amount_added),
]
].try_into().unwrap()
));

assert_balance!(BOB, asset_a, 100 * ONE);
Expand Down Expand Up @@ -207,7 +207,7 @@ fn add_liquidity_should_work_when_order_is_not_sorted() {
vec![
AssetAmount::new(asset_b, amount_added),
AssetAmount::new(asset_a, amount_added),
]
].try_into().unwrap()
));

assert_balance!(BOB, asset_a, 100 * ONE);
Expand Down Expand Up @@ -262,7 +262,7 @@ fn add_liquidity_should_fail_when_providing_insufficient_liquidity() {
vec![
AssetAmount::new(asset_b, amount_added),
AssetAmount::new(asset_a, amount_added),
]
].try_into().unwrap()
),
Error::<Test>::InsufficientTradingAmount
);
Expand Down Expand Up @@ -316,7 +316,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),]
vec![AssetAmount::new(asset_a, amount_added),].try_into().unwrap()
));
});
}
Expand Down Expand Up @@ -375,7 +375,7 @@ fn add_liquidity_should_fail_when_providing_one_asset_not_in_pool() {
vec![
AssetAmount::new(asset_a, amount_added),
AssetAmount::new(asset_e, amount_added),
]
].try_into().unwrap()
),
Error::<Test>::AssetNotInPool
);
Expand Down Expand Up @@ -425,7 +425,7 @@ fn add_liquidity_should_fail_when_provided_list_contains_same_assets() {
vec![
AssetAmount::new(asset_a, amount_added),
AssetAmount::new(asset_a, amount_added),
]
].try_into().unwrap()
),
Error::<Test>::IncorrectAssets
);
Expand Down Expand Up @@ -471,7 +471,7 @@ fn add_initial_liquidity_should_work_when_asset_have_different_decimals() {
vec![
AssetAmount::new(asset_a, initial_liquidity_amount_a),
AssetAmount::new(asset_b, initial_liquidity_amount_b),
]
].try_into().unwrap()
));

assert_balance!(BOB, asset_a, to_precision!(100, dec_a));
Expand Down Expand Up @@ -526,7 +526,7 @@ fn add_liquidity_should_work_correctly() {
assert_ok!(Stableswap::add_liquidity(
RuntimeOrigin::signed(BOB),
pool_id,
vec![AssetAmount::new(asset_a, amount),]
vec![AssetAmount::new(asset_a, amount),].try_into().unwrap()
));
let received = Tokens::free_balance(pool_id, &BOB);
assert_eq!(received, 1947597621401945851);
Expand Down Expand Up @@ -576,7 +576,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),]
vec![AssetAmount::new(asset_a, amount),].try_into().unwrap()
));
let received = Tokens::free_balance(pool_id, &BOB);
assert_eq!(received, 1947487201901031408);
Expand Down
2 changes: 1 addition & 1 deletion pallets/stableswap/src/tests/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,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)],
vec![AssetAmount::new(asset_a, amount)].try_into().unwrap(),
));
let (p, state) = last_liquidity_changed_hook_state().unwrap();
assert_eq!(p, pool_id);
Expand Down
10 changes: 5 additions & 5 deletions pallets/stableswap/src/tests/invariants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ proptest! {
pool_id,
vec![
AssetAmount::new(asset_a, added_liquidity),
]
].try_into().unwrap()
));
let final_shares = Tokens::total_issuance(pool_id);
let delta_s = final_shares - initial_shares;
Expand Down Expand Up @@ -167,7 +167,7 @@ proptest! {
pool_id,
vec![
AssetAmount::new(asset_a, added_liquidity),
]
].try_into().unwrap()
));
let final_shares = Tokens::total_issuance(pool_id);
let delta_s = final_shares - initial_shares;
Expand Down Expand Up @@ -843,7 +843,7 @@ proptest! {
pool_id,
vec![
AssetAmount::new(asset_a, added_liquidity),
]
].try_into().unwrap()
));
let final_shares = Tokens::total_issuance(pool_id);
let delta_s = final_shares - initial_shares;
Expand Down Expand Up @@ -945,7 +945,7 @@ proptest! {
pool_id,
vec![
AssetAmount::new(asset_a, added_liquidity),
]
].try_into().unwrap()
));
let final_shares = Tokens::total_issuance(pool_id);
let delta_s = final_shares - initial_shares;
Expand Down Expand Up @@ -1264,7 +1264,7 @@ proptest! {
pool_id,
vec![
AssetAmount::new(asset_a, added_liquidity),
]
].try_into().unwrap()
));
let final_shares = Tokens::total_issuance(pool_id);
let delta_s = final_shares - initial_shares;
Expand Down
2 changes: 1 addition & 1 deletion pallets/stableswap/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ impl ExtBuilder {
assert_ok!(Stableswap::add_liquidity(
RuntimeOrigin::signed(initial_liquid.account),
pool_id,
initial_liquid.assets
initial_liquid.assets.try_into().unwrap()
));
}
}
Expand Down
4 changes: 2 additions & 2 deletions pallets/stableswap/src/tests/price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,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)],
vec![AssetAmount::new(asset_a, amount)].try_into().unwrap(),
));

let final_shares = Tokens::total_issuance(pool_id);
Expand Down Expand Up @@ -327,7 +327,7 @@ fn test_share_price_case() {
assert_ok!(Stableswap::add_liquidity(
RuntimeOrigin::signed(BOB),
pool_id,
vec![AssetAmount::new(asset_a, amount)],
vec![AssetAmount::new(asset_a, amount)].try_into().unwrap(),
));

let final_shares = Tokens::total_issuance(pool_id);
Expand Down
18 changes: 9 additions & 9 deletions pallets/stableswap/src/tests/remove_liquidity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,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),]
vec![AssetAmount::new(asset_a, amount_added),].try_into().unwrap()
));

let shares = Tokens::free_balance(pool_id, &BOB);
Expand Down Expand Up @@ -121,7 +121,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)]
vec![AssetAmount::new(asset_a, amount_added)].try_into().unwrap()
));

let shares = Tokens::free_balance(pool_id, &BOB);
Expand Down Expand Up @@ -246,7 +246,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)]
vec![AssetAmount::new(asset_a, amount_added)].try_into().unwrap()
));

let shares = Tokens::free_balance(pool_id, &BOB);
Expand Down Expand Up @@ -302,7 +302,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)]
vec![AssetAmount::new(asset_a, amount_added)].try_into().unwrap()
));

let shares = Tokens::free_balance(pool_id, &BOB);
Expand Down Expand Up @@ -370,7 +370,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)]
vec![AssetAmount::new(asset_a, amount_added)].try_into().unwrap()
));

let shares = Tokens::free_balance(pool_id, &BOB);
Expand Down Expand Up @@ -436,7 +436,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)]
vec![AssetAmount::new(asset_a, amount_added)].try_into().unwrap()
));

let shares = Tokens::free_balance(pool_id, &BOB);
Expand Down Expand Up @@ -496,7 +496,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)]
vec![AssetAmount::new(asset_c, 20 * one_c)].try_into().unwrap()
));

let shares = Tokens::free_balance(pool_id, &BOB);
Expand Down Expand Up @@ -1041,7 +1041,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),]
vec![AssetAmount::new(asset_a, amount_added),].try_into().unwrap()
));

let shares = Tokens::free_balance(pool_id, &BOB);
Expand Down Expand Up @@ -1130,7 +1130,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),]
vec![AssetAmount::new(asset_a, amount_added),].try_into().unwrap()
));

let shares = Tokens::free_balance(pool_id, &BOB);
Expand Down
19 changes: 9 additions & 10 deletions pallets/stableswap/src/trade_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use hydra_dx_math::stableswap::types::AssetReserve;
use hydradx_traits::router::{ExecutorError, PoolType, TradeExecution};
use hydradx_traits::stableswap::AssetAmount;
use orml_traits::MultiCurrency;
use sp_core::Get;
use sp_core::{Get};
use sp_runtime::{ArithmeticError, DispatchError, FixedU128};
use sp_std::vec;

Expand Down Expand Up @@ -149,15 +149,14 @@ where
Self::remove_liquidity_one_asset(who, pool_id, asset_out, amount_in, min_limit)
.map_err(ExecutorError::Error)
} else if asset_out == pool_id {
Self::add_liquidity(
who,
pool_id,
vec![AssetAmount {
asset_id: asset_in,
amount: amount_in,
}],
)
.map_err(ExecutorError::Error)
let asset = vec![AssetAmount {
asset_id: asset_in,
amount: amount_in,
}]
.try_into()
.map_err(|_| ExecutorError::Error(ArithmeticError::Overflow.into()))?;

Self::add_liquidity(who, pool_id, asset).map_err(ExecutorError::Error)
} else {
Self::sell(who, pool_id, asset_in, asset_out, amount_in, min_limit).map_err(ExecutorError::Error)
}
Expand Down

0 comments on commit e4517bb

Please sign in to comment.