0.7.36
This commit is contained in:
@@ -2,6 +2,14 @@
|
||||
|
||||
//! Meteora DAMM v1 transaction decoder.
|
||||
|
||||
const DAMM_V1_DISCRIMINATOR_INITIALIZE_POOL: [u8; 8] =
|
||||
[0x5f, 0xb4, 0x0a, 0xac, 0x54, 0xae, 0xe8, 0x28];
|
||||
|
||||
const DAMM_V1_DISCRIMINATOR_INITIALIZE_POOL_WITH_CONFIG: [u8; 8] =
|
||||
[0x49, 0xfe, 0x76, 0xf3, 0xab, 0xc4, 0x4c, 0xd0];
|
||||
|
||||
const DAMM_V1_DISCRIMINATOR_SWAP: [u8; 8] = [0xf8, 0xc6, 0x9e, 0x91, 0xe1, 0x75, 0x87, 0xc8];
|
||||
|
||||
/// Decoded Meteora DAMM v1 create-pool event.
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct MeteoraDammV1CreatePoolDecoded {
|
||||
@@ -109,9 +117,6 @@ impl MeteoraDammV1Decoder {
|
||||
let log_messages = extract_log_messages(&transaction_json);
|
||||
let mut decoded_events = std::vec::Vec::new();
|
||||
for instruction in instructions {
|
||||
if instruction.parent_instruction_id.is_some() {
|
||||
continue;
|
||||
}
|
||||
let program_id_option = &instruction.program_id;
|
||||
let program_id = match program_id_option {
|
||||
Some(program_id) => program_id,
|
||||
@@ -135,7 +140,20 @@ impl MeteoraDammV1Decoder {
|
||||
Ok(parsed_json) => parsed_json,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
let instruction_kind = classify_instruction_kind(parsed_json.as_ref(), &log_messages);
|
||||
let instruction_data_result =
|
||||
decode_instruction_data_json(instruction.data_json.as_ref());
|
||||
let instruction_data = match instruction_data_result {
|
||||
Ok(instruction_data) => instruction_data,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
if instruction.parent_instruction_id.is_some() && instruction_data.is_none() {
|
||||
continue;
|
||||
}
|
||||
let instruction_kind = classify_instruction_kind(
|
||||
parsed_json.as_ref(),
|
||||
instruction_data.as_deref(),
|
||||
&log_messages,
|
||||
);
|
||||
let pool_account = extract_string_by_candidate_keys(
|
||||
parsed_json.as_ref(),
|
||||
&["pool", "poolAddress", "poolAccount", "amm", "ammPool", "poolState"],
|
||||
@@ -169,6 +187,9 @@ impl MeteoraDammV1Decoder {
|
||||
let payload_json = serde_json::json!({
|
||||
"decoder": "meteora_damm_v1",
|
||||
"eventKind": "create_pool",
|
||||
"dataDiscriminatorHex": instruction_data
|
||||
.as_ref()
|
||||
.and_then(|data| return first_8_bytes_hex(data.as_slice())),
|
||||
"classifiedInstructionKind": if used_config { "create_pool_with_config" } else { "create_pool" },
|
||||
"signature": transaction.signature,
|
||||
"instructionId": instruction_id,
|
||||
@@ -204,6 +225,9 @@ impl MeteoraDammV1Decoder {
|
||||
let payload_json = serde_json::json!({
|
||||
"decoder": "meteora_damm_v1",
|
||||
"eventKind": "swap",
|
||||
"dataDiscriminatorHex": instruction_data
|
||||
.as_ref()
|
||||
.and_then(|data| return first_8_bytes_hex(data.as_slice())),
|
||||
"classifiedInstructionKind": "swap",
|
||||
"signature": transaction.signature,
|
||||
"instructionId": instruction_id,
|
||||
@@ -237,8 +261,16 @@ impl MeteoraDammV1Decoder {
|
||||
|
||||
fn classify_instruction_kind(
|
||||
parsed_json: std::option::Option<&serde_json::Value>,
|
||||
instruction_data: std::option::Option<&[u8]>,
|
||||
log_messages: &[std::string::String],
|
||||
) -> MeteoraDammV1InstructionKind {
|
||||
let data_kind = classify_instruction_kind_from_data(instruction_data);
|
||||
if data_kind != MeteoraDammV1InstructionKind::Unknown {
|
||||
return data_kind;
|
||||
}
|
||||
if instruction_data_has_full_discriminator(instruction_data) {
|
||||
return MeteoraDammV1InstructionKind::Unknown;
|
||||
}
|
||||
let parsed_instruction_name = extract_string_by_candidate_keys(
|
||||
parsed_json,
|
||||
&["instruction", "instructionName", "type", "name"],
|
||||
@@ -274,6 +306,46 @@ fn classify_instruction_kind(
|
||||
return MeteoraDammV1InstructionKind::Unknown;
|
||||
}
|
||||
|
||||
fn instruction_data_has_full_discriminator(instruction_data: std::option::Option<&[u8]>) -> bool {
|
||||
let instruction_data = match instruction_data {
|
||||
Some(instruction_data) => instruction_data,
|
||||
None => return false,
|
||||
};
|
||||
return instruction_data.len() >= 8;
|
||||
}
|
||||
|
||||
fn classify_instruction_kind_from_data(
|
||||
instruction_data: std::option::Option<&[u8]>,
|
||||
) -> MeteoraDammV1InstructionKind {
|
||||
let instruction_data = match instruction_data {
|
||||
Some(instruction_data) => instruction_data,
|
||||
None => return MeteoraDammV1InstructionKind::Unknown,
|
||||
};
|
||||
if instruction_data.len() < 8 {
|
||||
return MeteoraDammV1InstructionKind::Unknown;
|
||||
}
|
||||
let discriminator = [
|
||||
instruction_data[0],
|
||||
instruction_data[1],
|
||||
instruction_data[2],
|
||||
instruction_data[3],
|
||||
instruction_data[4],
|
||||
instruction_data[5],
|
||||
instruction_data[6],
|
||||
instruction_data[7],
|
||||
];
|
||||
if discriminator == DAMM_V1_DISCRIMINATOR_INITIALIZE_POOL_WITH_CONFIG {
|
||||
return MeteoraDammV1InstructionKind::CreatePoolWithConfig;
|
||||
}
|
||||
if discriminator == DAMM_V1_DISCRIMINATOR_INITIALIZE_POOL {
|
||||
return MeteoraDammV1InstructionKind::CreatePool;
|
||||
}
|
||||
if discriminator == DAMM_V1_DISCRIMINATOR_SWAP {
|
||||
return MeteoraDammV1InstructionKind::Swap;
|
||||
}
|
||||
return MeteoraDammV1InstructionKind::Unknown;
|
||||
}
|
||||
|
||||
fn extract_log_messages(
|
||||
transaction_json: &serde_json::Value,
|
||||
) -> std::vec::Vec<std::string::String> {
|
||||
@@ -341,6 +413,10 @@ fn parse_accounts_json(
|
||||
let text_option = value.as_str();
|
||||
if let Some(text) = text_option {
|
||||
accounts.push(text.to_string());
|
||||
continue;
|
||||
}
|
||||
if let Some(pubkey) = value.get("pubkey").and_then(|nested| return nested.as_str()) {
|
||||
accounts.push(pubkey.to_string());
|
||||
}
|
||||
}
|
||||
return Ok(accounts);
|
||||
@@ -365,6 +441,48 @@ fn parse_optional_parsed_json(
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_instruction_data_json(
|
||||
data_json: std::option::Option<&std::string::String>,
|
||||
) -> Result<std::option::Option<std::vec::Vec<u8>>, crate::Error> {
|
||||
let data_json = match data_json {
|
||||
Some(data_json) => data_json,
|
||||
None => return Ok(None),
|
||||
};
|
||||
let value_result = serde_json::from_str::<serde_json::Value>(data_json.as_str());
|
||||
let value = match value_result {
|
||||
Ok(value) => value,
|
||||
Err(error) => {
|
||||
return Err(crate::Error::Json(format!(
|
||||
"cannot parse Meteora DAMM v1 data_json: {}",
|
||||
error
|
||||
)));
|
||||
},
|
||||
};
|
||||
if let serde_json::Value::String(base58_text) = value {
|
||||
let decoded_result = bs58::decode(base58_text.as_str()).into_vec();
|
||||
match decoded_result {
|
||||
Ok(decoded) => return Ok(Some(decoded)),
|
||||
Err(error) => {
|
||||
return Err(crate::Error::Json(format!(
|
||||
"cannot decode Meteora DAMM v1 instruction data from base58: {}",
|
||||
error
|
||||
)));
|
||||
},
|
||||
}
|
||||
}
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
fn first_8_bytes_hex(bytes: &[u8]) -> std::option::Option<std::string::String> {
|
||||
if bytes.len() < 8 {
|
||||
return None;
|
||||
}
|
||||
return Some(format!(
|
||||
"{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
|
||||
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
|
||||
));
|
||||
}
|
||||
|
||||
fn extract_string_by_candidate_keys(
|
||||
value: std::option::Option<&serde_json::Value>,
|
||||
candidate_keys: &[&str],
|
||||
@@ -640,4 +758,43 @@ mod tests {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn meteora_damm_v1_swap_discriminator_is_detected() {
|
||||
let data = [0xf8, 0xc6, 0x9e, 0x91, 0xe1, 0x75, 0x87, 0xc8, 0x01];
|
||||
let kind = super::classify_instruction_kind_from_data(Some(&data));
|
||||
assert_eq!(kind, super::MeteoraDammV1InstructionKind::Swap);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn meteora_damm_v1_unknown_data_discriminator_does_not_fallback_to_global_swap_logs() {
|
||||
let data = [0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x01];
|
||||
let logs = vec!["Program log: Instruction: Swap".to_string()];
|
||||
let kind = super::classify_instruction_kind(None, Some(&data), &logs);
|
||||
assert_eq!(kind, super::MeteoraDammV1InstructionKind::Unknown);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn meteora_damm_v1_inner_swap_instruction_with_data_is_not_skipped() {
|
||||
let decoder = crate::MeteoraDammV1Decoder::new();
|
||||
let transaction = make_swap_transaction();
|
||||
let mut instruction = make_swap_instruction();
|
||||
instruction.parent_instruction_id = Some(500);
|
||||
instruction.data_json = Some(format!(
|
||||
"\"{}\"",
|
||||
bs58::encode(&[0xf8_u8, 0xc6, 0x9e, 0x91, 0xe1, 0x75, 0x87, 0xc8, 0x01]).into_string()
|
||||
));
|
||||
let decoded_result = decoder.decode_transaction(&transaction, &[instruction]);
|
||||
let decoded = match decoded_result {
|
||||
Ok(decoded) => decoded,
|
||||
Err(error) => panic!("decode must succeed: {}", error),
|
||||
};
|
||||
assert_eq!(decoded.len(), 1);
|
||||
match &decoded[0] {
|
||||
crate::MeteoraDammV1DecodedEvent::Swap(event) => {
|
||||
assert_eq!(event.pool_account, Some("DammV1SwapPool111".to_string()));
|
||||
},
|
||||
crate::MeteoraDammV1DecodedEvent::CreatePool(_) => panic!("unexpected create event"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,19 @@
|
||||
|
||||
//! Meteora DAMM v2 transaction decoder.
|
||||
|
||||
const DAMM_V2_DISCRIMINATOR_INITIALIZE_POOL: [u8; 8] =
|
||||
[0x5f, 0xb4, 0x0a, 0xac, 0x54, 0xae, 0xe8, 0x28];
|
||||
|
||||
const DAMM_V2_DISCRIMINATOR_INITIALIZE_POOL_WITH_DYNAMIC_CONFIG: [u8; 8] =
|
||||
[0x95, 0x52, 0x48, 0xc5, 0xfd, 0xfc, 0x44, 0x0f];
|
||||
|
||||
const DAMM_V2_DISCRIMINATOR_INITIALIZE_CUSTOMIZABLE_POOL: [u8; 8] =
|
||||
[0x14, 0xa1, 0xf1, 0x18, 0xbd, 0xdd, 0xb4, 0x02];
|
||||
|
||||
const DAMM_V2_DISCRIMINATOR_SWAP: [u8; 8] = [0xf8, 0xc6, 0x9e, 0x91, 0xe1, 0x75, 0x87, 0xc8];
|
||||
|
||||
const DAMM_V2_DISCRIMINATOR_SWAP2: [u8; 8] = [0x41, 0x4b, 0x3f, 0x4c, 0xeb, 0x5b, 0x5b, 0x88];
|
||||
|
||||
/// Decoded Meteora DAMM v2 create-pool event.
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct MeteoraDammV2CreatePoolDecoded {
|
||||
@@ -112,9 +125,6 @@ impl MeteoraDammV2Decoder {
|
||||
let log_messages = extract_log_messages(&transaction_json);
|
||||
let mut decoded_events = std::vec::Vec::new();
|
||||
for instruction in instructions {
|
||||
if instruction.parent_instruction_id.is_some() {
|
||||
continue;
|
||||
}
|
||||
let program_id_option = &instruction.program_id;
|
||||
let program_id = match program_id_option {
|
||||
Some(program_id) => program_id,
|
||||
@@ -138,7 +148,20 @@ impl MeteoraDammV2Decoder {
|
||||
Ok(parsed_json) => parsed_json,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
let instruction_kind = classify_instruction_kind(parsed_json.as_ref(), &log_messages);
|
||||
let instruction_data_result =
|
||||
decode_instruction_data_json(instruction.data_json.as_ref());
|
||||
let instruction_data = match instruction_data_result {
|
||||
Ok(instruction_data) => instruction_data,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
if instruction.parent_instruction_id.is_some() && instruction_data.is_none() {
|
||||
continue;
|
||||
}
|
||||
let instruction_kind = classify_instruction_kind(
|
||||
parsed_json.as_ref(),
|
||||
instruction_data.as_deref(),
|
||||
&log_messages,
|
||||
);
|
||||
let pool_account = extract_string_by_candidate_keys(
|
||||
parsed_json.as_ref(),
|
||||
&["pool", "poolAddress", "poolAccount", "poolState", "cpAmm"],
|
||||
@@ -179,6 +202,9 @@ impl MeteoraDammV2Decoder {
|
||||
let payload_json = serde_json::json!({
|
||||
"decoder": "meteora_damm_v2",
|
||||
"eventKind": "create_pool",
|
||||
"dataDiscriminatorHex": instruction_data
|
||||
.as_ref()
|
||||
.and_then(|data| return first_8_bytes_hex(data.as_slice())),
|
||||
"classifiedInstructionKind": create_kind,
|
||||
"signature": transaction.signature,
|
||||
"instructionId": instruction_id,
|
||||
@@ -213,10 +239,32 @@ impl MeteoraDammV2Decoder {
|
||||
let used_swap2 = log_messages_contain_keyword(&log_messages, "swap2")
|
||||
|| value_contains_any_key(parsed_json.as_ref(), &["swap2", "isSwap2"]);
|
||||
let trade_side = infer_trade_side(&log_messages);
|
||||
let has_trade_amount_payload =
|
||||
parsed_json_has_trade_amount_or_price_payload(parsed_json.as_ref());
|
||||
let event_actionability = if has_trade_amount_payload {
|
||||
"trade_candidate"
|
||||
} else {
|
||||
"non_actionable_trade"
|
||||
};
|
||||
let materialization_skip_reason = if has_trade_amount_payload {
|
||||
serde_json::Value::Null
|
||||
} else {
|
||||
serde_json::Value::String("swap_without_amount_payload".to_string())
|
||||
};
|
||||
let payload_json = serde_json::json!({
|
||||
"decoder": "meteora_damm_v2",
|
||||
"eventKind": "swap",
|
||||
"dataDiscriminatorHex": instruction_data
|
||||
.as_ref()
|
||||
.and_then(|data| return first_8_bytes_hex(data.as_slice())),
|
||||
"classifiedInstructionKind": if used_swap2 { "swap2" } else { "swap" },
|
||||
"eventCategory": "trade",
|
||||
"eventLifecycleKind": "trade_swap",
|
||||
"eventActionability": event_actionability,
|
||||
"tradeCandidate": has_trade_amount_payload,
|
||||
"candleCandidate": has_trade_amount_payload,
|
||||
"nonTradeUseful": false,
|
||||
"materializationSkipReason": materialization_skip_reason,
|
||||
"signature": transaction.signature,
|
||||
"instructionId": instruction_id,
|
||||
"instructionIndex": instruction.instruction_index,
|
||||
@@ -278,8 +326,16 @@ fn extract_log_messages(
|
||||
|
||||
fn classify_instruction_kind(
|
||||
parsed_json: std::option::Option<&serde_json::Value>,
|
||||
instruction_data: std::option::Option<&[u8]>,
|
||||
log_messages: &[std::string::String],
|
||||
) -> MeteoraDammV2InstructionKind {
|
||||
let data_kind = classify_instruction_kind_from_data(instruction_data);
|
||||
if data_kind != MeteoraDammV2InstructionKind::Unknown {
|
||||
return data_kind;
|
||||
}
|
||||
if instruction_data_has_full_discriminator(instruction_data) {
|
||||
return MeteoraDammV2InstructionKind::Unknown;
|
||||
}
|
||||
let parsed_instruction_name = extract_string_by_candidate_keys(
|
||||
parsed_json,
|
||||
&["instruction", "instructionName", "type", "name"],
|
||||
@@ -331,6 +387,49 @@ fn classify_instruction_kind(
|
||||
return MeteoraDammV2InstructionKind::Unknown;
|
||||
}
|
||||
|
||||
fn instruction_data_has_full_discriminator(instruction_data: std::option::Option<&[u8]>) -> bool {
|
||||
let instruction_data = match instruction_data {
|
||||
Some(instruction_data) => instruction_data,
|
||||
None => return false,
|
||||
};
|
||||
return instruction_data.len() >= 8;
|
||||
}
|
||||
|
||||
fn classify_instruction_kind_from_data(
|
||||
instruction_data: std::option::Option<&[u8]>,
|
||||
) -> MeteoraDammV2InstructionKind {
|
||||
let instruction_data = match instruction_data {
|
||||
Some(instruction_data) => instruction_data,
|
||||
None => return MeteoraDammV2InstructionKind::Unknown,
|
||||
};
|
||||
if instruction_data.len() < 8 {
|
||||
return MeteoraDammV2InstructionKind::Unknown;
|
||||
}
|
||||
let discriminator = [
|
||||
instruction_data[0],
|
||||
instruction_data[1],
|
||||
instruction_data[2],
|
||||
instruction_data[3],
|
||||
instruction_data[4],
|
||||
instruction_data[5],
|
||||
instruction_data[6],
|
||||
instruction_data[7],
|
||||
];
|
||||
if discriminator == DAMM_V2_DISCRIMINATOR_INITIALIZE_POOL_WITH_DYNAMIC_CONFIG {
|
||||
return MeteoraDammV2InstructionKind::CreatePoolDynamic;
|
||||
}
|
||||
if discriminator == DAMM_V2_DISCRIMINATOR_INITIALIZE_CUSTOMIZABLE_POOL {
|
||||
return MeteoraDammV2InstructionKind::CreatePoolCustomizable;
|
||||
}
|
||||
if discriminator == DAMM_V2_DISCRIMINATOR_INITIALIZE_POOL {
|
||||
return MeteoraDammV2InstructionKind::CreatePoolStatic;
|
||||
}
|
||||
if discriminator == DAMM_V2_DISCRIMINATOR_SWAP || discriminator == DAMM_V2_DISCRIMINATOR_SWAP2 {
|
||||
return MeteoraDammV2InstructionKind::Swap;
|
||||
}
|
||||
return MeteoraDammV2InstructionKind::Unknown;
|
||||
}
|
||||
|
||||
fn log_messages_contain_keyword(log_messages: &[std::string::String], keyword: &str) -> bool {
|
||||
let keyword_normalized = normalize_text(keyword);
|
||||
for log_message in log_messages {
|
||||
@@ -370,6 +469,10 @@ fn parse_accounts_json(
|
||||
let text_option = value.as_str();
|
||||
if let Some(text) = text_option {
|
||||
accounts.push(text.to_string());
|
||||
continue;
|
||||
}
|
||||
if let Some(pubkey) = value.get("pubkey").and_then(|nested| return nested.as_str()) {
|
||||
accounts.push(pubkey.to_string());
|
||||
}
|
||||
}
|
||||
return Ok(accounts);
|
||||
@@ -394,6 +497,48 @@ fn parse_optional_parsed_json(
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_instruction_data_json(
|
||||
data_json: std::option::Option<&std::string::String>,
|
||||
) -> Result<std::option::Option<std::vec::Vec<u8>>, crate::Error> {
|
||||
let data_json = match data_json {
|
||||
Some(data_json) => data_json,
|
||||
None => return Ok(None),
|
||||
};
|
||||
let value_result = serde_json::from_str::<serde_json::Value>(data_json.as_str());
|
||||
let value = match value_result {
|
||||
Ok(value) => value,
|
||||
Err(error) => {
|
||||
return Err(crate::Error::Json(format!(
|
||||
"cannot parse Meteora DAMM v2 data_json: {}",
|
||||
error
|
||||
)));
|
||||
},
|
||||
};
|
||||
if let serde_json::Value::String(base58_text) = value {
|
||||
let decoded_result = bs58::decode(base58_text.as_str()).into_vec();
|
||||
match decoded_result {
|
||||
Ok(decoded) => return Ok(Some(decoded)),
|
||||
Err(error) => {
|
||||
return Err(crate::Error::Json(format!(
|
||||
"cannot decode Meteora DAMM v2 instruction data from base58: {}",
|
||||
error
|
||||
)));
|
||||
},
|
||||
}
|
||||
}
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
fn first_8_bytes_hex(bytes: &[u8]) -> std::option::Option<std::string::String> {
|
||||
if bytes.len() < 8 {
|
||||
return None;
|
||||
}
|
||||
return Some(format!(
|
||||
"{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
|
||||
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
|
||||
));
|
||||
}
|
||||
|
||||
fn extract_string_by_candidate_keys(
|
||||
value: std::option::Option<&serde_json::Value>,
|
||||
candidate_keys: &[&str],
|
||||
@@ -495,6 +640,62 @@ fn infer_trade_side(log_messages: &[std::string::String]) -> crate::SwapTradeSid
|
||||
return crate::SwapTradeSide::Unknown;
|
||||
}
|
||||
|
||||
fn parsed_json_has_trade_amount_or_price_payload(
|
||||
parsed_json: std::option::Option<&serde_json::Value>,
|
||||
) -> bool {
|
||||
let parsed_json = match parsed_json {
|
||||
Some(parsed_json) => parsed_json,
|
||||
None => return false,
|
||||
};
|
||||
return json_value_contains_any_trade_amount_or_price_key(parsed_json);
|
||||
}
|
||||
|
||||
fn json_value_contains_any_trade_amount_or_price_key(value: &serde_json::Value) -> bool {
|
||||
match value {
|
||||
serde_json::Value::Object(map) => {
|
||||
for key in map.keys() {
|
||||
let normalized = normalize_text(key.as_str());
|
||||
if is_trade_amount_or_price_key(normalized.as_str()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for child in map.values() {
|
||||
if json_value_contains_any_trade_amount_or_price_key(child) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
},
|
||||
serde_json::Value::Array(values) => {
|
||||
for child in values {
|
||||
if json_value_contains_any_trade_amount_or_price_key(child) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
},
|
||||
_ => return false,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_trade_amount_or_price_key(normalized_key: &str) -> bool {
|
||||
return normalized_key == "baseamountraw"
|
||||
|| normalized_key == "quoteamountraw"
|
||||
|| normalized_key == "baseamount"
|
||||
|| normalized_key == "quoteamount"
|
||||
|| normalized_key == "amountin"
|
||||
|| normalized_key == "amountout"
|
||||
|| normalized_key == "tokenain"
|
||||
|| normalized_key == "tokenaout"
|
||||
|| normalized_key == "tokenbin"
|
||||
|| normalized_key == "tokenbout"
|
||||
|| normalized_key == "inputamount"
|
||||
|| normalized_key == "outputamount"
|
||||
|| normalized_key == "swapamount"
|
||||
|| normalized_key == "price"
|
||||
|| normalized_key == "pricequoteperbase";
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
fn make_create_transaction() -> crate::ChainTransactionDto {
|
||||
@@ -670,4 +871,43 @@ mod tests {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn meteora_damm_v2_swap2_discriminator_is_detected() {
|
||||
let data = [0x41, 0x4b, 0x3f, 0x4c, 0xeb, 0x5b, 0x5b, 0x88, 0x01];
|
||||
let kind = super::classify_instruction_kind_from_data(Some(&data));
|
||||
assert_eq!(kind, super::MeteoraDammV2InstructionKind::Swap);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn meteora_damm_v2_unknown_data_discriminator_does_not_fallback_to_global_swap_logs() {
|
||||
let data = [0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x01];
|
||||
let logs = vec!["Program log: Instruction: Swap2".to_string()];
|
||||
let kind = super::classify_instruction_kind(None, Some(&data), &logs);
|
||||
assert_eq!(kind, super::MeteoraDammV2InstructionKind::Unknown);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn meteora_damm_v2_inner_swap2_instruction_with_data_is_not_skipped() {
|
||||
let decoder = crate::MeteoraDammV2Decoder::new();
|
||||
let transaction = make_swap_transaction();
|
||||
let mut instruction = make_swap_instruction();
|
||||
instruction.parent_instruction_id = Some(400);
|
||||
instruction.data_json = Some(format!(
|
||||
"\"{}\"",
|
||||
bs58::encode(&[0x41_u8, 0x4b, 0x3f, 0x4c, 0xeb, 0x5b, 0x5b, 0x88, 0x01]).into_string()
|
||||
));
|
||||
let decoded_result = decoder.decode_transaction(&transaction, &[instruction]);
|
||||
let decoded = match decoded_result {
|
||||
Ok(decoded) => decoded,
|
||||
Err(error) => panic!("decode must succeed: {}", error),
|
||||
};
|
||||
assert_eq!(decoded.len(), 1);
|
||||
match &decoded[0] {
|
||||
crate::MeteoraDammV2DecodedEvent::Swap(event) => {
|
||||
assert_eq!(event.pool_account, Some("DammV2SwapPool111".to_string()));
|
||||
},
|
||||
crate::MeteoraDammV2DecodedEvent::CreatePool(_) => panic!("unexpected create event"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,16 @@
|
||||
|
||||
//! Meteora Dynamic Bonding Curve (DBC) transaction decoder.
|
||||
|
||||
const DBC_DISCRIMINATOR_CREATE_POOL: [u8; 8] = [0xe9, 0x92, 0xd1, 0x8e, 0xcf, 0x68, 0x40, 0xbc];
|
||||
|
||||
const DBC_DISCRIMINATOR_INITIALIZE_POOL: [u8; 8] = [0x5f, 0xb4, 0x0a, 0xac, 0x54, 0xae, 0xe8, 0x28];
|
||||
|
||||
const DBC_DISCRIMINATOR_LAUNCH_POOL: [u8; 8] = [0xa6, 0x77, 0xd1, 0xb6, 0xd6, 0x6d, 0x3a, 0xb5];
|
||||
|
||||
const DBC_DISCRIMINATOR_SWAP: [u8; 8] = [0xf8, 0xc6, 0x9e, 0x91, 0xe1, 0x75, 0x87, 0xc8];
|
||||
|
||||
const DBC_DISCRIMINATOR_SWAP2: [u8; 8] = [0x41, 0x4b, 0x3f, 0x4c, 0xeb, 0x5b, 0x5b, 0x88];
|
||||
|
||||
/// Decoded Meteora DBC create-pool event.
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct MeteoraDbcCreatePoolDecoded {
|
||||
@@ -106,9 +116,6 @@ impl MeteoraDbcDecoder {
|
||||
let log_messages = extract_log_messages(&transaction_json);
|
||||
let mut decoded_events = std::vec::Vec::new();
|
||||
for instruction in instructions {
|
||||
if instruction.parent_instruction_id.is_some() {
|
||||
continue;
|
||||
}
|
||||
let program_id_option = &instruction.program_id;
|
||||
let program_id = match program_id_option {
|
||||
Some(program_id) => program_id,
|
||||
@@ -132,7 +139,20 @@ impl MeteoraDbcDecoder {
|
||||
Ok(parsed_json) => parsed_json,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
let instruction_kind = classify_instruction_kind(parsed_json.as_ref(), &log_messages);
|
||||
let instruction_data_result =
|
||||
decode_instruction_data_json(instruction.data_json.as_ref());
|
||||
let instruction_data = match instruction_data_result {
|
||||
Ok(instruction_data) => instruction_data,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
if instruction.parent_instruction_id.is_some() && instruction_data.is_none() {
|
||||
continue;
|
||||
}
|
||||
let instruction_kind = classify_instruction_kind(
|
||||
parsed_json.as_ref(),
|
||||
instruction_data.as_deref(),
|
||||
&log_messages,
|
||||
);
|
||||
let pool_account = extract_string_by_candidate_keys(
|
||||
parsed_json.as_ref(),
|
||||
&["pool", "poolAccount", "poolState", "virtualPool", "poolKey"],
|
||||
@@ -162,6 +182,9 @@ impl MeteoraDbcDecoder {
|
||||
let payload_json = serde_json::json!({
|
||||
"decoder": "meteora_dbc",
|
||||
"eventKind": "create_pool",
|
||||
"dataDiscriminatorHex": instruction_data
|
||||
.as_ref()
|
||||
.and_then(|data| return first_8_bytes_hex(data.as_slice())),
|
||||
"classifiedInstructionKind": "create_pool",
|
||||
"signature": transaction.signature,
|
||||
"instructionId": instruction_id,
|
||||
@@ -193,10 +216,32 @@ impl MeteoraDbcDecoder {
|
||||
}
|
||||
if instruction_kind == MeteoraDbcInstructionKind::Swap {
|
||||
let trade_side = infer_trade_side(&log_messages);
|
||||
let has_trade_amount_payload =
|
||||
parsed_json_has_trade_amount_or_price_payload(parsed_json.as_ref());
|
||||
let event_actionability = if has_trade_amount_payload {
|
||||
"trade_candidate"
|
||||
} else {
|
||||
"non_actionable_trade"
|
||||
};
|
||||
let materialization_skip_reason = if has_trade_amount_payload {
|
||||
serde_json::Value::Null
|
||||
} else {
|
||||
serde_json::Value::String("swap_without_amount_payload".to_string())
|
||||
};
|
||||
let payload_json = serde_json::json!({
|
||||
"decoder": "meteora_dbc",
|
||||
"eventKind": "swap",
|
||||
"dataDiscriminatorHex": instruction_data
|
||||
.as_ref()
|
||||
.and_then(|data| return first_8_bytes_hex(data.as_slice())),
|
||||
"classifiedInstructionKind": "swap",
|
||||
"eventCategory": "trade",
|
||||
"eventLifecycleKind": "trade_swap",
|
||||
"eventActionability": event_actionability,
|
||||
"tradeCandidate": has_trade_amount_payload,
|
||||
"candleCandidate": has_trade_amount_payload,
|
||||
"nonTradeUseful": false,
|
||||
"materializationSkipReason": materialization_skip_reason,
|
||||
"signature": transaction.signature,
|
||||
"instructionId": instruction_id,
|
||||
"instructionIndex": instruction.instruction_index,
|
||||
@@ -306,6 +351,10 @@ fn parse_accounts_json(
|
||||
let text_option = value.as_str();
|
||||
if let Some(text) = text_option {
|
||||
accounts.push(text.to_string());
|
||||
continue;
|
||||
}
|
||||
if let Some(pubkey) = value.get("pubkey").and_then(|nested| return nested.as_str()) {
|
||||
accounts.push(pubkey.to_string());
|
||||
}
|
||||
}
|
||||
return Ok(accounts);
|
||||
@@ -411,6 +460,48 @@ fn value_contains_any_key_inner(value: &serde_json::Value, candidate_keys: &[&st
|
||||
return false;
|
||||
}
|
||||
|
||||
fn decode_instruction_data_json(
|
||||
data_json: std::option::Option<&std::string::String>,
|
||||
) -> Result<std::option::Option<std::vec::Vec<u8>>, crate::Error> {
|
||||
let data_json = match data_json {
|
||||
Some(data_json) => data_json,
|
||||
None => return Ok(None),
|
||||
};
|
||||
let parsed_result = serde_json::from_str::<serde_json::Value>(data_json.as_str());
|
||||
let parsed = match parsed_result {
|
||||
Ok(parsed) => parsed,
|
||||
Err(error) => {
|
||||
return Err(crate::Error::Json(format!(
|
||||
"cannot parse Meteora DBC data_json: {}",
|
||||
error
|
||||
)));
|
||||
},
|
||||
};
|
||||
if let serde_json::Value::String(base58_text) = parsed {
|
||||
let decoded_result = bs58::decode(base58_text.as_str()).into_vec();
|
||||
match decoded_result {
|
||||
Ok(decoded) => return Ok(Some(decoded)),
|
||||
Err(error) => {
|
||||
return Err(crate::Error::Json(format!(
|
||||
"cannot decode Meteora DBC instruction data from base58: {}",
|
||||
error
|
||||
)));
|
||||
},
|
||||
}
|
||||
}
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
fn first_8_bytes_hex(bytes: &[u8]) -> std::option::Option<std::string::String> {
|
||||
if bytes.len() < 8 {
|
||||
return None;
|
||||
}
|
||||
return Some(format!(
|
||||
"{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
|
||||
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
|
||||
));
|
||||
}
|
||||
|
||||
fn extract_account(
|
||||
accounts: &[std::string::String],
|
||||
index: usize,
|
||||
@@ -433,8 +524,16 @@ fn infer_trade_side(log_messages: &[std::string::String]) -> crate::SwapTradeSid
|
||||
|
||||
fn classify_instruction_kind(
|
||||
parsed_json: std::option::Option<&serde_json::Value>,
|
||||
instruction_data: std::option::Option<&[u8]>,
|
||||
log_messages: &[std::string::String],
|
||||
) -> MeteoraDbcInstructionKind {
|
||||
let data_kind = classify_instruction_kind_from_data(instruction_data);
|
||||
if data_kind != MeteoraDbcInstructionKind::Unknown {
|
||||
return data_kind;
|
||||
}
|
||||
if instruction_data_has_full_discriminator(instruction_data) {
|
||||
return MeteoraDbcInstructionKind::Unknown;
|
||||
}
|
||||
let parsed_instruction_name = extract_string_by_candidate_keys(
|
||||
parsed_json,
|
||||
&["instruction", "instructionName", "type", "name"],
|
||||
@@ -470,6 +569,102 @@ fn classify_instruction_kind(
|
||||
return MeteoraDbcInstructionKind::Unknown;
|
||||
}
|
||||
|
||||
fn instruction_data_has_full_discriminator(instruction_data: std::option::Option<&[u8]>) -> bool {
|
||||
let instruction_data = match instruction_data {
|
||||
Some(instruction_data) => instruction_data,
|
||||
None => return false,
|
||||
};
|
||||
return instruction_data.len() >= 8;
|
||||
}
|
||||
|
||||
fn classify_instruction_kind_from_data(
|
||||
instruction_data: std::option::Option<&[u8]>,
|
||||
) -> MeteoraDbcInstructionKind {
|
||||
let instruction_data = match instruction_data {
|
||||
Some(instruction_data) => instruction_data,
|
||||
None => return MeteoraDbcInstructionKind::Unknown,
|
||||
};
|
||||
if instruction_data.len() < 8 {
|
||||
return MeteoraDbcInstructionKind::Unknown;
|
||||
}
|
||||
let discriminator = [
|
||||
instruction_data[0],
|
||||
instruction_data[1],
|
||||
instruction_data[2],
|
||||
instruction_data[3],
|
||||
instruction_data[4],
|
||||
instruction_data[5],
|
||||
instruction_data[6],
|
||||
instruction_data[7],
|
||||
];
|
||||
if discriminator == DBC_DISCRIMINATOR_CREATE_POOL
|
||||
|| discriminator == DBC_DISCRIMINATOR_INITIALIZE_POOL
|
||||
|| discriminator == DBC_DISCRIMINATOR_LAUNCH_POOL
|
||||
{
|
||||
return MeteoraDbcInstructionKind::CreatePool;
|
||||
}
|
||||
if discriminator == DBC_DISCRIMINATOR_SWAP || discriminator == DBC_DISCRIMINATOR_SWAP2 {
|
||||
return MeteoraDbcInstructionKind::Swap;
|
||||
}
|
||||
return MeteoraDbcInstructionKind::Unknown;
|
||||
}
|
||||
|
||||
fn parsed_json_has_trade_amount_or_price_payload(
|
||||
parsed_json: std::option::Option<&serde_json::Value>,
|
||||
) -> bool {
|
||||
let parsed_json = match parsed_json {
|
||||
Some(parsed_json) => parsed_json,
|
||||
None => return false,
|
||||
};
|
||||
return json_value_contains_any_trade_amount_or_price_key(parsed_json);
|
||||
}
|
||||
|
||||
fn json_value_contains_any_trade_amount_or_price_key(value: &serde_json::Value) -> bool {
|
||||
match value {
|
||||
serde_json::Value::Object(map) => {
|
||||
for key in map.keys() {
|
||||
let normalized = normalize_log_text(key.as_str());
|
||||
if is_trade_amount_or_price_key(normalized.as_str()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for child in map.values() {
|
||||
if json_value_contains_any_trade_amount_or_price_key(child) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
},
|
||||
serde_json::Value::Array(values) => {
|
||||
for child in values {
|
||||
if json_value_contains_any_trade_amount_or_price_key(child) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
},
|
||||
_ => return false,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_trade_amount_or_price_key(normalized_key: &str) -> bool {
|
||||
return normalized_key == "baseamountraw"
|
||||
|| normalized_key == "quoteamountraw"
|
||||
|| normalized_key == "baseamount"
|
||||
|| normalized_key == "quoteamount"
|
||||
|| normalized_key == "amountin"
|
||||
|| normalized_key == "amountout"
|
||||
|| normalized_key == "tokenain"
|
||||
|| normalized_key == "tokenaout"
|
||||
|| normalized_key == "tokenbin"
|
||||
|| normalized_key == "tokenbout"
|
||||
|| normalized_key == "inputamount"
|
||||
|| normalized_key == "outputamount"
|
||||
|| normalized_key == "swapamount"
|
||||
|| normalized_key == "price"
|
||||
|| normalized_key == "pricequoteperbase";
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
fn make_create_transaction() -> crate::ChainTransactionDto {
|
||||
@@ -688,4 +883,43 @@ mod tests {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn meteora_dbc_swap2_discriminator_is_detected() {
|
||||
let data = [0x41, 0x4b, 0x3f, 0x4c, 0xeb, 0x5b, 0x5b, 0x88, 0x01];
|
||||
let kind = super::classify_instruction_kind_from_data(Some(&data));
|
||||
assert_eq!(kind, super::MeteoraDbcInstructionKind::Swap);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn meteora_dbc_unknown_data_discriminator_does_not_fallback_to_global_swap_logs() {
|
||||
let data = [0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x01];
|
||||
let logs = vec!["Program log: Instruction: Swap2".to_string()];
|
||||
let kind = super::classify_instruction_kind(None, Some(&data), &logs);
|
||||
assert_eq!(kind, super::MeteoraDbcInstructionKind::Unknown);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn meteora_dbc_inner_swap2_instruction_with_data_is_not_skipped() {
|
||||
let decoder = crate::MeteoraDbcDecoder::new();
|
||||
let transaction = make_swap_transaction();
|
||||
let mut instruction = make_swap_instruction();
|
||||
instruction.parent_instruction_id = Some(300);
|
||||
instruction.data_json = Some(format!(
|
||||
"\"{}\"",
|
||||
bs58::encode(&[0x41_u8, 0x4b, 0x3f, 0x4c, 0xeb, 0x5b, 0x5b, 0x88, 0x01]).into_string()
|
||||
));
|
||||
let decoded_result = decoder.decode_transaction(&transaction, &[instruction]);
|
||||
let decoded = match decoded_result {
|
||||
Ok(decoded) => decoded,
|
||||
Err(error) => panic!("decode must succeed: {}", error),
|
||||
};
|
||||
assert_eq!(decoded.len(), 1);
|
||||
match &decoded[0] {
|
||||
crate::MeteoraDbcDecodedEvent::Swap(event) => {
|
||||
assert_eq!(event.pool_account, Some("DbcPoolSwap111".to_string()));
|
||||
},
|
||||
crate::MeteoraDbcDecodedEvent::CreatePool(_) => panic!("unexpected create event"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -235,6 +235,25 @@ impl LocalPipelineValidationConfig {
|
||||
config.profile_code = "0.7.35_non_trade_fee_reward_admin".to_string();
|
||||
return config;
|
||||
}
|
||||
|
||||
/// Builds the `0.7.36` Meteora family consolidation validation config.
|
||||
///
|
||||
/// This profile keeps the `0.7.35` non-trade counters while making the four
|
||||
/// Meteora variants explicit in the expected set. Targeted backfills may
|
||||
/// still miss some variants, so missing variants remain warnings.
|
||||
pub fn v0_7_36_meteora_family_consolidation() -> Self {
|
||||
let mut config = Self::v0_7_35_non_trade_fee_reward_admin();
|
||||
config.profile_code = "0.7.36_meteora_family_consolidation".to_string();
|
||||
config.expected_dex_codes = vec![
|
||||
"meteora_dbc".to_string(),
|
||||
"meteora_damm_v1".to_string(),
|
||||
"meteora_damm_v2".to_string(),
|
||||
"meteora_dlmm".to_string(),
|
||||
];
|
||||
config.require_all_expected_dexes = false;
|
||||
config.allow_unexpected_dexes = true;
|
||||
return config;
|
||||
}
|
||||
}
|
||||
|
||||
/// A single local pipeline validation issue.
|
||||
@@ -421,6 +440,14 @@ impl LocalPipelineValidationService {
|
||||
let config = crate::LocalPipelineValidationConfig::v0_7_35_non_trade_fee_reward_admin();
|
||||
return self.validate_current_database(&config).await;
|
||||
}
|
||||
|
||||
/// Diagnoses the current database with the `0.7.36` Meteora family profile.
|
||||
pub async fn validate_v0_7_36_current_database(
|
||||
&self,
|
||||
) -> Result<crate::LocalPipelineValidationRunDto, crate::Error> {
|
||||
let config = crate::LocalPipelineValidationConfig::v0_7_36_meteora_family_consolidation();
|
||||
return self.validate_current_database(&config).await;
|
||||
}
|
||||
}
|
||||
|
||||
/// Validates a diagnostics summary without performing database access.
|
||||
@@ -540,7 +567,8 @@ pub fn validate_local_pipeline_diagnostics_summary(
|
||||
}
|
||||
let missing_expected_dex_is_warning = config.profile_code
|
||||
== "0.7.34_non_trade_liquidity_lifecycle"
|
||||
|| config.profile_code == "0.7.35_non_trade_fee_reward_admin";
|
||||
|| config.profile_code == "0.7.35_non_trade_fee_reward_admin"
|
||||
|| config.profile_code == "0.7.36_meteora_family_consolidation";
|
||||
if config.require_all_expected_dexes || missing_expected_dex_is_warning {
|
||||
for expected_dex_code in &expected_dex_codes {
|
||||
if !observed_dex_codes.contains(expected_dex_code) {
|
||||
@@ -1175,6 +1203,24 @@ mod tests {
|
||||
assert_eq!(report.pool_admin_event_count, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validation_accepts_0_7_36_meteora_family_partial_corpus() {
|
||||
let mut summary = make_0_7_28_summary_with_meteora();
|
||||
summary.dex_summaries.retain(|dex_summary| {
|
||||
return dex_summary.dex_code == "meteora_dlmm";
|
||||
});
|
||||
let config = crate::LocalPipelineValidationConfig::v0_7_36_meteora_family_consolidation();
|
||||
let report = crate::validate_local_pipeline_diagnostics_summary(&summary, &config);
|
||||
assert!(report.validation_passed);
|
||||
assert_eq!(report.validation_profile_code, "0.7.36_meteora_family_consolidation");
|
||||
assert_eq!(report.blocking_issue_count, 0);
|
||||
assert_eq!(report.warning_count, 3);
|
||||
assert!(report.expected_dex_codes.contains(&"meteora_dbc".to_string()));
|
||||
assert!(report.expected_dex_codes.contains(&"meteora_damm_v1".to_string()));
|
||||
assert!(report.expected_dex_codes.contains(&"meteora_damm_v2".to_string()));
|
||||
assert!(report.expected_dex_codes.contains(&"meteora_dlmm".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validation_rejects_0_7_33_pair_trading_readiness_mismatch() {
|
||||
let mut summary = make_0_7_28_summary_with_meteora();
|
||||
|
||||
Reference in New Issue
Block a user