add docs to transfer multiple program

This commit is contained in:
Sergio Chouhy 2025-07-18 17:49:28 -03:00
parent e4d4402bde
commit 272e2dfc83
3 changed files with 20 additions and 9 deletions

View File

@ -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<Account> = 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);

View File

@ -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<Account> = env::read();
let balance_to_move: u128 = env::read();

View File

@ -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<Account> = env::read();
// Read the balances to be send to each recipient
let target_balances: Vec<u128> = 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<Account> = vec![sender]
.into_iter()
.chain(receivers)
.chain(recipients)
.chain(vec![sender_post])
.chain(receivers_post)
.collect();