pub struct FTBalance {
amount: u128,
decimals: u8,
symbol: &'static str,
}Expand description
A helper type that represents the fungible token balance with a given precision.
The type is created to simplify the usage of fungible tokens in similar way as the NearToken type does.
The symbol is used only for display purposes.
The type has static instances for some of the most popular tokens with correct decimals and symbol.
- USDT_BALANCE - USDT token with 6 decimals
- USDC_BALANCE - USDC token with 6 decimals
- W_NEAR_BALANCE - wNEAR token with 24 decimals
§Examples
§Defining 2.5 USDT
use near_api_types::tokens::FTBalance;
let usdt_balance = FTBalance::with_decimals(6).with_float_str("2.5").unwrap();
assert_eq!(usdt_balance.amount(), 2_500_000);§Defining 3 USDT using smaller precision
use near_api_types::tokens::FTBalance;
let usdt = FTBalance::with_decimals(6);
let usdt_balance = usdt.with_amount(3 * 10u128.pow(6));
assert_eq!(usdt_balance, usdt.with_whole_amount(3));§Defining 3 wETH using 18 decimals
use near_api_types::tokens::FTBalance;
let weth = FTBalance::with_decimals_and_symbol(18, "wETH");
let weth_balance = weth.with_whole_amount(3);
assert_eq!(weth_balance, weth.with_amount(3 * 10u128.pow(18)));Fields§
§amount: u128§decimals: u8§symbol: &'static strImplementations§
Source§impl FTBalance
impl FTBalance
Sourcepub const fn with_decimals(decimals: u8) -> FTBalance
pub const fn with_decimals(decimals: u8) -> FTBalance
Creates a new FTBalance with a given precision.
The balance is initialized to 0.
Sourcepub const fn with_decimals_and_symbol(
decimals: u8,
symbol: &'static str,
) -> FTBalance
pub const fn with_decimals_and_symbol( decimals: u8, symbol: &'static str, ) -> FTBalance
Creates a new FTBalance with a given precision and symbol.
The balance is initialized to 0.
Sourcepub const fn with_amount(&self, amount: u128) -> FTBalance
pub const fn with_amount(&self, amount: u128) -> FTBalance
Stores the given amount without any transformations.
The NearToken equivalent to this method is NearToken::from_yoctonear.
§Example
use near_api_types::tokens::FTBalance;
let usdt_balance = FTBalance::with_decimals(6).with_amount(2_500_000);
assert_eq!(usdt_balance.amount(), 2_500_000);
assert_eq!(usdt_balance.to_whole(), 2);Sourcepub const fn with_whole_amount(&self, amount: u128) -> FTBalance
pub const fn with_whole_amount(&self, amount: u128) -> FTBalance
Stores the number as an integer token value utilizing the given precision.
The NearToken equivalent to this method is NearToken::from_near.
§Example
use near_api_types::tokens::FTBalance;
let usdt_balance = FTBalance::with_decimals(6).with_whole_amount(3);
assert_eq!(usdt_balance.amount(), 3 * 10u128.pow(6));
assert_eq!(usdt_balance.to_whole(), 3);Sourcepub fn with_float_str(
&self,
float_str: &str,
) -> Result<FTBalance, DecimalNumberParsingError>
pub fn with_float_str( &self, float_str: &str, ) -> Result<FTBalance, DecimalNumberParsingError>
Sourcepub const fn amount(&self) -> u128
pub const fn amount(&self) -> u128
Returns the amount without any transformations.
The NearToken equivalent to this method is NearToken::as_yoctonear.
Sourcepub const fn to_whole(&self) -> u128
pub const fn to_whole(&self) -> u128
Returns the amount as a whole number in the integer precision.
The method rounds down the fractional part, so 2.5 USDT will be 2.
The NearToken equivalent to this method is NearToken::as_near.
Trait Implementations§
Source§impl Deserialize<'static> for FTBalance
impl Deserialize<'static> for FTBalance
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<FTBalance, <__D as Deserializer<'static>>::Error>where
__D: Deserializer<'static>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<FTBalance, <__D as Deserializer<'static>>::Error>where
__D: Deserializer<'static>,
Source§impl PartialOrd for FTBalance
impl PartialOrd for FTBalance
Source§impl Serialize for FTBalance
impl Serialize for FTBalance
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
impl Eq for FTBalance
impl StructuralPartialEq for FTBalance
Auto Trait Implementations§
impl Freeze for FTBalance
impl RefUnwindSafe for FTBalance
impl Send for FTBalance
impl Sync for FTBalance
impl Unpin for FTBalance
impl UnwindSafe for FTBalance
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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
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.