crema-cli 0.1.0

Blockchain, Crema for Solana
Documentation
use std::ops::{Div, Sub};
use std::sync::Arc;

use borsh::BorshDeserialize;
use clap::ArgMatches;
use colored::Colorize;
use solana_clap_utils::keypair::DefaultSigner;
use solana_client::rpc_client::RpcClient;
use solana_remote_wallet::remote_wallet::RemoteWalletManager;
use solana_sdk::pubkey::Pubkey;
use tabled::{Alignment, Modify, Panel, Rotate, Style, Table};
use tabled::object::Segment;
use tabled::style::Color;

use crate::check_and_update_err;
use crate::command::{CliCommand, CliCommandInfo, CliError, ProcessResult};
use crate::contract::state::{
    clmmpools::Clmmpool,
    tick::Tick,
    tick_array::TickArray,
};

pub fn parse_tick_info<'a>(
    matches: &ArgMatches,
    default_signer: &DefaultSigner,
    wallet_manager: &mut Option<Arc<RemoteWalletManager>>,
) -> Result<CliCommandInfo<'a>, CliError> {
    let pool = matches.value_of("pool");
    let tick_index = matches.value_of("tick_index");

    Ok(CliCommandInfo {
        command: CliCommand::TickInfo {
            pool: pool.unwrap().parse::<Pubkey>().unwrap(),
            tick_index: tick_index.unwrap().parse::<i32>().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_tick_info(rpc_client: &RpcClient, tick_index: i32, pool: &Pubkey) -> ProcessResult {
    let clmmpool_info: Clmmpool = Clmmpool::get_info(rpc_client, pool);

    let array_index = TickArray::array_index(tick_index, clmmpool_info.tick_spacing).unwrap();

    println!("array_index: {:?}", array_index);

    let tick_array_address = TickArray::get_tick_array_address(pool, array_index);

    let data = rpc_client.get_account_data(&tick_array_address).unwrap();

    let array_spacing = clmmpool_info.tick_spacing as usize * TickArray::CAP;
    let start_tick_index =
        Tick::min(clmmpool_info.tick_spacing) + (array_index as usize * array_spacing) as i32;
    let tick_offset = tick_index
        .sub(start_tick_index)
        .div(clmmpool_info.tick_spacing as i32) as usize;
    let tick: Tick = Tick::try_from_slice(
        &data[tick_offset * Tick::LEN + 44..(tick_offset + 1) * Tick::LEN + 44],
    )
        .unwrap();
    let color = Color::try_from(" ".cyan().to_string()).unwrap();
    Ok(Table::new(vec![tick])
        .with(Rotate::Bottom)
        .with(Rotate::Right)
        .with(Style::modern())
        .with(color)
        .with(Panel("Tick Info", 0))
        .with(Modify::new(Segment::all()).with(Alignment::center()))
        .to_string())
}