0.7.5
This commit is contained in:
@@ -20,6 +20,12 @@ pub struct KbPumpSwapTradeDecoded {
|
||||
pub trade_side: crate::KbSwapTradeSide,
|
||||
/// Optional heuristic pool account.
|
||||
pub pool_account: std::option::Option<std::string::String>,
|
||||
/// Optional token A mint.
|
||||
pub token_a_mint: std::option::Option<std::string::String>,
|
||||
/// Optional token B mint.
|
||||
pub token_b_mint: std::option::Option<std::string::String>,
|
||||
/// Optional pool_v2 account or upgraded pool account.
|
||||
pub pool_v2: std::option::Option<std::string::String>,
|
||||
/// Decoded payload.
|
||||
pub payload_json: serde_json::Value,
|
||||
}
|
||||
@@ -94,7 +100,52 @@ impl KbPumpSwapDecoder {
|
||||
Ok(accounts) => accounts,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
let pool_account = kb_extract_account(&accounts, 0);
|
||||
let parsed_json_result =
|
||||
kb_parse_optional_parsed_json(instruction.parsed_json.as_ref());
|
||||
let parsed_json = match parsed_json_result {
|
||||
Ok(parsed_json) => parsed_json,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
let pool_account = kb_extract_string_by_candidate_keys(
|
||||
parsed_json.as_ref(),
|
||||
&["pool", "poolAccount", "amm", "ammPool", "poolState"],
|
||||
)
|
||||
.or_else(|| kb_extract_account(&accounts, 0));
|
||||
let token_a_mint = kb_extract_string_by_candidate_keys(
|
||||
parsed_json.as_ref(),
|
||||
&[
|
||||
"tokenAMint",
|
||||
"baseMint",
|
||||
"mintA",
|
||||
"coinMint",
|
||||
"token0Mint",
|
||||
"mint0",
|
||||
],
|
||||
)
|
||||
.or_else(|| kb_extract_account(&accounts, 1));
|
||||
let token_b_mint = kb_extract_string_by_candidate_keys(
|
||||
parsed_json.as_ref(),
|
||||
&[
|
||||
"tokenBMint",
|
||||
"quoteMint",
|
||||
"mintB",
|
||||
"pcMint",
|
||||
"token1Mint",
|
||||
"mint1",
|
||||
],
|
||||
)
|
||||
.or_else(|| kb_extract_account(&accounts, 2));
|
||||
let pool_v2 = kb_extract_string_by_candidate_keys(
|
||||
parsed_json.as_ref(),
|
||||
&[
|
||||
"poolV2",
|
||||
"pool_v2",
|
||||
"ammV2",
|
||||
"bondingCurveV2",
|
||||
"bonding_curve_v2",
|
||||
],
|
||||
)
|
||||
.or_else(|| kb_extract_account(&accounts, 3));
|
||||
let is_buy = kb_log_messages_contain_keyword(&log_messages, "buy");
|
||||
let is_sell = kb_log_messages_contain_keyword(&log_messages, "sell");
|
||||
if !is_buy && !is_sell {
|
||||
@@ -106,8 +157,12 @@ impl KbPumpSwapDecoder {
|
||||
"instructionId": instruction_id,
|
||||
"instructionIndex": instruction.instruction_index,
|
||||
"accounts": accounts,
|
||||
"parsed": parsed_json,
|
||||
"logMessages": log_messages,
|
||||
"poolAccount": pool_account
|
||||
"poolAccount": pool_account,
|
||||
"tokenAMint": token_a_mint,
|
||||
"tokenBMint": token_b_mint,
|
||||
"poolV2": pool_v2
|
||||
});
|
||||
if is_buy {
|
||||
decoded_events.push(crate::KbPumpSwapDecodedEvent::BuyTrade(
|
||||
@@ -118,6 +173,9 @@ impl KbPumpSwapDecoder {
|
||||
program_id: program_id.clone(),
|
||||
trade_side: crate::KbSwapTradeSide::BuyBase,
|
||||
pool_account: pool_account.clone(),
|
||||
token_a_mint: token_a_mint.clone(),
|
||||
token_b_mint: token_b_mint.clone(),
|
||||
pool_v2: pool_v2.clone(),
|
||||
payload_json: payload_json.clone(),
|
||||
},
|
||||
));
|
||||
@@ -131,6 +189,9 @@ impl KbPumpSwapDecoder {
|
||||
program_id: program_id.clone(),
|
||||
trade_side: crate::KbSwapTradeSide::SellBase,
|
||||
pool_account,
|
||||
token_a_mint,
|
||||
token_b_mint,
|
||||
pool_v2,
|
||||
payload_json,
|
||||
},
|
||||
));
|
||||
@@ -213,6 +274,69 @@ fn kb_extract_account(
|
||||
Some(accounts[index].clone())
|
||||
}
|
||||
|
||||
fn kb_parse_optional_parsed_json(
|
||||
parsed_json: std::option::Option<&std::string::String>,
|
||||
) -> Result<std::option::Option<serde_json::Value>, crate::KbError> {
|
||||
let parsed_json = match parsed_json {
|
||||
Some(parsed_json) => parsed_json,
|
||||
None => return Ok(None),
|
||||
};
|
||||
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
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
fn kb_extract_string_by_candidate_keys(
|
||||
value: std::option::Option<&serde_json::Value>,
|
||||
candidate_keys: &[&str],
|
||||
) -> std::option::Option<std::string::String> {
|
||||
let value = match value {
|
||||
Some(value) => value,
|
||||
None => return None,
|
||||
};
|
||||
kb_extract_string_by_candidate_keys_inner(value, candidate_keys)
|
||||
}
|
||||
|
||||
fn kb_extract_string_by_candidate_keys_inner(
|
||||
value: &serde_json::Value,
|
||||
candidate_keys: &[&str],
|
||||
) -> std::option::Option<std::string::String> {
|
||||
if let Some(object) = value.as_object() {
|
||||
for candidate_key in candidate_keys {
|
||||
let direct_option = object.get(*candidate_key);
|
||||
if let Some(direct) = direct_option {
|
||||
let direct_text_option = direct.as_str();
|
||||
if let Some(direct_text) = direct_text_option {
|
||||
return Some(direct_text.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
for nested_value in object.values() {
|
||||
let nested_result =
|
||||
kb_extract_string_by_candidate_keys_inner(nested_value, candidate_keys);
|
||||
if nested_result.is_some() {
|
||||
return nested_result;
|
||||
}
|
||||
}
|
||||
return None;
|
||||
}
|
||||
if let Some(array) = value.as_array() {
|
||||
for nested_value in array {
|
||||
let nested_result =
|
||||
kb_extract_string_by_candidate_keys_inner(nested_value, candidate_keys);
|
||||
if nested_result.is_some() {
|
||||
return nested_result;
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
fn make_transaction_with_buy_log() -> crate::KbChainTransactionDto {
|
||||
@@ -252,10 +376,20 @@ mod tests {
|
||||
Some(crate::KB_PUMP_SWAP_PROGRAM_ID.to_string()),
|
||||
Some("pump-amm".to_string()),
|
||||
Some(1),
|
||||
serde_json::json!(["PumpPool111", "Other1", "Other2"]).to_string(),
|
||||
None,
|
||||
serde_json::json!(["PumpPool111", "TokenA111", "TokenB111", "PoolV2_111"]).to_string(),
|
||||
None,
|
||||
None,
|
||||
Some(
|
||||
serde_json::json!({
|
||||
"info": {
|
||||
"pool": "PumpPool111",
|
||||
"baseMint": "TokenA111",
|
||||
"quoteMint": "TokenB111",
|
||||
"poolV2": "PoolV2_111"
|
||||
}
|
||||
})
|
||||
.to_string(),
|
||||
),
|
||||
);
|
||||
dto.id = Some(18);
|
||||
dto
|
||||
@@ -266,7 +400,6 @@ mod tests {
|
||||
let decoder = crate::KbPumpSwapDecoder::new();
|
||||
let transaction = make_transaction_with_buy_log();
|
||||
let instructions = vec![make_instruction()];
|
||||
|
||||
let decoded_result = decoder.decode_transaction(&transaction, &instructions);
|
||||
let decoded = match decoded_result {
|
||||
Ok(decoded) => decoded,
|
||||
@@ -278,6 +411,9 @@ mod tests {
|
||||
assert_eq!(event.transaction_id, 92);
|
||||
assert_eq!(event.instruction_id, 18);
|
||||
assert_eq!(event.pool_account, Some("PumpPool111".to_string()));
|
||||
assert_eq!(event.token_a_mint, Some("TokenA111".to_string()));
|
||||
assert_eq!(event.token_b_mint, Some("TokenB111".to_string()));
|
||||
assert_eq!(event.pool_v2, Some("PoolV2_111".to_string()));
|
||||
assert_eq!(event.trade_side, crate::KbSwapTradeSide::BuyBase);
|
||||
}
|
||||
crate::KbPumpSwapDecodedEvent::SellTrade(_) => {
|
||||
|
||||
Reference in New Issue
Block a user