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 spl_associated_token_account::get_associated_token_address;
use spl_token::ui_amount_to_amount;
use crate::check_and_update_err;
use crate::client::get_decimals;
use crate::command::{CliCommand, CliCommandInfo, CliConfig, CliError, ProcessResult};
use crate::contract::instructions::farming::minter::perform_mint::new_preform_mint;
use crate::contract::state::farming::mint_wrapper::MintWrapper;
use crate::program::FARMING_MINT_WRAPPER_PROGRAM_ID;
use crate::utils::send::send_tx;
pub fn parse_farming_minter_mint<'a>(matches: &'a ArgMatches, default_signer: &DefaultSigner, wallet_manager: &mut Option<Arc<RemoteWalletManager>>) -> Result<CliCommandInfo<'a>, CliError> {
let wrapper = matches.value_of("wrapper");
let dest_owner = matches.value_of("dest_owner");
let amount = matches.value_of("amount");
Ok(CliCommandInfo {
command: CliCommand::FarmingMinterMint {
wrapper: Pubkey::from_str(wrapper.unwrap()).unwrap(),
owner: Pubkey::from_str(dest_owner.unwrap()).unwrap(),
amount: amount.unwrap().parse::<f64>().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_minter_mint(
rpc_client: &RpcClient,
config: &CliConfig,
wrapper: Pubkey,
owner: Pubkey,
ui_amount: f64,
) -> ProcessResult {
let minter =
Pubkey::find_program_address(
&[
b"MintWrapperMinter",
wrapper.as_ref(),
config.pubkey().unwrap().as_ref(),
],
&FARMING_MINT_WRAPPER_PROGRAM_ID,
);
let mint_wrapper_info = MintWrapper::get_info(rpc_client, &wrapper);
let token_decimals = get_decimals(rpc_client, &mint_wrapper_info.token_mint);
let amount = ui_amount_to_amount(ui_amount, token_decimals);
let token = get_associated_token_address(
&owner,
&mint_wrapper_info.token_mint,
);
let ixs = [
new_preform_mint(
wrapper,
amount,
mint_wrapper_info.token_mint,
token,
minter.0,
config.pubkey().unwrap(),
)
];
let res = send_tx(rpc_client, config, &ixs)?;
Ok("signers : ".to_owned() + res.to_string().as_str())
}