The NEAR token helpers provide convenient functions for working with NEAR token amounts, handling conversions between human-readable NEAR values and yoctoNEAR (the smallest unit).Documentation Index
Fetch the complete documentation index at: https://mintlify.com/near/near-api-ts/llms.txt
Use this file to discover all available pages before exploring further.
Overview
1 NEAR = 10^24 yoctoNEAR The library provides both “safe” variants (returnResult types) and “throwable” variants (throw errors) for all functions.
Functions
near()
Creates a NearToken from a human-readable NEAR amount.near(string): Human-readable NEAR amount with up to 24 decimal places
NearToken
Safe variant: safeNear()
yoctoNear()
Creates a NearToken from yoctoNEAR (smallest unit).yoctoNear(bigint | string): Amount in yoctoNEAR
NearToken
Safe variant: safeYoctoNear()
nearToken()
Creates a NearToken from either NEAR or yoctoNEAR.args(object): Either{ near: string }or{ yoctoNear: bigint | string }
NearToken
Safe variant: safeNearToken()
isNearToken()
Type guard to check if a value is a NearToken.NearToken Object
TheNearToken object provides the following properties and methods:
Properties
near(string): Human-readable NEAR amount (lazy-loaded)yoctoNear(bigint): Amount in yoctoNEAR (lazy-loaded)
Methods
add()
Add two token amounts.safeAdd()
sub()
Subtract token amounts.safeSub()
gt()
Compare if greater than.safeGt()
lt()
Compare if less than.safeLt()
Type Definitions
See NearToken Types for complete type information.Examples
Basic Usage
Safe Operations
Transfer Action
Validation
The library validates input:-
NEAR values: Must be valid decimal strings with up to 24 decimal places
- Valid:
"1","1.5","0.000000000000000000000001" - Invalid:
".5","1.","1.5555555555555555555555555"(too many decimals)
- Valid:
-
YoctoNEAR values: Must be non-negative bigint or numeric string
- Valid:
1000000n,"1000000" - Invalid:
"abc",-1000n
- Valid: