97 lines
4.0 KiB
Rust
Raw Normal View History

use core::{bytes_to_words, types::Address, visibility::AccountVisibility};
2025-07-17 15:34:35 -03:00
2025-07-18 18:10:33 -03:00
use nssa::program::PinataProgram;
2025-07-17 13:34:12 -03:00
2025-07-18 18:23:13 -03:00
use crate::mocked_components::sequencer::{print_accounts, MockedSequencer, PINATA_ADDRESS};
use crate::mocked_components::{client::MockedClient, USER_CLIENTS};
2025-07-17 12:46:57 -03:00
2025-07-17 17:03:18 -03:00
mod mocked_components;
2025-07-17 12:46:57 -03:00
fn main() {
2025-07-17 13:34:12 -03:00
let mut sequencer = MockedSequencer::new();
let addresses: [Address; 3] = USER_CLIENTS.map(|client| client.user_address());
2025-07-18 15:56:41 -03:00
println!("📝 Initial balances");
print_accounts(&sequencer, &[]);
2025-07-17 13:34:12 -03:00
// A public execution of the Transfer Program
// User1 sends 3 tokens to another account
USER_CLIENTS[1]
.transfer_public(&[0xdeadbeef; 8], 3, &mut sequencer)
.unwrap();
println!("📝 Balances after transfer");
print_accounts(&sequencer, &[]);
2025-07-17 13:34:12 -03:00
// A public execution of the Transfer Program
2025-07-18 18:23:13 -03:00
// User1 sends 51 tokens to the piñata account
2025-07-18 15:56:41 -03:00
USER_CLIENTS[1]
2025-07-18 18:23:13 -03:00
.transfer_public(&PINATA_ADDRESS, 51, &mut sequencer)
.unwrap();
2025-07-18 15:56:41 -03:00
println!("📝 Balances after transfer");
print_accounts(&sequencer, &[]);
2025-07-17 13:34:12 -03:00
2025-07-17 15:34:35 -03:00
// A shielded execution of the Transfer Program
2025-07-18 18:23:13 -03:00
// User0 shields 15 tokens to a new private account of User1
2025-07-18 15:56:41 -03:00
let private_account_user_1 = USER_CLIENTS[0]
.transfer_shielded(&addresses[1], 15, &mut sequencer)
.unwrap();
2025-07-18 15:56:41 -03:00
println!("📝 Balances after shielded execution");
print_accounts(&sequencer, &[&private_account_user_1]);
2025-07-17 13:34:12 -03:00
// A private execution of the Transfer Program
2025-07-18 18:23:13 -03:00
// User1 uses it's private account to send 8 tokens to a new private account of User2
2025-07-18 15:56:41 -03:00
let [private_account_user_1, private_account_user_2] = USER_CLIENTS[1]
.transfer_private(private_account_user_1, &addresses[2], 8, &mut sequencer)
.unwrap();
2025-07-18 20:34:07 -03:00
println!("📝 Balances after private execution");
2025-07-18 17:30:38 -03:00
print_accounts(&sequencer, &[&private_account_user_1, &private_account_user_2]);
2025-07-17 15:34:35 -03:00
// A deshielded execution of the Transfer Program
2025-07-18 18:23:13 -03:00
// User2 deshields 1 token to the public account of User0
let private_account_user_2 = USER_CLIENTS[2]
2025-07-18 15:56:41 -03:00
.transfer_deshielded(private_account_user_2, &addresses[0], 1, &mut sequencer)
.unwrap();
2025-07-18 15:56:41 -03:00
println!("📝 Balances after deshielded execution");
2025-07-18 18:23:13 -03:00
print_accounts(&sequencer, &[&private_account_user_1, &private_account_user_2]);
2025-07-17 16:05:24 -03:00
2025-07-18 15:56:41 -03:00
// A public execution of the Piñata program
2025-07-18 18:23:13 -03:00
// User2 claims the prize of the Piñata program to its public account
2025-07-17 16:05:24 -03:00
let preimage = bytes_to_words(b"NSSA Selective privacy is great!").to_vec();
sequencer
2025-07-18 18:23:13 -03:00
.process_public_execution::<PinataProgram>(&[PINATA_ADDRESS, addresses[2]], preimage)
2025-07-17 16:05:24 -03:00
.unwrap();
2025-07-18 15:56:41 -03:00
println!("📝 Balances after public piñata execution");
2025-07-18 18:23:13 -03:00
print_accounts(&sequencer, &[&private_account_user_1, &private_account_user_2]);
2025-07-18 15:56:41 -03:00
2025-07-18 21:47:29 -03:00
// A shielded execution of the Piñata program
2025-07-18 18:23:13 -03:00
// User1 claims the prize of the Piñata program to a new self-owned private account
2025-07-18 21:47:29 -03:00
let another_private_account_user_1 = {
2025-07-18 18:23:13 -03:00
// All of this is executed locally by the User1
let pinata_account = sequencer.get_account(&PINATA_ADDRESS);
2025-07-18 18:23:13 -03:00
let receiver_account = MockedClient::fresh_account_for_mint(USER_CLIENTS[1].user_address());
let visibilities = [AccountVisibility::Public, AccountVisibility::Private(None)];
2025-07-18 15:56:41 -03:00
let preimage = bytes_to_words(b"NSSA Selective privacy is great!").to_vec();
let private_outputs = MockedClient::prove_and_send_to_sequencer::<PinataProgram>(
&[pinata_account, receiver_account],
preimage,
&visibilities,
sequencer.get_commitment_tree_root(),
&mut sequencer,
)
.unwrap();
2025-07-18 21:47:29 -03:00
let [private_account_user_1] = private_outputs.try_into().unwrap();
private_account_user_1
2025-07-18 15:56:41 -03:00
};
println!("📝 Balances after private piñata execution");
print_accounts(
&sequencer,
2025-07-18 20:34:07 -03:00
&[
&private_account_user_1,
&private_account_user_2,
2025-07-18 21:47:29 -03:00
&another_private_account_user_1,
2025-07-18 20:34:07 -03:00
],
2025-07-18 15:56:41 -03:00
);
2025-07-18 17:30:38 -03:00
println!("Ok!");
2025-07-17 15:34:35 -03:00
}