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
use std::fs;
use std::io::Write;
use std::str::FromStr;
use std::sync::Arc;

use clap::ArgMatches;
use serde::{Deserialize, Serialize};
use solana_clap_utils::keypair::DefaultSigner;
use solana_client::rpc_client::RpcClient;
use solana_program::pubkey::Pubkey;
use solana_remote_wallet::remote_wallet::RemoteWalletManager;
use tabled::Tabled;

use crate::check_and_update_err;
use crate::command::{CliCommand, CliCommandInfo, CliConfig, CliError, ProcessResult};
use crate::contract::instructions::create_tick_array::new_create_tick_array;
use crate::program::get_pubkey_for_program_with_seeds;
use crate::utils::file::{get_template_dir, read_for_str};
use crate::utils::send::send_tx;

pub const TICK_ARRAY_TEMPLATE_DIR: &str = "./tick-array-template.yaml";

#[derive(Debug, Serialize, Deserialize, Tabled)]
pub struct TickArrayTemplate {
    pub clmmpool: String,
    pub array_index: u16,
}


pub fn parse_create_tick_array<'a>(matches: &'a ArgMatches, default_signer: &DefaultSigner, wallet_manager: &mut Option<Arc<RemoteWalletManager>>) -> Result<CliCommandInfo<'a>, CliError> {
    let entry_file = get_template_dir(matches, "entry_file", TICK_ARRAY_TEMPLATE_DIR);
    Ok(CliCommandInfo {
        command: CliCommand::PairTickArrayCreate {
            entry_file: entry_file.unwrap(),
        },
        signers: vec![check_and_update_err!(default_signer.signer_from_path(matches, wallet_manager), CliError::RpcRequestError("owner key is invalid".to_string()))?],
    })
}


pub fn process_create_tick_array(rpc_client: &RpcClient, config: &CliConfig, output: &str) -> ProcessResult {
    let tick_array: TickArrayTemplate = read_for_str(output);

    let (tick_array_pubkey, _) = calculate_tick_array_key(
        Pubkey::from_str(tick_array.clmmpool.as_str()).unwrap().as_ref(),
        tick_array.array_index.to_le_bytes().as_ref(),
    );

    let ixs = [new_create_tick_array(&tick_array, tick_array_pubkey, config.pubkey().unwrap())];

    let res = send_tx(rpc_client, config, &ixs)?;

    println!("tick array key: {}", tick_array_pubkey.to_string());

    Ok("signers : ".to_owned() + res.to_string().as_str())
}

pub fn parse_create_tick_array_template<'a>(matches: &'a ArgMatches) -> Result<CliCommandInfo<'a>, CliError> {
    let output_file = get_template_dir(matches, "output-file", TICK_ARRAY_TEMPLATE_DIR);
    Ok(CliCommandInfo {
        command: CliCommand::PairTickArrayCreateTemplate {
            output_file: output_file.unwrap(),
        },
        signers: vec![],
    })
}

pub fn process_create_tick_array_template(output: &str) -> ProcessResult {
    let tick_array = TickArrayTemplate {
        clmmpool: Pubkey::from_str("BPFLoaderUpgradeab1e11111111111111111111111").unwrap().to_string(),
        array_index: 100,
    };

    let mut file = fs::File::create(output).expect("create failed");
    file.write_all(serde_yaml::to_string(&tick_array).unwrap().as_bytes()).expect("write failed");

    Ok(output.to_string() + " file create success".to_string().as_str())
}

pub fn calculate_tick_array_key(tick_array: &[u8], array_index: &[u8]) -> (Pubkey, u8) {
    get_pubkey_for_program_with_seeds(
        &[
            b"tick_array",
            tick_array,
            array_index,
        ]
    )
}