Skip to content

Commit

Permalink
remove key checking to reduce computation
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhengYuTay authored and Arrowana committed May 26, 2023
1 parent 9f2316c commit a6efe3e
Showing 1 changed file with 29 additions and 42 deletions.
71 changes: 29 additions & 42 deletions programs/amm/src/instructions/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ pub fn swap_internal<'b, 'info>(
};

// check observation account is owned by the pool
require_keys_eq!(observation_state.pool_id, pool_state.key());

let (state, amount_0, amount_1) = swap_on_swap_state(
&amm_config,
Expand Down Expand Up @@ -286,39 +285,40 @@ pub fn swap_on_swap_state(
.pop_front()
.ok_or(ErrorCode::InsufficientTickArrayStates)?;
// check tick_array account is owned by the pool
require_keys_eq!(tick_array_current.pool_id, pool_state.key());
// require_keys_eq!(tick_array_current.pool_id, pool_state.key());

let (mut is_match_pool_current_tick_array, first_vaild_tick_array_start_index) =
pool_state.get_first_initialized_tick_array(zero_for_one)?;
let mut current_vaild_tick_array_start_index = first_vaild_tick_array_start_index;

let expected_first_tick_array_address = Pubkey::find_program_address(
&[
TICK_ARRAY_SEED.as_bytes(),
pool_state.key().as_ref(),
&current_vaild_tick_array_start_index.to_be_bytes(),
],
&crate::id(),
)
.0;

let mut tick_array_current = tick_array_states.pop_front().unwrap();
for _ in 0..tick_array_states.len() {
// check tick_array account is owned by the pool
require_keys_eq!(tick_array_current.pool_id, pool_state.key());
if tick_array_current.key() == expected_first_tick_array_address {
break;
}
tick_array_current = tick_array_states
.pop_front()
.ok_or(ErrorCode::NotEnoughTickArrayAccount)?;
}
// check first tick array account is correct
require_keys_eq!(
tick_array_current.key(),
expected_first_tick_array_address,
ErrorCode::InvalidFirstTickArrayAccount
);
// Unecessary validation for quote estimation
// let expected_first_tick_array_address = Pubkey::find_program_address(
// &[
// TICK_ARRAY_SEED.as_bytes(),
// pool_state.key().as_ref(),
// &current_vaild_tick_array_start_index.to_be_bytes(),
// ],
// &crate::id(),
// )
// .0;

// let mut tick_array_current = tick_array_states.pop_front().unwrap();
// for _ in 0..tick_array_states.len() {
// // check tick_array account is owned by the pool
// require_keys_eq!(tick_array_current.pool_id, pool_state.key());
// if tick_array_current.key() == expected_first_tick_array_address {
// break;
// }
// tick_array_current = tick_array_states
// .pop_front()
// .ok_or(ErrorCode::NotEnoughTickArrayAccount)?;
// }
// // check first tick array account is correct
// require_keys_eq!(
// tick_array_current.key(),
// expected_first_tick_array_address,
// ErrorCode::InvalidFirstTickArrayAccount
// );

// continue swapping as long as we haven't used the entire input/output and haven't
// reached the price limit
Expand Down Expand Up @@ -375,19 +375,6 @@ pub fn swap_on_swap_state(
.pop_front()
.ok_or(ErrorCode::InsufficientTickArrayStates)?;

require_keys_eq!(tick_array_current.pool_id, pool_state.key());
require_keys_eq!(
tick_array_current.key(),
Pubkey::find_program_address(
&[
TICK_ARRAY_SEED.as_bytes(),
pool_state.key().as_ref(),
&current_vaild_tick_array_start_index.to_be_bytes(),
],
&crate::id()
)
.0
);
let first_initialized_tick = tick_array_current.first_initialized_tick(zero_for_one)?;
next_initialized_tick = Box::new(*first_initialized_tick);
}
Expand Down

0 comments on commit a6efe3e

Please sign in to comment.