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
impl Number
sourcepub fn from_ratio_u256<A: Into<Uint256>, B: Into<Uint256>>(
numerator: A,
denominator: B,
) -> Self
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
sourcepub fn to_u128_with_precision(&self, precision: u32) -> Option<u128>
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
sourcepub fn from_fixed_u128(amount: u128, places: u32) -> Self
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
sourcepub fn to_unsigned_key_bytes(&self) -> Option<[u8; 32]>
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
sourcepub fn from_unsigned_key_bytes(bytes: [u8; 32]) -> Self
pub fn from_unsigned_key_bytes(bytes: [u8; 32]) -> Self
Round-tripping with Self::to_unsigned_key_bytes
source§impl Number
impl Number
sourcepub fn checked_mul(self, rhs: Self) -> Result<Self>
pub fn checked_mul(self, rhs: Self) -> Result<Self>
Multiplication that checks for integer overflow
sourcepub fn checked_div(self, rhs: Self) -> Result<Self>
pub fn checked_div(self, rhs: Self) -> Result<Self>
Division that checks for underflow and divide-by-zero.
sourcepub fn approx_eq(self, other: Number) -> Result<bool>
pub fn approx_eq(self, other: Number) -> Result<bool>
equality check with allowance for precision diff
sourcepub fn approx_eq_eps(self, other: Number, eps: Number) -> Result<bool>
pub fn approx_eq_eps(self, other: Number, eps: Number) -> Result<bool>
equality check with allowance for precision diff
sourcepub fn approx_lt_relaxed(self, other: Number) -> Result<bool>
pub fn approx_lt_relaxed(self, other: Number) -> Result<bool>
less-than with allowance for precision diff
sourcepub fn approx_gt_relaxed(self, other: Number) -> Result<bool>
pub fn approx_gt_relaxed(self, other: Number) -> Result<bool>
greater-than with allowance for precision diff
sourcepub fn approx_gt_strict(self, other: Number) -> Result<bool>
pub fn approx_gt_strict(self, other: Number) -> Result<bool>
greater-than with restriction for precision diff
source§impl Number
impl Number
sourcepub fn to_pyth_price(
&self,
conf: u64,
publish_time: UnixTimestamp,
) -> Result<Price>
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>
impl Signed<Decimal256>
source§impl<T: UnsignedDecimal> Signed<T>
impl<T: UnsignedDecimal> Signed<T>
sourcepub fn abs_unsigned(self) -> T
pub fn abs_unsigned(self) -> T
Absolute value, but return the T
underlying type directly
sourcepub fn is_strictly_positive(&self) -> bool
pub fn is_strictly_positive(&self) -> bool
Checks if this number is greater than 0.
sourcepub fn is_positive_or_zero(&self) -> bool
pub fn is_positive_or_zero(&self) -> bool
Checks if this number is greater than or equal to 0.
sourcepub fn map<U: UnsignedDecimal, F: FnOnce(T) -> U>(self, f: F) -> Signed<U>
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§impl<T: UnsignedDecimal> Signed<T>
impl<T: UnsignedDecimal> Signed<T>
sourcepub fn checked_add(self, rhs: Self) -> Result<Self>
pub fn checked_add(self, rhs: Self) -> Result<Self>
Addition that checks for integer overflow.
sourcepub fn checked_sub(self, rhs: Self) -> Result<Self>
pub fn checked_sub(self, rhs: Self) -> Result<Self>
Subtraction that checks for underflow
source§impl<T: UnsignedDecimal> Signed<T>
impl<T: UnsignedDecimal> Signed<T>
sourcepub fn is_negative(&self) -> bool
pub fn is_negative(&self) -> bool
Strictly less than 0, returns false on 0
sourcepub fn into_number(self) -> Signed<Decimal256>
pub fn into_number(self) -> Signed<Decimal256>
Convert into a general purpose Number.
sourcepub fn from_number(src: Signed<Decimal256>) -> Self
pub fn from_number(src: Signed<Decimal256>) -> Self
convert from a general purpose Number.
sourcepub fn try_into_non_negative_value(self) -> Option<T>
pub fn try_into_non_negative_value(self) -> Option<T>
If the value is positive or zero, return the inner T
. Otherwise return None
.
sourcepub fn try_into_non_zero(self) -> Option<NonZero<T>>
pub fn try_into_non_zero(self) -> Option<NonZero<T>>
Try to convert into a non-zero value
source§impl<T: UnsignedDecimal> Signed<T>
impl<T: UnsignedDecimal> Signed<T>
sourcepub fn checked_mul_number(self, rhs: Signed<Decimal256>) -> Result<Self>
pub fn checked_mul_number(self, rhs: Signed<Decimal256>) -> Result<Self>
Multiply by a raw number
Trait Implementations§
source§impl<T: UnsignedDecimal> Add for Signed<T>
impl<T: UnsignedDecimal> Add for Signed<T>
source§impl<T: UnsignedDecimal> Debug for Signed<T>
impl<T: UnsignedDecimal> Debug for Signed<T>
source§impl<T: UnsignedDecimal> Default for Signed<T>
impl<T: UnsignedDecimal> Default for Signed<T>
source§impl<'de, T: UnsignedDecimal> Deserialize<'de> for Signed<T>
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>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
source§impl<T: UnsignedDecimal> Display for Signed<T>
impl<T: UnsignedDecimal> Display for Signed<T>
source§impl<T: UnsignedDecimal> FromStr for Signed<T>
impl<T: UnsignedDecimal> FromStr for Signed<T>
source§impl<T: UnsignedDecimal> JsonSchema for Signed<T>
impl<T: UnsignedDecimal> JsonSchema for Signed<T>
source§fn schema_name() -> String
fn schema_name() -> String
source§fn is_referenceable() -> bool
fn is_referenceable() -> bool
$ref
keyword. Read moresource§fn json_schema(_: &mut SchemaGenerator) -> Schema
fn json_schema(_: &mut SchemaGenerator) -> Schema
source§impl<T: UnsignedDecimal> Neg for Signed<T>
impl<T: UnsignedDecimal> Neg for Signed<T>
source§impl<T: UnsignedDecimal> Ord for Signed<T>
impl<T: UnsignedDecimal> Ord for Signed<T>
source§impl<T: PartialEq> PartialEq for Signed<T>
impl<T: PartialEq> PartialEq for Signed<T>
source§impl<T: UnsignedDecimal> PartialOrd for Signed<T>
impl<T: UnsignedDecimal> PartialOrd for Signed<T>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<T: UnsignedDecimal> Serialize for Signed<T>
impl<T: UnsignedDecimal> Serialize for Signed<T>
Serializes as a string for serde