0.7.27 +Refactor
This commit is contained in:
@@ -2,12 +2,9 @@
|
||||
|
||||
//! Raydium AmmV4 transaction decoder.
|
||||
|
||||
/// Raydium AmmV4 program id.
|
||||
pub const KB_RAYDIUM_AMM_V4_PROGRAM_ID: &str = "675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8";
|
||||
|
||||
/// Decoded Raydium AmmV4 initialize2 pool event.
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct KbRaydiumAmmV4Initialize2PoolDecoded {
|
||||
pub struct RaydiumAmmV4Initialize2PoolDecoded {
|
||||
/// Parent transaction id.
|
||||
pub transaction_id: i64,
|
||||
/// Parent instruction id.
|
||||
@@ -32,16 +29,16 @@ pub struct KbRaydiumAmmV4Initialize2PoolDecoded {
|
||||
|
||||
/// Decoded Raydium AmmV4 event.
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub enum KbRaydiumAmmV4DecodedEvent {
|
||||
pub enum RaydiumAmmV4DecodedEvent {
|
||||
/// `initialize2` pool creation-like event.
|
||||
Initialize2Pool(KbRaydiumAmmV4Initialize2PoolDecoded),
|
||||
Initialize2Pool(RaydiumAmmV4Initialize2PoolDecoded),
|
||||
}
|
||||
|
||||
/// Raydium AmmV4 decoder.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct KbRaydiumAmmV4Decoder;
|
||||
pub struct RaydiumAmmV4Decoder;
|
||||
|
||||
impl KbRaydiumAmmV4Decoder {
|
||||
impl RaydiumAmmV4Decoder {
|
||||
/// Creates a new decoder.
|
||||
pub fn new() -> Self {
|
||||
return Self;
|
||||
@@ -50,14 +47,14 @@ impl KbRaydiumAmmV4Decoder {
|
||||
/// Decodes one projected transaction into zero or more Raydium AmmV4 events.
|
||||
pub fn decode_transaction(
|
||||
&self,
|
||||
transaction: &crate::KbChainTransactionDto,
|
||||
instructions: &[crate::KbChainInstructionDto],
|
||||
) -> Result<std::vec::Vec<crate::KbRaydiumAmmV4DecodedEvent>, crate::KbError> {
|
||||
transaction: &crate::ChainTransactionDto,
|
||||
instructions: &[crate::ChainInstructionDto],
|
||||
) -> Result<std::vec::Vec<crate::RaydiumAmmV4DecodedEvent>, crate::Error> {
|
||||
let transaction_id_option = transaction.id;
|
||||
let transaction_id = match transaction_id_option {
|
||||
Some(transaction_id) => transaction_id,
|
||||
None => {
|
||||
return Err(crate::KbError::InvalidState(format!(
|
||||
return Err(crate::Error::InvalidState(format!(
|
||||
"chain transaction '{}' has no internal id",
|
||||
transaction.signature
|
||||
)));
|
||||
@@ -68,14 +65,14 @@ impl KbRaydiumAmmV4Decoder {
|
||||
let transaction_json = match transaction_json_result {
|
||||
Ok(transaction_json) => transaction_json,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Json(format!(
|
||||
return Err(crate::Error::Json(format!(
|
||||
"cannot parse transaction_json for signature '{}': {}",
|
||||
transaction.signature, error
|
||||
)));
|
||||
},
|
||||
};
|
||||
let log_messages = kb_extract_log_messages(&transaction_json);
|
||||
let has_initialize2_log = kb_log_messages_contain_initialize2(&log_messages);
|
||||
let log_messages = extract_log_messages(&transaction_json);
|
||||
let has_initialize2_log = log_messages_contain_initialize2(&log_messages);
|
||||
let mut decoded_events = std::vec::Vec::new();
|
||||
for instruction in instructions {
|
||||
if instruction.parent_instruction_id.is_some() {
|
||||
@@ -86,7 +83,7 @@ impl KbRaydiumAmmV4Decoder {
|
||||
Some(program_id) => program_id,
|
||||
None => continue,
|
||||
};
|
||||
if program_id.as_str() != crate::KB_RAYDIUM_AMM_V4_PROGRAM_ID {
|
||||
if program_id.as_str() != crate::RAYDIUM_AMM_V4_PROGRAM_ID {
|
||||
continue;
|
||||
}
|
||||
if !has_initialize2_log {
|
||||
@@ -97,7 +94,7 @@ impl KbRaydiumAmmV4Decoder {
|
||||
Some(instruction_id) => instruction_id,
|
||||
None => continue,
|
||||
};
|
||||
let accounts_result = kb_parse_accounts_json(instruction.accounts_json.as_str());
|
||||
let accounts_result = parse_accounts_json(instruction.accounts_json.as_str());
|
||||
let accounts = match accounts_result {
|
||||
Ok(accounts) => accounts,
|
||||
Err(error) => return Err(error),
|
||||
@@ -105,11 +102,11 @@ impl KbRaydiumAmmV4Decoder {
|
||||
if accounts.len() < 10 {
|
||||
continue;
|
||||
}
|
||||
let pool_account = kb_extract_account(&accounts, 4);
|
||||
let lp_mint = kb_extract_account(&accounts, 7);
|
||||
let token_a_mint = kb_extract_account(&accounts, 8);
|
||||
let token_b_mint = kb_extract_account(&accounts, 9);
|
||||
let market_account = kb_extract_account(&accounts, 16);
|
||||
let pool_account = extract_account(&accounts, 4);
|
||||
let lp_mint = extract_account(&accounts, 7);
|
||||
let token_a_mint = extract_account(&accounts, 8);
|
||||
let token_b_mint = extract_account(&accounts, 9);
|
||||
let market_account = extract_account(&accounts, 16);
|
||||
let payload_json = serde_json::json!({
|
||||
"decoder": "raydium_amm_v4",
|
||||
"eventKind": "initialize2_pool",
|
||||
@@ -124,8 +121,8 @@ impl KbRaydiumAmmV4Decoder {
|
||||
"tokenBMint": token_b_mint,
|
||||
"marketAccount": market_account
|
||||
});
|
||||
decoded_events.push(crate::KbRaydiumAmmV4DecodedEvent::Initialize2Pool(
|
||||
crate::KbRaydiumAmmV4Initialize2PoolDecoded {
|
||||
decoded_events.push(crate::RaydiumAmmV4DecodedEvent::Initialize2Pool(
|
||||
crate::RaydiumAmmV4Initialize2PoolDecoded {
|
||||
transaction_id,
|
||||
instruction_id,
|
||||
signature: transaction.signature.clone(),
|
||||
@@ -139,11 +136,11 @@ impl KbRaydiumAmmV4Decoder {
|
||||
},
|
||||
));
|
||||
}
|
||||
return Ok(decoded_events)
|
||||
return Ok(decoded_events);
|
||||
}
|
||||
}
|
||||
|
||||
fn kb_extract_log_messages(
|
||||
fn extract_log_messages(
|
||||
transaction_json: &serde_json::Value,
|
||||
) -> std::vec::Vec<std::string::String> {
|
||||
let mut messages = std::vec::Vec::new();
|
||||
@@ -168,26 +165,26 @@ fn kb_extract_log_messages(
|
||||
messages.push(text.to_string());
|
||||
}
|
||||
}
|
||||
return messages
|
||||
return messages;
|
||||
}
|
||||
|
||||
fn kb_log_messages_contain_initialize2(log_messages: &[std::string::String]) -> bool {
|
||||
fn log_messages_contain_initialize2(log_messages: &[std::string::String]) -> bool {
|
||||
for log_message in log_messages {
|
||||
if log_message.contains("initialize2") {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
|
||||
fn kb_parse_accounts_json(
|
||||
fn parse_accounts_json(
|
||||
accounts_json: &str,
|
||||
) -> Result<std::vec::Vec<std::string::String>, crate::KbError> {
|
||||
) -> Result<std::vec::Vec<std::string::String>, crate::Error> {
|
||||
let values_result = serde_json::from_str::<std::vec::Vec<serde_json::Value>>(accounts_json);
|
||||
let values = match values_result {
|
||||
Ok(values) => values,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Json(format!(
|
||||
return Err(crate::Error::Json(format!(
|
||||
"cannot parse instruction accounts_json '{}': {}",
|
||||
accounts_json, error
|
||||
)));
|
||||
@@ -200,23 +197,23 @@ fn kb_parse_accounts_json(
|
||||
accounts.push(text.to_string());
|
||||
}
|
||||
}
|
||||
return Ok(accounts)
|
||||
return Ok(accounts);
|
||||
}
|
||||
|
||||
fn kb_extract_account(
|
||||
fn extract_account(
|
||||
accounts: &[std::string::String],
|
||||
index: usize,
|
||||
) -> std::option::Option<std::string::String> {
|
||||
if index >= accounts.len() {
|
||||
return None;
|
||||
}
|
||||
return Some(accounts[index].clone())
|
||||
return Some(accounts[index].clone());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
fn make_transaction() -> crate::KbChainTransactionDto {
|
||||
let mut dto = crate::KbChainTransactionDto::new(
|
||||
fn make_transaction() -> crate::ChainTransactionDto {
|
||||
let mut dto = crate::ChainTransactionDto::new(
|
||||
"sig-raydium-test-1".to_string(),
|
||||
Some(888888),
|
||||
Some(1778000000),
|
||||
@@ -240,16 +237,16 @@ mod tests {
|
||||
.to_string(),
|
||||
);
|
||||
dto.id = Some(42);
|
||||
return dto
|
||||
return dto;
|
||||
}
|
||||
|
||||
fn make_instruction() -> crate::KbChainInstructionDto {
|
||||
let mut dto = crate::KbChainInstructionDto::new(
|
||||
fn make_instruction() -> crate::ChainInstructionDto {
|
||||
let mut dto = crate::ChainInstructionDto::new(
|
||||
42,
|
||||
None,
|
||||
0,
|
||||
None,
|
||||
Some(crate::KB_RAYDIUM_AMM_V4_PROGRAM_ID.to_string()),
|
||||
Some(crate::RAYDIUM_AMM_V4_PROGRAM_ID.to_string()),
|
||||
Some("raydium-amm-v4".to_string()),
|
||||
Some(1),
|
||||
serde_json::json!([
|
||||
@@ -277,12 +274,12 @@ mod tests {
|
||||
None,
|
||||
);
|
||||
dto.id = Some(7);
|
||||
return dto
|
||||
return dto;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raydium_amm_v4_initialize2_logs_are_detected() {
|
||||
let decoder = crate::KbRaydiumAmmV4Decoder::new();
|
||||
let decoder = crate::RaydiumAmmV4Decoder::new();
|
||||
let transaction = make_transaction();
|
||||
let instructions = vec![make_instruction()];
|
||||
let decoded_result = decoder.decode_transaction(&transaction, &instructions);
|
||||
@@ -292,7 +289,7 @@ mod tests {
|
||||
};
|
||||
assert_eq!(decoded.len(), 1);
|
||||
match &decoded[0] {
|
||||
crate::KbRaydiumAmmV4DecodedEvent::Initialize2Pool(event) => {
|
||||
crate::RaydiumAmmV4DecodedEvent::Initialize2Pool(event) => {
|
||||
assert_eq!(event.transaction_id, 42);
|
||||
assert_eq!(event.instruction_id, 7);
|
||||
assert_eq!(event.pool_account, Some("Pool111".to_string()));
|
||||
@@ -306,7 +303,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn raydium_amm_v4_initialize2_returns_none_without_expected_log() {
|
||||
let decoder = crate::KbRaydiumAmmV4Decoder::new();
|
||||
let decoder = crate::RaydiumAmmV4Decoder::new();
|
||||
let mut transaction = make_transaction();
|
||||
transaction.transaction_json = serde_json::json!({
|
||||
"slot": 888888,
|
||||
|
||||
Reference in New Issue
Block a user