2025-10-10 17:47:23 -03:00
|
|
|
use nssa_core::{
|
|
|
|
|
account::{Account, AccountWithMetadata},
|
|
|
|
|
program::{ProgramInput, read_nssa_inputs, write_nssa_outputs},
|
|
|
|
|
};
|
2025-08-10 18:59:29 -03:00
|
|
|
|
2025-10-13 18:06:02 -03:00
|
|
|
/// Initializes a default account under the ownership of this program.
|
|
|
|
|
/// This is achieved by a noop.
|
2025-11-18 01:38:47 -03:00
|
|
|
fn initialize_account(pre_state: AccountWithMetadata) -> (AccountWithMetadata, Account) {
|
2025-10-10 17:47:23 -03:00
|
|
|
let account_to_claim = pre_state.account.clone();
|
|
|
|
|
let is_authorized = pre_state.is_authorized;
|
|
|
|
|
|
|
|
|
|
// Continue only if the account to claim has default values
|
|
|
|
|
if account_to_claim != Account::default() {
|
2025-11-18 01:38:47 -03:00
|
|
|
panic!("Invalid input");
|
2025-10-10 17:47:23 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Continue only if the owner authorized this operation
|
|
|
|
|
if !is_authorized {
|
2025-11-18 01:38:47 -03:00
|
|
|
panic!("Invalid input");
|
2025-10-10 17:47:23 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Noop will result in account being claimed for this program
|
2025-11-18 01:38:47 -03:00
|
|
|
(pre_state, account_to_claim)
|
2025-10-10 17:47:23 -03:00
|
|
|
}
|
2025-08-06 20:05:04 -03:00
|
|
|
|
2025-10-13 18:06:02 -03:00
|
|
|
/// Transfers `balance_to_move` native balance from `sender` to `recipient`.
|
2025-11-18 01:38:47 -03:00
|
|
|
fn transfer(
|
|
|
|
|
sender: AccountWithMetadata,
|
|
|
|
|
recipient: AccountWithMetadata,
|
|
|
|
|
balance_to_move: u128,
|
|
|
|
|
) -> (Vec<AccountWithMetadata>, Vec<Account>) {
|
2025-08-09 18:24:53 -03:00
|
|
|
// Continue only if the sender has authorized this operation
|
|
|
|
|
if !sender.is_authorized {
|
2025-11-18 01:38:47 -03:00
|
|
|
panic!("Invalid input");
|
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 enough balance
|
|
|
|
|
if sender.account.balance < balance_to_move {
|
2025-11-18 01:38:47 -03:00
|
|
|
panic!("Invalid input");
|
2025-08-09 18:24:53 -03:00
|
|
|
}
|
2025-08-06 20:05:04 -03:00
|
|
|
|
|
|
|
|
// Create accounts post states, with updated balances
|
|
|
|
|
let mut sender_post = sender.account.clone();
|
2025-10-13 17:45:03 -03:00
|
|
|
let mut recipient_post = recipient.account.clone();
|
2025-08-06 20:05:04 -03:00
|
|
|
sender_post.balance -= balance_to_move;
|
2025-10-13 17:45:03 -03:00
|
|
|
recipient_post.balance += balance_to_move;
|
2025-11-18 01:38:47 -03:00
|
|
|
(vec![sender, recipient], vec![sender_post, recipient_post])
|
2025-08-06 20:05:04 -03:00
|
|
|
}
|
2025-09-04 12:44:22 -03:00
|
|
|
|
2025-10-10 17:47:23 -03:00
|
|
|
/// A transfer of balance program.
|
|
|
|
|
/// To be used both in public and private contexts.
|
|
|
|
|
fn main() {
|
|
|
|
|
// Read input accounts.
|
2025-11-18 01:38:47 -03:00
|
|
|
let (
|
|
|
|
|
ProgramInput {
|
|
|
|
|
pre_states,
|
|
|
|
|
instruction: balance_to_move,
|
|
|
|
|
},
|
|
|
|
|
instruction_words,
|
|
|
|
|
) = read_nssa_inputs();
|
2025-10-10 17:47:23 -03:00
|
|
|
|
2025-10-13 17:45:03 -03:00
|
|
|
match (pre_states.as_slice(), balance_to_move) {
|
2025-11-18 01:38:47 -03:00
|
|
|
([account_to_claim], 0) => {
|
|
|
|
|
let (pre, post) = initialize_account(account_to_claim.clone());
|
|
|
|
|
write_nssa_outputs(instruction_words, vec![pre], vec![post]);
|
|
|
|
|
}
|
2025-10-13 17:45:03 -03:00
|
|
|
([sender, recipient], balance_to_move) => {
|
2025-11-18 01:38:47 -03:00
|
|
|
let (pre_states, post_states) =
|
|
|
|
|
transfer(sender.clone(), recipient.clone(), balance_to_move);
|
|
|
|
|
write_nssa_outputs(instruction_words, pre_states, post_states);
|
2025-10-13 17:45:03 -03:00
|
|
|
}
|
|
|
|
|
_ => panic!("invalid params"),
|
2025-10-10 17:47:23 -03:00
|
|
|
}
|
|
|
|
|
}
|