Sergio Chouhy 04f6474799 clippy
2025-08-09 19:20:19 -03:00

36 lines
1.1 KiB
Rust

use nssa_core::account::AccountWithMetadata;
use risc0_zkvm::guest::env;
/// 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]
let input_accounts: Vec<AccountWithMetadata> = env::read();
let balance_to_move: u128 = env::read();
// Continue only if input_accounts is an array of two elements
let [sender, receiver] = match input_accounts.try_into() {
Ok(array) => array,
Err(_) => return, // silently return on bad input
};
// Continue only if the sender has authorized this operation
if !sender.is_authorized {
return;
}
// Continue only if the sender has enough balance
if sender.account.balance < balance_to_move {
return;
}
// 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;
env::commit(&vec![sender_post, receiver_post]);
}