0.7.10
This commit is contained in:
720
kb_lib/src/dex/orca_whirlpools.rs
Normal file
720
kb_lib/src/dex/orca_whirlpools.rs
Normal file
@@ -0,0 +1,720 @@
|
||||
// file: kb_lib/src/dex/orca_whirlpools.rs
|
||||
|
||||
//! Orca Whirlpools transaction decoder.
|
||||
|
||||
/// Orca Whirlpools program id.
|
||||
pub const KB_ORCA_WHIRLPOOLS_PROGRAM_ID: &str =
|
||||
"whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc";
|
||||
|
||||
/// Decoded Orca Whirlpools create-pool event.
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct KbOrcaWhirlpoolsCreatePoolDecoded {
|
||||
/// Parent transaction id.
|
||||
pub transaction_id: i64,
|
||||
/// Parent instruction id.
|
||||
pub instruction_id: i64,
|
||||
/// Transaction signature.
|
||||
pub signature: std::string::String,
|
||||
/// Program id.
|
||||
pub program_id: std::string::String,
|
||||
/// Optional whirlpool 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 whirlpools config account.
|
||||
pub config_account: std::option::Option<std::string::String>,
|
||||
/// Optional creator / funder.
|
||||
pub creator: std::option::Option<std::string::String>,
|
||||
/// Whether the instruction looked like `initialize_pool_v2`.
|
||||
pub used_v2: bool,
|
||||
/// Decoded payload.
|
||||
pub payload_json: serde_json::Value,
|
||||
}
|
||||
|
||||
/// Decoded Orca Whirlpools swap event.
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct KbOrcaWhirlpoolsSwapDecoded {
|
||||
/// Parent transaction id.
|
||||
pub transaction_id: i64,
|
||||
/// Parent instruction id.
|
||||
pub instruction_id: i64,
|
||||
/// Transaction signature.
|
||||
pub signature: std::string::String,
|
||||
/// Program id.
|
||||
pub program_id: std::string::String,
|
||||
/// Trade side relative to normalized base.
|
||||
pub trade_side: crate::KbSwapTradeSide,
|
||||
/// Optional whirlpool 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>,
|
||||
/// Whether the instruction looked like `swap_v2`.
|
||||
pub used_v2: bool,
|
||||
/// Decoded payload.
|
||||
pub payload_json: serde_json::Value,
|
||||
}
|
||||
|
||||
/// Decoded Orca Whirlpools event.
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub enum KbOrcaWhirlpoolsDecodedEvent {
|
||||
/// Pool creation.
|
||||
CreatePool(KbOrcaWhirlpoolsCreatePoolDecoded),
|
||||
/// Swap / swap_v2.
|
||||
Swap(KbOrcaWhirlpoolsSwapDecoded),
|
||||
}
|
||||
|
||||
/// Orca Whirlpools decoder.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct KbOrcaWhirlpoolsDecoder;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum KbOrcaWhirlpoolsInstructionKind {
|
||||
InitializePool,
|
||||
InitializePoolV2,
|
||||
Swap,
|
||||
SwapV2,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
impl KbOrcaWhirlpoolsDecoder {
|
||||
/// Creates a new decoder.
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
|
||||
/// Decodes one projected transaction into zero or more Orca Whirlpools events.
|
||||
pub fn decode_transaction(
|
||||
&self,
|
||||
transaction: &crate::KbChainTransactionDto,
|
||||
instructions: &[crate::KbChainInstructionDto],
|
||||
) -> Result<std::vec::Vec<crate::KbOrcaWhirlpoolsDecodedEvent>, crate::KbError> {
|
||||
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!(
|
||||
"chain transaction '{}' has no internal id",
|
||||
transaction.signature
|
||||
)));
|
||||
}
|
||||
};
|
||||
let transaction_json_result =
|
||||
serde_json::from_str::<serde_json::Value>(transaction.transaction_json.as_str());
|
||||
let transaction_json = match transaction_json_result {
|
||||
Ok(transaction_json) => transaction_json,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Json(format!(
|
||||
"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();
|
||||
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,
|
||||
None => continue,
|
||||
};
|
||||
if program_id.as_str() != crate::KB_ORCA_WHIRLPOOLS_PROGRAM_ID {
|
||||
continue;
|
||||
}
|
||||
let instruction_id_option = instruction.id;
|
||||
let instruction_id = match instruction_id_option {
|
||||
Some(instruction_id) => instruction_id,
|
||||
None => continue,
|
||||
};
|
||||
let accounts_result = kb_parse_accounts_json(instruction.accounts_json.as_str());
|
||||
let accounts = match accounts_result {
|
||||
Ok(accounts) => accounts,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
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 instruction_kind =
|
||||
kb_classify_instruction_kind(parsed_json.as_ref(), &log_messages);
|
||||
let pool_account = kb_extract_string_by_candidate_keys(
|
||||
parsed_json.as_ref(),
|
||||
&[
|
||||
"whirlpool",
|
||||
"pool",
|
||||
"poolAddress",
|
||||
"poolAccount",
|
||||
"whirlpoolAddress",
|
||||
],
|
||||
)
|
||||
.or_else(|| kb_extract_account(&accounts, 0));
|
||||
let token_a_mint = kb_extract_string_by_candidate_keys(
|
||||
parsed_json.as_ref(),
|
||||
&[
|
||||
"tokenMintA",
|
||||
"tokenAMint",
|
||||
"mintA",
|
||||
"baseMint",
|
||||
"token0Mint",
|
||||
"mint0",
|
||||
],
|
||||
)
|
||||
.or_else(|| kb_extract_account(&accounts, 1));
|
||||
let token_b_mint = kb_extract_string_by_candidate_keys(
|
||||
parsed_json.as_ref(),
|
||||
&[
|
||||
"tokenMintB",
|
||||
"tokenBMint",
|
||||
"mintB",
|
||||
"quoteMint",
|
||||
"token1Mint",
|
||||
"mint1",
|
||||
],
|
||||
)
|
||||
.or_else(|| kb_extract_account(&accounts, 2));
|
||||
let config_account = kb_extract_string_by_candidate_keys(
|
||||
parsed_json.as_ref(),
|
||||
&[
|
||||
"whirlpoolsConfig",
|
||||
"config",
|
||||
"configAccount",
|
||||
"whirlpoolConfig",
|
||||
],
|
||||
)
|
||||
.or_else(|| kb_extract_account(&accounts, 3));
|
||||
let creator = kb_extract_string_by_candidate_keys(
|
||||
parsed_json.as_ref(),
|
||||
&["funder", "creator", "payer", "user", "owner"],
|
||||
)
|
||||
.or_else(|| kb_extract_account(&accounts, 4));
|
||||
if instruction_kind == KbOrcaWhirlpoolsInstructionKind::InitializePool
|
||||
|| instruction_kind == KbOrcaWhirlpoolsInstructionKind::InitializePoolV2
|
||||
{
|
||||
let used_v2 =
|
||||
instruction_kind == KbOrcaWhirlpoolsInstructionKind::InitializePoolV2;
|
||||
let payload_json = serde_json::json!({
|
||||
"decoder": "orca_whirlpools",
|
||||
"eventKind": "create_pool",
|
||||
"classifiedInstructionKind": if used_v2 { "initialize_pool_v2" } else { "initialize_pool" },
|
||||
"signature": transaction.signature,
|
||||
"instructionId": instruction_id,
|
||||
"instructionIndex": instruction.instruction_index,
|
||||
"accounts": accounts,
|
||||
"parsed": parsed_json,
|
||||
"logMessages": log_messages,
|
||||
"poolAccount": pool_account,
|
||||
"tokenAMint": token_a_mint,
|
||||
"tokenBMint": token_b_mint,
|
||||
"configAccount": config_account,
|
||||
"creator": creator
|
||||
});
|
||||
decoded_events.push(crate::KbOrcaWhirlpoolsDecodedEvent::CreatePool(
|
||||
crate::KbOrcaWhirlpoolsCreatePoolDecoded {
|
||||
transaction_id,
|
||||
instruction_id,
|
||||
signature: transaction.signature.clone(),
|
||||
program_id: program_id.clone(),
|
||||
pool_account,
|
||||
token_a_mint,
|
||||
token_b_mint,
|
||||
config_account,
|
||||
creator,
|
||||
used_v2,
|
||||
payload_json,
|
||||
},
|
||||
));
|
||||
continue;
|
||||
}
|
||||
if instruction_kind == KbOrcaWhirlpoolsInstructionKind::Swap
|
||||
|| instruction_kind == KbOrcaWhirlpoolsInstructionKind::SwapV2
|
||||
{
|
||||
let used_v2 = instruction_kind == KbOrcaWhirlpoolsInstructionKind::SwapV2;
|
||||
let trade_side = kb_infer_trade_side(&log_messages);
|
||||
let payload_json = serde_json::json!({
|
||||
"decoder": "orca_whirlpools",
|
||||
"eventKind": "swap",
|
||||
"classifiedInstructionKind": if used_v2 { "swap_v2" } else { "swap" },
|
||||
"signature": transaction.signature,
|
||||
"instructionId": instruction_id,
|
||||
"instructionIndex": instruction.instruction_index,
|
||||
"accounts": accounts,
|
||||
"parsed": parsed_json,
|
||||
"logMessages": log_messages,
|
||||
"poolAccount": pool_account,
|
||||
"tokenAMint": token_a_mint,
|
||||
"tokenBMint": token_b_mint,
|
||||
"tradeSide": format!("{:?}", trade_side)
|
||||
});
|
||||
decoded_events.push(crate::KbOrcaWhirlpoolsDecodedEvent::Swap(
|
||||
crate::KbOrcaWhirlpoolsSwapDecoded {
|
||||
transaction_id,
|
||||
instruction_id,
|
||||
signature: transaction.signature.clone(),
|
||||
program_id: program_id.clone(),
|
||||
trade_side,
|
||||
pool_account,
|
||||
token_a_mint,
|
||||
token_b_mint,
|
||||
used_v2,
|
||||
payload_json,
|
||||
},
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(decoded_events)
|
||||
}
|
||||
}
|
||||
|
||||
fn kb_classify_instruction_kind(
|
||||
parsed_json: std::option::Option<&serde_json::Value>,
|
||||
log_messages: &[std::string::String],
|
||||
) -> KbOrcaWhirlpoolsInstructionKind {
|
||||
let parsed_instruction_name = kb_extract_string_by_candidate_keys(
|
||||
parsed_json,
|
||||
&["instruction", "instructionName", "type", "name"],
|
||||
);
|
||||
if let Some(parsed_instruction_name) = parsed_instruction_name {
|
||||
let normalized = kb_normalize_text(parsed_instruction_name.as_str());
|
||||
if normalized.contains("initializepoolv2") {
|
||||
return KbOrcaWhirlpoolsInstructionKind::InitializePoolV2;
|
||||
}
|
||||
if normalized.contains("initializepool") {
|
||||
return KbOrcaWhirlpoolsInstructionKind::InitializePool;
|
||||
}
|
||||
if normalized == "swapv2" {
|
||||
return KbOrcaWhirlpoolsInstructionKind::SwapV2;
|
||||
}
|
||||
if normalized == "swap" {
|
||||
return KbOrcaWhirlpoolsInstructionKind::Swap;
|
||||
}
|
||||
}
|
||||
if kb_value_contains_any_key(
|
||||
parsed_json,
|
||||
&["tokenProgramA", "tokenProgramB", "memoProgram"],
|
||||
) && kb_log_messages_contain_keyword(log_messages, "initialize_pool")
|
||||
{
|
||||
return KbOrcaWhirlpoolsInstructionKind::InitializePoolV2;
|
||||
}
|
||||
if kb_value_contains_any_key(
|
||||
parsed_json,
|
||||
&["tokenProgramA", "tokenProgramB", "memoProgram"],
|
||||
) && kb_log_messages_contain_keyword(log_messages, "swap")
|
||||
{
|
||||
return KbOrcaWhirlpoolsInstructionKind::SwapV2;
|
||||
}
|
||||
if kb_log_messages_contain_keyword(log_messages, "initialize_pool_v2")
|
||||
|| kb_log_messages_contain_keyword(log_messages, "initializepoolv2")
|
||||
{
|
||||
return KbOrcaWhirlpoolsInstructionKind::InitializePoolV2;
|
||||
}
|
||||
if kb_log_messages_contain_keyword(log_messages, "initialize_pool")
|
||||
|| kb_log_messages_contain_keyword(log_messages, "initializepool")
|
||||
{
|
||||
return KbOrcaWhirlpoolsInstructionKind::InitializePool;
|
||||
}
|
||||
if kb_log_messages_contain_keyword(log_messages, "swap_v2")
|
||||
|| kb_log_messages_contain_keyword(log_messages, "swapv2")
|
||||
{
|
||||
return KbOrcaWhirlpoolsInstructionKind::SwapV2;
|
||||
}
|
||||
if kb_log_messages_contain_keyword(log_messages, "swap") {
|
||||
return KbOrcaWhirlpoolsInstructionKind::Swap;
|
||||
}
|
||||
KbOrcaWhirlpoolsInstructionKind::Unknown
|
||||
}
|
||||
|
||||
fn kb_extract_log_messages(
|
||||
transaction_json: &serde_json::Value,
|
||||
) -> std::vec::Vec<std::string::String> {
|
||||
let mut messages = std::vec::Vec::new();
|
||||
let meta_option = transaction_json.get("meta");
|
||||
let meta = match meta_option {
|
||||
Some(meta) => meta,
|
||||
None => return messages,
|
||||
};
|
||||
let logs_option = meta.get("logMessages");
|
||||
let logs = match logs_option {
|
||||
Some(logs) => logs,
|
||||
None => return messages,
|
||||
};
|
||||
let logs_array_option = logs.as_array();
|
||||
let logs_array = match logs_array_option {
|
||||
Some(logs_array) => logs_array,
|
||||
None => return messages,
|
||||
};
|
||||
for value in logs_array {
|
||||
let text_option = value.as_str();
|
||||
if let Some(text) = text_option {
|
||||
messages.push(text.to_string());
|
||||
}
|
||||
}
|
||||
messages
|
||||
}
|
||||
|
||||
fn kb_log_messages_contain_keyword(
|
||||
log_messages: &[std::string::String],
|
||||
keyword: &str,
|
||||
) -> bool {
|
||||
let keyword_normalized = kb_normalize_text(keyword);
|
||||
for log_message in log_messages {
|
||||
let log_normalized = kb_normalize_text(log_message.as_str());
|
||||
if log_normalized.contains(keyword_normalized.as_str()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn kb_normalize_text(value: &str) -> std::string::String {
|
||||
let mut normalized = std::string::String::new();
|
||||
for character in value.chars() {
|
||||
if character.is_ascii_alphanumeric() {
|
||||
normalized.push(character.to_ascii_lowercase());
|
||||
}
|
||||
}
|
||||
normalized
|
||||
}
|
||||
|
||||
fn kb_parse_accounts_json(
|
||||
accounts_json: &str,
|
||||
) -> Result<std::vec::Vec<std::string::String>, crate::KbError> {
|
||||
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!(
|
||||
"cannot parse instruction accounts_json '{}': {}",
|
||||
accounts_json, error
|
||||
)));
|
||||
}
|
||||
};
|
||||
let mut accounts = std::vec::Vec::new();
|
||||
for value in values {
|
||||
let text_option = value.as_str();
|
||||
if let Some(text) = text_option {
|
||||
accounts.push(text.to_string());
|
||||
}
|
||||
}
|
||||
Ok(accounts)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
fn kb_value_contains_any_key(
|
||||
value: std::option::Option<&serde_json::Value>,
|
||||
candidate_keys: &[&str],
|
||||
) -> bool {
|
||||
let value = match value {
|
||||
Some(value) => value,
|
||||
None => return false,
|
||||
};
|
||||
kb_value_contains_any_key_inner(value, candidate_keys)
|
||||
}
|
||||
|
||||
fn kb_value_contains_any_key_inner(
|
||||
value: &serde_json::Value,
|
||||
candidate_keys: &[&str],
|
||||
) -> bool {
|
||||
if let Some(object) = value.as_object() {
|
||||
for candidate_key in candidate_keys {
|
||||
if object.contains_key(*candidate_key) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for nested_value in object.values() {
|
||||
if kb_value_contains_any_key_inner(nested_value, candidate_keys) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if let Some(array) = value.as_array() {
|
||||
for nested_value in array {
|
||||
if kb_value_contains_any_key_inner(nested_value, candidate_keys) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn kb_extract_account(
|
||||
accounts: &[std::string::String],
|
||||
index: usize,
|
||||
) -> std::option::Option<std::string::String> {
|
||||
if index >= accounts.len() {
|
||||
return None;
|
||||
}
|
||||
Some(accounts[index].clone())
|
||||
}
|
||||
|
||||
fn kb_infer_trade_side(
|
||||
log_messages: &[std::string::String],
|
||||
) -> crate::KbSwapTradeSide {
|
||||
if kb_log_messages_contain_keyword(log_messages, "buy") {
|
||||
return crate::KbSwapTradeSide::BuyBase;
|
||||
}
|
||||
if kb_log_messages_contain_keyword(log_messages, "sell") {
|
||||
return crate::KbSwapTradeSide::SellBase;
|
||||
}
|
||||
crate::KbSwapTradeSide::Unknown
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
fn make_create_transaction() -> crate::KbChainTransactionDto {
|
||||
let mut dto = crate::KbChainTransactionDto::new(
|
||||
"sig-orca-create-1".to_string(),
|
||||
Some(891001),
|
||||
Some(1779600001),
|
||||
Some("helius_primary_http".to_string()),
|
||||
Some("0".to_string()),
|
||||
None,
|
||||
None,
|
||||
serde_json::json!({
|
||||
"slot": 891001,
|
||||
"meta": {
|
||||
"logMessages": [
|
||||
"Program log: Instruction: InitializePoolV2"
|
||||
]
|
||||
},
|
||||
"transaction": {
|
||||
"message": {
|
||||
"instructions": []
|
||||
}
|
||||
}
|
||||
})
|
||||
.to_string(),
|
||||
);
|
||||
dto.id = Some(601);
|
||||
dto
|
||||
}
|
||||
|
||||
fn make_create_instruction() -> crate::KbChainInstructionDto {
|
||||
let mut dto = crate::KbChainInstructionDto::new(
|
||||
601,
|
||||
None,
|
||||
0,
|
||||
None,
|
||||
Some(crate::KB_ORCA_WHIRLPOOLS_PROGRAM_ID.to_string()),
|
||||
Some("orca-whirlpools".to_string()),
|
||||
Some(1),
|
||||
serde_json::json!([
|
||||
"OrcaPool111",
|
||||
"OrcaTokenA111",
|
||||
"So11111111111111111111111111111111111111112",
|
||||
"OrcaConfig111",
|
||||
"OrcaCreator111"
|
||||
])
|
||||
.to_string(),
|
||||
None,
|
||||
None,
|
||||
Some(
|
||||
serde_json::json!({
|
||||
"info": {
|
||||
"instruction": "initialize_pool_v2",
|
||||
"whirlpool": "OrcaPool111",
|
||||
"tokenMintA": "OrcaTokenA111",
|
||||
"tokenMintB": "So11111111111111111111111111111111111111112",
|
||||
"whirlpoolsConfig": "OrcaConfig111",
|
||||
"funder": "OrcaCreator111",
|
||||
"tokenProgramA": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
|
||||
"tokenProgramB": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
|
||||
}
|
||||
})
|
||||
.to_string(),
|
||||
),
|
||||
);
|
||||
dto.id = Some(602);
|
||||
dto
|
||||
}
|
||||
|
||||
fn make_swap_transaction() -> crate::KbChainTransactionDto {
|
||||
let mut dto = crate::KbChainTransactionDto::new(
|
||||
"sig-orca-swap-1".to_string(),
|
||||
Some(891002),
|
||||
Some(1779600002),
|
||||
Some("helius_primary_http".to_string()),
|
||||
Some("0".to_string()),
|
||||
None,
|
||||
None,
|
||||
serde_json::json!({
|
||||
"slot": 891002,
|
||||
"meta": {
|
||||
"logMessages": [
|
||||
"Program log: Instruction: SwapV2"
|
||||
]
|
||||
},
|
||||
"transaction": {
|
||||
"message": {
|
||||
"instructions": []
|
||||
}
|
||||
}
|
||||
})
|
||||
.to_string(),
|
||||
);
|
||||
dto.id = Some(603);
|
||||
dto
|
||||
}
|
||||
|
||||
fn make_swap_instruction() -> crate::KbChainInstructionDto {
|
||||
let mut dto = crate::KbChainInstructionDto::new(
|
||||
603,
|
||||
None,
|
||||
0,
|
||||
None,
|
||||
Some(crate::KB_ORCA_WHIRLPOOLS_PROGRAM_ID.to_string()),
|
||||
Some("orca-whirlpools".to_string()),
|
||||
Some(1),
|
||||
serde_json::json!([
|
||||
"OrcaSwapPool111",
|
||||
"OrcaSwapTokenA111",
|
||||
"So11111111111111111111111111111111111111112"
|
||||
])
|
||||
.to_string(),
|
||||
None,
|
||||
None,
|
||||
Some(
|
||||
serde_json::json!({
|
||||
"info": {
|
||||
"instruction": "swap_v2",
|
||||
"whirlpool": "OrcaSwapPool111",
|
||||
"tokenMintA": "OrcaSwapTokenA111",
|
||||
"tokenMintB": "So11111111111111111111111111111111111111112"
|
||||
}
|
||||
})
|
||||
.to_string(),
|
||||
),
|
||||
);
|
||||
dto.id = Some(604);
|
||||
dto
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn orca_whirlpools_create_pool_is_detected() {
|
||||
let decoder = crate::KbOrcaWhirlpoolsDecoder::new();
|
||||
let transaction = make_create_transaction();
|
||||
let instructions = vec![make_create_instruction()];
|
||||
let decoded_result = decoder.decode_transaction(&transaction, &instructions);
|
||||
let decoded = match decoded_result {
|
||||
Ok(decoded) => decoded,
|
||||
Err(error) => panic!("decode must succeed: {}", error),
|
||||
};
|
||||
assert_eq!(decoded.len(), 1);
|
||||
match &decoded[0] {
|
||||
crate::KbOrcaWhirlpoolsDecodedEvent::CreatePool(event) => {
|
||||
assert_eq!(event.transaction_id, 601);
|
||||
assert_eq!(event.instruction_id, 602);
|
||||
assert_eq!(event.pool_account, Some("OrcaPool111".to_string()));
|
||||
assert_eq!(event.token_a_mint, Some("OrcaTokenA111".to_string()));
|
||||
assert_eq!(
|
||||
event.token_b_mint,
|
||||
Some("So11111111111111111111111111111111111111112".to_string())
|
||||
);
|
||||
assert_eq!(event.config_account, Some("OrcaConfig111".to_string()));
|
||||
assert!(event.used_v2);
|
||||
}
|
||||
crate::KbOrcaWhirlpoolsDecodedEvent::Swap(_) => {
|
||||
panic!("unexpected swap event")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn orca_whirlpools_swap_is_detected() {
|
||||
let decoder = crate::KbOrcaWhirlpoolsDecoder::new();
|
||||
let transaction = make_swap_transaction();
|
||||
let instructions = vec![make_swap_instruction()];
|
||||
let decoded_result = decoder.decode_transaction(&transaction, &instructions);
|
||||
let decoded = match decoded_result {
|
||||
Ok(decoded) => decoded,
|
||||
Err(error) => panic!("decode must succeed: {}", error),
|
||||
};
|
||||
assert_eq!(decoded.len(), 1);
|
||||
match &decoded[0] {
|
||||
crate::KbOrcaWhirlpoolsDecodedEvent::Swap(event) => {
|
||||
assert_eq!(event.transaction_id, 603);
|
||||
assert_eq!(event.instruction_id, 604);
|
||||
assert_eq!(event.pool_account, Some("OrcaSwapPool111".to_string()));
|
||||
assert_eq!(event.token_a_mint, Some("OrcaSwapTokenA111".to_string()));
|
||||
assert_eq!(
|
||||
event.token_b_mint,
|
||||
Some("So11111111111111111111111111111111111111112".to_string())
|
||||
);
|
||||
assert!(event.used_v2);
|
||||
}
|
||||
crate::KbOrcaWhirlpoolsDecodedEvent::CreatePool(_) => {
|
||||
panic!("unexpected create event")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user