Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Log Program for Raydium AMM #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 17 additions & 35 deletions program/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use arrform::{arrform, ArrForm};
use serde::{Deserialize, Serialize};
use solana_program::{
msg,
// entrypoint::ProgramResult,
pubkey::Pubkey,
};

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -97,85 +92,72 @@ 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,
}

#[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<T: Serialize>(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<T: Serialize>(log: &T) -> String {
let bytes = bincode::serialize(log).expect("Serialization failed");
base64::encode(bytes)
}

pub fn log_ray_log<T: Serialize>(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);
}
}
Expand Down