From 40eb37e0e3ff5bb44014a40281217f06a124572d Mon Sep 17 00:00:00 2001 From: 0xpapercut <0xpapercut@gmail.com> Date: Wed, 10 Jul 2024 13:17:25 +0200 Subject: [PATCH] Update llib.rs --- src/lib.rs | 136 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 78 insertions(+), 58 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0d093eb..e980fce 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,12 +25,12 @@ 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 { @@ -38,7 +38,7 @@ fn raydium_block_events(block: Block) -> Result { Ok(RaydiumBlockEvents { transactions }) } -fn parse_block(block: &Block) -> Vec { +pub fn parse_block(block: &Block) -> Vec { let mut block_events: Vec = Vec::new(); for transaction in &block.transactions { let events = parse_transaction(transaction); @@ -52,7 +52,7 @@ fn parse_block(block: &Block) -> Vec { block_events } -fn parse_transaction(transaction: &ConfirmedTransaction) -> Vec { +pub fn parse_transaction(transaction: &ConfirmedTransaction) -> Vec { let context = TransactionContext::construct(transaction); let mut events: Vec = Vec::new(); let instructions = get_structured_instructions(transaction); @@ -66,7 +66,9 @@ fn parse_transaction(transaction: &ConfirmedTransaction) -> Vec { 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)) } @@ -77,7 +79,7 @@ fn parse_transaction(transaction: &ConfirmedTransaction) -> Vec { pub fn parse_instruction( instruction: &StructuredInstruction, context: &TransactionContext -) -> Result, String> { +) -> Result, 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()); } @@ -85,16 +87,20 @@ pub fn parse_instruction( 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), } @@ -103,7 +109,7 @@ pub fn parse_instruction( fn _parse_swap_instruction( instruction: &StructuredInstruction, context: &TransactionContext, -) -> Result { +) -> Result { 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(); @@ -111,16 +117,18 @@ fn _parse_swap_instruction( 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, }) } @@ -128,7 +136,7 @@ fn _parse_initialize_instruction( instruction: &StructuredInstruction, context: &TransactionContext, nonce: u8, -) -> Result { +) -> Result { 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(); @@ -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 { +) -> Result { 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 { +) -> Result { 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, }) }