lssa/nssa/program_methods/guest/src/bin/authenticated_transfer.rs

80 lines
2.6 KiB
Rust
Raw Normal View History

2025-10-10 17:47:23 -03:00
use nssa_core::{
account::{Account, AccountWithMetadata},
2025-12-03 16:39:33 -03:00
program::{
AccountPostState, DEFAULT_PROGRAM_ID, ProgramInput, read_nssa_inputs, write_nssa_outputs,
},
2025-10-10 17:47:23 -03:00
};
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.
2025-10-13 17:45:03 -03:00
fn initialize_account(pre_state: AccountWithMetadata) {
2025-12-03 16:39:33 -03:00
let account_to_claim = AccountPostState::new_claimed(pre_state.account.clone());
2025-10-10 17:47:23 -03:00
let is_authorized = pre_state.is_authorized;
// Continue only if the account to claim has default values
if account_to_claim.account != Account::default() {
2025-10-10 17:47:23 -03:00
return;
}
// Continue only if the owner authorized this operation
if !is_authorized {
return;
}
// Noop will result in account being claimed for this program
write_nssa_outputs(vec![pre_state], vec![account_to_claim]);
}
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-10-13 17:45:03 -03:00
fn transfer(sender: AccountWithMetadata, recipient: AccountWithMetadata, balance_to_move: u128) {
// Continue only if the sender has authorized this operation
if !sender.is_authorized {
return;
}
2025-08-06 20:05:04 -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
2025-12-03 16:39:33 -03:00
let sender_post: AccountPostState = {
// Modify sender's balance
let mut sender_post_account = sender.account.clone();
sender_post_account.balance -= balance_to_move;
AccountPostState::new(sender_post_account)
};
2025-08-06 20:05:04 -03:00
2025-12-03 16:39:33 -03:00
let recipient_post = {
// Modify recipient's balance
let mut recipient_post_account = recipient.account.clone();
recipient_post_account.balance += balance_to_move;
// Claim recipient account if it has default program owner
if recipient_post_account.program_owner == DEFAULT_PROGRAM_ID {
AccountPostState::new_claimed(recipient_post_account)
} else {
AccountPostState::new(recipient_post_account)
}
};
write_nssa_outputs(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.
let ProgramInput {
pre_states,
instruction: balance_to_move,
} = read_nssa_inputs();
2025-10-13 17:45:03 -03:00
match (pre_states.as_slice(), balance_to_move) {
([account_to_claim], 0) => initialize_account(account_to_claim.clone()),
([sender, recipient], balance_to_move) => {
transfer(sender.clone(), recipient.clone(), balance_to_move)
}
_ => panic!("invalid params"),
2025-10-10 17:47:23 -03:00
}
}