use std::str::FromStr;
use std::sync::Arc;
use clap::ArgMatches;
use solana_clap_utils::keypair::DefaultSigner;
use solana_client::rpc_client::RpcClient;
use solana_program::pubkey::Pubkey;
use solana_remote_wallet::remote_wallet::RemoteWalletManager;
use solana_sdk::signature::Keypair;
use solana_sdk::signer::Signer;
use spl_token::instruction::set_authority;
use crate::check_and_update_err;
use crate::command::{CliCommand, CliCommandInfo, CliConfig, CliError, ProcessResult};
use crate::contract::instructions::farming::mint_wrapper::new_wrapper_v2::new_wrapper_v2;
use crate::program::FARMING_MINT_WRAPPER_PROGRAM_ID;
use crate::utils::send::send_tx;
pub fn parse_farming_mint_wrapper_new<'a>(matches: &'a ArgMatches, default_signer: &DefaultSigner, wallet_manager: &mut Option<Arc<RemoteWalletManager>>) -> Result<CliCommandInfo<'a>, CliError> {
let mint = matches.value_of("mint");
let hardcap = matches.value_of("hardcap");
Ok(CliCommandInfo {
command: CliCommand::FarmingMintWrapperNew {
mint: Pubkey::from_str(mint.unwrap()).unwrap(),
hardcap: hardcap.unwrap().parse::<u64>().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_farming_mint_wrapper_new(
rpc_client: &RpcClient,
config: &mut CliConfig,
mint: Pubkey,
hardcap: u64,
) -> ProcessResult {
let base = Keypair::new();
let (mint_wrapper_pubkey, _) =
Pubkey::find_program_address(
&[b"MintWrapper", base.pubkey().as_ref()],
&FARMING_MINT_WRAPPER_PROGRAM_ID,
);
let ixs = [
set_authority(
&spl_token::id(),
&mint,
Some(&mint_wrapper_pubkey),
spl_token::instruction::AuthorityType::MintTokens,
&config.pubkey().unwrap(),
&[&config.pubkey().unwrap()]
).unwrap(),
set_authority(
&spl_token::id(),
&mint,
Some(&mint_wrapper_pubkey),
spl_token::instruction::AuthorityType::FreezeAccount,
&config.pubkey().unwrap(),
&[&config.pubkey().unwrap()]
).unwrap(),
new_wrapper_v2(
base.pubkey(),
mint,
mint_wrapper_pubkey,
config.pubkey().unwrap(),
hardcap,
),
];
config.signers.push(Box::new(base));
let res = send_tx(rpc_client, config, &ixs)?;
Ok("signers : ".to_owned() + res.to_string().as_str())
}