-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
dj8yf0μl
committed
Feb 23, 2024
1 parent
9ee4363
commit 3fcd94f
Showing
37 changed files
with
174 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,4 @@ build/ | |
|
||
fmt_buffer/Cargo.lock | ||
near_token/Cargo.lock | ||
near_gas/Cargo.lock |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,6 @@ popd | |
pushd near_token | ||
cargo +stable test | ||
popd | ||
pushd near_gas | ||
cargo +stable test | ||
popd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[build] | ||
target = "x86_64-unknown-linux-gnu" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "near_gas" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
fmt_buffer = { version = "0.1.0", path = "../fmt_buffer" } | ||
numtoa = "0.2.4" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
#![no_std] | ||
use fmt_buffer::Buffer; | ||
use numtoa::NumToA; | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Ord, Eq)] | ||
pub struct NearGas(u64); | ||
|
||
/// Gas is a type for storing amount of gas. | ||
pub type Gas = u64; | ||
|
||
const ONE_GIGA_GAS: u64 = 10u64.pow(9); | ||
const ONE_TERA_GAS: u64 = 10u64.pow(12); | ||
|
||
impl NearGas { | ||
/// Returns the total number of a whole part of tera Gas contained by this `NearGas`. | ||
/// | ||
/// # Examples | ||
/// ``` | ||
/// use near_gas::NearGas; | ||
/// let neargas = NearGas::from_gas(1 * 1_000_000_000_000); | ||
/// assert_eq!(neargas.as_tgas(), 1); | ||
/// ``` | ||
pub const fn as_tgas(self) -> u64 { | ||
self.0 / ONE_TERA_GAS | ||
} | ||
/// Creates a new `NearGas` from the specified number of whole giga Gas. | ||
/// | ||
/// # Examples | ||
/// ``` | ||
/// use near_gas::NearGas; | ||
/// | ||
/// let giga_gas = NearGas::from_ggas(5); | ||
/// | ||
/// assert_eq!(giga_gas.as_gas(), 5 * 1_000_000_000); | ||
/// ``` | ||
pub const fn from_ggas(mut inner: u64) -> Self { | ||
inner *= ONE_GIGA_GAS; | ||
Self(inner) | ||
} | ||
|
||
/// Creates a new `NearGas` from the specified number of whole Gas. | ||
/// | ||
/// # Examples | ||
/// ``` | ||
/// use near_gas::NearGas; | ||
/// | ||
/// let gas = NearGas::from_gas(5 * 1_000_000_000_000); | ||
/// | ||
/// assert_eq!(gas.as_tgas(), 5); | ||
/// ``` | ||
pub const fn from_gas(inner: u64) -> Self { | ||
Self(inner) | ||
} | ||
|
||
/// Returns the total number of whole Gas contained by this `NearGas`. | ||
/// | ||
/// # Examples | ||
/// ``` | ||
/// use near_gas::NearGas; | ||
/// let neargas = NearGas::from_gas(12345); | ||
/// assert_eq!(neargas.as_gas(), 12345); | ||
/// ``` | ||
pub const fn as_gas(self) -> u64 { | ||
self.0 | ||
} | ||
|
||
pub fn display_as_buffer(&self, result: &mut Buffer<30>) { | ||
if *self == NearGas::from_gas(0) { | ||
result.write_str("0 Tgas"); | ||
} else if *self < NearGas::from_ggas(1) { | ||
result.write_str("less than 0.001 Tgas"); | ||
} else if *self <= NearGas::from_ggas(999) { | ||
let gigagas_rounded_up: u32 = | ||
(self.as_gas().saturating_add(ONE_GIGA_GAS - 1) / ONE_GIGA_GAS) as u32; | ||
let mut millis_str_buf = [0u8; 10]; | ||
|
||
result.write_str("0."); | ||
let millis_str = gigagas_rounded_up.numtoa_str(10, &mut millis_str_buf); | ||
let leading_zeros = 3 - millis_str.len(); | ||
|
||
for _ in 0..leading_zeros { | ||
result.write_str("0"); | ||
} | ||
|
||
result.write_str(millis_str); | ||
result.write_str(" Tgas"); | ||
} else { | ||
let terragas_rounded_up: u64 = | ||
self.as_gas().saturating_add(100 * ONE_GIGA_GAS - 1) / ONE_GIGA_GAS / 100; | ||
let mut str_buf = [0u8; 20]; | ||
|
||
result.write_str((terragas_rounded_up / 10).numtoa_str(10, &mut str_buf)); | ||
result.write_str("."); | ||
result.write_str((terragas_rounded_up % 10).numtoa_str(10, &mut str_buf)); | ||
|
||
result.write_str(" Tgas"); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::NearGas; | ||
use fmt_buffer::Buffer; | ||
|
||
#[test] | ||
fn test_display() { | ||
for (near_gas, expected_display) in [ | ||
(NearGas::from_gas(0), "0 Tgas"), | ||
(NearGas::from_gas(1), "less than 0.001 Tgas"), | ||
(NearGas::from_gas(999_999_999), "less than 0.001 Tgas"), | ||
(NearGas::from_gas(1_000_000_000), "0.001 Tgas"), | ||
(NearGas::from_gas(1_000_000_001), "0.002 Tgas"), | ||
(NearGas::from_gas(2_000_000_000), "0.002 Tgas"), | ||
(NearGas::from_gas(200_000_000_000), "0.200 Tgas"), | ||
(NearGas::from_gas(999_000_000_000), "0.999 Tgas"), | ||
(NearGas::from_gas(999_000_000_001), "1.0 Tgas"), | ||
(NearGas::from_gas(999_999_999_999), "1.0 Tgas"), | ||
(NearGas::from_gas(1_000_000_000_000), "1.0 Tgas"), | ||
(NearGas::from_gas(1_000_000_000_001), "1.1 Tgas"), | ||
(NearGas::from_gas(1_234_567_000_000), "1.3 Tgas"), | ||
(NearGas::from_gas(1_500_000_000_000), "1.5 Tgas"), | ||
(NearGas::from_gas(10_000_000_000_000), "10.0 Tgas"), | ||
(NearGas::from_gas(10_500_000_000_000), "10.5 Tgas"), | ||
(NearGas::from_gas(99_999_999_999_999), "100.0 Tgas"), | ||
(NearGas::from_gas(100_000_000_000_000), "100.0 Tgas"), | ||
(NearGas::from_gas(100_500_000_000_000), "100.5 Tgas"), | ||
(NearGas::from_gas(1_000_500_000_000_000), "1000.5 Tgas"), | ||
( | ||
NearGas::from_gas(1_000_000_500_000_000_000), | ||
"1000000.5 Tgas", | ||
), | ||
] { | ||
let mut buffer: Buffer<30> = Buffer::new(); | ||
near_gas.display_as_buffer(&mut buffer); | ||
|
||
assert_eq!(buffer.as_str(), expected_display); | ||
assert_eq!(buffer.truncated(), false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+36 Bytes
(110%)
...napshots/nanos/test_sign_batch_transaction_all_actions/10_next_action/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+36 Bytes
(110%)
tests/snapshots/nanos/test_sign_batch_transaction_all_actions/12_sign/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+36 Bytes
(110%)
...snapshots/nanos/test_sign_batch_transaction_all_actions/8_next_action/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+36 Bytes
(110%)
tests/snapshots/nanos/test_sign_delegate_action_batch/10_next_subaction/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+36 Bytes
(110%)
...s/snapshots/nanos/test_sign_delegate_action_batch/12_to_nep366_suffix/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+36 Bytes
(110%)
tests/snapshots/nanos/test_sign_delegate_action_batch/8_next_subaction/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+36 Bytes
(110%)
tests/snapshots/nanos/test_sign_function_call_binary_hexdump/2_sign/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+36 Bytes
(110%)
.../nanos/test_sign_function_call_binary_hexdump_after_utf8_error/2_sign/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+36 Bytes
(110%)
tests/snapshots/nanos/test_sign_function_call_string/2_sign/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+41 Bytes
(110%)
...apshots/nanosp/test_sign_batch_transaction_all_actions/10_next_action/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+41 Bytes
(110%)
tests/snapshots/nanosp/test_sign_batch_transaction_all_actions/12_sign/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+41 Bytes
(110%)
...napshots/nanosp/test_sign_batch_transaction_all_actions/8_next_action/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+41 Bytes
(110%)
tests/snapshots/nanosp/test_sign_delegate_action_batch/10_next_subaction/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+41 Bytes
(110%)
.../snapshots/nanosp/test_sign_delegate_action_batch/12_to_nep366_suffix/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+41 Bytes
(110%)
tests/snapshots/nanosp/test_sign_delegate_action_batch/8_next_subaction/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+41 Bytes
(110%)
tests/snapshots/nanosp/test_sign_function_call_binary_hexdump/2_sign/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+41 Bytes
(110%)
...nanosp/test_sign_function_call_binary_hexdump_after_utf8_error/2_sign/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+41 Bytes
(110%)
tests/snapshots/nanosp/test_sign_function_call_string/2_sign/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+41 Bytes
(110%)
...napshots/nanox/test_sign_batch_transaction_all_actions/10_next_action/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+41 Bytes
(110%)
tests/snapshots/nanox/test_sign_batch_transaction_all_actions/12_sign/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+41 Bytes
(110%)
...snapshots/nanox/test_sign_batch_transaction_all_actions/8_next_action/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+41 Bytes
(110%)
tests/snapshots/nanox/test_sign_delegate_action_batch/10_next_subaction/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+41 Bytes
(110%)
...s/snapshots/nanox/test_sign_delegate_action_batch/12_to_nep366_suffix/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+41 Bytes
(110%)
tests/snapshots/nanox/test_sign_delegate_action_batch/8_next_subaction/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+41 Bytes
(110%)
tests/snapshots/nanox/test_sign_function_call_binary_hexdump/2_sign/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+41 Bytes
(110%)
.../nanox/test_sign_function_call_binary_hexdump_after_utf8_error/2_sign/00003.png
Oops, something went wrong.
Binary file modified
BIN
+41 Bytes
(110%)
tests/snapshots/nanox/test_sign_function_call_string/2_sign/00003.png
Oops, something went wrong.