use zera_proto::zera_txn::SmartContractExecuteTxn;
use crate::error::{Result, ZeraError};
use crate::grpc::{UnaryTransport, ValidatorApiClient};
use crate::smart_contracts::send_smart_contract_execute_txn;
use super::super::types::{InstantStakeOptions, StakingOptions};
use super::super::utils::{
create_staking_transaction, create_staking_transaction_with_client, DEFAULT_STAKING_FEE_ID,
};
pub async fn instant_stake(
stake_opts: InstantStakeOptions,
public_key_base58_identifier: &str,
private_key_base58: &str,
options: StakingOptions,
) -> Result<SmartContractExecuteTxn> {
if stake_opts.amount.is_empty() {
return Err(ZeraError::Validation("amount is required".to_string()));
}
if stake_opts.term.is_empty() {
return Err(ZeraError::Validation("term is required".to_string()));
}
if public_key_base58_identifier.is_empty() {
return Err(ZeraError::Validation(
"publicKeyBase58Identifier is required".to_string(),
));
}
if private_key_base58.is_empty() {
return Err(ZeraError::Validation(
"privateKeyBase58 is required".to_string(),
));
}
let parameter_value = format!("{},{}", stake_opts.amount, stake_opts.term);
let fee_id = options
.fee_id
.as_deref()
.unwrap_or(DEFAULT_STAKING_FEE_ID)
.to_string();
create_staking_transaction(
"instant_stake",
¶meter_value,
public_key_base58_identifier,
private_key_base58,
&fee_id,
options,
)
.await
}
pub async fn instant_stake_with_client<T>(
stake_opts: InstantStakeOptions,
public_key_base58_identifier: &str,
private_key_base58: &str,
options: StakingOptions,
client: &ValidatorApiClient<T>,
) -> Result<SmartContractExecuteTxn>
where
T: UnaryTransport,
{
if stake_opts.amount.is_empty() {
return Err(ZeraError::Validation("amount is required".to_string()));
}
if stake_opts.term.is_empty() {
return Err(ZeraError::Validation("term is required".to_string()));
}
if public_key_base58_identifier.is_empty() {
return Err(ZeraError::Validation(
"publicKeyBase58Identifier is required".to_string(),
));
}
if private_key_base58.is_empty() {
return Err(ZeraError::Validation(
"privateKeyBase58 is required".to_string(),
));
}
let parameter_value = format!("{},{}", stake_opts.amount, stake_opts.term);
let fee_id = options
.fee_id
.as_deref()
.unwrap_or(DEFAULT_STAKING_FEE_ID)
.to_string();
create_staking_transaction_with_client(
"instant_stake",
¶meter_value,
public_key_base58_identifier,
private_key_base58,
&fee_id,
options,
client,
)
.await
}
pub async fn instant_stake_and_send(
stake_opts: InstantStakeOptions,
public_key_base58_identifier: &str,
private_key_base58: &str,
options: StakingOptions,
) -> Result<String> {
let grpc_config = options
.grpc_config
.clone()
.unwrap_or_default();
let txn = instant_stake(
stake_opts,
public_key_base58_identifier,
private_key_base58,
options,
)
.await?;
send_smart_contract_execute_txn(&txn, Some(grpc_config)).await
}