2025-10-29 15:34:11 -03:00
|
|
|
use nssa_core::program::{
|
2025-11-27 13:49:56 -03:00
|
|
|
ChainedCall, PdaSeed, ProgramId, ProgramInput, read_nssa_inputs,
|
|
|
|
|
write_nssa_outputs_with_chained_call,
|
2025-10-29 15:34:11 -03:00
|
|
|
};
|
|
|
|
|
use risc0_zkvm::serde::to_vec;
|
|
|
|
|
|
2025-11-27 13:49:56 -03:00
|
|
|
type Instruction = (u128, ProgramId, u32, Option<PdaSeed>);
|
2025-10-29 15:34:11 -03:00
|
|
|
|
2025-11-12 19:55:02 -03:00
|
|
|
/// A program that calls another program `num_chain_calls` times.
|
2025-10-30 10:52:31 -03:00
|
|
|
/// It permutes the order of the input accounts on the subsequent call
|
2025-11-27 13:49:56 -03:00
|
|
|
/// The `ProgramId` in the instruction must be the program_id of the authenticated transfers program
|
2025-10-29 15:34:11 -03:00
|
|
|
fn main() {
|
|
|
|
|
let ProgramInput {
|
|
|
|
|
pre_states,
|
2025-11-27 13:49:56 -03:00
|
|
|
instruction: (balance, auth_transfer_id, num_chain_calls, pda_seed),
|
2025-10-29 15:34:11 -03:00
|
|
|
} = read_nssa_inputs::<Instruction>();
|
|
|
|
|
|
2025-11-27 13:49:56 -03:00
|
|
|
let [recipient_pre, sender_pre] = match pre_states.try_into() {
|
2025-10-29 15:34:11 -03:00
|
|
|
Ok(array) => array,
|
|
|
|
|
Err(_) => return,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let instruction_data = to_vec(&balance).unwrap();
|
|
|
|
|
|
2025-11-27 13:49:56 -03:00
|
|
|
let mut running_recipient_pre = recipient_pre.clone();
|
|
|
|
|
let mut running_sender_pre = sender_pre.clone();
|
|
|
|
|
|
|
|
|
|
if pda_seed.is_some() {
|
|
|
|
|
running_sender_pre.is_authorized = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut chained_calls = Vec::new();
|
|
|
|
|
for _i in 0..num_chain_calls {
|
|
|
|
|
let new_chained_call = ChainedCall {
|
|
|
|
|
program_id: auth_transfer_id,
|
2025-11-12 19:08:46 -03:00
|
|
|
instruction_data: instruction_data.clone(),
|
2025-11-27 13:49:56 -03:00
|
|
|
pre_states: vec![running_sender_pre.clone(), running_recipient_pre.clone()], // <- Account order permutation here
|
|
|
|
|
pda_seeds: pda_seed.iter().cloned().collect(),
|
2025-11-12 19:55:02 -03:00
|
|
|
};
|
2025-11-27 13:49:56 -03:00
|
|
|
chained_calls.push(new_chained_call);
|
2025-10-29 15:34:11 -03:00
|
|
|
|
2025-11-27 13:49:56 -03:00
|
|
|
running_sender_pre.account.balance -= balance;
|
|
|
|
|
running_recipient_pre.account.balance += balance;
|
|
|
|
|
}
|
2025-11-12 19:55:02 -03:00
|
|
|
|
2025-10-29 15:34:11 -03:00
|
|
|
write_nssa_outputs_with_chained_call(
|
2025-11-27 13:49:56 -03:00
|
|
|
vec![recipient_pre.clone(), sender_pre.clone()],
|
|
|
|
|
vec![recipient_pre.account, sender_pre.account],
|
|
|
|
|
chained_calls,
|
2025-10-29 15:34:11 -03:00
|
|
|
);
|
|
|
|
|
}
|