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
52
53
54
55
56
57
58
59
use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::instruction::{AccountMeta, Instruction};
use solana_program::pubkey::Pubkey;

use crate::program::PROGRAM_ID;
use crate::utils::sighash;

#[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug, Clone)]
pub struct DecreaseLiquidityArgs {
    pub delta_liquidity: u128,
    pub token_a_min: u64,
    pub token_b_min: u64,
}

pub fn new_decrease_liquidity(
    clmmpool: &Pubkey,
    position: &Pubkey,
    position_ata: &Pubkey,
    token_a_ata: &Pubkey,
    token_b_ata: &Pubkey,
    token_a_value: &Pubkey,
    token_b_value: &Pubkey,
    tick_array_lower: &Pubkey,
    tick_array_upper: &Pubkey,
    tick_array_map: &Pubkey,
    delta_liquidity: &u128,
    token_a_min: &u64,
    token_b_min: &u64,
    payer: Pubkey,
) -> Instruction {
    let data = &DecreaseLiquidityArgs {
        delta_liquidity: *delta_liquidity,
        token_a_min: *token_a_min,
        token_b_min: *token_b_min,
    };

    let mut dsa = data.try_to_vec().unwrap();
    let mut distor = sighash::sighash("global", "decrease_liquidity").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_readonly(*position_ata, false),
            AccountMeta::new(*token_a_ata, false),
            AccountMeta::new(*token_b_ata, false),
            AccountMeta::new(*token_a_value, false),
            AccountMeta::new(*token_b_value, false),
            AccountMeta::new(*tick_array_lower, false),
            AccountMeta::new(*tick_array_upper, false),
            AccountMeta::new(*tick_array_map, false),
            AccountMeta::new_readonly(spl_token::id(), false),
        ],
        data: distor,
    }
}