Trait ProvideAssetPrice

Source
pub trait ProvideAssetPrice {
    type AssetId;
    type Price: FixedPointNumber;

    // Required method
    fn get_price(asset_id: Self::AssetId) -> Option<Self::Price>;

    // Provided methods
    fn calculate_decimals_aware_price(
        original_price: Self::Price,
        usd_decimals: u8,
        asset_decimals: u8,
    ) -> Option<Self::Price> { ... }
    fn convert_back_to_normal_price(
        decimals_aware_price: Self::Price,
        usd_decimals: u8,
        asset_decimals: u8,
    ) -> Option<Self::Price> { ... }
    fn get_decimals_aware_price(
        asset_id: Self::AssetId,
        usd_decimals: u8,
        asset_decimals: u8,
    ) -> Option<Self::Price> { ... }
}

Required Associated Types§

Required Methods§

Source

fn get_price(asset_id: Self::AssetId) -> Option<Self::Price>

Gets the price of an asset.

Returns None if the price is not available.

Provided Methods§

Source

fn calculate_decimals_aware_price( original_price: Self::Price, usd_decimals: u8, asset_decimals: u8, ) -> Option<Self::Price>

Prices define the relationship between USD/Asset. When to and from that asset, we need to be aware that they might have different decimals. This function calculates the relationship having in mind the decimals. For example: if the price is 2.5, our underlying USD unit has 6 decimals, and the asset has 8 decimals, the price will be calculated like so: (2.5USD * 10^6) / (1 * 10^8) = 0.025. And so if we want to convert 20 of the asset to USD, we would do 0.025(USD/Asset)FixedPointNumber * 20_000_000_00(Asset)u128 = 50_000_000 which is 50 USD with 6 decimals

Source

fn convert_back_to_normal_price( decimals_aware_price: Self::Price, usd_decimals: u8, asset_decimals: u8, ) -> Option<Self::Price>

Source

fn get_decimals_aware_price( asset_id: Self::AssetId, usd_decimals: u8, asset_decimals: u8, ) -> Option<Self::Price>

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§