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 crate::utils::amount::to_smallest_units;
use super::super::types::{DexOptions, RemoveLiquidityOptions};
use super::super::utils::{
create_dex_transaction_with_client, fee_id_or_default, validate_signing_inputs,
};
const LP_TOKEN_DENOMINATION: &str = "1000000000";
pub async fn remove_liquidity(
removal: RemoveLiquidityOptions,
public_key_base58_identifier: &str,
private_key_base58: &str,
options: DexOptions,
) -> Result<SmartContractExecuteTxn> {
let client = ValidatorApiClient::new(options.grpc_config.clone().unwrap_or_default())?;
remove_liquidity_with_client(
removal,
public_key_base58_identifier,
private_key_base58,
options,
&client,
)
.await
}
pub async fn remove_liquidity_with_client<T>(
removal: RemoveLiquidityOptions,
public_key_base58_identifier: &str,
private_key_base58: &str,
options: DexOptions,
client: &ValidatorApiClient<T>,
) -> Result<SmartContractExecuteTxn>
where
T: UnaryTransport,
{
validate_signing_inputs(public_key_base58_identifier, private_key_base58)?;
if removal.token_a.is_empty() {
return Err(ZeraError::Validation("tokenA is required".to_string()));
}
if removal.token_b.is_empty() {
return Err(ZeraError::Validation("tokenB is required".to_string()));
}
if removal.lp_amount.is_empty() {
return Err(ZeraError::Validation("lpAmount is required".to_string()));
}
let lp_amount_parts = to_smallest_units(
&removal.lp_amount,
"$LP+0000",
Some(LP_TOKEN_DENOMINATION),
None,
)?;
let parameter_value = format!(
"{},{},{},{}",
removal.token_a, removal.token_b, lp_amount_parts, removal.fee_rate
);
let mut create_options = options;
create_options.fee_id = Some(fee_id_or_default(&create_options));
create_dex_transaction_with_client(
"remove_liquidity",
parameter_value,
public_key_base58_identifier,
private_key_base58,
create_options,
client,
)
.await
}
pub async fn remove_liquidity_and_send(
removal: RemoveLiquidityOptions,
public_key_base58_identifier: &str,
private_key_base58: &str,
options: DexOptions,
) -> Result<String> {
let grpc_config = options.grpc_config.clone();
let txn = remove_liquidity(
removal,
public_key_base58_identifier,
private_key_base58,
options,
)
.await?;
send_smart_contract_execute_txn(&txn, grpc_config).await
}