From 272e2dfc83c9698d75883744c76d0171fe9f9c51 Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Fri, 18 Jul 2025 17:49:28 -0300 Subject: [PATCH] add docs to transfer multiple program --- .../program_methods/guest/src/bin/pinata.rs | 6 +++--- .../program_methods/guest/src/bin/transfer.rs | 2 ++ .../guest/src/bin/transfer_multiple.rs | 21 +++++++++++++------ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/risc0-selective-privacy-poc/program_methods/guest/src/bin/pinata.rs b/risc0-selective-privacy-poc/program_methods/guest/src/bin/pinata.rs index 17b73f6..d864f81 100644 --- a/risc0-selective-privacy-poc/program_methods/guest/src/bin/pinata.rs +++ b/risc0-selective-privacy-poc/program_methods/guest/src/bin/pinata.rs @@ -10,7 +10,8 @@ const PINATA_PRIZE: u128 = 100; /// A PiƱata program /// To be used both in public and privacy contexts. fn main() { - // Read input accounts. It is expected to receive only two accounts: [pinata_account, winner_account] + // Read input accounts. + // It is expected to receive only two accounts: [pinata_account, winner_account] let mut input_accounts: Vec = env::read(); // Read claimed preimage @@ -18,8 +19,7 @@ fn main() { // Unpack accounts. assert_eq!(input_accounts.len(), 2); - let [winner_account] = input_accounts.split_off(1).try_into().unwrap(); - let [pinata_account] = input_accounts.try_into().unwrap(); + let [pinata_account, winner_account] = input_accounts.try_into().unwrap(); // Check that the given `pinata_account` is correct assert_eq!(pinata_account.address, PINATA_ACCOUNT_ADDR); diff --git a/risc0-selective-privacy-poc/program_methods/guest/src/bin/transfer.rs b/risc0-selective-privacy-poc/program_methods/guest/src/bin/transfer.rs index 26b3d1e..5364837 100644 --- a/risc0-selective-privacy-poc/program_methods/guest/src/bin/transfer.rs +++ b/risc0-selective-privacy-poc/program_methods/guest/src/bin/transfer.rs @@ -4,6 +4,8 @@ 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 = env::read(); let balance_to_move: u128 = env::read(); diff --git a/risc0-selective-privacy-poc/program_methods/guest/src/bin/transfer_multiple.rs b/risc0-selective-privacy-poc/program_methods/guest/src/bin/transfer_multiple.rs index 60b5c34..dc495f1 100644 --- a/risc0-selective-privacy-poc/program_methods/guest/src/bin/transfer_multiple.rs +++ b/risc0-selective-privacy-poc/program_methods/guest/src/bin/transfer_multiple.rs @@ -1,26 +1,35 @@ use core::account::Account; use risc0_zkvm::guest::env; -/// A transfer of balance program with multiple recipients. +/// A transfer of balance program with one sender and multiple recipients. /// To be used both in public and private contexts. fn main() { + // Read input accounts. + // First account is the sender. let mut input_accounts: Vec = env::read(); + + // Read the balances to be send to each recipient let target_balances: Vec = env::read(); + // Check that there is at least one recipient assert!(input_accounts.len() > 1); + + // Check that there's one target balance for each recipient. assert_eq!(target_balances.len() + 1, input_accounts.len()); - let receivers = input_accounts.split_off(1); + // Unpack sender and recipients + let recipients = input_accounts.split_off(1); let sender = input_accounts.pop().unwrap(); - let total_balance_to_move = target_balances.iter().sum(); - // Check sender has enough balance + // Check that the sender has enough balance to pay to all recipients + let total_balance_to_move = target_balances.iter().sum(); assert!(sender.balance >= total_balance_to_move); // Create accounts post states, with updated balances let mut sender_post = sender.clone(); - let mut receivers_post = receivers.clone(); + let mut receivers_post = recipients.clone(); + // Transfer balances sender_post.balance -= total_balance_to_move; for (receiver, balance_for_receiver) in receivers_post.iter_mut().zip(target_balances) { receiver.balance += balance_for_receiver; @@ -29,7 +38,7 @@ fn main() { // Flatten pre and post states for output let inputs_outputs: Vec = vec![sender] .into_iter() - .chain(receivers) + .chain(recipients) .chain(vec![sender_post]) .chain(receivers_post) .collect();