Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: stableswap add liquidity does not accept same assets [A3] #632

Merged
merged 4 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 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 pallets/stableswap/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = 'pallet-stableswap'
version = '2.0.0'
version = '2.0.1'
description = 'AMM for correlated assets'
authors = ['GalacticCouncil']
edition = '2021'
Expand Down
7 changes: 6 additions & 1 deletion pallets/stableswap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ pub mod pallet {

/// New amplification is equal to the previous value.
SameAmplification,

/// Provided list of assets contains same assets.
SameAssets,
enthusiastmartin marked this conversation as resolved.
Show resolved Hide resolved
}

#[pallet::call]
Expand Down Expand Up @@ -864,7 +867,9 @@ impl<T: Config> Pallet<T> {
Error::<T>::InsufficientBalance
);
ensure!(pool.find_asset(asset.asset_id).is_some(), Error::<T>::AssetNotInPool);
added_assets.insert(asset.asset_id, asset.amount);
if added_assets.insert(asset.asset_id, asset.amount).is_some() {
return Err(Error::<T>::SameAssets.into());
}
}

let pool_account = Self::pool_account(pool_id);
Expand Down
63 changes: 63 additions & 0 deletions pallets/stableswap/src/tests/add_liquidity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,66 @@ fn add_liquidity_should_fail_when_providing_one_asset_not_in_pool() {
);
});
}

#[test]
fn add_liquidity_should_fail_when_provided_list_contains_same_assets() {
let asset_a: AssetId = 1;
let asset_b: AssetId = 2;

ExtBuilder::default()
.with_endowed_accounts(vec![
(BOB, 1, 200 * ONE),
(BOB, 2, 200 * ONE),
(ALICE, 1, 200 * ONE),
(ALICE, 2, 200 * ONE),
])
.with_registered_asset("one".as_bytes().to_vec(), asset_a)
.with_registered_asset("two".as_bytes().to_vec(), asset_b)
.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,
trade_fee: Permill::from_percent(0),
withdraw_fee: Permill::from_percent(0),
},
InitialLiquidity {
account: ALICE,
assets: vec![
AssetLiquidity {
asset_id: asset_a,
amount: 100 * ONE,
},
AssetLiquidity {
asset_id: asset_b,
amount: 100 * ONE,
},
],
},
)
.build()
.execute_with(|| {
let pool_id = get_pool_id_at(0);
let amount_added = 100 * ONE;
assert_noop!(
Stableswap::add_liquidity(
RuntimeOrigin::signed(BOB),
pool_id,
vec![
AssetLiquidity {
asset_id: asset_a,
amount: amount_added
},
AssetLiquidity {
asset_id: asset_a,
amount: amount_added
}
]
),
Error::<Test>::SameAssets
);
});
}
Loading