pub struct Decimal {
pub value: u32,
pub decimal_multiplier: u8,
}Expand description
Decimal type for storing prices.
Fields§
§value: u32Value.
decimal_multiplier: u8Decimal multiplier.
Implementations§
Source§impl Decimal
impl Decimal
Sourcepub const MAX_DECIMALS: u8 = 20u8
pub const MAX_DECIMALS: u8 = 20u8
The Maximum Decimals.
Should satisfy MAX_DECIMALS <= 30.
Sourcepub const MAX_DECIMAL_MULTIPLIER: u8 = 20u8
pub const MAX_DECIMAL_MULTIPLIER: u8 = 20u8
The Maximum Decimal Multiplier,
which should satisfy u32::MAX * 10^{MAX_DECIMAL_MULTIPLIER} <= u128::MAX.
Sourcepub fn to_unit_price(&self) -> u128
pub fn to_unit_price(&self) -> u128
Returns the price of one unit (with decimals to be MAX_DECIMALS).
Sourcepub fn try_from_price(
price: u128,
decimals: u8,
token_decimals: u8,
precision: u8,
) -> Result<Self, DecimalError>
pub fn try_from_price( price: u128, decimals: u8, token_decimals: u8, precision: u8, ) -> Result<Self, DecimalError>
Create price decimal from the given price with decimals,
where token_decimals is the expected unit and with expected precision.
Sourcepub const fn decimal_multiplier_from_precision(
decimals: u8,
precision: u8,
) -> u8
pub const fn decimal_multiplier_from_precision( decimals: u8, precision: u8, ) -> u8
Calculate the decimal multiplier with the desired precision.
§Warning
One should check that decimals + precision is not greater than MAX_DECIMALS,
otherwise the result might be incorrect due to underflow.
Trait Implementations§
Source§impl BorshDeserialize for Decimal
impl BorshDeserialize for Decimal
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self, Error>
§fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>
Deserializes this instance from a given slice of bytes.
Updates the buffer to point at the remaining bytes.
§fn try_from_slice(v: &[u8]) -> Result<Self, Error>
fn try_from_slice(v: &[u8]) -> Result<Self, Error>
Deserialize this instance from a slice of bytes.
fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>where
R: Read,
impl Copy for Decimal
impl Eq for Decimal
impl StructuralPartialEq for Decimal
Auto Trait Implementations§
impl Freeze for Decimal
impl RefUnwindSafe for Decimal
impl Send for Decimal
impl Sync for Decimal
impl Unpin for Decimal
impl UnwindSafe for Decimal
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Checks if this value is equivalent to the given key. Read more
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more