This commit is contained in:
2026-05-05 05:03:11 +02:00
parent 3e994995d7
commit f2c227e08f
132 changed files with 5767 additions and 4461 deletions

View File

@@ -79,7 +79,7 @@ enum KbMeteoraDammV1InstructionKind {
impl KbMeteoraDammV1Decoder {
/// Creates a new decoder.
pub fn new() -> Self {
Self
return Self;
}
/// Decodes one projected transaction into zero or more Meteora DAMM v1 events.
@@ -96,7 +96,7 @@ impl KbMeteoraDammV1Decoder {
"chain transaction '{}' has no internal id",
transaction.signature
)));
}
},
};
let transaction_json_result =
serde_json::from_str::<serde_json::Value>(transaction.transaction_json.as_str());
@@ -107,7 +107,7 @@ impl KbMeteoraDammV1Decoder {
"cannot parse transaction_json for signature '{}': {}",
transaction.signature, error
)));
}
},
};
let log_messages = kb_extract_log_messages(&transaction_json);
let mut decoded_events = std::vec::Vec::new();
@@ -143,50 +143,29 @@ impl KbMeteoraDammV1Decoder {
kb_classify_instruction_kind(parsed_json.as_ref(), &log_messages);
let pool_account = kb_extract_string_by_candidate_keys(
parsed_json.as_ref(),
&[
"pool",
"poolAddress",
"poolAccount",
"amm",
"ammPool",
"poolState",
],
&["pool", "poolAddress", "poolAccount", "amm", "ammPool", "poolState"],
)
.or_else(|| kb_extract_account(&accounts, 0));
.or_else(|| return kb_extract_account(&accounts, 0));
let token_a_mint = kb_extract_string_by_candidate_keys(
parsed_json.as_ref(),
&[
"tokenAMint",
"mintA",
"baseMint",
"token0Mint",
"mint0",
"coinMint",
],
&["tokenAMint", "mintA", "baseMint", "token0Mint", "mint0", "coinMint"],
)
.or_else(|| kb_extract_account(&accounts, 1));
.or_else(|| return kb_extract_account(&accounts, 1));
let token_b_mint = kb_extract_string_by_candidate_keys(
parsed_json.as_ref(),
&[
"tokenBMint",
"mintB",
"quoteMint",
"token1Mint",
"mint1",
"pcMint",
],
&["tokenBMint", "mintB", "quoteMint", "token1Mint", "mint1", "pcMint"],
)
.or_else(|| kb_extract_account(&accounts, 2));
.or_else(|| return kb_extract_account(&accounts, 2));
let config_account = kb_extract_string_by_candidate_keys(
parsed_json.as_ref(),
&["config", "poolConfig", "ammConfig", "tradeFeeConfig"],
)
.or_else(|| kb_extract_account(&accounts, 3));
.or_else(|| return kb_extract_account(&accounts, 3));
let creator = kb_extract_string_by_candidate_keys(
parsed_json.as_ref(),
&["creator", "payer", "user", "owner"],
)
.or_else(|| kb_extract_account(&accounts, 4));
.or_else(|| return kb_extract_account(&accounts, 4));
if instruction_kind == KbMeteoraDammV1InstructionKind::CreatePool
|| instruction_kind == KbMeteoraDammV1InstructionKind::CreatePoolWithConfig
{
@@ -257,7 +236,7 @@ impl KbMeteoraDammV1Decoder {
));
}
}
Ok(decoded_events)
return Ok(decoded_events);
}
}
@@ -297,7 +276,7 @@ fn kb_classify_instruction_kind(
if kb_log_messages_contain_keyword(log_messages, "swap") {
return KbMeteoraDammV1InstructionKind::Swap;
}
KbMeteoraDammV1InstructionKind::Unknown
return KbMeteoraDammV1InstructionKind::Unknown;
}
fn kb_extract_log_messages(
@@ -325,7 +304,7 @@ fn kb_extract_log_messages(
messages.push(text.to_string());
}
}
messages
return messages;
}
fn kb_log_messages_contain_keyword(log_messages: &[std::string::String], keyword: &str) -> bool {
@@ -336,7 +315,7 @@ fn kb_log_messages_contain_keyword(log_messages: &[std::string::String], keyword
return true;
}
}
false
return false;
}
fn kb_normalize_text(value: &str) -> std::string::String {
@@ -346,7 +325,7 @@ fn kb_normalize_text(value: &str) -> std::string::String {
normalized.push(character.to_ascii_lowercase());
}
}
normalized
return normalized;
}
fn kb_parse_accounts_json(
@@ -360,7 +339,7 @@ fn kb_parse_accounts_json(
"cannot parse instruction accounts_json '{}': {}",
accounts_json, error
)));
}
},
};
let mut accounts = std::vec::Vec::new();
for value in values {
@@ -369,7 +348,7 @@ fn kb_parse_accounts_json(
accounts.push(text.to_string());
}
}
Ok(accounts)
return Ok(accounts);
}
fn kb_parse_optional_parsed_json(
@@ -381,11 +360,13 @@ fn kb_parse_optional_parsed_json(
};
let value_result = serde_json::from_str::<serde_json::Value>(parsed_json.as_str());
match value_result {
Ok(value) => Ok(Some(value)),
Err(error) => Err(crate::KbError::Json(format!(
"cannot parse instruction parsed_json '{}': {}",
parsed_json, error
))),
Ok(value) => return Ok(Some(value)),
Err(error) => {
return Err(crate::KbError::Json(format!(
"cannot parse instruction parsed_json '{}': {}",
parsed_json, error
)));
},
}
}
@@ -397,7 +378,7 @@ fn kb_extract_string_by_candidate_keys(
Some(value) => value,
None => return None,
};
kb_extract_string_by_candidate_keys_inner(value, candidate_keys)
return kb_extract_string_by_candidate_keys_inner(value, candidate_keys);
}
fn kb_extract_string_by_candidate_keys_inner(
@@ -432,7 +413,7 @@ fn kb_extract_string_by_candidate_keys_inner(
}
}
}
None
return None;
}
fn kb_value_contains_any_key(
@@ -443,7 +424,7 @@ fn kb_value_contains_any_key(
Some(value) => value,
None => return false,
};
kb_value_contains_any_key_inner(value, candidate_keys)
return kb_value_contains_any_key_inner(value, candidate_keys);
}
fn kb_value_contains_any_key_inner(value: &serde_json::Value, candidate_keys: &[&str]) -> bool {
@@ -467,7 +448,7 @@ fn kb_value_contains_any_key_inner(value: &serde_json::Value, candidate_keys: &[
}
}
}
false
return false;
}
fn kb_extract_account(
@@ -477,7 +458,7 @@ fn kb_extract_account(
if index >= accounts.len() {
return None;
}
Some(accounts[index].clone())
return Some(accounts[index].clone());
}
fn kb_infer_trade_side(log_messages: &[std::string::String]) -> crate::KbSwapTradeSide {
@@ -487,7 +468,7 @@ fn kb_infer_trade_side(log_messages: &[std::string::String]) -> crate::KbSwapTra
if kb_log_messages_contain_keyword(log_messages, "sell") {
return crate::KbSwapTradeSide::SellBase;
}
crate::KbSwapTradeSide::Unknown
return crate::KbSwapTradeSide::Unknown;
}
#[cfg(test)]
@@ -517,7 +498,7 @@ mod tests {
.to_string(),
);
dto.id = Some(501);
dto
return dto;
}
fn make_create_instruction() -> crate::KbChainInstructionDto {
@@ -554,7 +535,7 @@ mod tests {
),
);
dto.id = Some(502);
dto
return dto;
}
fn make_swap_transaction() -> crate::KbChainTransactionDto {
@@ -582,7 +563,7 @@ mod tests {
.to_string(),
);
dto.id = Some(503);
dto
return dto;
}
fn make_swap_instruction() -> crate::KbChainInstructionDto {
@@ -615,7 +596,7 @@ mod tests {
),
);
dto.id = Some(504);
dto
return dto;
}
#[test]
@@ -640,10 +621,10 @@ mod tests {
Some("So11111111111111111111111111111111111111112".to_string())
);
assert!(event.used_config);
}
},
crate::KbMeteoraDammV1DecodedEvent::Swap(_) => {
panic!("unexpected swap event")
}
},
}
}
@@ -668,10 +649,10 @@ mod tests {
event.token_b_mint,
Some("So11111111111111111111111111111111111111112".to_string())
);
}
},
crate::KbMeteoraDammV1DecodedEvent::CreatePool(_) => {
panic!("unexpected create event")
}
},
}
}
}