pub struct Tokens {
account_id: AccountId,
}
Expand description
A wrapper struct that simplifies interactions with NEAR, FT, NFT
This struct provides convenient methods to interact with different types of tokens on NEAR Protocol:
- Native NEAR token operations
- Fungible Token - Documentation and examples, NEP-141
- Non-Fungible Token - Documentation and examples, NEP-171
§Examples
§Fungible Token Operations
use near_api::*;
let bob_tokens = Tokens::account("bob.testnet".parse()?);
// Check FT balance
let balance = bob_tokens.ft_balance("usdt.tether-token.near".parse()?)?.fetch_from_mainnet().await?;
println!("Bob balance: {}", balance);
// Transfer FT tokens
bob_tokens.send_to("alice.testnet".parse()?)
.ft(
"usdt.tether-token.near".parse()?,
USDT_BALANCE.with_whole_amount(100)
)?
.with_signer(Signer::new(Signer::from_ledger())?)
.send_to_mainnet()
.await?;
§NFT Operations
use near_api::*;
let alice_tokens = Tokens::account("alice.testnet".parse()?);
// Check NFT assets
let tokens = alice_tokens.nft_assets("nft-contract.testnet".parse()?)?.fetch_from_testnet().await?;
println!("NFT count: {}", tokens.data.len());
// Transfer NFT
alice_tokens.send_to("bob.testnet".parse()?)
.nft("nft-contract.testnet".parse()?, "token-id".to_string())?
.with_signer(Signer::new(Signer::from_ledger())?)
.send_to_testnet()
.await?;
§NEAR Token Operations
use near_api::*;
let alice_account = Tokens::account("alice.testnet".parse()?);
// Check NEAR balance
let balance = alice_account.near_balance().fetch_from_testnet().await?;
println!("NEAR balance: {}", balance.total);
// Send NEAR
alice_account.send_to("bob.testnet".parse()?)
.near(NearToken::from_near(1))
.with_signer(Signer::new(Signer::from_ledger())?)
.send_to_testnet()
.await?;
Fields§
§account_id: AccountId
Implementations§
Source§impl Tokens
impl Tokens
pub const fn account(account_id: AccountId) -> Self
Sourcepub fn near_balance(
&self,
) -> QueryBuilder<PostprocessHandler<UserBalance, AccountViewHandler>>
pub fn near_balance( &self, ) -> QueryBuilder<PostprocessHandler<UserBalance, AccountViewHandler>>
Fetches the total NEAR balance (UserBalance) of the account.
§Example
use near_api::*;
let alice_tokens = Tokens::account("alice.testnet".parse()?);
let balance = alice_tokens.near_balance().fetch_from_testnet().await?;
println!("Alice's NEAR balance: {:?}", balance);
Sourcepub fn nft_metadata(
contract_id: AccountId,
) -> Result<QueryBuilder<CallResultHandler<NFTContractMetadata>>, BuilderError>
pub fn nft_metadata( contract_id: AccountId, ) -> Result<QueryBuilder<CallResultHandler<NFTContractMetadata>>, BuilderError>
Prepares a new contract query (nft_metadata
) for fetching the NFT metadata (NFTContractMetadata).
The function depends that the contract implements NEP-171
§Example
use near_api::*;
let metadata = Tokens::nft_metadata("nft-contract.testnet".parse()?)?
.fetch_from_testnet()
.await?;
println!("NFT metadata: {:?}", metadata);
Sourcepub fn nft_assets(
&self,
nft_contract: AccountId,
) -> Result<QueryBuilder<CallResultHandler<Vec<Token>>>, BuilderError>
pub fn nft_assets( &self, nft_contract: AccountId, ) -> Result<QueryBuilder<CallResultHandler<Vec<Token>>>, BuilderError>
Prepares a new contract query (nft_tokens_for_owner
) for fetching the NFT assets of the account (Vec<Token>).
The function depends that the contract implements NEP-171
§Example
use near_api::*;
let alice_tokens = Tokens::account("alice.testnet".parse()?);
let alice_assets = alice_tokens.nft_assets("nft-contract.testnet".parse()?)?
.fetch_from_testnet()
.await?;
println!("Alice's NFT assets: {:?}", alice_assets);
Sourcepub fn ft_metadata(
contract_id: AccountId,
) -> Result<QueryBuilder<CallResultHandler<FungibleTokenMetadata>>, BuilderError>
pub fn ft_metadata( contract_id: AccountId, ) -> Result<QueryBuilder<CallResultHandler<FungibleTokenMetadata>>, BuilderError>
Prepares a new contract query (ft_metadata
) for fetching the FT metadata (FungibleTokenMetadata).
The function depends that the contract implements NEP-141
§Example
use near_api::*;
let metadata = Tokens::ft_metadata("usdt.tether-token.near".parse()?)?
.fetch_from_testnet()
.await?
.data;
println!("FT metadata: {} {}", metadata.name, metadata.symbol);
Sourcepub fn ft_balance(
&self,
ft_contract: AccountId,
) -> Result<MultiQueryBuilder<PostprocessHandler<FTBalance, MultiQueryHandler<(CallResultHandler<FungibleTokenMetadata>, CallResultHandler<U128>)>>>, BuilderError>
pub fn ft_balance( &self, ft_contract: AccountId, ) -> Result<MultiQueryBuilder<PostprocessHandler<FTBalance, MultiQueryHandler<(CallResultHandler<FungibleTokenMetadata>, CallResultHandler<U128>)>>>, BuilderError>
Prepares a new contract query (ft_balance_of
, ft_metadata
) for fetching the FTBalance of the account.
This query is a multi-query, meaning it will fetch the FT metadata and the FT balance of the account.
The result is then postprocessed to create a FTBalance
instance.
The function depends that the contract implements NEP-141
§Example
use near_api::*;
let alice_usdt_balance = Tokens::account("alice.near".parse()?)
.ft_balance("usdt.tether-token.near".parse()?)?
.fetch_from_mainnet()
.await?;
println!("Alice's USDT balance: {}", alice_usdt_balance);
Sourcepub fn send_to(&self, receiver_id: AccountId) -> SendToBuilder
pub fn send_to(&self, receiver_id: AccountId) -> SendToBuilder
Prepares a new transaction builder for sending tokens to another account.
This builder is used to construct transactions for sending NEAR, FT, and NFT tokens.
§Sending NEAR
use near_api::*;
let alice_tokens = Tokens::account("alice.near".parse()?);
let result: near_primitives::views::FinalExecutionOutcomeView = alice_tokens.send_to("bob.near".parse()?)
.near(NearToken::from_near(1))
.with_signer(Signer::new(Signer::from_ledger())?)
.send_to_mainnet()
.await?;
§Sending FT
use near_api::*;
let alice_tokens = Tokens::account("alice.near".parse()?);
let result: near_primitives::views::FinalExecutionOutcomeView = alice_tokens.send_to("bob.near".parse()?)
.ft("usdt.tether-token.near".parse()?, USDT_BALANCE.with_whole_amount(100))?
.with_signer(Signer::new(Signer::from_ledger())?)
.send_to_mainnet()
.await?;
§Sending NFT
use near_api::*;
let alice_tokens = Tokens::account("alice.near".parse()?);
let result: near_primitives::views::FinalExecutionOutcomeView = alice_tokens.send_to("bob.near".parse()?)
.nft("nft-contract.testnet".parse()?, "token-id".to_string())?
.with_signer(Signer::new(Signer::from_ledger())?)
.send_to_testnet()
.await?;
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Tokens
impl RefUnwindSafe for Tokens
impl Send for Tokens
impl Sync for Tokens
impl Unpin for Tokens
impl UnwindSafe for Tokens
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<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.