Struct pyth_sdk_solana::Price

source ·
pub struct Price {
    pub price: i64,
    pub conf: u64,
    pub expo: i32,
    pub publish_time: i64,
}
Expand description

A price with a degree of uncertainty at a certain time, represented as a price +- a confidence interval.

Please refer to the documentation at https://docs.pyth.network/consumers/best-practices for using this price safely.

The confidence interval roughly corresponds to the standard error of a normal distribution. Both the price and confidence are stored in a fixed-point numeric representation, x * 10^expo, where expo is the exponent. For example:

use pyth_sdk::Price;
Price { price: 12345, conf: 267, expo: -2, publish_time: 100 }; // represents 123.45 +- 2.67 published at UnixTimestamp 100
Price { price: 123, conf: 1, expo: 2,  publish_time: 100 }; // represents 12300 +- 100 published at UnixTimestamp 100

Price supports a limited set of mathematical operations. All of these operations will propagate any uncertainty in the arguments into the result. However, the uncertainty in the result may overestimate the true uncertainty (by at most a factor of sqrt(2)) due to computational limitations. Furthermore, all of these operations may return None if their result cannot be represented within the numeric representation (e.g., the exponent is so small that the price does not fit into an i64). Users of these methods should (1) select their exponents to avoid this problem, and (2) handle the None case gracefully.

Fields§

§price: i64

Price.

§conf: u64

Confidence interval.

§expo: i32

Exponent.

§publish_time: i64

Publish time.

Implementations§

source§

impl Price

source

pub fn get_price_in_quote(&self, quote: &Price, result_expo: i32) -> Option<Price>

Get the current price of this account in a different quote currency.

If this account represents the price of the product X/Z, and quote represents the price of the product Y/Z, this method returns the price of X/Y. Use this method to get the price of e.g., mSOL/SOL from the mSOL/USD and SOL/USD accounts.

result_expo determines the exponent of the result, i.e., the number of digits below the decimal point. This method returns None if either the price or confidence are too large to be represented with the requested exponent.

Example:

let btc_usd: Price = ...;
let eth_usd: Price = ...;
// -8 is the desired exponent for the result
let btc_eth: Price = btc_usd.get_price_in_quote(&eth_usd, -8);
println!("BTC/ETH price: ({} +- {}) x 10^{}", price.price, price.conf, price.expo);
source

pub fn price_basket(
amounts: &[(Price, i64, i32)],
result_expo: i32
) -> Option<Price>

Get the price of a basket of currencies.

Each entry in amounts is of the form (price, qty, qty_expo), and the result is the sum of price * qty * 10^qty_expo. The result is returned with exponent result_expo.

An example use case for this function is to get the value of an LP token.

Example:

let btc_usd: Price = ...;
let eth_usd: Price = ...;
// Quantity of each asset in fixed-point a * 10^e.
// This represents 0.1 BTC and .05 ETH.
// -8 is desired exponent for result
let basket_price: Price = Price::price_basket(&[
    (btc_usd, 10, -2),
    (eth_usd, 5, -2)
  ], -8);
println!("0.1 BTC and 0.05 ETH are worth: ({} +- {}) x 10^{} USD",
         basket_price.price, basket_price.conf, basket_price.expo);
source

pub fn div(&self, other: &Price) -> Option<Price>

Divide this price by other while propagating the uncertainty in both prices into the result.

This method will automatically select a reasonable exponent for the result. If both self and other are normalized, the exponent is self.expo + PD_EXPO - other.expo (i.e., the fraction has PD_EXPO digits of additional precision). If they are not normalized, this method will normalize them, resulting in an unpredictable result exponent. If the result is used in a context that requires a specific exponent, please call scale_to_exponent on it.

source

pub fn add(&self, other: &Price) -> Option<Price>

Add other to this, propagating uncertainty in both prices.

Requires both Prices to have the same exponent – use scale_to_exponent on the arguments if necessary.

TODO: could generalize this method to support different exponents.

source

pub fn cmul(&self, c: i64, e: i32) -> Option<Price>

Multiply this Price by a constant c * 10^e.

source

pub fn mul(&self, other: &Price) -> Option<Price>

Multiply this Price by other, propagating any uncertainty.

source

pub fn normalize(&self) -> Option<Price>

Get a copy of this struct where the price and confidence have been normalized to be between MIN_PD_V_I64 and MAX_PD_V_I64.

source

pub fn scale_to_exponent(&self, target_expo: i32) -> Option<Price>

Scale this price/confidence so that its exponent is target_expo.

Return None if this number is outside the range of numbers representable in target_expo, which will happen if target_expo is too small.

Warning: if target_expo is significantly larger than the current exponent, this function will return 0 +- 0.

Trait Implementations§

source§

impl BorshDeserialize for Pricewhere
i64: BorshDeserialize,
u64: BorshDeserialize,
i32: BorshDeserialize,

source§

fn deserialize(buf: &mut &[u8]) -> Result<Price, Error>

Deserializes this instance from a given slice of bytes. Updates the buffer to point at the remaining bytes.
source§

fn try_from_slice(v: &[u8]) -> Result<Self, Error>

Deserialize this instance from a slice of bytes.
source§

impl BorshSerialize for Pricewhere
i64: BorshSerialize,
u64: BorshSerialize,
i32: BorshSerialize,

source§

fn serialize<W>(&self, writer: &mut W) -> Result<(), Error>where
W: Write,

source§

fn try_to_vec(&self) -> Result<Vec<u8, Global>, Error>

Serialize this instance into a vector of bytes.
source§

impl Clone for Price

source§

fn clone(&self) -> Price

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Price

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Default for Price

source§

fn default() -> Price

Returns the “default value” for a type. Read more
source§

impl<'de> Deserialize<'de> for Price

source§

fn deserialize<__D>(
__deserializer: __D
) -> Result<Price, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl JsonSchema for Price

source§

fn schema_name() -> String

The name of the generated JSON Schema. Read more
source§

fn json_schema(gen: &mut SchemaGenerator) -> Schema

Generates a JSON Schema for this type. Read more
§

fn is_referenceable() -> bool

Whether JSON Schemas generated for this type should be re-used where possible using the $ref keyword. Read more
source§

impl PartialEq<Price> for Price

source§

fn eq(&self, other: &Price) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for Price

source§

fn serialize<__S>(
&self,
__serializer: __S
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Copy for Price

source§

impl Eq for Price

source§

impl StructuralEq for Price

source§

impl StructuralPartialEq for Price

Auto Trait Implementations§

§

impl RefUnwindSafe for Price

§

impl Send for Price

§

impl Sync for Price

§

impl Unpin for Price

§

impl UnwindSafe for Price

Blanket Implementations§

§

impl<T> AbiEnumVisitor for Twhere
T: Serialize + ?Sized,

§

default fn visit_for_abi(
&self,
_digester: &mut AbiDigester
) -> Result<AbiDigester, DigestError>

§

impl<T> AbiEnumVisitor for Twhere
T: Serialize + AbiExample + ?Sized,

§

default fn visit_for_abi(
&self,
digester: &mut AbiDigester
) -> Result<AbiDigester, DigestError>

§

impl<T> AbiExample for T

§

default fn example() -> T

source§

impl<T> Any for Twhere
T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> DynClone for Twhere
T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere
U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere
T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere
U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere
V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,