Crates.io | unc-token |
lib.rs | unc-token |
version | 0.10.2 |
source | src |
created_at | 2024-03-06 08:55:11.337258 |
updated_at | 2024-07-04 03:57:55.070891 |
description | a small crate to work with UNC token values ergonomically and efficiently (UNC Protocol) |
homepage | |
repository | https://github.com/unc/unc-token |
max_upload_size | |
id | 1164561 |
size | 51,107 |
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.
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)
);
}
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"}"#
);
}
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]
);
}
UNC is used to price computation and storage on the UNC infrastructure. The network charges transaction fees in UNC to process changes and transactions.
This project is licensed under the MIT license and Apache-2.0 license.