110 lines
3.2 KiB
Python
110 lines
3.2 KiB
Python
"""
|
|
Base interfaces for instruction parsers
|
|
"""
|
|
from abc import ABC, abstractmethod
|
|
from typing import Dict, List, Optional, Tuple, Any
|
|
|
|
|
|
class InstructionParser(ABC):
|
|
"""
|
|
Base class for all instruction parsers
|
|
"""
|
|
|
|
@property
|
|
@abstractmethod
|
|
def program_id(self) -> str:
|
|
"""
|
|
Return the program ID that this parser handles
|
|
"""
|
|
pass
|
|
|
|
@abstractmethod
|
|
def parse_instruction(self, instruction: Dict, accounts: List[str], instruction_data: bytes) -> Dict:
|
|
"""
|
|
Parse a single instruction from transaction data
|
|
|
|
Args:
|
|
instruction: The instruction object from transaction data
|
|
accounts: List of account addresses in the transaction
|
|
instruction_data: Raw instruction data bytes
|
|
|
|
Returns:
|
|
Dict with parsed instruction information
|
|
"""
|
|
pass
|
|
|
|
def can_handle(self, program_id: str) -> bool:
|
|
"""
|
|
Check if this parser can handle instructions from a given program
|
|
|
|
Args:
|
|
program_id: Program ID to check
|
|
|
|
Returns:
|
|
True if this parser can handle the program, False otherwise
|
|
"""
|
|
return program_id == self.program_id
|
|
|
|
|
|
class TransferParser(InstructionParser):
|
|
"""
|
|
Base class for transfer instruction parsers
|
|
"""
|
|
|
|
@abstractmethod
|
|
def extract_transfer_info(self, parsed_instruction: Dict) -> Tuple[Optional[str], Optional[str], Optional[str], Optional[float]]:
|
|
"""
|
|
Extract transfer information from a parsed instruction
|
|
|
|
Args:
|
|
parsed_instruction: Instruction data parsed by parse_instruction
|
|
|
|
Returns:
|
|
Tuple of (token_address, from_address, to_address, amount)
|
|
Any of these values can be None if not applicable
|
|
"""
|
|
pass
|
|
|
|
|
|
class SwapParser(InstructionParser):
|
|
"""
|
|
Base class for swap instruction parsers
|
|
"""
|
|
|
|
@abstractmethod
|
|
def extract_swap_info(self, parsed_instruction: Dict) -> Tuple[Optional[str], Optional[str], Optional[float], Optional[float]]:
|
|
"""
|
|
Extract swap information from a parsed instruction
|
|
|
|
Args:
|
|
parsed_instruction: Instruction data parsed by parse_instruction
|
|
|
|
Returns:
|
|
Tuple of (token_in_address, token_out_address, amount_in, amount_out)
|
|
Any of these values can be None if not applicable
|
|
"""
|
|
pass
|
|
|
|
|
|
class LiquidityParser(InstructionParser):
|
|
"""
|
|
Base class for liquidity instruction parsers (add/remove)
|
|
"""
|
|
|
|
@abstractmethod
|
|
def extract_liquidity_info(self, parsed_instruction: Dict) -> Dict[str, Any]:
|
|
"""
|
|
Extract liquidity information from a parsed instruction
|
|
|
|
Args:
|
|
parsed_instruction: Instruction data parsed by parse_instruction
|
|
|
|
Returns:
|
|
Dict with keys:
|
|
- action: "add" or "remove"
|
|
- pool_address: Address of the liquidity pool
|
|
- tokens: List of token addresses involved
|
|
- amounts: List of token amounts
|
|
- user: User address
|
|
"""
|
|
pass |