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
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 solana_sdk::signature::Keypair;
use solana_sdk::signer::Signer;
use tabled::Tabled;

use crate::check_and_update_err;
use common::command::{CliCommand, CliCommandInfo, CliConfig, CliError, ProcessResult};
use common::contract::instructions::initialize_clmm_config::{new_init_clmm_config};
use common::math::fee::ui_fee_to_lamport_4;
use common::utils::file::{get_template_dir, 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_fee_authority: String,
    pub protocol_fee_claim_authority: String,
    pub protocol_fee_rate: f64,
}

pub fn parse_init_config<'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", CONFIG_TEMPLATE_DIR);
    Ok(CliCommandInfo {
        command: CliCommand::PairConfigInit {
            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_init_config(
    rpc_client: &RpcClient,
    config: &mut CliConfig,
    output: &str,
) -> ProcessResult {
    let pool_config: ClmmConfigTemplate = read_for_str(output);

    let clmm_config_keypair = Keypair::new();
    let clmm_config_pubkey = clmm_config_keypair.pubkey();

    config.signers.push(Box::new(clmm_config_keypair));
    println!("config: {:?}", pool_config);
    let ixs = [new_init_clmm_config(
        Pubkey::from_str(pool_config.protocol_fee_authority.as_str()).unwrap(),
        Pubkey::from_str(pool_config.protocol_fee_claim_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 parse_init_config_template<'a>(
    matches: &'a ArgMatches,
) -> Result<CliCommandInfo<'a>, CliError> {
    let output_file = get_template_dir(matches, "output-file", CONFIG_TEMPLATE_DIR);
    Ok(CliCommandInfo {
        command: CliCommand::PairConfigInitTemplate {
            output_file: output_file.unwrap(),
        },
        signers: vec![],
    })
}

pub fn process_init_config_template(output: &str) -> ProcessResult {
    let config_template = ClmmConfigTemplate {
        protocol_fee_authority: Pubkey::from_str("BPFLoaderUpgradeab1e11111111111111111111111")
            .unwrap()
            .to_string(),
        protocol_fee_claim_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())
}