Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 8 additions & 1 deletion pallets/asset-index/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub mod pallet {
use xcm::opaque::v0::MultiLocation;

use pallet_asset_depository::MultiAssetDepository;
use pallet_price_feed::{AssetPricePair, PriceFeed};
use pallet_price_feed::{AssetPricePair, Price, PriceFeed};
use pallet_remote_asset_manager::RemoteAssetManager;

use crate::traits::WithdrawalFee;
Expand Down Expand Up @@ -190,6 +190,13 @@ pub mod pallet {
) -> DispatchResultWithPostInfo {
T::AdminOrigin::ensure_origin(origin.clone())?;
let caller = ensure_signed(origin)?;

// Store intial price pair if not exists
T::PriceFeed::ensure_price(
asset_id,
Price::from_inner(value.saturating_mul(units).into()),
)?;

<Self as AssetRecorder<T::AssetId, T::Balance>>::add_asset(
&asset_id,
&units,
Expand Down
14 changes: 8 additions & 6 deletions pallets/asset-index/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,16 @@ pub const ASSET_B_PRICE_MULTIPLIER: Balance = 3;
pub struct MockPriceFeed;
impl PriceFeed<AssetId> for MockPriceFeed {
fn get_price(quote: AssetId) -> Result<AssetPricePair<AssetId>, DispatchError> {
if quote == UNKNOWN_ASSET_ID {
Err(pallet_asset_index::Error::<Test>::UnsupportedAsset.into())
} else {
Self::get_price_pair(PINT_ASSET_ID, quote)
}
Self::get_price_pair(PINT_ASSET_ID, quote)
}

fn get_price_pair(
base: AssetId,
quote: AssetId,
) -> Result<AssetPricePair<AssetId>, DispatchError> {
let price = match quote {
ASSET_A_ID => {
// includes unknown asset id since we don't need to mock initial price pair here
ASSET_A_ID | UNKNOWN_ASSET_ID => {
Price::checked_from_rational(600, 600 / ASSET_A_PRICE_MULTIPLIER).unwrap()
}
ASSET_B_ID => {
Expand All @@ -200,6 +197,11 @@ impl PriceFeed<AssetId> for MockPriceFeed {
};
Ok(AssetPricePair { base, quote, price })
}

fn ensure_price(_: AssetId, _: Price) -> Result<AssetPricePair<AssetId>, DispatchError> {
// pass all unknown asset ids
Self::get_price(UNKNOWN_ASSET_ID)
}
}

// Build genesis storage according to the mock runtime.
Expand Down
26 changes: 8 additions & 18 deletions pallets/asset-index/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,6 @@ fn deposit_works_with_user_balance() {
fn deposit_fails_for_unknown_assets() {
let initial_balances: Vec<(AccountId, Balance)> = vec![(ADMIN_ACCOUNT_ID, 0)];
new_test_ext(initial_balances).execute_with(|| {
assert_ok!(AssetIndex::add_asset(
Origin::signed(ADMIN_ACCOUNT_ID),
ASSET_A_ID,
100,
AssetAvailability::Liquid(MultiLocation::Null),
5
));
assert_noop!(
AssetIndex::deposit(Origin::signed(ASHLEY), UNKNOWN_ASSET_ID, 1_000),
pallet::Error::<Test>::UnsupportedAsset
Expand All @@ -163,7 +156,7 @@ fn deposit_fails_for_unknown_assets() {
}

#[test]
fn deposit_fails_for_when_price_feed_unavailable() {
fn deposit_ok_for_when_price_feed_unavailable() {
let initial_balances: Vec<(AccountId, Balance)> = vec![(ADMIN_ACCOUNT_ID, 0)];
new_test_ext(initial_balances).execute_with(|| {
assert_ok!(AssetIndex::add_asset(
Expand All @@ -173,10 +166,12 @@ fn deposit_fails_for_when_price_feed_unavailable() {
AssetAvailability::Liquid(MultiLocation::Null),
5
));
assert_noop!(
AssetIndex::deposit(Origin::signed(ASHLEY), UNKNOWN_ASSET_ID, 1_000),
pallet::Error::<Test>::UnsupportedAsset
);
assert_ok!(AssetDepository::deposit(&UNKNOWN_ASSET_ID, &ASHLEY, 1_000));
assert_ok!(AssetIndex::deposit(
Origin::signed(ASHLEY),
UNKNOWN_ASSET_ID,
1
),);
})
}

Expand All @@ -191,16 +186,11 @@ fn deposit_fails_on_overflowing() {
AssetAvailability::Liquid(MultiLocation::Null),
5
));
assert_ok!(AssetDepository::deposit(&ASSET_A_ID, &ASHLEY, Balance::MAX));

assert_noop!(
AssetIndex::deposit(Origin::signed(ASHLEY), ASSET_A_ID, Balance::MAX),
pallet::Error::<Test>::AssetVolumeOverflow
);
assert_ok!(AssetIndex::deposit(
Origin::signed(ASHLEY),
ASSET_A_ID,
1_000
));
})
}

Expand Down
29 changes: 29 additions & 0 deletions pallets/price-feed/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ pub mod pallet {
pub type AssetFeeds<T: Config> =
StorageMap<_, Blake2_128Concat, T::AssetId, FeedIdFor<T>, OptionQuery>;

#[pallet::storage]
/// (AssetId) -> AssetPricePair
///
/// This storage stores the initial price pair for quote assets based on `SelfAssetId`
///
/// * insert: adding a new asset with no price pair been set yet
pub type InitialPricePairs<T: Config> =
StorageMap<_, Blake2_128Concat, T::AssetId, AssetPricePair<T::AssetId>, OptionQuery>;

#[pallet::genesis_config]
pub struct GenesisConfig<T: Config>
where
Expand Down Expand Up @@ -272,6 +281,26 @@ pub mod pallet {

Ok(AssetPricePair { base, quote, price })
}

/// Ensure quote asset has an initial price pair
///
/// * Set initial price pair if feed not exists
fn ensure_price(
quote: T::AssetId,
price: Price,
) -> Result<AssetPricePair<T::AssetId>, DispatchError> {
let pair = AssetPricePair {
base: T::SelfAssetId::get(),
quote: quote.clone(),
price,
};

if Self::get_price_pair(T::SelfAssetId::get(), quote.clone()).is_err() {
<InitialPricePairs<T>>::insert(quote, pair.clone());
}

Ok(pair)
}
}

/// Trait for the asset-index pallet extrinsic weights.
Expand Down
6 changes: 5 additions & 1 deletion pallets/price-feed/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2021 ChainSafe Systems
// SPDX-License-Identifier: LGPL-3.0-only

use crate::types::AssetPricePair;
use crate::types::{AssetPricePair, Price};
use frame_support::dispatch::DispatchError;

/// An interface to access price data
Expand All @@ -14,4 +14,8 @@ pub trait PriceFeed<AssetId> {
base: AssetId,
quote: AssetId,
) -> Result<AssetPricePair<AssetId>, DispatchError>;

/// Set initial price pair if feed not exists
fn ensure_price(quote: AssetId, units: Price)
-> Result<AssetPricePair<AssetId>, DispatchError>;
}