Skip to content

Commit 1a1e397

Browse files
committed
try get_instruction_relative function
1 parent 2eb9a16 commit 1a1e397

1 file changed

Lines changed: 26 additions & 22 deletions

File tree

programs/cp-amm/src/instructions/swap/ix_p_swap.rs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ use crate::{
1010
ProcessSwapParams, ProcessSwapResult, SwapCtx,
1111
};
1212
use anchor_lang::prelude::*;
13-
use anchor_lang::solana_program::instruction::{get_stack_height, Instruction};
13+
use anchor_lang::solana_program::instruction::get_stack_height;
1414

1515
use pinocchio::account_info::AccountInfo;
1616
use pinocchio::sysvars::instructions::{Instructions, IntrospectedInstruction, INSTRUCTIONS_ID};
17-
use solana_program::instruction::get_processed_sibling_instruction;
1817

19-
use crate::safe_math::SafeMath;
2018
use crate::{
2119
activation_handler::ActivationHandler,
2220
get_pool_access_validator,
@@ -297,16 +295,32 @@ pub fn validate_single_swap_instruction<'c, 'info>(
297295
if get_stack_height() > 2 {
298296
return Err(PoolError::FailToValidateSingleSwapInstruction.into());
299297
}
300-
// check for any sibling instruction
301-
let mut sibling_index = 0;
302-
while let Some(sibling_instruction) = get_processed_sibling_instruction(sibling_index) {
303-
if sibling_instruction.program_id == crate::ID {
304-
require!(
305-
!is_instruction_include_pool_swap(&sibling_instruction, pool),
306-
PoolError::FailToValidateSingleSwapInstruction
307-
);
298+
299+
// Iterate backwards through previous instructions.
300+
// Start from the immediately preceding instruction.
301+
let mut relative_index: i64 = -1;
302+
loop {
303+
match instruction_sysvar_instructions.get_instruction_relative(relative_index) {
304+
Ok(prev_instruction) => {
305+
if prev_instruction.get_program_id() == crate::ID.as_array() {
306+
require!(
307+
!is_p_instruction_include_pool_swap(&prev_instruction, pool)?,
308+
PoolError::FailToValidateSingleSwapInstruction
309+
);
310+
}
311+
relative_index -= 1;
312+
}
313+
// TODO: check handle error case.
314+
Err(err) => {
315+
if err == pinocchio::program_error::ProgramError::InvalidInstructionData {
316+
// Reached the beginning of the transaction.
317+
// No more instructions to check.
318+
break;
319+
} else {
320+
return Err(PoolError::UndeterminedError.into());
321+
}
322+
}
308323
}
309-
sibling_index = sibling_index.safe_add(1)?;
310324
}
311325
}
312326

@@ -343,16 +357,6 @@ pub fn validate_single_swap_instruction<'c, 'info>(
343357
Ok(())
344358
}
345359

346-
fn is_instruction_include_pool_swap(instruction: &Instruction, pool: &Pubkey) -> bool {
347-
let instruction_discriminator = &instruction.data[..8];
348-
if instruction_discriminator.eq(SwapInstruction::DISCRIMINATOR)
349-
|| instruction_discriminator.eq(Swap2Instruction::DISCRIMINATOR)
350-
{
351-
return instruction.accounts[1].pubkey.eq(pool);
352-
}
353-
false
354-
}
355-
356360
fn is_p_instruction_include_pool_swap(
357361
instruction: &IntrospectedInstruction,
358362
pool: &Pubkey,

0 commit comments

Comments
 (0)