mirror of
https://github.com/0xfnzero/solana-streamer.git
synced 2026-08-16 18:38:05 +00:00
fix raydium clmm bitmap extension account disc & support whirlpool account event
This commit is contained in:
@@ -53,6 +53,7 @@ pub enum ProtocolType {
|
||||
RaydiumAmmV4,
|
||||
MeteoraDammV2,
|
||||
MeteoraDlmm,
|
||||
Whirlpool,
|
||||
Common,
|
||||
}
|
||||
|
||||
@@ -136,6 +137,7 @@ pub enum EventType {
|
||||
AccountRaydiumCpmmAmmConfig,
|
||||
AccountRaydiumCpmmPoolState,
|
||||
AccountMeteoraDlmmLbPair,
|
||||
AccountWhirlpool,
|
||||
|
||||
NonceAccount,
|
||||
TokenAccount,
|
||||
@@ -164,6 +166,7 @@ pub const ACCOUNT_EVENT_TYPES: &[EventType] = &[
|
||||
EventType::AccountRaydiumCpmmAmmConfig,
|
||||
EventType::AccountRaydiumCpmmPoolState,
|
||||
EventType::AccountMeteoraDlmmLbPair,
|
||||
EventType::AccountWhirlpool,
|
||||
EventType::TokenAccount,
|
||||
EventType::NonceAccount,
|
||||
];
|
||||
@@ -241,6 +244,7 @@ impl fmt::Display for EventType {
|
||||
EventType::AccountRaydiumCpmmAmmConfig => write!(f, "AccountRaydiumCpmmAmmConfig"),
|
||||
EventType::AccountRaydiumCpmmPoolState => write!(f, "AccountRaydiumCpmmPoolState"),
|
||||
EventType::AccountMeteoraDlmmLbPair => write!(f, "AccountMeteoraDlmmLbPair"),
|
||||
EventType::AccountWhirlpool => write!(f, "AccountWhirlpool"),
|
||||
EventType::TokenAccount => write!(f, "TokenAccount"),
|
||||
EventType::NonceAccount => write!(f, "NonceAccount"),
|
||||
EventType::BlockMeta => write!(f, "BlockMeta"),
|
||||
|
||||
@@ -15,6 +15,7 @@ use crate::streaming::event_parser::{
|
||||
meteora_dlmm::parser as meteora_dlmm, pumpfun::parser as pumpfun,
|
||||
pumpswap::parser as pumpswap, raydium_amm_v4::parser as raydium_amm_v4,
|
||||
raydium_clmm::parser as raydium_clmm, raydium_cpmm::parser as raydium_cpmm,
|
||||
whirlpool::parser as whirlpool,
|
||||
},
|
||||
DexEvent, Protocol,
|
||||
};
|
||||
@@ -56,6 +57,7 @@ impl EventDispatcher {
|
||||
Protocol::RaydiumAmmV4 => ProtocolType::RaydiumAmmV4,
|
||||
Protocol::MeteoraDammV2 => ProtocolType::MeteoraDammV2,
|
||||
Protocol::MeteoraDlmm => ProtocolType::MeteoraDlmm,
|
||||
Protocol::Whirlpool => ProtocolType::Whirlpool,
|
||||
};
|
||||
|
||||
match protocol {
|
||||
@@ -105,6 +107,10 @@ impl EventDispatcher {
|
||||
// Meteora DLMM 目前不需要解析指令数据,返回 None
|
||||
None
|
||||
}
|
||||
Protocol::Whirlpool => {
|
||||
// Whirlpool 目前不需要解析指令数据,返回 None
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,6 +142,7 @@ impl EventDispatcher {
|
||||
Protocol::RaydiumAmmV4 => ProtocolType::RaydiumAmmV4,
|
||||
Protocol::MeteoraDammV2 => ProtocolType::MeteoraDammV2,
|
||||
Protocol::MeteoraDlmm => ProtocolType::MeteoraDlmm,
|
||||
Protocol::Whirlpool => ProtocolType::Whirlpool,
|
||||
};
|
||||
|
||||
match protocol {
|
||||
@@ -178,6 +185,10 @@ impl EventDispatcher {
|
||||
// Meteora DLMM 目前不需要解析 inner instruction 数据,返回 None
|
||||
None
|
||||
}
|
||||
Protocol::Whirlpool => {
|
||||
// Whirlpool 目前不需要解析 inner instruction 数据,返回 None
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,6 +211,8 @@ impl EventDispatcher {
|
||||
Some(Protocol::MeteoraDammV2)
|
||||
} else if program_id == &meteora_dlmm::METEORA_DLMM_PROGRAM_ID {
|
||||
Some(Protocol::MeteoraDlmm)
|
||||
} else if program_id == &whirlpool::WHIRLPOOL_PROGRAM_ID {
|
||||
Some(Protocol::Whirlpool)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -239,6 +252,7 @@ impl EventDispatcher {
|
||||
Protocol::RaydiumAmmV4 => raydium_amm_v4::RAYDIUM_AMM_V4_PROGRAM_ID,
|
||||
Protocol::MeteoraDammV2 => meteora_damm_v2::METEORA_DAMM_V2_PROGRAM_ID,
|
||||
Protocol::MeteoraDlmm => meteora_dlmm::METEORA_DLMM_PROGRAM_ID,
|
||||
Protocol::Whirlpool => whirlpool::WHIRLPOOL_PROGRAM_ID,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -276,6 +290,7 @@ impl EventDispatcher {
|
||||
Protocol::RaydiumAmmV4 => ProtocolType::RaydiumAmmV4,
|
||||
Protocol::MeteoraDammV2 => ProtocolType::MeteoraDammV2,
|
||||
Protocol::MeteoraDlmm => ProtocolType::MeteoraDlmm,
|
||||
Protocol::Whirlpool => ProtocolType::Whirlpool,
|
||||
};
|
||||
|
||||
match protocol {
|
||||
@@ -302,6 +317,9 @@ impl EventDispatcher {
|
||||
Protocol::MeteoraDlmm => {
|
||||
meteora_dlmm::parse_meteora_dlmm_account_data(discriminator, account, metadata)
|
||||
}
|
||||
Protocol::Whirlpool => {
|
||||
whirlpool::parse_whirlpool_account_data(discriminator, account, metadata)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ use crate::streaming::event_parser::protocols::pumpswap::events::*;
|
||||
use crate::streaming::event_parser::protocols::raydium_amm_v4::events::*;
|
||||
use crate::streaming::event_parser::protocols::raydium_clmm::events::*;
|
||||
use crate::streaming::event_parser::protocols::raydium_cpmm::events::*;
|
||||
use crate::streaming::event_parser::protocols::whirlpool::events::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt::Debug;
|
||||
|
||||
@@ -86,6 +87,9 @@ pub enum DexEvent {
|
||||
// Meteora DLMM events
|
||||
MeteoraDlmmLbPairAccountEvent(MeteoraDlmmLbPairAccountEvent),
|
||||
|
||||
// Whirlpool events
|
||||
WhirlpoolAccountEvent(WhirlpoolAccountEvent),
|
||||
|
||||
// Common events
|
||||
TokenAccountEvent(TokenAccountEvent),
|
||||
NonceAccountEvent(NonceAccountEvent),
|
||||
@@ -148,6 +152,7 @@ impl DexEvent {
|
||||
DexEvent::MeteoraDammV2InitializeCustomizablePoolEvent(e) => &e.metadata,
|
||||
DexEvent::MeteoraDammV2InitializePoolWithDynamicConfigEvent(e) => &e.metadata,
|
||||
DexEvent::MeteoraDlmmLbPairAccountEvent(e) => &e.metadata,
|
||||
DexEvent::WhirlpoolAccountEvent(e) => &e.metadata,
|
||||
DexEvent::TokenAccountEvent(e) => &e.metadata,
|
||||
DexEvent::NonceAccountEvent(e) => &e.metadata,
|
||||
DexEvent::TokenInfoEvent(e) => &e.metadata,
|
||||
@@ -209,6 +214,7 @@ impl DexEvent {
|
||||
DexEvent::MeteoraDammV2InitializeCustomizablePoolEvent(e) => &mut e.metadata,
|
||||
DexEvent::MeteoraDammV2InitializePoolWithDynamicConfigEvent(e) => &mut e.metadata,
|
||||
DexEvent::MeteoraDlmmLbPairAccountEvent(e) => &mut e.metadata,
|
||||
DexEvent::WhirlpoolAccountEvent(e) => &mut e.metadata,
|
||||
DexEvent::TokenAccountEvent(e) => &mut e.metadata,
|
||||
DexEvent::NonceAccountEvent(e) => &mut e.metadata,
|
||||
DexEvent::TokenInfoEvent(e) => &mut e.metadata,
|
||||
|
||||
@@ -8,5 +8,6 @@ pub mod raydium_amm_v4;
|
||||
pub mod raydium_clmm;
|
||||
pub mod raydium_cpmm;
|
||||
pub mod types;
|
||||
pub mod whirlpool;
|
||||
pub use block::block_meta_event::BlockMetaEvent;
|
||||
pub use types::Protocol;
|
||||
|
||||
@@ -275,5 +275,5 @@ pub mod discriminators {
|
||||
pub const AMM_CONFIG: &[u8] = &[218, 244, 33, 104, 203, 203, 43, 111];
|
||||
pub const POOL_STATE: &[u8] = &[247, 237, 227, 245, 215, 195, 222, 70];
|
||||
pub const TICK_ARRAY_STATE: &[u8] = &[192, 155, 85, 205, 49, 249, 129, 42];
|
||||
pub const TICK_ARRAY_BITMAP_EXTENSION: &[u8] = &[80, 111, 124, 113, 55, 237, 18, 5];
|
||||
pub const TICK_ARRAY_BITMAP_EXTENSION: &[u8] = &[60, 150, 36, 219, 97, 128, 139, 153];
|
||||
}
|
||||
|
||||
@@ -236,7 +236,7 @@ pub fn tick_array_state_parser(
|
||||
pub const EXTENSION_TICKARRAY_BITMAP_SIZE: usize = 14;
|
||||
|
||||
#[repr(C, packed)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct TickArrayBitmapExtension {
|
||||
pub pool_id: Pubkey,
|
||||
/// Packed initialized tick array state for start_tick_index is positive
|
||||
@@ -255,8 +255,7 @@ impl Default for TickArrayBitmapExtension {
|
||||
}
|
||||
}
|
||||
|
||||
// 计算大小:Pubkey (32) + positive_bitmap (12 * 8 * 8) + negative_bitmap (12 * 8 * 8)
|
||||
pub const TICK_ARRAY_BITMAP_EXTENSION_SIZE: usize = 32 + (EXTENSION_TICKARRAY_BITMAP_SIZE * 8 * 64) + (EXTENSION_TICKARRAY_BITMAP_SIZE * 8 * 64);
|
||||
pub const TICK_ARRAY_BITMAP_EXTENSION_SIZE: usize = 32 + (EXTENSION_TICKARRAY_BITMAP_SIZE * 8 * 8) + (EXTENSION_TICKARRAY_BITMAP_SIZE * 8 * 8);
|
||||
|
||||
pub fn tick_array_bitmap_extension_decode(data: &[u8]) -> Option<TickArrayBitmapExtension> {
|
||||
if data.len() < TICK_ARRAY_BITMAP_EXTENSION_SIZE {
|
||||
|
||||
@@ -3,6 +3,7 @@ use crate::streaming::event_parser::protocols::{
|
||||
meteora_dlmm::parser::METEORA_DLMM_PROGRAM_ID, pumpfun::parser::PUMPFUN_PROGRAM_ID,
|
||||
pumpswap::parser::PUMPSWAP_PROGRAM_ID, raydium_amm_v4::parser::RAYDIUM_AMM_V4_PROGRAM_ID,
|
||||
raydium_clmm::parser::RAYDIUM_CLMM_PROGRAM_ID, raydium_cpmm::parser::RAYDIUM_CPMM_PROGRAM_ID,
|
||||
whirlpool::parser::WHIRLPOOL_PROGRAM_ID,
|
||||
};
|
||||
use anyhow::{anyhow, Result};
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
@@ -18,6 +19,7 @@ pub enum Protocol {
|
||||
RaydiumAmmV4,
|
||||
MeteoraDammV2,
|
||||
MeteoraDlmm,
|
||||
Whirlpool,
|
||||
}
|
||||
|
||||
impl Protocol {
|
||||
@@ -31,6 +33,7 @@ impl Protocol {
|
||||
Protocol::RaydiumAmmV4 => vec![RAYDIUM_AMM_V4_PROGRAM_ID],
|
||||
Protocol::MeteoraDammV2 => vec![METEORA_DAMM_V2_PROGRAM_ID],
|
||||
Protocol::MeteoraDlmm => vec![METEORA_DLMM_PROGRAM_ID],
|
||||
Protocol::Whirlpool => vec![WHIRLPOOL_PROGRAM_ID],
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -46,6 +49,7 @@ impl std::fmt::Display for Protocol {
|
||||
Protocol::RaydiumAmmV4 => write!(f, "RaydiumAmmV4"),
|
||||
Protocol::MeteoraDammV2 => write!(f, "MeteoraDammV2"),
|
||||
Protocol::MeteoraDlmm => write!(f, "MeteoraDlmm"),
|
||||
Protocol::Whirlpool => write!(f, "Whirlpool"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,6 +67,7 @@ impl std::str::FromStr for Protocol {
|
||||
"raydiumammv4" => Ok(Protocol::RaydiumAmmV4),
|
||||
"meteoradamm_v2" => Ok(Protocol::MeteoraDammV2),
|
||||
"meteoradlmm" => Ok(Protocol::MeteoraDlmm),
|
||||
"whirlpool" => Ok(Protocol::Whirlpool),
|
||||
_ => Err(anyhow!("Unsupported protocol: {}", s)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
use crate::streaming::event_parser::common::EventMetadata;
|
||||
use crate::streaming::event_parser::protocols::whirlpool::types::Whirlpool;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
|
||||
/// Whirlpool 账户事件
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct WhirlpoolAccountEvent {
|
||||
pub metadata: EventMetadata,
|
||||
pub pubkey: Pubkey,
|
||||
pub executable: bool,
|
||||
pub lamports: u64,
|
||||
pub owner: Pubkey,
|
||||
pub rent_epoch: u64,
|
||||
pub whirlpool: Whirlpool,
|
||||
}
|
||||
|
||||
/// 事件鉴别器常量
|
||||
pub mod discriminators {
|
||||
// 账户鉴别器 - Anchor discriminator for "Whirlpool" account
|
||||
// 这是通过 Anchor 的账户名称 "account:Whirlpool" 计算得出的 8 字节哈希
|
||||
pub const WHIRLPOOL: &[u8] = &[63, 149, 209, 12, 225, 128, 99, 9];
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
pub mod events;
|
||||
pub mod parser;
|
||||
pub mod types;
|
||||
|
||||
pub use events::*;
|
||||
@@ -0,0 +1,25 @@
|
||||
use crate::streaming::event_parser::protocols::whirlpool::discriminators;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
|
||||
/// Whirlpool 程序ID
|
||||
pub const WHIRLPOOL_PROGRAM_ID: Pubkey =
|
||||
solana_sdk::pubkey!("whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc");
|
||||
|
||||
/// 解析 Whirlpool 账户数据
|
||||
///
|
||||
/// 根据判别器路由到具体的账户解析函数
|
||||
pub fn parse_whirlpool_account_data(
|
||||
discriminator: &[u8],
|
||||
account: &crate::streaming::grpc::AccountPretty,
|
||||
metadata: crate::streaming::event_parser::common::EventMetadata,
|
||||
) -> Option<crate::streaming::event_parser::DexEvent> {
|
||||
match discriminator {
|
||||
discriminators::WHIRLPOOL => {
|
||||
crate::streaming::event_parser::protocols::whirlpool::types::whirlpool_parser(
|
||||
account,
|
||||
metadata,
|
||||
)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,291 @@
|
||||
use borsh::BorshDeserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
|
||||
use crate::streaming::{
|
||||
event_parser::{
|
||||
common::{EventMetadata, EventType},
|
||||
protocols::whirlpool::WhirlpoolAccountEvent,
|
||||
DexEvent,
|
||||
},
|
||||
grpc::AccountPretty,
|
||||
};
|
||||
|
||||
// Number of rewards supported by Whirlpools
|
||||
pub const NUM_REWARDS: usize = 3;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize, BorshDeserialize)]
|
||||
pub struct WhirlpoolRewardInfo {
|
||||
/// Reward token mint.
|
||||
pub mint: Pubkey,
|
||||
/// Reward vault token account.
|
||||
pub vault: Pubkey,
|
||||
/// Authority account that has permission to initialize the reward and set emissions.
|
||||
pub authority: Pubkey,
|
||||
/// Q64.64 number that indicates how many tokens per second are earned per unit of liquidity.
|
||||
pub emissions_per_second_x64: u128,
|
||||
/// Q64.64 number that tracks the total tokens earned per unit of liquidity since the reward
|
||||
/// emissions were turned on.
|
||||
pub growth_global_x64: u128,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Whirlpool {
|
||||
pub whirlpools_config: Pubkey,
|
||||
pub whirlpool_bump: [u8; 1],
|
||||
pub tick_spacing: u16,
|
||||
pub fee_tier_index_seed: [u8; 2],
|
||||
pub fee_rate: u16,
|
||||
pub protocol_fee_rate: u16,
|
||||
pub liquidity: u128,
|
||||
pub sqrt_price: u128,
|
||||
pub tick_current_index: i32,
|
||||
pub protocol_fee_owed_a: u64,
|
||||
pub protocol_fee_owed_b: u64,
|
||||
pub token_mint_a: Pubkey,
|
||||
pub token_vault_a: Pubkey,
|
||||
pub fee_growth_global_a: u128,
|
||||
pub token_mint_b: Pubkey,
|
||||
pub token_vault_b: Pubkey,
|
||||
pub fee_growth_global_b: u128,
|
||||
pub reward_last_updated_timestamp: u64,
|
||||
pub reward_infos: [WhirlpoolRewardInfo; NUM_REWARDS],
|
||||
}
|
||||
|
||||
// Whirlpool::LEN = 8 (discriminator) + 261 + 384 = 653
|
||||
// 261 = 32 + 1 + 2 + 2 + 2 + 2 + 16 + 16 + 4 + 8 + 8 + 32 + 32 + 16 + 32 + 32 + 16 + 8
|
||||
// 384 = 3 * 128 (each WhirlpoolRewardInfo is 128 bytes)
|
||||
// 总数据大小(不包括 discriminator)= 261 + 384 = 645
|
||||
pub const WHIRLPOOL_SIZE: usize = 261 + 384; // 645 bytes (不包括 discriminator)
|
||||
pub const WHIRLPOOL_REWARD_INFO_SIZE: usize = 128;
|
||||
|
||||
pub fn whirlpool_decode(data: &[u8]) -> Option<Whirlpool> {
|
||||
if data.len() < WHIRLPOOL_SIZE {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut offset = 0;
|
||||
|
||||
// whirlpools_config: Pubkey (32 bytes)
|
||||
let whirlpools_config = Pubkey::try_from(&data[offset..offset + 32]).ok()?;
|
||||
offset += 32;
|
||||
|
||||
// whirlpool_bump: [u8; 1] (1 byte)
|
||||
let whirlpool_bump = [data[offset]];
|
||||
offset += 1;
|
||||
|
||||
// tick_spacing: u16 (2 bytes)
|
||||
let tick_spacing = u16::from_le_bytes([data[offset], data[offset + 1]]);
|
||||
offset += 2;
|
||||
|
||||
// fee_tier_index_seed: [u8; 2] (2 bytes)
|
||||
let fee_tier_index_seed = [data[offset], data[offset + 1]];
|
||||
offset += 2;
|
||||
|
||||
// fee_rate: u16 (2 bytes)
|
||||
let fee_rate = u16::from_le_bytes([data[offset], data[offset + 1]]);
|
||||
offset += 2;
|
||||
|
||||
// protocol_fee_rate: u16 (2 bytes)
|
||||
let protocol_fee_rate = u16::from_le_bytes([data[offset], data[offset + 1]]);
|
||||
offset += 2;
|
||||
|
||||
// liquidity: u128 (16 bytes)
|
||||
let mut liquidity_bytes = [0u8; 16];
|
||||
liquidity_bytes.copy_from_slice(&data[offset..offset + 16]);
|
||||
let liquidity = u128::from_le_bytes(liquidity_bytes);
|
||||
offset += 16;
|
||||
|
||||
// sqrt_price: u128 (16 bytes)
|
||||
let mut sqrt_price_bytes = [0u8; 16];
|
||||
sqrt_price_bytes.copy_from_slice(&data[offset..offset + 16]);
|
||||
let sqrt_price = u128::from_le_bytes(sqrt_price_bytes);
|
||||
offset += 16;
|
||||
|
||||
// tick_current_index: i32 (4 bytes)
|
||||
let tick_current_index = i32::from_le_bytes([
|
||||
data[offset],
|
||||
data[offset + 1],
|
||||
data[offset + 2],
|
||||
data[offset + 3],
|
||||
]);
|
||||
offset += 4;
|
||||
|
||||
// protocol_fee_owed_a: u64 (8 bytes)
|
||||
let protocol_fee_owed_a = u64::from_le_bytes([
|
||||
data[offset],
|
||||
data[offset + 1],
|
||||
data[offset + 2],
|
||||
data[offset + 3],
|
||||
data[offset + 4],
|
||||
data[offset + 5],
|
||||
data[offset + 6],
|
||||
data[offset + 7],
|
||||
]);
|
||||
offset += 8;
|
||||
|
||||
// protocol_fee_owed_b: u64 (8 bytes)
|
||||
let protocol_fee_owed_b = u64::from_le_bytes([
|
||||
data[offset],
|
||||
data[offset + 1],
|
||||
data[offset + 2],
|
||||
data[offset + 3],
|
||||
data[offset + 4],
|
||||
data[offset + 5],
|
||||
data[offset + 6],
|
||||
data[offset + 7],
|
||||
]);
|
||||
offset += 8;
|
||||
|
||||
// token_mint_a: Pubkey (32 bytes)
|
||||
let token_mint_a = Pubkey::try_from(&data[offset..offset + 32]).ok()?;
|
||||
offset += 32;
|
||||
|
||||
// token_vault_a: Pubkey (32 bytes)
|
||||
let token_vault_a = Pubkey::try_from(&data[offset..offset + 32]).ok()?;
|
||||
offset += 32;
|
||||
|
||||
// fee_growth_global_a: u128 (16 bytes)
|
||||
let mut fee_growth_global_a_bytes = [0u8; 16];
|
||||
fee_growth_global_a_bytes.copy_from_slice(&data[offset..offset + 16]);
|
||||
let fee_growth_global_a = u128::from_le_bytes(fee_growth_global_a_bytes);
|
||||
offset += 16;
|
||||
|
||||
// token_mint_b: Pubkey (32 bytes)
|
||||
let token_mint_b = Pubkey::try_from(&data[offset..offset + 32]).ok()?;
|
||||
offset += 32;
|
||||
|
||||
// token_vault_b: Pubkey (32 bytes)
|
||||
let token_vault_b = Pubkey::try_from(&data[offset..offset + 32]).ok()?;
|
||||
offset += 32;
|
||||
|
||||
// fee_growth_global_b: u128 (16 bytes)
|
||||
let mut fee_growth_global_b_bytes = [0u8; 16];
|
||||
fee_growth_global_b_bytes.copy_from_slice(&data[offset..offset + 16]);
|
||||
let fee_growth_global_b = u128::from_le_bytes(fee_growth_global_b_bytes);
|
||||
offset += 16;
|
||||
|
||||
// reward_last_updated_timestamp: u64 (8 bytes)
|
||||
let reward_last_updated_timestamp = u64::from_le_bytes([
|
||||
data[offset],
|
||||
data[offset + 1],
|
||||
data[offset + 2],
|
||||
data[offset + 3],
|
||||
data[offset + 4],
|
||||
data[offset + 5],
|
||||
data[offset + 6],
|
||||
data[offset + 7],
|
||||
]);
|
||||
offset += 8;
|
||||
|
||||
// reward_infos: [WhirlpoolRewardInfo; NUM_REWARDS] (384 bytes)
|
||||
// 检查是否有足够的数据来解析所有奖励信息
|
||||
if data.len() < offset + (NUM_REWARDS * WHIRLPOOL_REWARD_INFO_SIZE) {
|
||||
log::warn!(
|
||||
"Whirlpool 奖励信息数据不足: 需要 {} 字节,实际 {} 字节",
|
||||
offset + (NUM_REWARDS * WHIRLPOOL_REWARD_INFO_SIZE),
|
||||
data.len()
|
||||
);
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut reward_infos = [WhirlpoolRewardInfo::default(); NUM_REWARDS];
|
||||
for i in 0..NUM_REWARDS {
|
||||
|
||||
// mint: Pubkey (32 bytes)
|
||||
let mint = Pubkey::try_from(&data[offset..offset + 32]).ok()?;
|
||||
offset += 32;
|
||||
|
||||
// vault: Pubkey (32 bytes)
|
||||
let vault = Pubkey::try_from(&data[offset..offset + 32]).ok()?;
|
||||
offset += 32;
|
||||
|
||||
// authority: Pubkey (32 bytes)
|
||||
let authority = Pubkey::try_from(&data[offset..offset + 32]).ok()?;
|
||||
offset += 32;
|
||||
|
||||
// emissions_per_second_x64: u128 (16 bytes)
|
||||
let mut emissions_bytes = [0u8; 16];
|
||||
emissions_bytes.copy_from_slice(&data[offset..offset + 16]);
|
||||
let emissions_per_second_x64 = u128::from_le_bytes(emissions_bytes);
|
||||
offset += 16;
|
||||
|
||||
// growth_global_x64: u128 (16 bytes)
|
||||
let mut growth_bytes = [0u8; 16];
|
||||
growth_bytes.copy_from_slice(&data[offset..offset + 16]);
|
||||
let growth_global_x64 = u128::from_le_bytes(growth_bytes);
|
||||
offset += 16;
|
||||
|
||||
reward_infos[i] = WhirlpoolRewardInfo {
|
||||
mint,
|
||||
vault,
|
||||
authority,
|
||||
emissions_per_second_x64,
|
||||
growth_global_x64,
|
||||
};
|
||||
}
|
||||
|
||||
Some(Whirlpool {
|
||||
whirlpools_config,
|
||||
whirlpool_bump,
|
||||
tick_spacing,
|
||||
fee_tier_index_seed,
|
||||
fee_rate,
|
||||
protocol_fee_rate,
|
||||
liquidity,
|
||||
sqrt_price,
|
||||
tick_current_index,
|
||||
protocol_fee_owed_a,
|
||||
protocol_fee_owed_b,
|
||||
token_mint_a,
|
||||
token_vault_a,
|
||||
fee_growth_global_a,
|
||||
token_mint_b,
|
||||
token_vault_b,
|
||||
fee_growth_global_b,
|
||||
reward_last_updated_timestamp,
|
||||
reward_infos,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn whirlpool_parser(account: &AccountPretty, mut metadata: EventMetadata) -> Option<DexEvent> {
|
||||
metadata.event_type = EventType::AccountWhirlpool;
|
||||
|
||||
// 账户总大小应该是 8 (discriminator) + 645 (数据) = 653 字节
|
||||
let expected_size = 8 + WHIRLPOOL_SIZE;
|
||||
if account.data.len() < expected_size {
|
||||
log::warn!(
|
||||
"Whirlpool 账户数据长度不足: 需要至少 {} 字节,实际 {} 字节",
|
||||
expected_size,
|
||||
account.data.len()
|
||||
);
|
||||
return None;
|
||||
}
|
||||
|
||||
log::debug!(
|
||||
"开始解析 Whirlpool 账户: pubkey={}, 数据长度={}, 期望长度={}",
|
||||
account.pubkey,
|
||||
account.data.len(),
|
||||
expected_size
|
||||
);
|
||||
|
||||
// 跳过前 8 字节的 discriminator,解析接下来的 645 字节
|
||||
if let Some(whirlpool) = whirlpool_decode(&account.data[8..8 + WHIRLPOOL_SIZE]) {
|
||||
Some(DexEvent::WhirlpoolAccountEvent(WhirlpoolAccountEvent {
|
||||
metadata,
|
||||
pubkey: account.pubkey,
|
||||
executable: account.executable,
|
||||
lamports: account.lamports,
|
||||
owner: account.owner,
|
||||
rent_epoch: account.rent_epoch,
|
||||
whirlpool,
|
||||
}))
|
||||
} else {
|
||||
log::warn!(
|
||||
"Whirlpool 账户数据解析失败: pubkey={}, 数据长度={}",
|
||||
account.pubkey,
|
||||
account.data.len()
|
||||
);
|
||||
None
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user