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
use borsh::BorshDeserialize;

use colored::Colorize;

use solana_client::rpc_client::RpcClient;

use solana_sdk::pubkey::Pubkey;
use spl_token::amount_to_ui_amount;
use tabled::object::Segment;
use tabled::style::Color;
use tabled::{Alignment, Modify, Panel, Rotate, Style, Table};


use common::client::get_decimals;
use common::command::{ProcessResult};
use common::contract::state::position::Position;
use common::contract::state::Clmmpool;
use common::program::SWAP_PROGRAM_ID;


pub fn process_position_info(rpc_client: &RpcClient, mint: &Pubkey) -> ProcessResult {
    let (position, _) = Pubkey::find_program_address(
        &[b"position", mint.as_ref()],
        &SWAP_PROGRAM_ID,
    );

    let data = rpc_client.get_account_data(&position).unwrap();

    let position_info: Position = Position::try_from_slice(&data[8..]).unwrap();

    let pool_info = Clmmpool::get_info(rpc_client, &position_info.clmmpool);

    let token_a_decimal = get_decimals(rpc_client, &pool_info.token_a);
    let token_b_decimal = get_decimals(rpc_client, &pool_info.token_b);

    let amount =
        position_info.get_amount(pool_info.current_tick_index, pool_info.current_sqrt_price)?;

    let amount_a_ui = amount_to_ui_amount(amount.0, token_a_decimal);
    let amount_b_ui = amount_to_ui_amount(amount.1, token_b_decimal);

    // let tick_lower = get_tick_info(rpc_client, position_info.tick_lower_index, &position_info.clmmpool);
    // let tick_upper = get_tick_info(rpc_client, position_info.tick_upper_index, &position_info.clmmpool);

    // let (fee_growth_inside_a, fee_growth_inside_b) = Tick::get_fee_in_tick_range(&pool_info, Some(&tick_lower), Some(&tick_upper), position_info.tick_lower_index, position_info.tick_upper_index);
    // position_info.update_fee(fee_growth_inside_a, fee_growth_inside_b);

    // let rewards = Tick::get_reward_in_tick_range(&mut pool_info, Some(&tick_lower), Some(&tick_upper), position_info.tick_lower_index, position_info.tick_upper_index);
    // position_info.update_rewarder(&rewards);

    Ok(Table::builder(vec![position_info])
        .build()
        .with(Rotate::Bottom)
        .with(Rotate::Right)
        .with(Style::modern())
        .with(Color::try_from(" ".cyan().to_string()).unwrap())
        .with(Panel("Position Info", 0))
        .with(Modify::new(Segment::all()).with(Alignment::center()))
        .to_string()
        + "\n"
        + &Table::builder(&[
        &["position", position.to_string().as_str()],
        &["amount_a", amount_a_ui.to_string().as_str()],
        &["amount_b", amount_b_ui.to_string().as_str()],
    ])
        .build()
        .with(Style::modern())
        .with(Color::try_from(" ".cyan().to_string()).unwrap())
        .with(Modify::new(Segment::all()).with(Alignment::center()))
        .with(Panel("Position calculate", 0))
        .to_string())
}