#unc #token #u128 #data #values #borsh #constructor

no-std unc-token

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

4 releases (breaking)

0.4.0 Apr 1, 2024
0.3.0 Mar 6, 2024
0.2.0 Mar 6, 2024
0.1.0 Mar 6, 2024

#596 in Parser implementations

Download history 249/week @ 2024-03-01 68/week @ 2024-03-08 27/week @ 2024-03-15 5/week @ 2024-03-22 189/week @ 2024-03-29 146/week @ 2024-04-05

370 downloads per month
Used in 7 crates (4 directly)

MIT/Apache

37KB
721 lines

Crates.io (latest) Docs.rs Rust Version

unc-token

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

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

Examples

Basic

Add unc-token to your dependencies:

cargo add unc-token

Here is the basic usage of unc-token crate:

use unc_token::UncToken;

fn main() {
    const TEN_UNC: UncToken = UncToken::from_unc(10);

    assert_eq!(TEN_UNC.to_string(), "10.00 UNC");
    assert_eq!(TEN_UNC.as_near(), 10);
    assert_eq!(TEN_UNC.as_millinear(), 10000);
    assert_eq!(TEN_UNC.as_attounc(), 10000000000000000000000000);

    let input_str = "0.123456 UNC";
    let input_near: UncToken = input_str.parse().unwrap();
    assert_eq!(
        input_near,
        UncToken::from_attounc(123456000000000000000000)
    );

}

serde support

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

cargo add unc-token --features serde

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

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

fn main() {
    const TEN_UNC: UncToken = UncToken::from_unc(10);

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

borsh support

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

cargo add unc-token --features borsh

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

use borsh::{to_vec, BorshSerialize};
use unc_token::UncToken;

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

fn main() {
    const TEN_UNC: UncToken = UncToken::from_unc(10);

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

UncToken information

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

License

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

Dependencies

~0–570KB
~12K SLoC