From 3188f98117619484aef6cf38ce30c22d51726985 Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Sat, 9 Aug 2025 18:24:53 -0300 Subject: [PATCH] early returns instead of panicking in program methods --- .../guest/src/bin/authenticated_transfer.rs | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/nssa/program_methods/guest/src/bin/authenticated_transfer.rs b/nssa/program_methods/guest/src/bin/authenticated_transfer.rs index b24e004..979064c 100644 --- a/nssa/program_methods/guest/src/bin/authenticated_transfer.rs +++ b/nssa/program_methods/guest/src/bin/authenticated_transfer.rs @@ -9,17 +9,21 @@ fn main() { let input_accounts: Vec = env::read(); let balance_to_move: u128 = env::read(); - // Unpack sender and receiver - assert_eq!(input_accounts.len(), 2); - let [sender, receiver] = input_accounts - .try_into() - .unwrap_or_else(|_| panic!("Bad input")); + // Continue only if input_accounts is an array of two elements + let [sender, receiver] = match input_accounts.try_into() { + Ok(array) => array, + Err(_) => return, // silently return on bad input + }; - // Check sender has authorized this operation - assert!(sender.is_authorized); + // Continue only if the sender has authorized this operation + if !sender.is_authorized { + return; + } - // Check sender has enough balance - assert!(sender.account.balance >= balance_to_move); + // Continue only if the sender has enough balance + if sender.account.balance < balance_to_move { + return; + } // Create accounts post states, with updated balances let mut sender_post = sender.account.clone();