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::{StakingOptions, UpdateWalletOptions};
use super::super::utils::{
create_staking_transaction, create_staking_transaction_with_client, DEFAULT_STAKING_FEE_ID,
};
pub async fn update_wallet(
update_opts: UpdateWalletOptions,
public_key_base58_identifier: &str,
private_key_base58: &str,
options: StakingOptions,
) -> Result<SmartContractExecuteTxn> {
if update_opts.wallet_address.is_empty() {
return Err(ZeraError::Validation(
"walletAddress is required".to_string(),
));
}
if update_opts.bump_id.is_empty() {
return Err(ZeraError::Validation("bumpId 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!("{},{}", update_opts.wallet_address, update_opts.bump_id);
let fee_id = options
.fee_id
.as_deref()
.unwrap_or(DEFAULT_STAKING_FEE_ID)
.to_string();
create_staking_transaction(
"update_wallet",
¶meter_value,
public_key_base58_identifier,
private_key_base58,
&fee_id,
options,
)
.await
}
pub async fn update_wallet_with_client<T>(
update_opts: UpdateWalletOptions,
public_key_base58_identifier: &str,
private_key_base58: &str,
options: StakingOptions,
client: &ValidatorApiClient<T>,
) -> Result<SmartContractExecuteTxn>
where
T: UnaryTransport,
{
if update_opts.wallet_address.is_empty() {
return Err(ZeraError::Validation(
"walletAddress is required".to_string(),
));
}
if update_opts.bump_id.is_empty() {
return Err(ZeraError::Validation("bumpId 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!("{},{}", update_opts.wallet_address, update_opts.bump_id);
let fee_id = options
.fee_id
.as_deref()
.unwrap_or(DEFAULT_STAKING_FEE_ID)
.to_string();
create_staking_transaction_with_client(
"update_wallet",
¶meter_value,
public_key_base58_identifier,
private_key_base58,
&fee_id,
options,
client,
)
.await
}
pub async fn update_wallet_and_send(
update_opts: UpdateWalletOptions,
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 = update_wallet(
update_opts,
public_key_base58_identifier,
private_key_base58,
options,
)
.await?;
send_smart_contract_execute_txn(&txn, Some(grpc_config)).await
}