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

@@ -76,7 +76,7 @@ pub struct KbMeteoraDbcDecoder;
impl KbMeteoraDbcDecoder {
/// Creates a new decoder.
pub fn new() -> Self {
Self
return Self;
}
/// Decodes one projected transaction into zero or more Meteora DBC events.
@@ -93,7 +93,7 @@ impl KbMeteoraDbcDecoder {
"chain transaction '{}' has no internal id",
transaction.signature
)));
}
},
};
let transaction_json_result =
serde_json::from_str::<serde_json::Value>(transaction.transaction_json.as_str());
@@ -104,7 +104,7 @@ impl KbMeteoraDbcDecoder {
"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();
@@ -142,27 +142,27 @@ impl KbMeteoraDbcDecoder {
parsed_json.as_ref(),
&["pool", "poolAccount", "poolState", "virtualPool", "poolKey"],
)
.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(),
&["baseMint", "tokenAMint", "mintA", "token0Mint", "mint0"],
)
.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(),
&["quoteMint", "tokenBMint", "mintB", "token1Mint", "mint1"],
)
.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(),
&["poolConfig", "config", "dbcConfig", "curveConfig"],
)
.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", "poolCreator", "owner", "user"],
)
.or_else(|| kb_extract_account(&accounts, 4));
.or_else(|| return kb_extract_account(&accounts, 4));
if instruction_kind == KbMeteoraDbcInstructionKind::CreatePool {
let payload_json = serde_json::json!({
"decoder": "meteora_dbc",
@@ -228,7 +228,7 @@ impl KbMeteoraDbcDecoder {
));
}
}
Ok(decoded_events)
return Ok(decoded_events);
}
}
@@ -257,7 +257,7 @@ fn kb_extract_log_messages(
messages.push(text.to_string());
}
}
messages
return messages;
}
fn kb_log_messages_contain_any_keyword(
@@ -269,7 +269,7 @@ fn kb_log_messages_contain_any_keyword(
return true;
}
}
false
return false;
}
fn kb_log_messages_contain_keyword(log_messages: &[std::string::String], keyword: &str) -> bool {
@@ -280,7 +280,7 @@ fn kb_log_messages_contain_keyword(log_messages: &[std::string::String], keyword
return true;
}
}
false
return false;
}
fn kb_normalize_log_text(value: &str) -> std::string::String {
@@ -290,7 +290,7 @@ fn kb_normalize_log_text(value: &str) -> std::string::String {
normalized.push(character.to_ascii_lowercase());
}
}
normalized
return normalized;
}
fn kb_parse_accounts_json(
@@ -304,7 +304,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 {
@@ -313,7 +313,7 @@ fn kb_parse_accounts_json(
accounts.push(text.to_string());
}
}
Ok(accounts)
return Ok(accounts);
}
fn kb_parse_optional_parsed_json(
@@ -325,11 +325,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
)));
},
}
}
@@ -341,7 +343,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(
@@ -376,7 +378,7 @@ fn kb_extract_string_by_candidate_keys_inner(
}
}
}
None
return None;
}
fn kb_value_contains_any_key(
@@ -387,7 +389,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 {
@@ -411,7 +413,7 @@ fn kb_value_contains_any_key_inner(value: &serde_json::Value, candidate_keys: &[
}
}
}
false
return false;
}
fn kb_extract_account(
@@ -421,7 +423,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 {
@@ -431,7 +433,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;
}
fn kb_classify_instruction_kind(
@@ -456,32 +458,21 @@ fn kb_classify_instruction_kind(
}
let has_create_config = kb_value_contains_any_key(
parsed_json,
&[
"poolConfig",
"migrationQuoteThreshold",
"curveConfig",
"dbcConfig",
],
&["poolConfig", "migrationQuoteThreshold", "curveConfig", "dbcConfig"],
);
if has_create_config {
return KbMeteoraDbcInstructionKind::CreatePool;
}
if kb_log_messages_contain_any_keyword(
log_messages,
&[
"create_pool",
"createpool",
"initialize_pool",
"initializepool",
"launch_pool",
],
&["create_pool", "createpool", "initialize_pool", "initializepool", "launch_pool"],
) {
return KbMeteoraDbcInstructionKind::CreatePool;
}
if kb_log_messages_contain_any_keyword(log_messages, &["swap2", "swap"]) {
return KbMeteoraDbcInstructionKind::Swap;
}
KbMeteoraDbcInstructionKind::Unknown
return KbMeteoraDbcInstructionKind::Unknown;
}
#[cfg(test)]
@@ -511,7 +502,7 @@ mod tests {
.to_string(),
);
dto.id = Some(301);
dto
return dto;
}
fn make_create_instruction() -> crate::KbChainInstructionDto {
@@ -547,7 +538,7 @@ mod tests {
),
);
dto.id = Some(302);
dto
return dto;
}
fn make_swap_transaction() -> crate::KbChainTransactionDto {
@@ -575,7 +566,7 @@ mod tests {
.to_string(),
);
dto.id = Some(303);
dto
return dto;
}
fn make_swap_instruction() -> crate::KbChainInstructionDto {
@@ -607,7 +598,7 @@ mod tests {
),
);
dto.id = Some(304);
dto
return dto;
}
#[test]
@@ -632,10 +623,10 @@ mod tests {
Some("So11111111111111111111111111111111111111112".to_string())
);
assert_eq!(event.config_account, Some("DbcConfig111".to_string()));
}
},
crate::KbMeteoraDbcDecodedEvent::Swap(_) => {
panic!("unexpected swap event")
}
},
}
}
@@ -660,10 +651,10 @@ mod tests {
event.token_b_mint,
Some("So11111111111111111111111111111111111111112".to_string())
);
}
},
crate::KbMeteoraDbcDecodedEvent::CreatePool(_) => {
panic!("unexpected create event")
}
},
}
}
@@ -687,10 +678,10 @@ mod tests {
};
assert_eq!(decoded.len(), 1);
match &decoded[0] {
crate::KbMeteoraDbcDecodedEvent::Swap(_) => {}
crate::KbMeteoraDbcDecodedEvent::Swap(_) => {},
crate::KbMeteoraDbcDecodedEvent::CreatePool(_) => {
panic!("unexpected create event")
}
},
}
}
@@ -706,10 +697,10 @@ mod tests {
};
assert_eq!(decoded.len(), 1);
match &decoded[0] {
crate::KbMeteoraDbcDecodedEvent::CreatePool(_) => {}
crate::KbMeteoraDbcDecodedEvent::CreatePool(_) => {},
crate::KbMeteoraDbcDecodedEvent::Swap(_) => {
panic!("unexpected swap event")
}
},
}
}
}