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
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::initialize_clmm_config::{new_init_clmm_config};
use common::math::fee::ui_fee_to_lamport_4;
use common::program::SWAP_PROGRAM_ID;
use common::utils::file::{read_for_str};
use common::utils::send::send_tx;

pub const CONFIG_TEMPLATE_DIR: &str = "./clmm-config-template.yaml";

#[derive(Debug, Serialize, Deserialize, Tabled)]
pub struct ClmmConfigTemplate {
    pub protocol_authority: String,
    pub protocol_fee_claim_authority: String,
    pub create_pool_authority: String,
    pub protocol_fee_rate: f64,
}

pub fn process_init_config(
    rpc_client: &RpcClient,
    config: &mut CliConfig,
    output: &str,
) -> ProcessResult {
    let pool_config: ClmmConfigTemplate = read_for_str(output);

    let (clmm_config_pubkey, _) = Pubkey::find_program_address(
        &[
            b"clmmconfig",
        ],
        &SWAP_PROGRAM_ID,
    );

    println!("config: {:?}", pool_config);
    let ixs = [new_init_clmm_config(
        Pubkey::from_str(pool_config.protocol_authority.as_str()).unwrap(),
        Pubkey::from_str(pool_config.protocol_fee_claim_authority.as_str()).unwrap(),
        Pubkey::from_str(pool_config.create_pool_authority.as_str()).unwrap(),
        ui_fee_to_lamport_4(pool_config.protocol_fee_rate) as u16,
        clmm_config_pubkey,
        config.pubkey().unwrap(),
    )];

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

    println!("clmm config key: {}", clmm_config_pubkey.to_string());

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

pub fn process_init_config_template(output: &str) -> ProcessResult {
    let config_template = ClmmConfigTemplate {
        protocol_authority: Pubkey::from_str("BPFLoaderUpgradeab1e11111111111111111111111")
            .unwrap()
            .to_string(),
        protocol_fee_claim_authority: Pubkey::from_str(
            "BPFLoaderUpgradeab1e11111111111111111111111",
        )
            .unwrap()
            .to_string(),
        create_pool_authority: Pubkey::from_str("BPFLoaderUpgradeab1e11111111111111111111111")
            .unwrap()
            .to_string(),
        protocol_fee_rate: 0.0001,
    };

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

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