pub struct NearToken(/* private fields */);Expand description
A NEAR token amount with yoctoNEAR precision (10^-24 NEAR).
§Creating Amounts
Use the typed constructors for compile-time safety:
use near_kit::NearToken;
// Preferred: typed constructors (const, zero-cost)
let five_near = NearToken::near(5);
let half_near = NearToken::millinear(500);
let one_yocto = NearToken::yocto(1);
// Also available: longer form
let amount = NearToken::from_near(5);§Parsing from Strings
String parsing is available for runtime input (CLI, config files):
"5 NEAR"or"5 near"- whole NEAR"1.5 NEAR"- decimal NEAR"500 milliNEAR"or"500 mNEAR"- milliNEAR"1000 yocto"or"1000 yoctoNEAR"- yoctoNEAR
Raw numbers are NOT accepted to prevent unit confusion.
use near_kit::NearToken;
// For runtime/user input only
let amount: NearToken = "5 NEAR".parse().unwrap();Implementations§
Source§impl NearToken
impl NearToken
Sourcepub const ONE_MILLINEAR: Self
pub const ONE_MILLINEAR: Self
One milliNEAR.
Sourcepub const fn near(near: u128) -> Self
pub const fn near(near: u128) -> Self
Create from whole NEAR (short alias for from_near).
§Example
use near_kit::NearToken;
let amount = NearToken::near(5);
assert_eq!(amount, NearToken::from_near(5));Sourcepub const fn millinear(millinear: u128) -> Self
pub const fn millinear(millinear: u128) -> Self
Create from milliNEAR (short alias for from_millinear).
§Example
use near_kit::NearToken;
let amount = NearToken::millinear(500); // 0.5 NEAR
assert_eq!(amount, NearToken::from_millinear(500));Sourcepub const fn yocto(yocto: u128) -> Self
pub const fn yocto(yocto: u128) -> Self
Create from yoctoNEAR (short alias for from_yoctonear).
§Example
use near_kit::NearToken;
let amount = NearToken::yocto(1);
assert_eq!(amount, NearToken::ONE_YOCTO);Sourcepub const fn from_yoctonear(yocto: u128) -> Self
pub const fn from_yoctonear(yocto: u128) -> Self
Create from yoctoNEAR (10^-24 NEAR).
Sourcepub const fn from_millinear(millinear: u128) -> Self
pub const fn from_millinear(millinear: u128) -> Self
Create from milliNEAR (10^-3 NEAR).
Sourcepub fn from_near_decimal(s: &str) -> Result<Self, ParseAmountError>
pub fn from_near_decimal(s: &str) -> Result<Self, ParseAmountError>
Parse from decimal NEAR (e.g., 1.5 NEAR).
Sourcepub const fn as_yoctonear(&self) -> u128
pub const fn as_yoctonear(&self) -> u128
Get the raw yoctoNEAR value.
Sourcepub fn as_near_f64(&self) -> f64
pub fn as_near_f64(&self) -> f64
Get the value as NEAR (may lose precision).
Sourcepub fn checked_add(self, other: Self) -> Option<Self>
pub fn checked_add(self, other: Self) -> Option<Self>
Checked addition.
Sourcepub fn checked_sub(self, other: Self) -> Option<Self>
pub fn checked_sub(self, other: Self) -> Option<Self>
Checked subtraction.
Sourcepub fn saturating_add(self, other: Self) -> Self
pub fn saturating_add(self, other: Self) -> Self
Saturating addition.
Sourcepub fn saturating_sub(self, other: Self) -> Self
pub fn saturating_sub(self, other: Self) -> Self
Saturating subtraction.
Trait Implementations§
Source§impl BorshDeserialize for NearToken
impl BorshDeserialize for NearToken
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self>
Source§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.
Source§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,
Source§impl BorshSerialize for NearToken
impl BorshSerialize for NearToken
Source§impl<'de> Deserialize<'de> for NearToken
impl<'de> Deserialize<'de> for NearToken
Source§fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>
Deserialize this value from the given Serde deserializer. Read more
Source§impl IntoNearToken for NearToken
impl IntoNearToken for NearToken
Source§fn into_near_token(self) -> Result<NearToken, ParseAmountError>
fn into_near_token(self) -> Result<NearToken, ParseAmountError>
Convert into a NearToken.
Source§impl Ord for NearToken
impl Ord for NearToken
Source§impl PartialOrd for NearToken
impl PartialOrd for NearToken
impl Copy for NearToken
impl Eq for NearToken
impl StructuralPartialEq for NearToken
Auto Trait Implementations§
impl Freeze for NearToken
impl RefUnwindSafe for NearToken
impl Send for NearToken
impl Sync for NearToken
impl Unpin for NearToken
impl UnsafeUnpin for NearToken
impl UnwindSafe for NearToken
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,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.