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
use nom::AsBytes;
use serde::{Deserialize, Serialize};
use solana_client::rpc_client::RpcClient;
use solana_program::pubkey::Pubkey;

use solana_sdk::signature::{read_keypair_file, Signer};
use tabled::Tabled;


use common::command::{CliConfig, Parse, ProcessResult};
use common::contract::instructions::create_fee_tier::new_create_fee_tier;
use common::math::fee::ui_fee_to_lamport;
use common::program::SWAP_PROGRAM_ID;
use common::utils::send::send_tx;

pub const FEE_TIER_TEMPLATE_DIR: &str = "./fee-tier-template.yaml";

#[derive(Debug, Serialize, Deserialize, Tabled)]
pub struct FeeTierTemplate {
    pub clmm_config: String,
    pub fee_authority: String,
    pub tick_spacing: u16,
    pub fee_rate: f64,
}

pub fn process_create_fee_tier(
    rpc_client: &RpcClient,
    config: &mut CliConfig,
) -> ProcessResult {
    let clmm_config = Parse::new("clmm_config address", true).to_pubkey()?;
    let fee_authority = Parse::new("fee_authority keypair file", true).to_file()?;
    let tick_spacing = Parse::new("tick_spacing", true).to_u16()?;
    let fee_rate = Parse::new("fee_rate with float (0.01 = 1%)", true).to_f64()?;
    Parse::confirm()?;

    let fee_rate = ui_fee_to_lamport(fee_rate) as u16;

    let (fee_tier_pubkey, _) = Pubkey::find_program_address(
        &[
            b"fee_tier",
            clmm_config.as_ref(),
            tick_spacing.to_le_bytes().as_bytes(),
        ],
        &SWAP_PROGRAM_ID,
    );
    
    let fee_authority_keypair = read_keypair_file(fee_authority.clone())?;

    let fee_authority_pubkey = fee_authority_keypair.pubkey();

    config.signers.push(Box::new(fee_authority_keypair));

    let ixs = [new_create_fee_tier(
        clmm_config,
        fee_authority_pubkey,
        tick_spacing,
        fee_rate,
        fee_tier_pubkey,
        config.pubkey().unwrap(),
    )];

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

    println!("fee tier key: {}", fee_tier_pubkey.to_string());

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