unc-token

Crates.iounc-token
lib.rsunc-token
version0.10.2
sourcesrc
created_at2024-03-06 08:55:11.337258
updated_at2024-07-04 03:57:55.070891
descriptiona small crate to work with UNC token values ergonomically and efficiently (UNC Protocol)
homepage
repositoryhttps://github.com/unc/unc-token
max_upload_size
id1164561
size51,107
Terrill Tsang (fadeAce)

documentation

README

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.

Commit count: 0

cargo fmt