Type Alias levana_perpswap_cosmos::number::Number

source ·
pub type Number = Signed<Decimal256>;
Expand description

A signed number type with high fidelity.

Similar in spirit to cosmwasm_bignumber::Decimal256 - it is a more ergonomic wrapper around cosmwasm-std by making more things public but we also add negative values and other methods as-needed

MANY OF THE METHODS ARE COPY/PASTE FROM cosmwasm_std the hope is that this is a temporary hack until cosmwasm_math lands

Aliased Type§

struct Number { /* private fields */ }

Implementations§

source§

impl Number

source

pub fn from_ratio_u256<A: Into<Uint256>, B: Into<Uint256>>( numerator: A, denominator: B, ) -> Self

Returns the ratio (nominator / denominator) as a positive Number

source

pub fn to_u128_with_precision(&self, precision: u32) -> Option<u128>

Represent as a u128 encoded with given decimal places

NOTE decimals may be dropped if precision isn’t sufficient to represent all digits completely

source

pub fn from_fixed_u128(amount: u128, places: u32) -> Self

helper to get from native currency to Number e.g. from uusd to UST, as a Decimal

source

pub fn to_unsigned_key_bytes(&self) -> Option<[u8; 32]>

Useful for when Number is used as a PrimaryKey and is guaranteed to always be positive

source

pub fn from_unsigned_key_bytes(bytes: [u8; 32]) -> Self

Round-tripping with Self::to_unsigned_key_bytes

source§

impl Number

source

pub fn checked_mul(self, rhs: Self) -> Result<Self>

Multiplication that checks for integer overflow

source

pub fn checked_div(self, rhs: Self) -> Result<Self>

Division that checks for underflow and divide-by-zero.

source

pub fn approx_eq(self, other: Number) -> Result<bool>

equality check with allowance for precision diff

source

pub fn approx_eq_eps(self, other: Number, eps: Number) -> Result<bool>

equality check with allowance for precision diff

source

pub fn approx_lt_relaxed(self, other: Number) -> Result<bool>

less-than with allowance for precision diff

source

pub fn approx_gt_relaxed(self, other: Number) -> Result<bool>

greater-than with allowance for precision diff

source

pub fn approx_gt_strict(self, other: Number) -> Result<bool>

greater-than with restriction for precision diff

source§

impl Number

source

pub fn to_pyth_price( &self, conf: u64, publish_time: UnixTimestamp, ) -> Result<Price>

Converts a Number into a pyth price the exponent will always be 0 or negative

Trait Implementations§

source§

impl Div<u64> for Number

§

type Output = Result<Signed<Decimal256>, Error>

The resulting type after applying the / operator.
source§

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

Performs the / operation. Read more
source§

impl Div for Number

§

type Output = Result<Signed<Decimal256>, Error>

The resulting type after applying the / operator.
source§

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

Performs the / operation. Read more
source§

impl Mul<u64> for Number

§

type Output = Result<Signed<Decimal256>, Error>

The resulting type after applying the * operator.
source§

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

Performs the * operation. Read more
source§

impl Mul for Number

§

type Output = Result<Signed<Decimal256>, Error>

The resulting type after applying the * operator.
source§

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

Performs the * operation. Read more
source§

impl TryFrom<&str> for Number

§

type Error = Error

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

fn try_from(val: &str) -> Result<Self>

Performs the conversion.
source§

impl TryFrom<Price> for Number

§

type Error = Error

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

fn try_from(price: Price) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<String> for Number

§

type Error = Error

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

fn try_from(val: String) -> Result<Self>

Performs the conversion.