From 639c282c61a6901f95b763b6ebf1295269c2b317 Mon Sep 17 00:00:00 2001 From: Pravdyvy Date: Mon, 22 Dec 2025 19:34:47 +0200 Subject: [PATCH] fix: suggestions fix 2 --- wallet/src/program_facades/amm.rs | 24 +++++++------------ wallet/src/program_facades/mod.rs | 40 ------------------------------- 2 files changed, 9 insertions(+), 55 deletions(-) diff --git a/wallet/src/program_facades/amm.rs b/wallet/src/program_facades/amm.rs index 72ee2fe..3beb92c 100644 --- a/wallet/src/program_facades/amm.rs +++ b/wallet/src/program_facades/amm.rs @@ -2,10 +2,7 @@ use common::{error::ExecutionFailureKind, rpc_primitives::requests::SendTxRespon use nssa::{AccountId, ProgramId, program::Program}; use nssa_core::program::PdaSeed; -use crate::{ - TokenHolding, WalletCore, - program_facades::{OrphanHack49BytesInput, OrphanHack65BytesInput}, -}; +use crate::{TokenHolding, WalletCore}; fn compute_pool_pda( amm_program_id: ProgramId, @@ -451,10 +448,7 @@ impl Amm<'_> { } } -fn amm_program_preparation_definition( - balance_a: u128, - balance_b: u128, -) -> (OrphanHack65BytesInput, Program) { +fn amm_program_preparation_definition(balance_a: u128, balance_b: u128) -> (Vec, Program) { // An instruction data of 65-bytes, indicating the initial amm reserves' balances and // token_program_id with the following layout: // [0x00 || array of balances (little-endian 16 bytes) || AMM_PROGRAM_ID)] @@ -474,7 +468,7 @@ fn amm_program_preparation_definition( instruction[57..61].copy_from_slice(&amm_program_id[6].to_le_bytes()); instruction[61..].copy_from_slice(&amm_program_id[7].to_le_bytes()); - let instruction_data = OrphanHack65BytesInput::expand(instruction); + let instruction_data = instruction.to_vec(); let program = Program::amm(); (instruction_data, program) @@ -484,7 +478,7 @@ fn amm_program_preparation_swap( amount_in: u128, min_amount_out: u128, token_definition_id: AccountId, -) -> (OrphanHack65BytesInput, Program) { +) -> (Vec, Program) { // An instruction data byte string of length 65, indicating which token type to swap, quantity // of tokens put into the swap (of type TOKEN_DEFINITION_ID) and min_amount_out. // [0x01 || amount (little-endian 16 bytes) || TOKEN_DEFINITION_ID]. @@ -496,7 +490,7 @@ fn amm_program_preparation_swap( // This can be done less verbose, but it is better to use same way, as in amm program instruction[33..].copy_from_slice(&token_definition_id.to_bytes()); - let instruction_data = OrphanHack65BytesInput::expand(instruction); + let instruction_data = instruction.to_vec(); let program = Program::amm(); (instruction_data, program) @@ -506,7 +500,7 @@ fn amm_program_preparation_add_liq( min_amount_lp: u128, max_amount_a: u128, max_amount_b: u128, -) -> (OrphanHack49BytesInput, Program) { +) -> (Vec, Program) { // An instruction data byte string of length 49, amounts for minimum amount of liquidity from // add (min_amount_lp), max amount added for each token (max_amount_a and max_amount_b); // indicate [0x02 || array of of balances (little-endian 16 bytes)]. @@ -517,7 +511,7 @@ fn amm_program_preparation_add_liq( instruction[17..33].copy_from_slice(&max_amount_a.to_le_bytes()); instruction[33..49].copy_from_slice(&max_amount_b.to_le_bytes()); - let instruction_data = OrphanHack49BytesInput::expand(instruction); + let instruction_data = instruction.to_vec(); let program = Program::amm(); (instruction_data, program) @@ -527,7 +521,7 @@ fn amm_program_preparation_remove_liq( balance_lp: u128, min_amount_a: u128, min_amount_b: u128, -) -> (OrphanHack49BytesInput, Program) { +) -> (Vec, Program) { // An instruction data byte string of length 49, amounts for minimum amount of liquidity to // redeem (balance_lp), minimum balance of each token to remove (min_amount_a and // min_amount_b); indicate [0x03 || array of balances (little-endian 16 bytes)]. @@ -538,7 +532,7 @@ fn amm_program_preparation_remove_liq( instruction[17..33].copy_from_slice(&min_amount_a.to_le_bytes()); instruction[33..49].copy_from_slice(&min_amount_b.to_le_bytes()); - let instruction_data = OrphanHack49BytesInput::expand(instruction); + let instruction_data = instruction.to_vec(); let program = Program::amm(); (instruction_data, program) diff --git a/wallet/src/program_facades/mod.rs b/wallet/src/program_facades/mod.rs index 2b69664..5fdcdb3 100644 --- a/wallet/src/program_facades/mod.rs +++ b/wallet/src/program_facades/mod.rs @@ -1,47 +1,7 @@ //! This module contains [`WalletCore`](crate::WalletCore) facades for interacting with various //! on-chain programs. -use serde::{Serialize, ser::SerializeSeq}; - pub mod amm; pub mod native_token_transfer; pub mod pinata; pub mod token; - -/// Why it is necessary: -/// -/// Serialize implemented only for `[u8; N]` where `N<=32` and orphan rules would disallow custom -/// Serialize impls for them. -/// -/// Additionally, RISC0 splits instructions into words of 4-byte size which glues bytes for custom -/// structs so we need to expand each byte into `u32` to preserve shape, because AMM awaits -/// `Vec` as instruction. -struct OrphanHackNBytesInput([u32; N]); - -impl OrphanHackNBytesInput { - fn expand(orig: [u8; N]) -> Self { - let mut res = [0u32; N]; - - for (idx, val) in orig.into_iter().enumerate() { - res[idx] = val as u32; - } - - Self(res) - } -} - -impl Serialize for OrphanHackNBytesInput { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - let mut seq = serializer.serialize_seq(Some(N))?; - for word in self.0 { - seq.serialize_element(&word)?; - } - seq.end() - } -} - -type OrphanHack65BytesInput = OrphanHackNBytesInput<65>; -type OrphanHack49BytesInput = OrphanHackNBytesInput<49>;