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

source§

impl Signed<Decimal256>

source

pub const MAX: Self = _

The maximum allowed

source

pub const MIN: Number = _

The minimum allowed

source

pub const ONE: Number = _

1 as a Number

source

pub const NEG_ONE: Number = _

-1 as a Number

source

pub const ZERO: Number = _

0 as a Number

source

pub const EPS_E7: Number = _

Default epsilon used for approximate comparisons

source

pub const EPS_E6: Number = _

An alternate epsilon that can be used for approximate comparisons

source

pub const EPS_E17: Number = _

Another alternate epsilon that can be used for approximate comparisons where the rounding error is due to the Decimal256 representation as opposed to, say, token precision

source§

impl<T: UnsignedDecimal> Signed<T>

source

pub fn abs(self) -> Self

absolute value

source

pub fn abs_unsigned(self) -> T

Absolute value, but return the T underlying type directly

source

pub fn is_strictly_positive(&self) -> bool

Checks if this number is greater than 0.

source

pub fn is_positive_or_zero(&self) -> bool

Checks if this number is greater than or equal to 0.

source

pub fn is_zero(&self) -> bool

Is the value 0?

source

pub fn map<U: UnsignedDecimal, F: FnOnce(T) -> U>(self, f: F) -> Signed<U>

Apply a function to the inner value and rewrap.

This will keep the current sign (positive or negative) in place, respecting invariants that a value of 0 must have negative set to false.

source

pub fn try_map<E, U: UnsignedDecimal, F: FnOnce(T) -> Result<U, E>>( self, f: F, ) -> Result<Signed<U>, E>

Like map but may fail

source§

impl<T: UnsignedDecimal> Signed<T>

source

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

Addition that checks for integer overflow.

source

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

Subtraction that checks for underflow

source§

impl<T: UnsignedDecimal> Signed<T>

source

pub fn is_negative(&self) -> bool

Strictly less than 0, returns false on 0

source

pub fn into_number(self) -> Signed<Decimal256>

Convert into a general purpose Number.

source

pub fn from_number(src: Signed<Decimal256>) -> Self

convert from a general purpose Number.

source

pub fn zero() -> Self

The value 0

source

pub fn two() -> Self

The value 2

source

pub fn try_into_non_negative_value(self) -> Option<T>

If the value is positive or zero, return the inner T. Otherwise return None.

source

pub fn try_into_non_zero(self) -> Option<NonZero<T>>

Try to convert into a non-zero value

source§

impl<T: UnsignedDecimal> Signed<T>

source

pub fn checked_mul_number(self, rhs: Signed<Decimal256>) -> Result<Self>

Multiply by a raw number

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.
source§

impl<T: UnsignedDecimal> Add for Signed<T>

§

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

The resulting type after applying the + operator.
source§

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

Performs the + operation. Read more
source§

impl<T: Clone> Clone for Signed<T>

source§

fn clone(&self) -> Signed<T>

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<T: UnsignedDecimal> Debug for Signed<T>

source§

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

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

impl<T: UnsignedDecimal> Default for Signed<T>

source§

fn default() -> Self

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

impl<'de, T: UnsignedDecimal> Deserialize<'de> for Signed<T>

Deserializes as string for serde

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<T: UnsignedDecimal> Display for Signed<T>

source§

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

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

impl<T: UnsignedDecimal> From<NonZero<T>> for Signed<T>

source§

fn from(src: NonZero<T>) -> Self

Converts to this type from the input type.
source§

impl<T> From<T> for Signed<T>

source§

fn from(value: T) -> Self

Converts to this type from the input type.
source§

impl<T: UnsignedDecimal> From<u128> for Signed<T>

source§

fn from(val: u128) -> Self

Converts to this type from the input type.
source§

impl<T: UnsignedDecimal> From<u64> for Signed<T>

source§

fn from(val: u64) -> Self

Converts to this type from the input type.
source§

impl<T: UnsignedDecimal> FromStr for Signed<T>

source§

fn from_str(input: &str) -> Result<Self>

Converts the decimal string to a Number Possible inputs: “1.23”, “1”, “000012”, “1.123000000”, “-1.23” Disallowed: “”, “.23”

This never performs any kind of rounding. More than 18 fractional digits, even zeros, result in an error.

§

type Err = Error

The associated error which can be returned from parsing.
source§

impl<T: UnsignedDecimal> JsonSchema for Signed<T>

source§

fn schema_name() -> String

The name of the generated JSON Schema. 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§

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

Generates a JSON Schema for this type. Read more
§

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

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

impl<T: UnsignedDecimal> Neg for Signed<T>

§

type Output = Signed<T>

The resulting type after applying the - operator.
source§

fn neg(self) -> Self

Performs the unary - operation. Read more
source§

impl<T: UnsignedDecimal> Ord for Signed<T>

source§

fn cmp(&self, other: &Self) -> 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 + PartialOrd,

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

impl<T: PartialEq> PartialEq for Signed<T>

source§

fn eq(&self, other: &Signed<T>) -> 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<T: UnsignedDecimal> PartialOrd for Signed<T>

source§

fn partial_cmp(&self, other: &Self) -> 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

This method 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

This method 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

This method 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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T: UnsignedDecimal> Serialize for Signed<T>

Serializes as a string for serde

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<T: UnsignedDecimal> Sub for Signed<T>

§

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

The resulting type after applying the - operator.
source§

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

Performs the - operation. Read more
source§

impl<T: Copy> Copy for Signed<T>

source§

impl<T: Eq> Eq for Signed<T>

source§

impl<T> StructuralPartialEq for Signed<T>