use std::sync::Arc;
use clap::ArgMatches;
use colored::Colorize;
use solana_clap_utils::keypair::DefaultSigner;
use solana_client::rpc_client::RpcClient;
use solana_client::rpc_config::RpcProgramAccountsConfig;
use solana_client::rpc_filter::{Memcmp, RpcFilterType};
use solana_client::rpc_filter::MemcmpEncodedBytes::Base58;
use solana_remote_wallet::remote_wallet::RemoteWalletManager;
use tabled::{Alignment, Modify, Panel, Style, Table};
use tabled::object::Segment;
use tabled::style::Color;
use borsh::{BorshDeserialize, BorshSerialize};
use solana_sdk::bs58;
use crate::check_and_update_err;
use crate::command::{CliCommand, CliCommandInfo, CliConfig, CliError, ProcessResult};
use crate::contract::state::farming::mint_wrapper::MintWrapper;
use crate::program::FARMING_MINT_WRAPPER_PROGRAM_ID;
pub fn parse_farming_mint_wrapper_list<'a>(matches: &'a ArgMatches, default_signer: &DefaultSigner, wallet_manager: &mut Option<Arc<RemoteWalletManager>>) -> Result<CliCommandInfo<'a>, CliError> {
Ok(CliCommandInfo {
command: CliCommand::FarmingMintWrapperList,
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_farming_mint_wrapper_list(
rpc_client: &RpcClient,
_: &CliConfig,
) -> ProcessResult {
let accounts = rpc_client.get_program_accounts_with_config(
&FARMING_MINT_WRAPPER_PROGRAM_ID,
RpcProgramAccountsConfig {
filters: Some(
vec![
RpcFilterType::Memcmp(
Memcmp {
offset: 0,
bytes: Base58(bs58::encode(MintWrapper::new().try_to_vec().unwrap()).into_string()),
encoding: None
}
)
]
),
account_config: Default::default(),
with_context: None
}
);
let mut list = Vec::new();
for account in accounts.unwrap() {
let data_slice = &account.1.data[8..];
list.push(MintWrapper::try_from_slice(data_slice).unwrap());
};
let color = Color::try_from(" ".cyan().to_string()).unwrap();
Ok(Table::new(list)
.with(Style::modern())
.with(color)
.with(Panel("Mint wrapper List", 0))
.with(Modify::new(Segment::all()).with(Alignment::center()))
.to_string())
}