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::position::Position;
use crate::program::get_pubkey_for_program_with_seeds;
pub fn parse_position_info<'a>(
matches: &ArgMatches,
default_signer: &DefaultSigner,
wallet_manager: &mut Option<Arc<RemoteWalletManager>>,
) -> Result<CliCommandInfo<'a>, CliError> {
let mint = matches.value_of("mint");
Ok(CliCommandInfo {
command: CliCommand::PositionInfo {
mint: mint.unwrap().parse::<Pubkey>().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_position_info(rpc_client: &RpcClient, mint: &Pubkey) -> ProcessResult {
let (position, _) = get_pubkey_for_program_with_seeds(&[b"position", mint.as_ref()]);
let data = rpc_client.get_account_data(&position).unwrap();
let position_info: Position = Position::try_from_slice(&data[8..]).unwrap();
let color = Color::try_from(" ".cyan().to_string()).unwrap();
Ok(Table::new(vec![position_info])
.with(Rotate::Bottom)
.with(Rotate::Right)
.with(Style::modern())
.with(color)
.with(Panel("Position Info", 0))
.with(Modify::new(Segment::all()).with(Alignment::center()))
.to_string())
}