Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
feat: impl near amount display
Browse files Browse the repository at this point in the history
  • Loading branch information
dj8yf0μl committed Feb 22, 2024
1 parent d5b2336 commit da202e5
Show file tree
Hide file tree
Showing 78 changed files with 141 additions and 67 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ include_gif = "1.0.1"
hex = { version = "0.4.3", default-features = false, features = ["serde"] }
bs58 = { version = "0.5.0", default-features = false }
numtoa = "0.2.4"
dtoa = "1.0.9"

[profile.release]
opt-level = 'z'
Expand Down
18 changes: 8 additions & 10 deletions src/app_ui/sign/common/action/function_call_common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
parsing::{self, types::ONE_NEAR},
utils::types::elipsis_fields::ElipsisFields,
parsing::{self},
utils::types::{elipsis_fields::ElipsisFields, fmt_buffer::FmtBuffer},
};
use ledger_device_sdk::ui::gadgets::Field;
use numtoa::NumToA;
Expand All @@ -10,15 +10,15 @@ use crate::app_ui::fields_writer::FieldsWriter;
pub struct FieldsContext {
pub method_name_display_buf: [u8; 20],
pub gas_buf: [u8; 20],
pub float_buffer: dtoa::Buffer,
pub deposit_buffer: FmtBuffer<30>,
}

impl FieldsContext {
pub fn new() -> Self {
Self {
method_name_display_buf: [0u8; 20],
gas_buf: [0u8; 20],
float_buffer: dtoa::Buffer::new(),
deposit_buffer: FmtBuffer::new(),
}
}
}
Expand Down Expand Up @@ -49,12 +49,10 @@ pub fn format<'b, 'a: 'b, const N: usize>(
}))
.unwrap();

let deposit_amount = (func_call_common.deposit as f64) / (ONE_NEAR as f64);
let printed_amount = field_context.float_buffer.format(deposit_amount);
func_call_common
.deposit
.display_as_buffer(&mut field_context.deposit_buffer);
writer
.push_fields(ElipsisFields::one(Field {
name: "Deposit (NEAR)",
value: printed_amount,
}))
.push_fields(field_context.deposit_buffer.ui_field("Deposit"))
.unwrap();
}
15 changes: 5 additions & 10 deletions src/app_ui/sign/common/action/function_call_permission.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
app_ui::fields_writer::FieldsWriter,
parsing::{self, types::ONE_NEAR},
parsing,
utils::types::{elipsis_fields::ElipsisFields, fmt_buffer::FmtBuffer},
};

Expand All @@ -11,7 +11,7 @@ pub struct FieldsContext {
pub num_buf: [u8; 10],
pub receiver_display_buf: [u8; 20],
pub method_names_display_buf: [u8; 20],
pub allowance_str: FmtBuffer<30>,
pub allowance_buffer: FmtBuffer<30>,
}

impl FieldsContext {
Expand All @@ -20,7 +20,7 @@ impl FieldsContext {
num_buf: [0u8; 10],
receiver_display_buf: [0u8; 20],
method_names_display_buf: [0u8; 20],
allowance_str: FmtBuffer::new(),
allowance_buffer: FmtBuffer::new(),
}
}
}
Expand All @@ -32,13 +32,8 @@ pub fn format<'b, 'a: 'b>(
) {
let allowance = match function_call_perm.allowance {
Some(allowance) => {
let mut float_buffer = dtoa::Buffer::new();
let allowance = (allowance as f64) / (ONE_NEAR as f64);
field_context
.allowance_str
.write_str(float_buffer.format(allowance));
field_context.allowance_str.write_str(" NEAR");
field_context.allowance_str.as_str()
allowance.display_as_buffer(&mut field_context.allowance_buffer);
field_context.allowance_buffer.as_str()
}
None => "Unlimited NEAR",
};
Expand Down
18 changes: 8 additions & 10 deletions src/app_ui/sign/common/action/stake.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use crate::{
parsing::{self, types::ONE_NEAR},
parsing::{self},
sign_ui::common::tx_public_key_context,
utils::types::elipsis_fields::ElipsisFields,
utils::types::{elipsis_fields::ElipsisFields, fmt_buffer::FmtBuffer},
};
use ledger_device_sdk::ui::gadgets::Field;

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

pub struct FieldsContext {
pub float_buffer: dtoa::Buffer,
pub stake_buffer: FmtBuffer<30>,
pub pub_key_context: tx_public_key_context::FieldsContext,
}

impl FieldsContext {
pub fn new() -> Self {
Self {
float_buffer: dtoa::Buffer::new(),
stake_buffer: FmtBuffer::new(),
pub_key_context: tx_public_key_context::FieldsContext::new(),
}
}
Expand All @@ -26,8 +26,6 @@ pub fn format<'b, 'a: 'b>(
field_context: &'a mut FieldsContext,
writer: &'_ mut FieldsWriter<'b, 3>,
) {
let stake_amount = (stake.stake as f64) / (ONE_NEAR as f64);
let printed_amount = field_context.float_buffer.format(stake_amount);
field_context
.pub_key_context
.format_public_key(&stake.public_key);
Expand All @@ -38,11 +36,11 @@ pub fn format<'b, 'a: 'b>(
}))
.unwrap();

stake
.stake
.display_as_buffer(&mut field_context.stake_buffer);
writer
.push_fields(ElipsisFields::one(Field {
name: "Stake (NEAR)",
value: printed_amount,
}))
.push_fields(field_context.stake_buffer.ui_field("Stake"))
.unwrap();

writer
Expand Down
19 changes: 9 additions & 10 deletions src/app_ui/sign/common/action/transfer.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use crate::{
parsing::{self, types::ONE_NEAR},
utils::types::elipsis_fields::ElipsisFields,
parsing::{self},
utils::types::{elipsis_fields::ElipsisFields, fmt_buffer::FmtBuffer},
};
use ledger_device_sdk::ui::gadgets::Field;

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

pub struct FieldsContext {
pub float_buffer: dtoa::Buffer,
pub amount_buffer: FmtBuffer<30>,
}

impl FieldsContext {
pub fn new() -> Self {
Self {
float_buffer: dtoa::Buffer::new(),
amount_buffer: FmtBuffer::new(),
}
}
}
Expand All @@ -30,12 +30,11 @@ pub fn format<'b, 'a: 'b>(
}))
.unwrap();

let deposit = (transfer.deposit as f64) / (ONE_NEAR as f64);
let printed = field_context.float_buffer.format(deposit);
transfer
.deposit
.display_as_buffer(&mut field_context.amount_buffer);

writer
.push_fields(ElipsisFields::one(Field {
name: "Amount (NEAR)",
value: printed,
}))
.push_fields(field_context.amount_buffer.ui_field("Amount"))
.unwrap();
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ pub mod parsing {
pub mod common {
pub mod action;
pub mod message_discriminant;
pub mod near_token;
pub mod tx_public_key;
}
pub mod nep413 {
Expand Down
12 changes: 8 additions & 4 deletions src/parsing/types/common/action/add_key.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::{
io::{Error, ErrorKind, Read, Result},
parsing::{borsh::BorshDeserialize, types::TxPublicKey},
parsing::{
borsh::BorshDeserialize,
types::{common::near_token::NearToken, TxPublicKey},
},
utils::types::{capped_string::CappedString, fmt_buffer::FmtBuffer},
};

use super::{Balance, Nonce};
use super::Nonce;

pub struct AddKey {
/// A public key which will be associated with an access_key
Expand Down Expand Up @@ -38,7 +41,7 @@ pub struct FunctionCallPermission {
/// `None` means unlimited allowance.
/// NOTE: To change or increase the allowance, the old access key needs to be deleted and a new
/// access key should be created.
pub allowance: Option<Balance>,
pub allowance: Option<NearToken>,

// This isn't an AccountId because already existing records in testnet genesis have invalid
// values for this field (see: https://github.com/near/nearcore/pull/4621#issuecomment-892099860)
Expand Down Expand Up @@ -95,7 +98,8 @@ impl FunctionCallPermission {
// NOTE: using this instead of `BorshDeserialize`
// allows to increase available buffers
pub fn deserialize_reader_in_place<R: Read>(&mut self, reader: &mut R) -> Result<()> {
self.allowance = BorshDeserialize::deserialize_reader(reader)?;
let allowance: Option<u128> = BorshDeserialize::deserialize_reader(reader)?;
self.allowance = allowance.map(NearToken::from_yoctonear);
self.receiver_id.deserialize_reader_in_place(reader)?;

self.number_of_method_names = BorshDeserialize::deserialize_reader(reader)?;
Expand Down
6 changes: 3 additions & 3 deletions src/parsing/types/common/action/function_call.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
io::{Read, Result},
parsing::borsh::BorshDeserialize,
parsing::{borsh::BorshDeserialize, types::common::near_token::NearToken},
utils::types::capped_string::CappedString,
};

Expand All @@ -9,7 +9,7 @@ use super::{Balance, Gas};
pub struct FunctionCallCommon {
pub method_name: CappedString<50>,
pub gas: Gas,
pub deposit: Balance,
pub deposit: NearToken,
}

impl FunctionCallCommon {
Expand All @@ -23,7 +23,7 @@ impl FunctionCallCommon {
let r = Self {
method_name,
gas,
deposit,
deposit: NearToken::from_yoctonear(deposit),
};
Ok(r)
}
Expand Down
3 changes: 2 additions & 1 deletion src/parsing/types/common/action/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ pub type Nonce = u64;
/// Height of the block.
pub type BlockHeight = u64;

pub const ONE_NEAR: Balance = 1_000_000_000_000_000_000_000_000;
pub const ONE_NEAR: u128 = 10_u128.pow(24);
pub const ONE_MILLINEAR: u128 = 10_u128.pow(21);

pub mod add_key;
pub mod create_account;
Expand Down
11 changes: 6 additions & 5 deletions src/parsing/types/common/action/stake.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::{
io::{Read, Result},
parsing::{borsh::BorshDeserialize, types::TxPublicKey},
parsing::{
borsh::BorshDeserialize,
types::{common::near_token::NearToken, TxPublicKey},
},
};

use super::Balance;

pub struct Stake {
/// Amount of tokens to stake.
pub stake: Balance,
pub stake: NearToken,
/// Validator key which will be used to sign transactions on behalf of signer_id
pub public_key: TxPublicKey,
}
Expand All @@ -16,7 +17,7 @@ impl BorshDeserialize for Stake {
fn deserialize_reader<R: Read>(rd: &mut R) -> Result<Self> {
let stake = u128::deserialize_reader(rd)?;
Ok(Self {
stake,
stake: NearToken::from_yoctonear(stake),
public_key: BorshDeserialize::deserialize_reader(rd)?,
})
}
Expand Down
10 changes: 5 additions & 5 deletions src/parsing/types/common/action/transfer.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use crate::{
io::{Read, Result},
parsing::borsh::BorshDeserialize,
parsing::{borsh::BorshDeserialize, types::common::near_token::NearToken},
};

use super::Balance;

pub struct Transfer {
pub deposit: Balance,
pub deposit: NearToken,
}

impl BorshDeserialize for Transfer {
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
let balance = u128::deserialize_reader(reader)?;
Ok(Self { deposit: balance })
Ok(Self {
deposit: NearToken::from_yoctonear(balance),
})
}
}
Loading

0 comments on commit da202e5

Please sign in to comment.