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, Signer};
use spl_associated_token_account::instruction::create_associated_token_account;
use crate::check_and_update_err;
use crate::command::{CliCommand, CliCommandInfo, CliConfig, CliError, ProcessResult};
use crate::contract::instructions::farming::quarry_mine::new_rewarder::new_rewarder;
use crate::contract::state::farming::mint_wrapper::MintWrapper;
use crate::program::FARMING_MINE_PROGRAM_ID;
use crate::utils::send::send_tx;
pub fn parse_farming_mine_rewarder_new<'a>(matches: &'a ArgMatches, default_signer: &DefaultSigner, wallet_manager: &mut Option<Arc<RemoteWalletManager>>) -> Result<CliCommandInfo<'a>, CliError> {
let wrapper = matches.value_of("wrapper");
let annual_rate = matches.value_of("annual-rate");
Ok(CliCommandInfo {
command: CliCommand::FarmingMineRewarderNew {
wrapper: Pubkey::from_str(wrapper.unwrap()).unwrap(),
annual_rate: annual_rate.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_mine_rewarder_new(
rpc_client: &RpcClient,
config: &CliConfig,
wrapper: Pubkey,
annual_rate: u64,
) -> ProcessResult {
let base = Keypair::new();
let (rewarder, _) = Pubkey::find_program_address(
&[
b"Rewarder",
base.pubkey().as_ref(),
],
&FARMING_MINE_PROGRAM_ID,
);
let mint_wrapper_info = MintWrapper::get_info(rpc_client, &wrapper);
let mut ixs = Vec::new();
let (claim_fee_token_account, _) = Pubkey::find_program_address(
&[
rewarder.as_ref(),
spl_token::id().as_ref(),
mint_wrapper_info.token_mint.as_ref(),
],
&FARMING_MINE_PROGRAM_ID,
);
if rpc_client.get_account(&claim_fee_token_account).is_err() {
ixs.push(create_associated_token_account(
&claim_fee_token_account,
&rewarder,
&mint_wrapper_info.token_mint,
));
}
ixs.push(new_rewarder(
base.pubkey(),
config.pubkey().unwrap(),
rewarder,
config.pubkey().unwrap(),
wrapper,
mint_wrapper_info.token_mint,
claim_fee_token_account,
annual_rate,
));
let res = send_tx(rpc_client, config, &ixs)?;
Ok("signers : ".to_owned() + res.to_string().as_str())
}