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

use serde::{Deserialize, Serialize};

use solana_client::rpc_client::RpcClient;
use solana_program::pubkey::Pubkey;
use tabled::Tabled;

use common::command::{CliConfig, ProcessResult};
use common::contract::instructions::create_tick_array::new_create_tick_array;
use common::contract::state::TickArray;
use common::utils::file::{read_for_str};
use common::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 process_create_tick_array(rpc_client: &RpcClient, config: &CliConfig, output: &str) -> ProcessResult {
    let tick_array: TickArrayTemplate = read_for_str(output);

    let (tick_array_pubkey, _) = TickArray::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(
        Pubkey::from_str(tick_array.clmmpool.as_str()).unwrap(),
        tick_array.array_index,
        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 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())
}