Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
79bea4b
chore(deps): cargo update --workspace
mattsse Apr 15, 2021
7aacf06
fix(deps): update renamed xcm crate names
mattsse Apr 15, 2021
c3799e4
fix: rename ModuleId to PalletId
mattsse Apr 15, 2021
7b3b477
feat:pallets/orcale: add oracle pallet template
mattsse Apr 9, 2021
9c566a6
feat:pallets/orcale: rename to price feed and include chainlink feed
mattsse Apr 15, 2021
82f4b46
feat:pallets/price-feed: integrate latest chainlink price feed pallet
mattsse Apr 15, 2021
09c2662
rustfmt
mattsse Apr 15, 2021
e5325d5
chore(deps): sync with chainlink deps
mattsse Apr 15, 2021
82ff8c8
chore(deps): sort members
mattsse Apr 16, 2021
39edc21
feat:pallets/price-feed: add types
mattsse Apr 16, 2021
2a07bf4
feat:pallets/price-feed: introduce price feed types and price feed trait
mattsse Apr 16, 2021
f0bd7e8
feat:pallets/price-feed: empty price feed trait implementation
mattsse Apr 16, 2021
6112a62
rustfmt
mattsse Apr 16, 2021
7851a9f
chore(deps): enable std feature for balances
mattsse Apr 16, 2021
ba70d39
feat:pallets/price-feed: add mock config
mattsse Apr 16, 2021
1946262
docs:pallets/price-feed: clarify notes on precision
mattsse Apr 16, 2021
1d609bc
test:pallets/price-feed: add basic feed creation test
mattsse Apr 16, 2021
b7ace6d
rustfmt
mattsse Apr 16, 2021
92bc95e
Update pallets/price-feed/src/lib.rs
mattsse Apr 19, 2021
b36ae3f
feat:pallets/price-feed: add mapping from assetId to feedId
mattsse Apr 19, 2021
d23ca01
feat:pallets/price-feed: add genesis build support
mattsse Apr 19, 2021
b7e8fea
refactor:pallets/price-feed: replace generic price type with FixedU128
mattsse Apr 19, 2021
a23ba9c
feat:pallets/price-feed: implement get_price_pair
mattsse Apr 19, 2021
7007279
rustfmt
mattsse Apr 19, 2021
bb074a6
chore(clippy): make clippy happy
mattsse Apr 19, 2021
f1b60ab
chore(deps): update chainlink feed pallet
mattsse Apr 19, 2021
9763582
feat:pallets/price-feed: drop unused type
mattsse Apr 19, 2021
549f8f0
test:pallets/price-feed: update mock template
mattsse Apr 19, 2021
46aef80
test:pallets/price-feed: add price pair tests
mattsse Apr 19, 2021
70444ee
test:pallets/price-feed: reexport feedbuilder for tests
mattsse Apr 19, 2021
0b3cc33
test:pallets/price-feed: add price feed tests
mattsse Apr 19, 2021
0b62072
chore:pallets/price-feed: remove commented out config typ eartefacts
mattsse Apr 19, 2021
fbf1c66
Merge branch 'main' into matt/add-price-feed
mattsse Apr 22, 2021
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
Prev Previous commit
Next Next commit
feat:pallets/price-feed: introduce price feed types and price feed trait
  • Loading branch information
mattsse committed Apr 16, 2021
commit 2a07bf4bda7d1ffd9048ce130ed04aaed84afff5
17 changes: 17 additions & 0 deletions pallets/price-feed/src/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2021 ChainSafe Systems
// SPDX-License-Identifier: LGPL-3.0-only

use crate::types::AssetPricePair;
use frame_support::dispatch::DispatchError;

/// An interface to access price data
pub trait PriceFeed<AssetId, Balance> {
/// Returns the current price pair for `base/quote` where `base` is the native token
fn get_price(quote: AssetId) -> Result<AssetPricePair<AssetId, Balance>, DispatchError>;

/// Returns the current price pair for `base/quote`
fn get_price_pair(
base: AssetId,
quote: AssetId,
) -> Result<AssetPricePair<AssetId, Balance>, DispatchError>;
}
74 changes: 74 additions & 0 deletions pallets/price-feed/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2021 ChainSafe Systems
// SPDX-License-Identifier: LGPL-3.0-only

use frame_support::pallet_prelude::*;
use frame_support::sp_runtime::traits::AtLeast32BitUnsigned;
use frame_support::sp_runtime::{PerThing, SaturatedConversion};

/// Defines an asset pair identifier
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
pub struct AssetPricePair<AssetId, Price> {
/// The base asset id of this pair.
pub base: AssetId,
/// The quote asset
pub quote: AssetId,
/// The price for `base/quote`
price: Price,
}

impl<AssetId, Price> AssetPricePair<AssetId, Price>
where
AssetId: Member,
Price: PerThing,
{
/// Whether this pair involves the `asset`
pub fn involves_asset(&self, asset: &AssetId) -> bool {
self.is_base(asset) || self.is_quote(asset)
}

/// Whether the provided asset is the `base` asset of this price pair
pub fn is_base(&self, asset: &AssetId) -> bool {
self.base == *asset
}

/// Whether the provided asset is the `quote` asset of this price pair
pub fn is_quote(&self, asset: &AssetId) -> bool {
self.quote == *asset
}

/// Returns the price fraction `base/quote`
pub fn price(&self) -> &Price {
&self.price
}

/// Calculates the total volume of the provided units of the `quote` assetId w.r.t. price pair
pub fn volume<N>(&self, units: N) -> Price
where
N: Into<Price>,
{
self.price * Price::saturated_from(units)
}

/// Calculates the total volume of the provided units of the `base` assetId w.r.t. price pair
pub fn reciprocal_volume<N>(&self, units: N) -> u128
where
N: Into<u128>,
{
self.price.saturating_reciprocal_mul(units.into())
}
}

impl<AssetId, Price> AssetPricePair<AssetId, Price>
where
AssetId: Member,
Price: PerThing + From<u128>,
{
/// Turns this price pair of `base/quote` into a price pair of `quote/base`
pub fn invert(self) -> Self {
Self {
base: self.quote,
quote: self.base,
price: self.price.saturating_reciprocal_mul(1).into(),
}
}
}