crema-cli 0.1.0

Blockchain, Crema for Solana
Documentation
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::instruction::Instruction;
use solana_program::pubkey::Pubkey;
use solana_remote_wallet::remote_wallet::RemoteWalletManager;
use spl_associated_token_account::get_associated_token_address;
use tabled::Tabled;

use crate::check_and_update_err;
use crate::command::{CliCommand, CliCommandInfo, CliConfig, CliError, ProcessResult};
use crate::contract::instructions::create_clmmpools::new_create_clmmpool;
use crate::contract::instructions::create_tick_array_map::new_create_tick_array_map;
use crate::contract::state::fee_tier::FeeTier;
use crate::program::get_pubkey_for_program_with_seeds;
use crate::utils::file::{get_template_dir, read_for_str};
use crate::utils::send::send_tx;

pub const TEMPLATE_DIR: &str = "./clmmpool-template.yaml";

#[derive(Debug, Serialize, Deserialize, Tabled)]
pub struct ClmmpoolTemplate {
    /// `clmm_config` provide the protocol_fee_rate and control the pool.
    pub clmm_config: String,

    /// `fee_tier` provide the tick_spacing and fee_rate to the pool.
    pub fee_tier: String,

    /// pool token_a mint address.
    pub token_a: String,
    /// pool token_b mint address.
    pub token_b: String,

    /// `init_sqrt_price` is pool init price.
    pub init_sqrt_price: u128,
}

pub fn parse_create_pool<'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", TEMPLATE_DIR);
    Ok(CliCommandInfo {
        command: CliCommand::PairCreatePool {
            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_create_pool(
    rpc_client: &RpcClient,
    config: &CliConfig,
    output: &str,
) -> ProcessResult {
    let pool: ClmmpoolTemplate = read_for_str(output);

    let fee_tier_info = FeeTier::get_info(
        rpc_client,
        &Pubkey::from_str(pool.fee_tier.as_str()).unwrap(),
    );

    let (clmmpool_pubkey, _) = get_pubkey_for_program_with_seeds(&[
        b"clmmpool",
        Pubkey::from_str(pool.clmm_config.as_str())
            .unwrap()
            .as_ref(),
        Pubkey::from_str(pool.token_a.as_str()).unwrap().as_ref(),
        Pubkey::from_str(pool.token_b.as_str()).unwrap().as_ref(),
        fee_tier_info.tick_spacing.to_le_bytes().as_ref(),
    ]);

    let token_a_vault = get_associated_token_address(
        &clmmpool_pubkey,
        &Pubkey::from_str(pool.token_a.as_str()).unwrap(),
    );
    let token_b_vault = get_associated_token_address(
        &clmmpool_pubkey,
        &Pubkey::from_str(pool.token_b.as_str()).unwrap(),
    );

    let mut ixs: Vec<Instruction> = Vec::new();
    ixs.push(new_create_clmmpool(
        &pool,
        &clmmpool_pubkey,
        &token_a_vault,
        &token_b_vault,
        config.pubkey().unwrap(),
    ));

    let (tick_array_map_pubkey, _) =
        get_pubkey_for_program_with_seeds(&[b"tick_array_map", clmmpool_pubkey.as_ref()]);
    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())
}

pub fn parse_create_pool_template<'a>(
    matches: &'a ArgMatches,
) -> Result<CliCommandInfo<'a>, CliError> {
    let output_file = get_template_dir(matches, "output-file", TEMPLATE_DIR);
    Ok(CliCommandInfo {
        command: CliCommand::PairCreatePoolTemplate {
            output_file: output_file.unwrap(),
        },
        signers: vec![],
    })
}

pub fn process_create_pool_template(output: &str) -> ProcessResult {
    let clmmpool_template = ClmmpoolTemplate {
        clmm_config: Pubkey::from_str("BPFLoaderUpgradeab1e11111111111111111111111")
            .unwrap()
            .to_string(),
        fee_tier: Pubkey::from_str("BPFLoaderUpgradeab1e11111111111111111111111")
            .unwrap()
            .to_string(),
        token_a: Pubkey::from_str("BPFLoaderUpgradeab1e11111111111111111111111")
            .unwrap()
            .to_string(),
        token_b: Pubkey::from_str("BPFLoaderUpgradeab1e11111111111111111111111")
            .unwrap()
            .to_string(),
        init_sqrt_price: 0,
    };

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

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