This repository has been archived by the owner on Aug 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from dj8yfo/stake_action
feat: impl `Stake` action
- Loading branch information
Showing
8 changed files
with
130 additions
and
4 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
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use crate::parsing::{self, types::action::ONE_NEAR}; | ||
use ledger_device_sdk::ui::gadgets::Field; | ||
|
||
use crate::{app_ui::fields_writer::FieldsWriter, utils::types::capped_string::ElipsisFields}; | ||
|
||
use super::delete_key; | ||
|
||
pub struct FieldsContext { | ||
pub float_buffer: dtoa::Buffer, | ||
pub pub_key_context: delete_key::FieldsContext, | ||
} | ||
|
||
impl FieldsContext { | ||
pub fn new() -> Self { | ||
Self { | ||
float_buffer: dtoa::Buffer::new(), | ||
pub_key_context: delete_key::FieldsContext::new(), | ||
} | ||
} | ||
} | ||
|
||
pub fn format<'b, 'a: 'b>( | ||
stake: &parsing::types::Stake, | ||
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); | ||
writer | ||
.push_fields(ElipsisFields::one(Field { | ||
name: "Action type:", | ||
value: "Stake", | ||
})) | ||
.unwrap(); | ||
|
||
writer | ||
.push_fields(ElipsisFields::one(Field { | ||
name: "Stake (NEAR)", | ||
value: printed_amount, | ||
})) | ||
.unwrap(); | ||
|
||
writer | ||
.push_fields(ElipsisFields::one(Field { | ||
name: "Public Key:", | ||
value: field_context.pub_key_context.buffer.as_str(), | ||
})) | ||
.unwrap(); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use crate::sign_ui; | ||
use crate::{ | ||
parsing::{borsh::BorshDeserialize, types::Stake, HashingStream, SingleTxStream}, | ||
AppSW, | ||
}; | ||
|
||
pub fn handle( | ||
stream: &mut HashingStream<SingleTxStream<'_>>, | ||
ordinal_action: u32, | ||
total_actions: u32, | ||
) -> Result<(), AppSW> { | ||
let stake = Stake::deserialize_reader(stream).map_err(|_err| AppSW::TxParsingFail)?; | ||
|
||
#[cfg(feature = "speculos")] | ||
stake.debug_print(); | ||
|
||
if !sign_ui::action::ui_display_stake(&stake, ordinal_action + 1, total_actions) { | ||
return Err(AppSW::Deny); | ||
} | ||
Ok(()) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use crate::{ | ||
io::{Read, Result}, | ||
parsing::{borsh::BorshDeserialize, types::tx_public_key::TxPublicKey}, | ||
}; | ||
|
||
use super::Balance; | ||
|
||
pub struct Stake { | ||
/// Amount of tokens to stake. | ||
pub stake: Balance, | ||
/// Validator key which will be used to sign transactions on behalf of signer_id | ||
pub public_key: TxPublicKey, | ||
} | ||
|
||
impl BorshDeserialize for Stake { | ||
fn deserialize_reader<R: Read>(rd: &mut R) -> Result<Self> { | ||
let stake = u128::deserialize_reader(rd)?; | ||
Ok(Self { | ||
stake, | ||
public_key: BorshDeserialize::deserialize_reader(rd)?, | ||
}) | ||
} | ||
} | ||
|
||
impl Stake { | ||
#[cfg(feature = "speculos")] | ||
pub fn debug_print(&self) { | ||
use ledger_device_sdk::testing; | ||
use numtoa::NumToA; | ||
|
||
let mut numtoa_buf = [0u8; 40]; | ||
|
||
testing::debug_print("debug printing stake action:\n"); | ||
testing::debug_print("size of self: \n"); | ||
testing::debug_print(core::mem::size_of_val(self).numtoa_str(10, &mut numtoa_buf)); | ||
testing::debug_print("\n"); | ||
testing::debug_print("debug printing stake action finish:\n"); | ||
} | ||
} |