Skip to content

Commit

Permalink
adjust position validation and burn test
Browse files Browse the repository at this point in the history
  • Loading branch information
none00y committed Aug 21, 2024
1 parent 62d3e73 commit 59269f5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
15 changes: 2 additions & 13 deletions protocol/programs/protocol/src/contexts/burn.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::cell::RefMut;

use crate::math::{
calculate_amount_delta, compute_lp_share_change, get_max_tick, get_min_tick,
calculate_amount_delta, compute_lp_share_change,
LiquidityChangeResult,
};
use crate::states::{DerivedAccountIdentifier, LpPool, State, LP_TOKEN_IDENT};
Expand Down Expand Up @@ -285,19 +285,8 @@ impl<'info> BurnLpTokenCtx<'info> {

pub fn validate_position(&self) -> Result<()> {
let lp_pool = &self.lp_pool.load()?;
if lp_pool.invariant_position != Pubkey::default() {
let upper_tick_index = get_max_tick(lp_pool.tick_spacing);
let lower_tick_index = get_min_tick(lp_pool.tick_spacing);
let position = try_from!(AccountLoader::<Position>, &self.position)?;
require_eq!(position.load()?.upper_tick_index, upper_tick_index);
require_eq!(position.load()?.lower_tick_index, lower_tick_index);
require_keys_eq!(lp_pool.invariant_position, self.position.key());

require_keys_eq!(self.position.key(), self.last_position.key());
let owner = self.program_authority.key();
let seeds = [b"positionv1", owner.as_ref(), &0i32.to_le_bytes()];
let (pubkey, _bump) = Pubkey::find_program_address(&seeds, &invariant::ID);
require_keys_eq!(self.last_position.key(), pubkey);
}
Ok(())
}

Expand Down
38 changes: 36 additions & 2 deletions protocol/tests/burn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ describe("burn lp token", () => {

const { positionAddress: lastPositionAddress2 } =
await market.getPositionAddress(protocol.programAuthority, 0);
const liquidityDelta2 = new BN(49000747);

accountLp = await getOrCreateAssociatedTokenAccount(
connection,
owner,
Expand All @@ -226,10 +226,44 @@ describe("burn lp token", () => {
ASSOCIATED_TOKEN_PROGRAM_ID
);
assert.equal(accountLp.amount, 23n);
await protocol.burnLpToken(
{
liquidityDelta: new BN(46000747),
pair,
index: positionId,
invProgram: INVARIANT_ADDRESS,
invState: stateAddress,
position: positionAddress,
lastPosition: lastPositionAddress2,
pool: poolAddress,
positionList: positionListAddress,
lowerTick: lowerTickAddress,
upperTick: upperTickAddress,
tickmap,
accountX: userTokenXAccount.address,
accountY: userTokenYAccount.address,
invReserveX: tokenXReserve,
invReserveY: tokenYReserve,
invProgramAuthority: market.programAuthority,
},
owner
);
accountLp = await getOrCreateAssociatedTokenAccount(
connection,
owner,
tokenLp,
owner.publicKey,
undefined,
undefined,
undefined,
TOKEN_2022_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID
);
assert.equal(accountLp.amount, 1n);

await protocol.burnLpToken(
{
liquidityDelta: liquidityDelta2,
liquidityDelta: new BN(2000030),
pair,
index: positionId,
invProgram: INVARIANT_ADDRESS,
Expand Down

0 comments on commit 59269f5

Please sign in to comment.