Skip to content

Commit

Permalink
feat: improve Gas display
Browse files Browse the repository at this point in the history
  • Loading branch information
dj8yf0μl committed Feb 23, 2024
1 parent 9ee4363 commit 3fcd94f
Show file tree
Hide file tree
Showing 37 changed files with 174 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ build/

fmt_buffer/Cargo.lock
near_token/Cargo.lock
near_gas/Cargo.lock
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ hex = { version = "0.4.3", default-features = false, features = ["serde"] }
bs58 = { version = "0.5.0", default-features = false }
fmt_buffer = { version = "0.1.0", path = "./fmt_buffer" }
near_token = { version = "0.1.0", path = "./near_token" }
near_gas = { version = "0.1.0", path = "./near_gas" }
numtoa = "0.2.4"

[profile.release]
Expand Down
3 changes: 3 additions & 0 deletions local_unit_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ popd
pushd near_token
cargo +stable test
popd
pushd near_gas
cargo +stable test
popd
2 changes: 2 additions & 0 deletions near_gas/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
target = "x86_64-unknown-linux-gnu"
8 changes: 8 additions & 0 deletions near_gas/Cargo.toml
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"
141 changes: 141 additions & 0 deletions near_gas/src/lib.rs
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);
}
}
}
12 changes: 6 additions & 6 deletions src/app_ui/sign/common/action/function_call_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ use crate::{
};
use fmt_buffer::Buffer;
use ledger_device_sdk::ui::gadgets::Field;
use numtoa::NumToA;

use crate::app_ui::fields_writer::FieldsWriter;

pub struct FieldsContext {
pub method_name_display_buf: [u8; 20],
pub gas_buf: [u8; 20],
pub gas_buf: Buffer<30>,
pub deposit_buffer: Buffer<30>,
}

impl FieldsContext {
pub fn new() -> Self {
Self {
method_name_display_buf: [0u8; 20],
gas_buf: [0u8; 20],
gas_buf: Buffer::new(),
deposit_buffer: Buffer::new(),
}
}
Expand All @@ -43,12 +42,13 @@ pub fn format<'b, 'a: 'b, const N: usize>(

writer.push_fields(method_name).unwrap();

func_call_common
.gas
.display_as_buffer(&mut field_context.gas_buf);
writer
.push_fields(ElipsisFields::one(Field {
name: "Gas",
value: func_call_common
.gas
.numtoa_str(10, &mut field_context.gas_buf),
value: field_context.gas_buf.as_str(),
}))
.unwrap();

Expand Down
7 changes: 3 additions & 4 deletions src/parsing/types/common/action/function_call.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use near_gas::{Gas, NearGas};
use near_token::{Balance, NearToken};

use crate::{
Expand All @@ -6,11 +7,9 @@ use crate::{
utils::types::capped_string::CappedString,
};

use super::Gas;

pub struct FunctionCallCommon {
pub method_name: CappedString<50>,
pub gas: Gas,
pub gas: NearGas,
pub deposit: NearToken,
}

Expand All @@ -24,7 +23,7 @@ impl FunctionCallCommon {

let r = Self {
method_name,
gas,
gas: NearGas::from_gas(gas),
deposit: NearToken::from_yoctonear(deposit),
};
Ok(r)
Expand Down
3 changes: 0 additions & 3 deletions src/parsing/types/common/action/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ use crate::{
parsing::borsh::BorshDeserialize,
};

/// Gas is a type for storing amount of gas.
pub type Gas = u64;

/// Nonce for transactions.
pub type Nonce = u64;

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3fcd94f

Please sign in to comment.