refactor program input

This commit is contained in:
Sergio Chouhy 2025-08-14 14:30:04 -03:00
parent c5a4e83e3e
commit d1ebb831ef
10 changed files with 47 additions and 32 deletions

View File

@ -7,17 +7,25 @@ pub type ProgramId = [u32; 8];
pub type InstructionData = Vec<u32>;
pub const DEFAULT_PROGRAM_ID: ProgramId = [0; 8];
pub struct ProgramInput<T> {
pub pre_states: Vec<AccountWithMetadata>,
pub instruction: T,
}
#[derive(Serialize, Deserialize)]
pub struct ProgramOutput {
pub pre_states: Vec<AccountWithMetadata>,
pub post_states: Vec<Account>,
}
pub fn read_nssa_inputs<T: DeserializeOwned>() -> (Vec<AccountWithMetadata>, T) {
pub fn read_nssa_inputs<T: DeserializeOwned>() -> ProgramInput<T> {
let pre_states: Vec<AccountWithMetadata> = env::read();
let words: InstructionData = env::read();
let instruction_data = T::deserialize(&mut Deserializer::new(words.as_ref())).unwrap();
(pre_states, instruction_data)
let instruction = T::deserialize(&mut Deserializer::new(words.as_ref())).unwrap();
ProgramInput {
pre_states,
instruction,
}
}
pub fn write_nssa_outputs(pre_states: Vec<AccountWithMetadata>, post_states: Vec<Account>) {

View File

@ -1,16 +1,17 @@
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
type Instruction = u128;
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs, ProgramInput};
/// 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, balance_to_move) = read_nssa_inputs::<Instruction>();
let ProgramInput {
pre_states,
instruction: balance_to_move,
} = read_nssa_inputs();
// Continue only if input_accounts is an array of two elements
let [sender, receiver] = match input_accounts.try_into() {
let [sender, receiver] = match pre_states.try_into() {
Ok(array) => array,
Err(_) => return,
};

View File

@ -1,11 +1,14 @@
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs, ProgramInput};
type Instruction = u128;
fn main() {
let (input_accounts, balance_to_burn) = read_nssa_inputs::<Instruction>();
let ProgramInput {
pre_states,
instruction: balance_to_burn,
} = read_nssa_inputs::<Instruction>();
let [pre] = match input_accounts.try_into() {
let [pre] = match pre_states.try_into() {
Ok(array) => array,
Err(_) => return,
};

View File

@ -1,11 +1,11 @@
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs, ProgramInput};
type Instruction = ();
fn main() {
let (input_accounts, _) = read_nssa_inputs::<Instruction>();
let ProgramInput { pre_states, .. } = read_nssa_inputs::<Instruction>();
let [pre] = match input_accounts.try_into() {
let [pre] = match pre_states.try_into() {
Ok(array) => array,
Err(_) => return,
};

View File

@ -1,14 +1,14 @@
use nssa_core::{
account::Account,
program::{read_nssa_inputs, write_nssa_outputs},
program::{read_nssa_inputs, write_nssa_outputs, ProgramInput},
};
type Instruction = ();
fn main() {
let (input_accounts, _) = read_nssa_inputs::<Instruction>();
let ProgramInput { pre_states, .. } = read_nssa_inputs::<Instruction>();
let [pre] = match input_accounts.try_into() {
let [pre] = match pre_states.try_into() {
Ok(array) => array,
Err(_) => return,
};

View File

@ -1,11 +1,11 @@
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs, ProgramInput};
type Instruction = ();
fn main() {
let (input_accounts, _) = read_nssa_inputs::<Instruction>();
let ProgramInput { pre_states, .. } = read_nssa_inputs::<Instruction>();
let [pre] = match input_accounts.try_into() {
let [pre] = match pre_states.try_into() {
Ok(array) => array,
Err(_) => return,
};

View File

@ -1,11 +1,11 @@
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs, ProgramInput};
type Instruction = ();
fn main() {
let (input_accounts, _) = read_nssa_inputs::<Instruction>();
let ProgramInput { pre_states, .. } = read_nssa_inputs::<Instruction>();
let [pre1, _] = match input_accounts.try_into() {
let [pre1, _] = match pre_states.try_into() {
Ok(array) => array,
Err(_) => return,
};

View File

@ -1,11 +1,11 @@
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs, ProgramInput};
type Instruction = ();
fn main() {
let (input_accounts, _) = read_nssa_inputs::<Instruction>();
let ProgramInput { pre_states, .. } = read_nssa_inputs::<Instruction>();
let [pre] = match input_accounts.try_into() {
let [pre] = match pre_states.try_into() {
Ok(array) => array,
Err(_) => return,
};

View File

@ -1,11 +1,11 @@
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs, ProgramInput};
type Instruction = ();
fn main() {
let (input_accounts, _) = read_nssa_inputs::<Instruction>();
let ProgramInput { pre_states, .. } = read_nssa_inputs::<Instruction>();
let [pre] = match input_accounts.try_into() {
let [pre] = match pre_states.try_into() {
Ok(array) => array,
Err(_) => return,
};

View File

@ -1,11 +1,14 @@
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs};
use nssa_core::program::{read_nssa_inputs, write_nssa_outputs, ProgramInput};
type Instruction = u128;
fn main() {
let (input_accounts, balance) = read_nssa_inputs::<Instruction>();
let ProgramInput {
pre_states,
instruction: balance,
} = read_nssa_inputs::<Instruction>();
let [sender_pre, receiver_pre] = match input_accounts.try_into() {
let [sender_pre, receiver_pre] = match pre_states.try_into() {
Ok(array) => array,
Err(_) => return,
};