From 6bebf2a4d9d0db3b15af4bb2503be74d304c387d Mon Sep 17 00:00:00 2001 From: Martin Date: Fri, 7 Jul 2023 09:01:33 +0200 Subject: [PATCH 1/2] fix: check if pool and asset exists when setting tradable state --- Cargo.lock | 2 +- pallets/stableswap/Cargo.toml | 2 +- pallets/stableswap/src/lib.rs | 3 + pallets/stableswap/src/tests/update_pool.rs | 88 ++++++++++++++++++++- 4 files changed, 91 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2b298eaa7..46f9f7d94 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7316,7 +7316,7 @@ dependencies = [ [[package]] name = "pallet-stableswap" -version = "2.0.0" +version = "2.0.1" dependencies = [ "bitflags", "frame-benchmarking", diff --git a/pallets/stableswap/Cargo.toml b/pallets/stableswap/Cargo.toml index fa86fe9ef..5901b83b5 100644 --- a/pallets/stableswap/Cargo.toml +++ b/pallets/stableswap/Cargo.toml @@ -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' diff --git a/pallets/stableswap/src/lib.rs b/pallets/stableswap/src/lib.rs index aba70aabd..0692d515c 100644 --- a/pallets/stableswap/src/lib.rs +++ b/pallets/stableswap/src/lib.rs @@ -717,6 +717,9 @@ pub mod pallet { ) -> DispatchResult { T::AuthorityOrigin::ensure_origin(origin)?; + let pool = Pools::::get(pool_id).ok_or(Error::::PoolNotFound)?; + let _ = pool.find_asset(asset_id).ok_or(Error::::AssetNotInPool)?; + AssetTradability::::mutate(pool_id, asset_id, |current_state| { *current_state = state; }); diff --git a/pallets/stableswap/src/tests/update_pool.rs b/pallets/stableswap/src/tests/update_pool.rs index 733a08657..c755c9209 100644 --- a/pallets/stableswap/src/tests/update_pool.rs +++ b/pallets/stableswap/src/tests/update_pool.rs @@ -1,6 +1,6 @@ use crate::tests::mock::*; -use crate::types::PoolInfo; -use crate::{Error, Pools}; +use crate::types::{PoolInfo, Tradability}; +use crate::{AssetTradability, Error, Pools}; use frame_support::{assert_noop, assert_ok}; use sp_runtime::Permill; use std::num::NonZeroU16; @@ -242,3 +242,87 @@ fn update_pool_should_fail_when_pool_does_not_exists() { ); }); } + +#[test] +fn set_tradable_state_should_work_when_asset_in_pool() { + let asset_a: AssetId = 1; + let asset_b: AssetId = 2; + let pool_id: AssetId = 100; + + ExtBuilder::default() + .with_endowed_accounts(vec![(ALICE, asset_a, 200 * ONE), (ALICE, asset_b, 200 * ONE)]) + .with_registered_asset("pool".as_bytes().to_vec(), pool_id) + .with_registered_asset("one".as_bytes().to_vec(), asset_a) + .with_registered_asset("two".as_bytes().to_vec(), asset_b) + .build() + .execute_with(|| { + assert_ok!(Stableswap::create_pool( + RuntimeOrigin::root(), + pool_id, + vec![asset_a, asset_b], + 100, + Permill::from_percent(0), + Permill::from_percent(0), + )); + + assert_ok!(Stableswap::set_asset_tradable_state( + RuntimeOrigin::root(), + pool_id, + asset_a, + Tradability::FROZEN, + )); + + assert_eq!( + >::get(pool_id, asset_a), + Tradability::FROZEN, + ); + }); +} +#[test] +fn set_tradable_state_should_fail_when_asset_not_in_pool() { + let asset_a: AssetId = 1; + let asset_b: AssetId = 2; + let pool_id: AssetId = 100; + + ExtBuilder::default() + .with_endowed_accounts(vec![(ALICE, asset_a, 200 * ONE), (ALICE, asset_b, 200 * ONE)]) + .with_registered_asset("pool".as_bytes().to_vec(), pool_id) + .with_registered_asset("one".as_bytes().to_vec(), asset_a) + .with_registered_asset("two".as_bytes().to_vec(), asset_b) + .build() + .execute_with(|| { + assert_ok!(Stableswap::create_pool( + RuntimeOrigin::root(), + pool_id, + vec![asset_a, asset_b], + 100, + Permill::from_percent(0), + Permill::from_percent(0), + )); + + assert_noop!(Stableswap::set_asset_tradable_state( + RuntimeOrigin::root(), + pool_id, + 3, + Tradability::FROZEN, + ), + Error::::AssetNotInPool); + }); +} +#[test] +fn set_tradable_state_should_fail_when_pool_does_not_exist() { + let pool_id: AssetId = 100; + + ExtBuilder::default() + .with_registered_asset("pool".as_bytes().to_vec(), pool_id) + .build() + .execute_with(|| { + assert_noop!(Stableswap::set_asset_tradable_state( + RuntimeOrigin::root(), + pool_id, + 1, + Tradability::FROZEN, + ), + Error::::PoolNotFound); + }); +} \ No newline at end of file From 4cb5f087df3e13af5d0f6783847ff12c2b3a7d23 Mon Sep 17 00:00:00 2001 From: Martin Date: Fri, 7 Jul 2023 14:37:56 +0200 Subject: [PATCH 2/2] reformat and version --- pallets/stableswap/Cargo.toml | 2 +- pallets/stableswap/src/tests/update_pool.rs | 29 +++++++-------------- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/pallets/stableswap/Cargo.toml b/pallets/stableswap/Cargo.toml index 5901b83b5..9da47e459 100644 --- a/pallets/stableswap/Cargo.toml +++ b/pallets/stableswap/Cargo.toml @@ -1,6 +1,6 @@ [package] name = 'pallet-stableswap' -version = '2.0.1' +version = '2.0.2' description = 'AMM for correlated assets' authors = ['GalacticCouncil'] edition = '2021' diff --git a/pallets/stableswap/src/tests/update_pool.rs b/pallets/stableswap/src/tests/update_pool.rs index c755c9209..1a9c6338a 100644 --- a/pallets/stableswap/src/tests/update_pool.rs +++ b/pallets/stableswap/src/tests/update_pool.rs @@ -272,10 +272,7 @@ fn set_tradable_state_should_work_when_asset_in_pool() { Tradability::FROZEN, )); - assert_eq!( - >::get(pool_id, asset_a), - Tradability::FROZEN, - ); + assert_eq!(>::get(pool_id, asset_a), Tradability::FROZEN,); }); } #[test] @@ -300,13 +297,10 @@ fn set_tradable_state_should_fail_when_asset_not_in_pool() { Permill::from_percent(0), )); - assert_noop!(Stableswap::set_asset_tradable_state( - RuntimeOrigin::root(), - pool_id, - 3, - Tradability::FROZEN, - ), - Error::::AssetNotInPool); + assert_noop!( + Stableswap::set_asset_tradable_state(RuntimeOrigin::root(), pool_id, 3, Tradability::FROZEN,), + Error::::AssetNotInPool + ); }); } #[test] @@ -317,12 +311,9 @@ fn set_tradable_state_should_fail_when_pool_does_not_exist() { .with_registered_asset("pool".as_bytes().to_vec(), pool_id) .build() .execute_with(|| { - assert_noop!(Stableswap::set_asset_tradable_state( - RuntimeOrigin::root(), - pool_id, - 1, - Tradability::FROZEN, - ), - Error::::PoolNotFound); + assert_noop!( + Stableswap::set_asset_tradable_state(RuntimeOrigin::root(), pool_id, 1, Tradability::FROZEN,), + Error::::PoolNotFound + ); }); -} \ No newline at end of file +}