2025-08-14 14:30:04 -03:00
|
|
|
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs, ProgramInput};
|
2025-08-10 18:59:29 -03:00
|
|
|
|
2025-08-06 20:05:04 -03:00
|
|
|
/// A transfer of balance program.
|
|
|
|
|
/// To be used both in public and private contexts.
|
|
|
|
|
fn main() {
|
|
|
|
|
// Read input accounts.
|
|
|
|
|
// It is expected to receive only two accounts: [sender_account, receiver_account]
|
2025-08-14 14:30:04 -03:00
|
|
|
let ProgramInput {
|
|
|
|
|
pre_states,
|
|
|
|
|
instruction: balance_to_move,
|
|
|
|
|
} = read_nssa_inputs();
|
2025-08-06 20:05:04 -03:00
|
|
|
|
2025-08-09 18:24:53 -03:00
|
|
|
// Continue only if input_accounts is an array of two elements
|
2025-08-14 14:30:04 -03:00
|
|
|
let [sender, receiver] = match pre_states.try_into() {
|
2025-08-09 18:24:53 -03:00
|
|
|
Ok(array) => array,
|
2025-08-10 00:53:53 -03:00
|
|
|
Err(_) => return,
|
2025-08-09 18:24:53 -03:00
|
|
|
};
|
2025-08-06 20:05:04 -03:00
|
|
|
|
2025-08-09 18:24:53 -03:00
|
|
|
// Continue only if the sender has authorized this operation
|
|
|
|
|
if !sender.is_authorized {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-08-06 20:05:04 -03:00
|
|
|
|
2025-08-09 18:24:53 -03:00
|
|
|
// Continue only if the sender has enough balance
|
|
|
|
|
if sender.account.balance < balance_to_move {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-08-06 20:05:04 -03:00
|
|
|
|
|
|
|
|
// Create accounts post states, with updated balances
|
|
|
|
|
let mut sender_post = sender.account.clone();
|
|
|
|
|
let mut receiver_post = receiver.account.clone();
|
|
|
|
|
sender_post.balance -= balance_to_move;
|
|
|
|
|
receiver_post.balance += balance_to_move;
|
|
|
|
|
|
2025-08-14 14:09:04 -03:00
|
|
|
write_nssa_outputs(vec![sender, receiver], vec![sender_post, receiver_post]);
|
2025-08-06 20:05:04 -03:00
|
|
|
}
|