feat(letsbonk): add idl manager

This commit is contained in:
smypmsa
2025-08-02 14:52:40 +00:00
parent 1ddadcd0af
commit 9a09862f0d
5 changed files with 282 additions and 70 deletions
+8 -21
View File
@@ -5,7 +5,6 @@ This module handles LetsBonk (Raydium LaunchLab) specific pool operations
by implementing the CurveManager interface using IDL-based decoding.
"""
import os
from typing import Any
from solders.pubkey import Pubkey
@@ -23,30 +22,18 @@ logger = get_logger(__name__)
class LetsBonkCurveManager(CurveManager):
"""LetsBonk (Raydium LaunchLab) implementation of CurveManager interface."""
def __init__(self, client: SolanaClient):
"""Initialize LetsBonk curve manager.
def __init__(self, client: SolanaClient, idl_parser: IDLParser):
"""Initialize LetsBonk curve manager with injected IDL parser.
Args:
client: Solana RPC client
idl_parser: Pre-loaded IDL parser for LetsBonk platform
"""
self.client = client
self.address_provider = LetsBonkAddressProvider()
self._idl_parser = self._load_idl_parser()
self._idl_parser = idl_parser
logger.info("LetsBonk curve manager initialized with IDL-based account parsing")
def _load_idl_parser(self) -> IDLParser:
"""Load the IDL parser for LetsBonk (Raydium LaunchLab)."""
# Get the IDL file path relative to the project root
current_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.join(current_dir, "..", "..", "..")
idl_path = os.path.join(project_root, "idl", "raydium_launchlab_idl.json")
idl_path = os.path.normpath(idl_path)
if not os.path.exists(idl_path):
raise FileNotFoundError(f"IDL file not found at {idl_path}")
return IDLParser(idl_path, verbose=False)
logger.info("LetsBonk curve manager initialized with injected IDL parser")
@property
def platform(self) -> Platform:
@@ -67,7 +54,7 @@ class LetsBonkCurveManager(CurveManager):
if not account.data:
raise ValueError(f"No data in pool state account {pool_address}")
# Decode pool state using IDL parser
# Decode pool state using injected IDL parser
pool_state_data = self._decode_pool_state_with_idl(account.data)
return pool_state_data
@@ -175,7 +162,7 @@ class LetsBonkCurveManager(CurveManager):
return (pool_state["virtual_base"], pool_state["virtual_quote"])
def _decode_pool_state_with_idl(self, data: bytes) -> dict[str, Any]:
"""Decode pool state data using IDL parser.
"""Decode pool state data using injected IDL parser.
Args:
data: Raw account data
@@ -186,7 +173,7 @@ class LetsBonkCurveManager(CurveManager):
Raises:
ValueError: If IDL parsing fails
"""
# Use IDL parser to decode PoolState account data
# Use injected IDL parser to decode PoolState account data
decoded_pool_state = self._idl_parser.decode_account_data(
data,
"PoolState",
+12 -22
View File
@@ -6,7 +6,6 @@ by implementing the EventParser interface with IDL-based parsing.
"""
import base64
import os
import struct
from time import monotonic
from typing import Any
@@ -25,30 +24,21 @@ logger = get_logger(__name__)
class LetsBonkEventParser(EventParser):
"""LetsBonk implementation of EventParser interface with IDL-based parsing."""
def __init__(self):
"""Initialize LetsBonk event parser with IDL support."""
self.address_provider = LetsBonkAddressProvider()
self._idl_parser = self._load_idl_parser()
def __init__(self, idl_parser: IDLParser):
"""Initialize LetsBonk event parser with injected IDL parser.
# Get discriminators from IDL
Args:
idl_parser: Pre-loaded IDL parser for LetsBonk platform
"""
self.address_provider = LetsBonkAddressProvider()
self._idl_parser = idl_parser
# Get discriminators from injected IDL parser
discriminators = self._idl_parser.get_instruction_discriminators()
self._initialize_discriminator_bytes = discriminators["initialize"]
self._initialize_discriminator = struct.unpack("<Q", self._initialize_discriminator_bytes)[0]
logger.info("LetsBonk event parser initialized with IDL-based discriminators")
def _load_idl_parser(self) -> IDLParser:
"""Load the IDL parser for LetsBonk (Raydium LaunchLab)."""
# Get the IDL file path relative to the project root
current_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.join(current_dir, "..", "..", "..")
idl_path = os.path.join(project_root, "idl", "raydium_launchlab_idl.json")
idl_path = os.path.normpath(idl_path)
if not os.path.exists(idl_path):
raise FileNotFoundError(f"IDL file not found at {idl_path}")
return IDLParser(idl_path, verbose=False)
logger.info("LetsBonk event parser initialized with injected IDL parser")
@property
def platform(self) -> Platform:
@@ -79,7 +69,7 @@ class LetsBonkEventParser(EventParser):
accounts: list[int],
account_keys: list[bytes]
) -> TokenInfo | None:
"""Parse token creation from LetsBonk instruction data using IDL.
"""Parse token creation from LetsBonk instruction data using injected IDL parser.
Args:
instruction_data: Raw instruction data
@@ -102,7 +92,7 @@ class LetsBonkEventParser(EventParser):
return None
return Pubkey.from_bytes(account_keys[account_index])
# Parse instruction data using IDL parser
# Parse instruction data using injected IDL parser
decoded = self._idl_parser.decode_instruction(instruction_data, account_keys, accounts)
if not decoded or decoded['instruction_name'] != 'initialize':
return None
+9 -19
View File
@@ -6,7 +6,6 @@ by implementing the InstructionBuilder interface with IDL-based discriminators.
"""
import hashlib
import os
import struct
import time
@@ -26,29 +25,20 @@ logger = get_logger(__name__)
class LetsBonkInstructionBuilder(InstructionBuilder):
"""LetsBonk (Raydium LaunchLab) implementation of InstructionBuilder interface with IDL-based discriminators."""
def __init__(self):
"""Initialize LetsBonk instruction builder with IDL support."""
self._idl_parser = self._load_idl_parser()
def __init__(self, idl_parser: IDLParser):
"""Initialize LetsBonk instruction builder with injected IDL parser.
# Get discriminators from IDL
Args:
idl_parser: Pre-loaded IDL parser for LetsBonk platform
"""
self._idl_parser = idl_parser
# Get discriminators from injected IDL parser
discriminators = self._idl_parser.get_instruction_discriminators()
self._buy_exact_in_discriminator = discriminators["buy_exact_in"]
self._sell_exact_in_discriminator = discriminators["sell_exact_in"]
logger.info("LetsBonk instruction builder initialized with IDL-based discriminators")
def _load_idl_parser(self) -> IDLParser:
"""Load the IDL parser for LetsBonk (Raydium LaunchLab)."""
# Get the IDL file path relative to the project root
current_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.join(current_dir, "..", "..", "..")
idl_path = os.path.join(project_root, "idl", "raydium_launchlab_idl.json")
idl_path = os.path.normpath(idl_path)
if not os.path.exists(idl_path):
raise FileNotFoundError(f"IDL file not found at {idl_path}")
return IDLParser(idl_path, verbose=False)
logger.info("LetsBonk instruction builder initialized with injected IDL parser")
@property
def platform(self) -> Platform: