Skip to content

Commit

Permalink
Merge pull request #258 from galacticcouncil/xyk-storage-query
Browse files Browse the repository at this point in the history
chore: change xyk storage value query to option query
  • Loading branch information
mrq1911 authored Nov 23, 2021
2 parents d7eee2b + 169566f commit b88d696
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
16 changes: 6 additions & 10 deletions pallets/xyk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,18 @@ pub mod pallet {
/// Asset id storage for shared pool tokens
#[pallet::storage]
#[pallet::getter(fn share_token)]
pub type ShareToken<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, AssetId, ValueQuery>;
pub(crate) type ShareToken<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, AssetId, ValueQuery>;

/// Total liquidity in a pool.
#[pallet::storage]
#[pallet::getter(fn total_liquidity)]
pub type TotalLiquidity<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, Balance, ValueQuery>;
pub(crate) type TotalLiquidity<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, Balance, ValueQuery>;

/// Asset pair in a pool.
#[pallet::storage]
#[pallet::getter(fn pool_assets)]
pub type PoolAssets<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, (AssetId, AssetId), ValueQuery>;
pub(crate) type PoolAssets<T: Config> =
StorageMap<_, Blake2_128Concat, T::AccountId, (AssetId, AssetId), OptionQuery>;

#[pallet::call]
impl<T: Config> Pallet<T> {
Expand Down Expand Up @@ -606,13 +607,8 @@ impl<T: Config> AMM<T::AccountId, AssetId, AssetPair, Balance> for Pallet<T> {
}

fn get_pool_assets(pool_account_id: &T::AccountId) -> Option<Vec<AssetId>> {
match <PoolAssets<T>>::contains_key(pool_account_id) {
true => {
let assets = Self::pool_assets(pool_account_id);
Some(vec![assets.0, assets.1])
}
false => None,
}
let maybe_assets = <PoolAssets<T>>::get(pool_account_id);
maybe_assets.map(|assets| vec![assets.0, assets.1])
}

fn get_spot_price_unchecked(asset_a: AssetId, asset_b: AssetId, amount: Balance) -> Balance {
Expand Down
2 changes: 1 addition & 1 deletion pallets/xyk/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ pub struct Disallow10_10Pool();

impl CanCreatePool<AssetId> for Disallow10_10Pool {
fn can_create(asset_a: AssetId, asset_b: AssetId) -> bool {
!matches!((asset_a, asset_b), (10u32,10u32))
!matches!((asset_a, asset_b), (10u32, 10u32))
}
}

Expand Down
2 changes: 2 additions & 0 deletions pallets/xyk/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ fn create_pool_should_work() {
});
let share_token = XYK::share_token(pair_account);

assert_eq!(XYK::get_pool_assets(&pair_account), Some(vec![asset_a, asset_b]));

assert_eq!(Currency::free_balance(asset_a, &pair_account), 100000000000000);
assert_eq!(Currency::free_balance(asset_b, &pair_account), 1000000000000000);
assert_eq!(Currency::free_balance(asset_a, &ALICE), 900000000000000);
Expand Down

0 comments on commit b88d696

Please sign in to comment.