Skip to content

Commit

Permalink
Merge pull request #632 from galacticcouncil/fix/stableswap-add-same-…
Browse files Browse the repository at this point in the history
…assets

fix: stableswap add liquidity does not accept same assets [A3]
  • Loading branch information
enthusiastmartin committed Jul 7, 2023
2 parents 27b0a27 + 240551c commit c105395
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
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
4 changes: 3 additions & 1 deletion pallets/stableswap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,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>::IncorrectAssets.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>::IncorrectAssets
);
});
}

0 comments on commit c105395

Please sign in to comment.