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
use std::vec;

use crate::contract::state::Clmmpool;
use crate::error::ErrorCode;
use crate::math::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 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,
}