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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use std::vec;

use crate::contract::state::Clmmpool;
use crate::error::ErrorCode;
use crate::math::{FullMath, get_amount_from_liquidity};
use borsh::BorshDeserialize;
use solana_client::rpc_client::RpcClient;
use solana_program::pubkey::Pubkey;
use tabled::Tabled;

#[derive(Default, Debug, BorshDeserialize, Tabled, Copy, Clone)]
pub struct Position {
    pub clmmpool: Pubkey,
    pub position_nft_mint: Pubkey,
    pub liquidity: u128,
    pub tick_lower_index: i32,
    pub tick_upper_index: i32,

    // Q64.64
    pub fee_growth_inside_a: u128,
    pub fee_owed_a: u64,

    // Q64.64
    pub fee_growth_inside_b: u128,
    pub fee_owed_b: u64,

    pub reward_infos: PositionRewarders,
}

#[derive(Copy, Clone, BorshDeserialize, Default, Debug, PartialEq)]
pub struct PositionReward {
    // Q64.64
    pub growth_inside: u128,
    pub amount_owed: u64,
}

impl PositionReward {
    pub const LEN: usize = 8 + 16;
}

#[derive(Copy, Clone, BorshDeserialize, Default, Debug, PartialEq)]
pub struct PositionRewarders(pub [PositionReward; 3]);

impl std::fmt::Display for PositionRewarders {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut res: Vec<String> = vec![];
        for (i, e) in self.0.iter().enumerate() {
            res.push(format!(
                "{}. growth_index: {}, amount_owned:{}",
                i, e.growth_inside, e.amount_owed
            ));
        }
        let res_str = res.join("\n");
        write!(f, "{}", res_str)
    }
}

impl Position {
    pub const LEN: usize =
        32 + 32 + 16 + 4 + 4 + 16 + 8 + 16 + 8 + Clmmpool::REWARD_NUM * PositionReward::LEN;

    pub fn get_info(rpc_client: &RpcClient, pubkey: &Pubkey) -> Self {
        let data_slice = &rpc_client.get_account_data(pubkey).unwrap()[8..];
        Position::try_from_slice(data_slice).unwrap()
    }

    pub fn update_fee(
        &mut self,
        fee_growth_inside_a: u128,
        fee_growth_inside_b: u128,
    ) -> () {
        let growth_delta_a = fee_growth_inside_a.wrapping_sub(self.fee_growth_inside_a);
        let fee_delta_a = self.liquidity.mul_shift_right(growth_delta_a, 64);
        let growth_delta_b = fee_growth_inside_b.wrapping_sub(self.fee_growth_inside_b);
        let fee_delta_b = self.liquidity.mul_shift_right(growth_delta_b, 64);

        self.fee_owed_a = self.fee_owed_a.checked_add(fee_delta_a as u64).unwrap();
        self.fee_owed_b = self.fee_owed_b.checked_add(fee_delta_b as u64).unwrap();

        self.fee_growth_inside_a = fee_growth_inside_a;
        self.fee_growth_inside_b = fee_growth_inside_b;
    }

    pub fn update_rewarder(
        &mut self,
        rewards_growths_inside: &[u128; Clmmpool::REWARD_NUM],
    ) -> () {
        for i in 0..Clmmpool::REWARD_NUM {
            let growth_delta =
                rewards_growths_inside[i].wrapping_sub(self.reward_infos.0.as_slice()[i].growth_inside);
            let amount_owed_delta = self.liquidity.mul_shift_right(growth_delta, 64);

            self.reward_infos.0.as_mut_slice()[i].growth_inside = rewards_growths_inside[i];
            self.reward_infos.0.as_mut_slice()[i].amount_owed = self.reward_infos.0.as_mut_slice()[i]
                .amount_owed
                .checked_add(amount_owed_delta as u64)
                .unwrap();
        }
    }

    pub fn get_ui_info(self, position: Pubkey) -> PositionUiList {
        PositionUiList {
            clmmpool: self.clmmpool,
            position,
            position_nft_mint: self.position_nft_mint,
            liquidity: self.liquidity,
            tick_lower_index: self.tick_lower_index,
            tick_upper_index: self.tick_upper_index,
        }
    }

    pub fn get_amount(
        self,
        current_tick_index: i32,
        current_sqrt_price: u128,
    ) -> Result<(u64, u64), ErrorCode> {
        get_amount_from_liquidity(
            self.tick_lower_index,
            self.tick_upper_index,
            self.liquidity,
            current_tick_index,
            current_sqrt_price,
        )
    }
}

#[derive(Tabled)]
pub struct PositionUiList {
    /// clmmpool
    pub clmmpool: Pubkey,
    /// position
    pub position: Pubkey,
    pub position_nft_mint: Pubkey,
    pub liquidity: u128,
    pub tick_lower_index: i32,
    pub tick_upper_index: i32,
}