Struct NormalizedPrice

Source
pub struct NormalizedPrice(/* private fields */);
Expand description

NormalizedPrice should be used in all operations involving calculating the value of coins given the oracle price. When comparing values of non-standard denominations, failing to normalize the price can cause unexpected and incorrect results.

Standard denominations have 6 decimal places, so we use that as the reference point.

Implementations§

Source§

impl NormalizedPrice

Source

pub fn unsafe_unchecked(price: Decimal) -> Self

This is unsafe because it does not check that the price is normalized to the reference decimal places. Most likely during testing.

Source

pub fn from_raw(price: Decimal, decimals: u8) -> Self

Source

pub fn from_delta(price: Decimal, delta: i16) -> Self

Source

pub fn from_oracle<T: Into<String>>( querier: &KujiraQuerier<'_>, denom: T, decimals: u8, ) -> StdResult<Self>

Source

pub fn inner(&self) -> Decimal

Methods from Deref<Target = Decimal>§

Source

pub const DECIMAL_PLACES: u32 = 18u32

Source

pub const MAX: Decimal

Source

pub const MIN: Decimal

Source

pub fn is_zero(&self) -> bool

Source

pub fn atomics(&self) -> Uint128

A decimal is an integer of atomic units plus a number that specifies the position of the decimal dot. So any decimal can be expressed as two numbers.

§Examples
// Value with whole and fractional part
let a = Decimal::from_str("1.234").unwrap();
assert_eq!(a.decimal_places(), 18);
assert_eq!(a.atomics(), Uint128::new(1234000000000000000));

// Smallest possible value
let b = Decimal::from_str("0.000000000000000001").unwrap();
assert_eq!(b.decimal_places(), 18);
assert_eq!(b.atomics(), Uint128::new(1));
Source

pub fn decimal_places(&self) -> u32

The number of decimal places. This is a constant value for now but this could potentially change as the type evolves.

See also Decimal::atomics().

Source

pub fn floor(&self) -> Decimal

Rounds value down after decimal places.

Source

pub fn ceil(&self) -> Decimal

Rounds value up after decimal places. Panics on overflow.

Source

pub fn checked_ceil(&self) -> Result<Decimal, RoundUpOverflowError>

Rounds value up after decimal places. Returns OverflowError on overflow.

Source

pub fn sqrt(&self) -> Decimal

Returns the approximate square root as a Decimal.

This should not overflow or panic.

Trait Implementations§

Source§

impl Clone for NormalizedPrice

Source§

fn clone(&self) -> NormalizedPrice

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 NormalizedPrice

Source§

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

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

impl Deref for NormalizedPrice

Source§

type Target = Decimal

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'de> Deserialize<'de> for NormalizedPrice

Source§

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

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

impl Div<NormalizedPrice> for Uint128

Source§

type Output = Option<Uint128>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: NormalizedPrice) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<Uint128> for NormalizedPrice

Source§

type Output = Option<Uint128>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Uint128) -> Self::Output

Performs the / operation. Read more
Source§

impl Div for NormalizedPrice

Source§

type Output = NormalizedPrice

The resulting type after applying the / operator.
Source§

fn div(self, rhs: NormalizedPrice) -> Self::Output

Performs the / operation. Read more
Source§

impl DivAssign for NormalizedPrice

Source§

fn div_assign(&mut self, rhs: NormalizedPrice)

Performs the /= operation. Read more
Source§

impl From<NormalizedPrice> for Decimal

Source§

fn from(price: NormalizedPrice) -> Self

Converts to this type from the input type.
Source§

impl JsonSchema for NormalizedPrice

Source§

fn schema_name() -> String

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

fn schema_id() -> Cow<'static, str>

Returns a string that uniquely identifies the schema produced by this type. Read more
Source§

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

Generates a JSON Schema for this type. Read more
Source§

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 Mul<NormalizedPrice> for Uint128

Source§

type Output = Uint128

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: NormalizedPrice) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Uint128> for NormalizedPrice

Source§

type Output = Uint128

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Uint128) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul for NormalizedPrice

Source§

type Output = NormalizedPrice

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: NormalizedPrice) -> Self::Output

Performs the * operation. Read more
Source§

impl MulAssign<NormalizedPrice> for Uint128

Source§

fn mul_assign(&mut self, rhs: NormalizedPrice)

Performs the *= operation. Read more
Source§

impl MulAssign for NormalizedPrice

Source§

fn mul_assign(&mut self, rhs: NormalizedPrice)

Performs the *= operation. Read more
Source§

impl Ord for NormalizedPrice

Source§

fn cmp(&self, other: &NormalizedPrice) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for NormalizedPrice

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for NormalizedPrice

Source§

fn partial_cmp(&self, other: &NormalizedPrice) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for NormalizedPrice

Source§

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

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

impl Copy for NormalizedPrice

Source§

impl Eq for NormalizedPrice

Source§

impl StructuralPartialEq for NormalizedPrice

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<U> As for U

Source§

fn as_<T>(self) -> T
where T: CastFrom<U>,

Casts self to type T. The semantics of numeric casting with the as operator are followed, so <T as As>::as_::<U> can be used in the same way as T as U for numeric conversions. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

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

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

fn vzip(self) -> V

Source§

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

Source§

impl<T, Rhs, Output> ScalarMul<Rhs, Output> for T
where T: Mul<Rhs, Output = Output> + MulAssign<Rhs>,