#near #token #values #ergonomically #u128 #borsh #near-protocol

no-std near-token

a small crate to work with NEAR token values ergonomically and efficiently (NEAR Protocol)

5 releases (3 breaking)

0.3.0 Aug 12, 2024
0.2.3 Aug 12, 2024
0.2.1 Jul 31, 2024
0.2.0 Oct 28, 2023
0.0.1 Oct 22, 2023

#3 in #near-protocol

Download history 763/week @ 2024-07-23 2159/week @ 2024-07-30 1828/week @ 2024-08-06 2158/week @ 2024-08-13 1086/week @ 2024-08-20 1452/week @ 2024-08-27 1876/week @ 2024-09-03 1625/week @ 2024-09-10 986/week @ 2024-09-17 1235/week @ 2024-09-24 1046/week @ 2024-10-01 1250/week @ 2024-10-08 1791/week @ 2024-10-15 1567/week @ 2024-10-22 1619/week @ 2024-10-29 1273/week @ 2024-11-05

6,550 downloads per month
Used in 19 crates (6 directly)

MIT/Apache

40KB
754 lines

Crates.io (latest) Docs.rs Rust Version

near-token

near-token is crate for work with tokens in near-protocol.

The crate includes NearToken type and constructors for converting data as NearToken and as u128 type values.

Examples

Basic

Add near-token to your dependencies:

cargo add near-token

Here is the basic usage of near-token crate:

use near_token::NearToken;

fn main() {
    const TEN_NEAR: NearToken = NearToken::from_near(10);

    assert_eq!(TEN_NEAR.to_string(), "10.00 NEAR");
    assert_eq!(TEN_NEAR.as_near(), 10);
    assert_eq!(TEN_NEAR.as_millinear(), 10000);
    assert_eq!(TEN_NEAR.as_yoctonear(), 10000000000000000000000000);

    let input_str = "0.123456 NEAR";
    let input_near: NearToken = input_str.parse().unwrap();
    assert_eq!(
        input_near,
        NearToken::from_yoctonear(123456000000000000000000)
    );

}

serde support

In order to use NearToken in serde-serializable structs, enable serde feature:

cargo add near-token --features serde

Here is the basic usage of near-token crate with serde:

// When `serde` feature is enabled, NearToken can be used in serde-serializable structs.
// NearToken will be serialized to a token-precision u128 value encoded as string.
#[derive(serde::Serialize)]
struct TransferDetails {
    amount: NearToken,
}

fn main() {
    const TEN_NEAR: NearToken = NearToken::from_near(10);

    let details = TransferDetails { amount: TEN_NEAR };
    assert_eq!(
        serde_json::to_string(&details).unwrap(),
        r#"{"amount":"10000000000000000000000000"}"#
    );
}

borsh support

In order to use NearToken in borsh-serializable structs, enable borsh feature:

cargo add near-token --features borsh

Here is the basic usage of near-token crate with borsh:

use borsh::{to_vec, BorshSerialize};
use near_token::NearToken;

#[derive(BorshSerialize)]
struct TransferDetails {
    amount: NearToken,
}

fn main() {
    const TEN_NEAR: NearToken = NearToken::from_near(10);

    let details = TransferDetails { amount: TEN_NEAR };
    assert_eq!(
        to_vec(&details).unwrap(),
        vec![0, 0, 0, 74, 72, 1, 20, 22, 149, 69, 8, 0, 0, 0, 0, 0]
    );
}

NearToken information

NEAR is used to price computation and storage on the NEAR infrastructure. The network charges transaction fees in NEAR to process changes and transactions.

License

This project is licensed under the MIT license and Apache-2.0 license.

Dependencies

~0–600KB
~12K SLoC