From 0e30ac0cef0a939350e7463d79e893ad1a5f75c4 Mon Sep 17 00:00:00 2001 From: techvoyagerX Date: Tue, 27 Aug 2024 22:23:18 +0800 Subject: [PATCH] Refactor Log Program: Improved naming, separated concerns, and enhanced error handling --- program/src/log.rs | 52 +++++++++++++++------------------------------- 1 file changed, 17 insertions(+), 35 deletions(-) diff --git a/program/src/log.rs b/program/src/log.rs index 2a28041..e574b7b 100644 --- a/program/src/log.rs +++ b/program/src/log.rs @@ -2,7 +2,6 @@ use arrform::{arrform, ArrForm}; use serde::{Deserialize, Serialize}; use solana_program::{ msg, - // entrypoint::ProgramResult, pubkey::Pubkey, }; @@ -29,7 +28,6 @@ pub fn log_keys_mismatch(msg: &str, input: Pubkey, expected: Pubkey) { .as_str()); } -/// LogType enum #[derive(Debug)] pub enum LogType { Init, @@ -47,11 +45,11 @@ impl LogType { 2 => LogType::Withdraw, 3 => LogType::SwapBaseIn, 4 => LogType::SwapBaseOut, - _ => unreachable!(), + _ => unreachable!("Invalid log type"), } } - pub fn into_u8(&self) -> u8 { + pub fn to_u8(&self) -> u8 { match self { LogType::Init => 0u8, LogType::Deposit => 1u8, @@ -65,7 +63,7 @@ impl LogType { #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] pub struct InitLog { pub log_type: u8, - pub time: u64, + pub timestamp: u64, pub pc_decimals: u8, pub coin_decimals: u8, pub pc_lot_size: u64, @@ -78,17 +76,14 @@ pub struct InitLog { #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] pub struct DepositLog { pub log_type: u8, - // input pub max_coin: u64, pub max_pc: u64, pub base: u64, - // pool info pub pool_coin: u64, pub pool_pc: u64, pub pool_lp: u64, pub calc_pnl_x: u128, pub calc_pnl_y: u128, - // calc result pub deduct_coin: u64, pub deduct_pc: u64, pub mint_lp: u64, @@ -97,17 +92,13 @@ pub struct DepositLog { #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] pub struct WithdrawLog { pub log_type: u8, - // input pub withdraw_lp: u64, - // user info pub user_lp: u64, - // pool info pub pool_coin: u64, pub pool_pc: u64, pub pool_lp: u64, pub calc_pnl_x: u128, pub calc_pnl_y: u128, - // calc result pub out_coin: u64, pub out_pc: u64, } @@ -115,67 +106,58 @@ pub struct WithdrawLog { #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] pub struct SwapBaseInLog { pub log_type: u8, - // input pub amount_in: u64, pub minimum_out: u64, pub direction: u64, - // user info pub user_source: u64, - // pool info pub pool_coin: u64, pub pool_pc: u64, - // calc result pub out_amount: u64, } #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] pub struct SwapBaseOutLog { pub log_type: u8, - // input pub max_in: u64, pub amount_out: u64, pub direction: u64, - // user info pub user_source: u64, - // pool info pub pool_coin: u64, pub pool_pc: u64, - // calc result pub deduct_in: u64, } -pub fn encode_ray_log(log: T) { - // encode - let bytes = bincode::serialize(&log).unwrap(); - let mut out_buf = Vec::new(); - out_buf.resize(bytes.len() * 4 / 3 + 4, 0); - let bytes_written = base64::encode_config_slice(bytes, base64::STANDARD, &mut out_buf); - out_buf.resize(bytes_written, 0); - let msg_str = unsafe { std::str::from_utf8_unchecked(&out_buf) }; - msg!(arrform!(LOG_SIZE, "ray_log: {}", msg_str).as_str()); +pub fn encode_ray_log(log: &T) -> String { + let bytes = bincode::serialize(log).expect("Serialization failed"); + base64::encode(bytes) +} + +pub fn log_ray_log(log: &T) { + let encoded_log = encode_ray_log(log); + msg!(arrform!(LOG_SIZE, "ray_log: {}", encoded_log).as_str()); } pub fn decode_ray_log(log: &str) { - let bytes = base64::decode_config(log, base64::STANDARD).unwrap(); + let bytes = base64::decode(log).expect("Decoding failed"); match LogType::from_u8(bytes[0]) { LogType::Init => { - let log: InitLog = bincode::deserialize(&bytes).unwrap(); + let log: InitLog = bincode::deserialize(&bytes).expect("Deserialization failed"); println!("{:?}", log); } LogType::Deposit => { - let log: DepositLog = bincode::deserialize(&bytes).unwrap(); + let log: DepositLog = bincode::deserialize(&bytes).expect("Deserialization failed"); println!("{:?}", log); } LogType::Withdraw => { - let log: WithdrawLog = bincode::deserialize(&bytes).unwrap(); + let log: WithdrawLog = bincode::deserialize(&bytes).expect("Deserialization failed"); println!("{:?}", log); } LogType::SwapBaseIn => { - let log: SwapBaseInLog = bincode::deserialize(&bytes).unwrap(); + let log: SwapBaseInLog = bincode::deserialize(&bytes).expect("Deserialization failed"); println!("{:?}", log); } LogType::SwapBaseOut => { - let log: SwapBaseOutLog = bincode::deserialize(&bytes).unwrap(); + let log: SwapBaseOutLog = bincode::deserialize(&bytes).expect("Deserialization failed"); println!("{:?}", log); } }