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

no-std unc-token

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

7 releases (4 breaking)

0.10.2 Jul 4, 2024
0.10.1 May 29, 2024
0.4.0 Apr 1, 2024
0.3.0 Mar 6, 2024
0.1.0 Mar 6, 2024

#666 in Parser implementations


Used in 9 crates (6 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_unc(), 10);
    assert_eq!(TEN_UNC.as_milliunc(), 10000);
    assert_eq!(TEN_UNC.as_attounc(), 10000000000000000000000000);

    let input_str = "0.123456 UNC";
    let input_unc: UncToken = input_str.parse().unwrap();
    assert_eq!(
        input_unc,
        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–590KB
~12K SLoC