Skip to content

Commit

Permalink
Update llib.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
0xpapercut committed Jul 10, 2024
1 parent 7c7a64e commit 40eb37e
Showing 1 changed file with 78 additions and 58 deletions.
136 changes: 78 additions & 58 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ use pb::raydium::{
RaydiumBlockEvents,
RaydiumTransactionEvents,
RaydiumEvent,
SwapData,
InitializeData,
DepositData,
WithdrawData,
InitializeEvent,
SwapEvent,
WithdrawEvent,
DepositEvent,
};
use pb::raydium::raydium_event::Data;
use pb::raydium::raydium_event::Event;

#[substreams::handlers::map]
fn raydium_block_events(block: Block) -> Result<RaydiumBlockEvents, Error> {
let transactions = parse_block(&block);
Ok(RaydiumBlockEvents { transactions })
}

fn parse_block(block: &Block) -> Vec<RaydiumTransactionEvents> {
pub fn parse_block(block: &Block) -> Vec<RaydiumTransactionEvents> {
let mut block_events: Vec<RaydiumTransactionEvents> = Vec::new();
for transaction in &block.transactions {
let events = parse_transaction(transaction);
Expand All @@ -52,7 +52,7 @@ fn parse_block(block: &Block) -> Vec<RaydiumTransactionEvents> {
block_events
}

fn parse_transaction(transaction: &ConfirmedTransaction) -> Vec<RaydiumEvent> {
pub fn parse_transaction(transaction: &ConfirmedTransaction) -> Vec<RaydiumEvent> {
let context = TransactionContext::construct(transaction);
let mut events: Vec<RaydiumEvent> = Vec::new();
let instructions = get_structured_instructions(transaction);
Expand All @@ -66,7 +66,9 @@ fn parse_transaction(transaction: &ConfirmedTransaction) -> Vec<RaydiumEvent> {
continue;
}
match parse_instruction(&instruction, &context) {
Ok(Some(event)) => events.push(event),
Ok(Some(event)) => {
events.push(RaydiumEvent { event: Some(event) })
}
Ok(None) => (),
Err(error) => substreams::log::println(format!("Failed to process instruction of transaction {}: {}", &context.signature, error))
}
Expand All @@ -77,24 +79,28 @@ fn parse_transaction(transaction: &ConfirmedTransaction) -> Vec<RaydiumEvent> {
pub fn parse_instruction(
instruction: &StructuredInstruction,
context: &TransactionContext
) -> Result<Option<RaydiumEvent>, String> {
) -> Result<Option<Event>, String> {
if bs58::encode(context.get_account_from_index(instruction.program_id_index as usize)).into_string() != RAYDIUM_LIQUIDITY_POOL {
return Err("Not a Raydium instruction.".to_string());
}
let unpacked = AmmInstruction::unpack(&instruction.data)?;
match unpacked {
AmmInstruction::SwapBaseIn(_) |
AmmInstruction::SwapBaseOut(_) => {
_parse_swap_instruction(instruction, context).map(Some)
let event = _parse_swap_instruction(instruction, context)?;
Ok(Some(Event::Swap(event)))
},
AmmInstruction::Initialize2(initialize) => {
_parse_initialize_instruction(instruction, context, initialize.nonce).map(Some)
let event = _parse_initialize_instruction(instruction, context, initialize.nonce)?;
Ok(Some(Event::Initialize(event)))
},
AmmInstruction::Deposit(_deposit) => {
_parse_deposit_instruction(instruction, context).map(Some)
let event = _parse_deposit_instruction(instruction, context)?;
Ok(Some(Event::Deposit(event)))
},
AmmInstruction::Withdraw(_withdraw) => {
_parse_withdraw_instruction(instruction, context).map(Some)
let event = _parse_withdraw_instruction(instruction, context)?;
Ok(Some(Event::Withdraw(event)))
}
_ => Ok(None),
}
Expand All @@ -103,32 +109,34 @@ pub fn parse_instruction(
fn _parse_swap_instruction(
instruction: &StructuredInstruction,
context: &TransactionContext,
) -> Result<RaydiumEvent, String> {
) -> Result<SwapEvent, String> {
let amm = bs58::encode(context.get_account_from_index(instruction.accounts[1] as usize)).into_string();
let user = bs58::encode(context.get_account_from_index(*instruction.accounts.last().unwrap() as usize)).into_string();

let instructions_len = instruction.inner_instructions.len();
let transfer_in = spl_token_substream::parse_transfer_instruction(&instruction.inner_instructions[instructions_len - 2], context)?;
let transfer_out = spl_token_substream::parse_transfer_instruction(&instruction.inner_instructions[instructions_len - 1], context)?;

let data = SwapData {
amount_in: transfer_in.amount,
mint_in: transfer_in.source.unwrap().mint,
amount_out: transfer_out.amount,
mint_out: transfer_out.source.unwrap().mint,
};
Ok(RaydiumEvent {
let amount_in = transfer_in.amount;
let amount_out = transfer_out.amount;
let mint_in = transfer_in.source.unwrap().mint;
let mint_out = transfer_out.source.unwrap().mint;

Ok(SwapEvent {
amm,
user,
data: Some(Data::Swap(data)),
mint_in,
mint_out,
amount_in,
amount_out,
})
}

fn _parse_initialize_instruction(
instruction: &StructuredInstruction,
context: &TransactionContext,
nonce: u8,
) -> Result<RaydiumEvent, String> {
) -> Result<InitializeEvent, String> {
let amm = bs58::encode(context.get_account_from_index(instruction.accounts[4] as usize)).into_string();
let user = bs58::encode(context.get_account_from_index(instruction.accounts[17] as usize)).into_string();

Expand All @@ -137,72 +145,84 @@ fn _parse_initialize_instruction(
let pc_transfer = spl_token_substream::parse_transfer_instruction(&instruction.inner_instructions[instructions_len - 2], context)?;
let lp_mint_to = spl_token_substream::parse_mint_to_instruction(&instruction.inner_instructions[instructions_len - 1], context)?;

let data = InitializeData {
pc_init_amount: pc_transfer.amount,
coin_init_amount: coin_transfer.amount,
lp_init_amount: lp_mint_to.amount,
pc_mint: pc_transfer.source.unwrap().mint,
coin_mint: coin_transfer.source.unwrap().mint,
lp_mint: lp_mint_to.mint,
nonce: nonce as u32,
};
Ok(RaydiumEvent {
let pc_init_amount = pc_transfer.amount;
let coin_init_amount = coin_transfer.amount;
let lp_init_amount = lp_mint_to.amount;
let pc_mint = pc_transfer.source.unwrap().mint;
let coin_mint = coin_transfer.source.unwrap().mint;
let lp_mint = lp_mint_to.mint;

Ok(InitializeEvent {
amm,
user,
data: Some(Data::Initialize(data)),
pc_init_amount,
coin_init_amount,
lp_init_amount,
pc_mint,
coin_mint,
lp_mint,
nonce: nonce as u32,
})
}

fn _parse_deposit_instruction(
instruction: &StructuredInstruction,
context: &TransactionContext
) -> Result<RaydiumEvent, String> {
) -> Result<DepositEvent, String> {
let amm = bs58::encode(context.get_account_from_index(instruction.accounts[1] as usize)).into_string();
let user = bs58::encode(context.get_account_from_index(instruction.accounts[12] as usize)).into_string();

let instructions_len = instruction.inner_instructions.len();
let coin_transfer = spl_token_substream::parse_transfer_instruction(&instruction.inner_instructions[instructions_len - 3], context)?;
let pc_transfer = spl_token_substream::parse_transfer_instruction(&instruction.inner_instructions[instructions_len - 2], context)?;
let coin_transfer = spl_token_substream::parse_transfer_instruction(&instruction.inner_instructions[instructions_len - 3], context)?;
let lp_mint_to = spl_token_substream::parse_mint_to_instruction(&instruction.inner_instructions[instructions_len - 1], context)?;

let data = DepositData {
pc_amount: pc_transfer.amount,
coin_amount: coin_transfer.amount,
lp_amount: lp_mint_to.amount,
pc_mint: pc_transfer.source.unwrap().mint,
coin_mint: coin_transfer.source.unwrap().mint,
lp_mint: lp_mint_to.mint,
};
Ok(RaydiumEvent {
let pc_amount = pc_transfer.amount;
let coin_amount = coin_transfer.amount;
let lp_amount = lp_mint_to.amount;
let pc_mint = pc_transfer.source.unwrap().mint;
let coin_mint = coin_transfer.source.unwrap().mint;
let lp_mint = lp_mint_to.mint;

Ok(DepositEvent {
amm,
user,
data: Some(Data::Deposit(data)),
pc_amount,
coin_amount,
lp_amount,
pc_mint,
coin_mint,
lp_mint,
})
}

fn _parse_withdraw_instruction(
instruction: &StructuredInstruction,
context: &TransactionContext,
) -> Result<RaydiumEvent, String> {
) -> Result<WithdrawEvent, String> {
let amm = bs58::encode(context.get_account_from_index(instruction.accounts[1] as usize)).into_string();
let user = bs58::encode(context.get_account_from_index(instruction.accounts[16] as usize)).into_string();

let instructions_len = instruction.inner_instructions.len();
let coin_transfer = spl_token_substream::parse_transfer_instruction(&instruction.inner_instructions[instructions_len - 3], context)?;
let pc_transfer = spl_token_substream::parse_transfer_instruction(&instruction.inner_instructions[instructions_len - 2], context)?;
let coin_transfer = spl_token_substream::parse_transfer_instruction(&instruction.inner_instructions[instructions_len - 3], context)?;
let lp_burn = spl_token_substream::parse_burn_instruction(&instruction.inner_instructions[instructions_len - 1], context)?;

let data = WithdrawData {
pc_amount: pc_transfer.amount,
coin_amount: coin_transfer.amount,
lp_amount: lp_burn.amount,
pc_mint: pc_transfer.source.unwrap().mint,
coin_mint: coin_transfer.source.unwrap().mint,
lp_mint: lp_burn.source.unwrap().mint,
};
Ok(RaydiumEvent {
let pc_amount = pc_transfer.amount;
let coin_amount = coin_transfer.amount;
let lp_amount = lp_burn.amount;
let pc_mint = pc_transfer.source.unwrap().mint;
let coin_mint = coin_transfer.source.unwrap().mint;
let lp_mint = lp_burn.source.unwrap().mint;

Ok(WithdrawEvent {
amm,
user,
data: Some(Data::Withdraw(data)),
pc_amount,
coin_amount,
lp_amount,
pc_mint,
coin_mint,
lp_mint,
})
}

0 comments on commit 40eb37e

Please sign in to comment.