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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use colored::Colorize;
use common::client::get_decimals;
use common::contract::state::config::ClmmConfig;
use common::contract::state::Clmmpool;
use common::token_list::pool_name;
use common::utils::sqrt_price::SqrtPrice;
use inquire::{required, Text};
use rust_decimal::Decimal;
use solana_client::rpc_client::RpcClient;
use solana_program::instruction::Instruction;
use solana_program::pubkey::Pubkey;
use spl_associated_token_account::get_associated_token_address;

use common::command::{CliConfig, Parser, ProcessResult};
use common::contract::instructions::create_clmmpools::new_create_clmmpool;
use common::contract::instructions::create_tick_array_map::new_create_tick_array_map;
use common::contract::state::fee_tier::FeeTier;
use common::program::SWAP_PROGRAM_ID;
use common::utils::send::send_tx;
use tabled::object::Segment;
use tabled::style::Color;
use tabled::{Alignment, Modify, Panel, Rotate, Style, Table, Tabled};

#[derive(Tabled)]
struct PoolInfo {
    name: String,
    pool_address: Pubkey,
    clmm_config: Pubkey,
    token_a_mint: Pubkey,
    token_b_mint: Pubkey,
    token_a_decimals: u8,
    token_b_decimals: u8,
    token_a_vault: Pubkey,
    token_b_vault: Pubkey,
    init_price: Decimal,
    init_sqrt_price: u128,
    fee_tier: Pubkey,
    tick_spacing: u16,
}

pub fn process(rpc_client: &RpcClient, config: &CliConfig) -> ProcessResult {
    let token_a = Text::new("token_a mint address ?")
        .with_validator(required!("token a mint address required"))
        .to_pubkey()?;
    let token_b = Text::new("token_b mint address ?")
        .with_validator(required!("token a mint address required"))
        .to_pubkey()?;
    let tick_spacing = Text::new("tick space ?")
        .with_validator(required!("tick space required"))
        .to_u16()?;
    let init_price = Text::new("init price")
        .with_validator(required!("init sqrt price required"))
        .to_decimal()?;

    let fee_tier = FeeTier::find_address(tick_spacing);
    let clmm_config = ClmmConfig::find_address();
    let clmmpool_pubkey = Clmmpool::find_address(&token_a, &token_b, tick_spacing);
    let token_a_vault = get_associated_token_address(&clmmpool_pubkey, &token_a);
    let token_b_vault = get_associated_token_address(&clmmpool_pubkey, &token_b);
    let base_decimal = get_decimals(rpc_client, &token_a);
    let quote_decimal = get_decimals(rpc_client, &token_b);
    let init_sqrt_price = SqrtPrice::from(init_price, base_decimal, quote_decimal);

    let pool_info = PoolInfo {
        name: pool_name(&token_a, &token_b),
        pool_address: clmmpool_pubkey,
        clmm_config,
        token_a_mint: token_a,
        token_b_mint: token_b,
        token_a_decimals: base_decimal,
        token_b_decimals: quote_decimal,
        token_a_vault,
        token_b_vault,
        init_price,
        init_sqrt_price: init_sqrt_price.fixed_point,
        fee_tier,
        tick_spacing,
    };

    let confirm_table = Table::new(vec![pool_info])
        .with(Rotate::Bottom)
        .with(Rotate::Right)
        .with(Style::modern().off_horizontal())
        .with(Color::try_from(" ".cyan().to_string()).unwrap())
        .with(Panel("Pool info", 0))
        .with(Modify::new(Segment::all()).with(Alignment::left()))
        .to_string();

    println!("{}", confirm_table);
    Text::confirm()?;

    let mut ixs: Vec<Instruction> = Vec::new();
    ixs.push(new_create_clmmpool(
        clmm_config,
        fee_tier,
        token_a,
        token_b,
        init_sqrt_price.fixed_point,
        clmmpool_pubkey,
        token_a_vault,
        token_b_vault,
        config.pubkey().unwrap(),
    ));

    let (tick_array_map_pubkey, _) = Pubkey::find_program_address(
        &[b"tick_array_map", clmmpool_pubkey.as_ref()],
        &SWAP_PROGRAM_ID,
    );
    ixs.push(new_create_tick_array_map(
        clmmpool_pubkey,
        tick_array_map_pubkey,
        config.pubkey().unwrap(),
    ));

    let res = send_tx(rpc_client, config, &ixs)?;
    println!("pool key: {}", clmmpool_pubkey.to_string());

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