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

use borsh::BorshDeserialize;
use clap::ArgMatches;
use colored::Colorize;
use solana_clap_utils::keypair::DefaultSigner;
use solana_client::rpc_client::RpcClient;
use solana_remote_wallet::remote_wallet::RemoteWalletManager;
use solana_sdk::pubkey::Pubkey;
use tabled::{Alignment, Modify, Panel, Rotate, Style, Table};
use tabled::object::Segment;
use tabled::style::Color;

use crate::check_and_update_err;
use common::command::{CliCommand, CliCommandInfo, CliError, ProcessResult};
use common::contract::state::{
    clmmpools::Clmmpool,
    tick::Tick,
    tick_array::{TickArray, TickArrayV2},
};
use common::contract::state::tick_array::TickArrayToShow;

pub fn parse_tick_array_info<'a>(
    matches: &'a ArgMatches,
    default_signer: Box<DefaultSigner>,
    mut wallet_manager: Box<Option<Arc<RemoteWalletManager>>>,
) -> Result<CliCommandInfo, CliError> {
    let pool = matches.value_of("pool");
    let tick_index = matches.value_of("tick_index");
    let array_index = matches.value_of("array_index");
    let tick_index = match tick_index {
        Some(index) => Some(index.parse::<i32>().unwrap()),
        None => None,
    };

    let array_index = match array_index {
        Some(index) => Some(index.parse::<u16>().unwrap()),
        None => None,
    };

    Ok(CliCommandInfo {
        command: Box::new(CliCommand::TickArrayIndex {
            pool: pool.unwrap().parse::<Pubkey>().unwrap(),
            tick_index,
            array_index,
        }),
        signers: vec![check_and_update_err!(
            default_signer.signer_from_path(matches, wallet_manager.as_mut()),
            CliError::RpcRequestError("owner key is invalid".to_string())
        )?],
    })
}

pub fn process_tick_array_info(
    rpc_client: &RpcClient,
    tick_index: Option<i32>,
    array_index: Option<u16>,
    pool: &Pubkey,
) -> ProcessResult {
    let data = rpc_client.get_account_data(pool).unwrap();
    let clmmpool_info: Clmmpool = Clmmpool::try_from_slice(&data[8..]).unwrap();
    // if tick_index.is_some() && array_index.is_some()
    //     || tick_index.is_none() && array_index.is_none()
    // {
    //     return std::error::Error("");
    // }
    let array_index = if array_index.is_some() {
        array_index.unwrap()
    } else {
        TickArray::array_index(tick_index.unwrap(), clmmpool_info.tick_spacing).unwrap()
    };

    let tick_array_address = TickArray::get_tick_array_address(pool, array_index);

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

    let tick_array: TickArrayV2 = TickArrayV2::try_from_slice(&data[8..44]).unwrap();
    let array_space = tick_array.tick_spacing as usize * TickArray::CAP;
    let start_tick_index =
        Tick::min(tick_array.tick_spacing) + (tick_array.array_index as usize * array_space) as i32;
    let tick_array_show = TickArrayToShow {
        array_index: tick_array.array_index,
        tick_spacing: tick_array.tick_spacing,
        clmmpool: tick_array.clmmpool,
        array_space: array_space as u32,
        start_tick_index,
        end_tick_index: start_tick_index + array_space as i32 - tick_array.tick_spacing as i32,
    };
    let color = Color::try_from(" ".cyan().to_string()).unwrap();
    Ok(Table::new(vec![tick_array_show])
        .with(Rotate::Bottom)
        .with(Rotate::Right)
        .with(Style::modern())
        .with(color)
        .with(Panel("Tick Array Info", 0))
        .with(Modify::new(Segment::all()).with(Alignment::center()))
        .to_string())
}