1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::instruction::{AccountMeta, Instruction};
use solana_program::pubkey::Pubkey;

use crate::program::{POSITION_NFT_UPDATE_AUTHORITY, PROGRAM_ID};
use crate::utils::sighash;

#[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug, Clone)]
pub struct OpenPositionArgs {
    pub tick_lower_index: i32,
    pub tick_upper_index: i32,
}

pub fn new_open_position(
    clmmpool: &Pubkey,
    position: &Pubkey,
    position_nft_mint: &Pubkey,
    position_metadata_account: &Pubkey,
    position_ata: &Pubkey,
    tick_lower_index: &i32,
    tick_upper_index: &i32,
    payer: Pubkey,
) -> Instruction {
    let data = &OpenPositionArgs {
        tick_lower_index: *tick_lower_index,
        tick_upper_index: *tick_upper_index,
    };

    let mut dsa = data.try_to_vec().unwrap();
    let mut distor = sighash::sighash("global", "open_position").to_vec();
    distor.append(&mut dsa);

    Instruction {
        program_id: PROGRAM_ID,
        accounts: vec![
            AccountMeta::new(payer, true),
            AccountMeta::new(*clmmpool, false),
            AccountMeta::new(*position, false),
            AccountMeta::new(*position_nft_mint, true),
            AccountMeta::new(*position_metadata_account, false),
            AccountMeta::new_readonly(*POSITION_NFT_UPDATE_AUTHORITY, false),
            AccountMeta::new(*position_ata, false),
            AccountMeta::new_readonly(spl_token::id(), false),
            AccountMeta::new_readonly(spl_associated_token_account::id(), false),
            AccountMeta::new_readonly(mpl_token_metadata::id(), false),
            AccountMeta::new_readonly(solana_program::system_program::id(), false),
            AccountMeta::new_readonly(solana_program::sysvar::rent::id(), false),
        ],
        data: distor,
    }
}