-
Notifications
You must be signed in to change notification settings - Fork 9
Add price feed pallet #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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 7aacf06
fix(deps): update renamed xcm crate names
mattsse c3799e4
fix: rename ModuleId to PalletId
mattsse 7b3b477
feat:pallets/orcale: add oracle pallet template
mattsse 9c566a6
feat:pallets/orcale: rename to price feed and include chainlink feed
mattsse 82f4b46
feat:pallets/price-feed: integrate latest chainlink price feed pallet
mattsse 09c2662
rustfmt
mattsse e5325d5
chore(deps): sync with chainlink deps
mattsse 82ff8c8
chore(deps): sort members
mattsse 39edc21
feat:pallets/price-feed: add types
mattsse 2a07bf4
feat:pallets/price-feed: introduce price feed types and price feed trait
mattsse f0bd7e8
feat:pallets/price-feed: empty price feed trait implementation
mattsse 6112a62
rustfmt
mattsse 7851a9f
chore(deps): enable std feature for balances
mattsse ba70d39
feat:pallets/price-feed: add mock config
mattsse 1946262
docs:pallets/price-feed: clarify notes on precision
mattsse 1d609bc
test:pallets/price-feed: add basic feed creation test
mattsse b7ace6d
rustfmt
mattsse 92bc95e
Update pallets/price-feed/src/lib.rs
mattsse b36ae3f
feat:pallets/price-feed: add mapping from assetId to feedId
mattsse d23ca01
feat:pallets/price-feed: add genesis build support
mattsse b7e8fea
refactor:pallets/price-feed: replace generic price type with FixedU128
mattsse a23ba9c
feat:pallets/price-feed: implement get_price_pair
mattsse 7007279
rustfmt
mattsse bb074a6
chore(clippy): make clippy happy
mattsse f1b60ab
chore(deps): update chainlink feed pallet
mattsse 9763582
feat:pallets/price-feed: drop unused type
mattsse 549f8f0
test:pallets/price-feed: update mock template
mattsse 46aef80
test:pallets/price-feed: add price pair tests
mattsse 70444ee
test:pallets/price-feed: reexport feedbuilder for tests
mattsse 0b3cc33
test:pallets/price-feed: add price feed tests
mattsse 0b62072
chore:pallets/price-feed: remove commented out config typ eartefacts
mattsse fbf1c66
Merge branch 'main' into matt/add-price-feed
mattsse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat:pallets/price-feed: introduce price feed types and price feed trait
- Loading branch information
commit 2a07bf4bda7d1ffd9048ce130ed04aaed84afff5
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>, | ||
mattsse marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| /// 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(), | ||
| } | ||
| } | ||
| } | ||
mattsse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.