minor refactor

This commit is contained in:
Sergio Chouhy 2025-10-13 17:45:03 -03:00
parent c772f1b0fe
commit a2b9aef152
2 changed files with 13 additions and 22 deletions

View File

@ -3,12 +3,7 @@ use nssa_core::{
program::{ProgramInput, read_nssa_inputs, write_nssa_outputs}, program::{ProgramInput, read_nssa_inputs, write_nssa_outputs},
}; };
fn initialize_account(pre_states: Vec<AccountWithMetadata>) { fn initialize_account(pre_state: AccountWithMetadata) {
// Continue only if input_accounts is an array of one element
let [pre_state] = match pre_states.try_into() {
Ok(array) => array,
Err(_) => return,
};
let account_to_claim = pre_state.account.clone(); let account_to_claim = pre_state.account.clone();
let is_authorized = pre_state.is_authorized; let is_authorized = pre_state.is_authorized;
@ -26,13 +21,7 @@ fn initialize_account(pre_states: Vec<AccountWithMetadata>) {
write_nssa_outputs(vec![pre_state], vec![account_to_claim]); write_nssa_outputs(vec![pre_state], vec![account_to_claim]);
} }
fn transfer(pre_states: Vec<AccountWithMetadata>, balance_to_move: u128) { fn transfer(sender: AccountWithMetadata, recipient: AccountWithMetadata, balance_to_move: u128) {
// Continue only if input_accounts is an array of two elements
let [sender, receiver] = match pre_states.try_into() {
Ok(array) => array,
Err(_) => return,
};
// Continue only if the sender has authorized this operation // Continue only if the sender has authorized this operation
if !sender.is_authorized { if !sender.is_authorized {
return; return;
@ -45,26 +34,27 @@ fn transfer(pre_states: Vec<AccountWithMetadata>, balance_to_move: u128) {
// Create accounts post states, with updated balances // Create accounts post states, with updated balances
let mut sender_post = sender.account.clone(); let mut sender_post = sender.account.clone();
let mut receiver_post = receiver.account.clone(); let mut recipient_post = recipient.account.clone();
sender_post.balance -= balance_to_move; sender_post.balance -= balance_to_move;
receiver_post.balance += balance_to_move; recipient_post.balance += balance_to_move;
write_nssa_outputs(vec![sender, receiver], vec![sender_post, receiver_post]); write_nssa_outputs(vec![sender, recipient], vec![sender_post, recipient_post]);
} }
/// A transfer of balance program. /// A transfer of balance program.
/// To be used both in public and private contexts. /// To be used both in public and private contexts.
fn main() { fn main() {
// Read input accounts. // Read input accounts.
// It is expected to receive only two accounts: [sender_account, receiver_account]
let ProgramInput { let ProgramInput {
pre_states, pre_states,
instruction: balance_to_move, instruction: balance_to_move,
} = read_nssa_inputs(); } = read_nssa_inputs();
match (pre_states.len(), balance_to_move) { match (pre_states.as_slice(), balance_to_move) {
(1, 0) => initialize_account(pre_states), ([account_to_claim], 0) => initialize_account(account_to_claim.clone()),
(2, balance_to_move) => transfer(pre_states, balance_to_move), ([sender, recipient], balance_to_move) => {
_ => panic!("Invalid parameters"), transfer(sender.clone(), recipient.clone(), balance_to_move)
}
_ => panic!("invalid params"),
} }
} }

View File

@ -789,7 +789,8 @@ pub async fn execute_subcommand(command: Command) -> Result<SubcommandReturnValu
Command::GetPrivateAccount { addr } => { Command::GetPrivateAccount { addr } => {
let addr: Address = addr.parse()?; let addr: Address = addr.parse()?;
if let Some(account) = wallet_core.get_account_private(&addr) { if let Some(account) = wallet_core.get_account_private(&addr) {
println!("{}", serde_json::to_string(&account).unwrap()); let account_hr: HumanReadableAccount = account.into();
println!("{}", serde_json::to_string(&account_hr).unwrap());
} else { } else {
println!("Private account not found."); println!("Private account not found.");
} }