use crate::key_pair::ArgEnvReader;
use crate::{
environment_config::NetworkProviderArgs,
key_pair::PrivateKeyArgs,
utils::{Cmd, HelperErrors},
};
use clap::Parser;
use hopr_bindings::hopr_winning_probability_oracle::HoprWinningProbabilityOracle;
use hopr_internal_types::prelude::{f64_to_win_prob, win_prob_to_f64};
use tracing::{debug, info};
#[derive(Clone, Debug, Parser)]
pub enum WinProbSubcommands {
#[command(visible_alias = "s")]
Set {
#[command(flatten)]
network_provider: NetworkProviderArgs,
#[clap(help = "New winning probability", short = 'w', long, default_value_t = 1.0f64)]
winning_probability: f64,
#[command(flatten)]
private_key: PrivateKeyArgs,
},
#[command(visible_alias = "g")]
Get {
#[command(flatten)]
network_provider: NetworkProviderArgs,
},
}
impl WinProbSubcommands {
pub async fn execute_set_win_prob(
network_provider: NetworkProviderArgs,
winning_probability: f64,
private_key: PrivateKeyArgs,
) -> Result<(), HelperErrors> {
let signer_private_key = private_key.read("PRIVATE_KEY")?;
let rpc_provider = network_provider.get_provider_with_signer(&signer_private_key).await?;
let contract_addresses = network_provider.get_network_details_from_name()?;
let hopr_win_prob = HoprWinningProbabilityOracle::new(
contract_addresses.addresses.winning_probability_oracle,
rpc_provider.clone(),
);
let winning_probability = f64_to_win_prob(winning_probability).map_err(|_| {
HelperErrors::ParseError("Failed to convert winning probability to the required format".into())
})?;
let mut win_prob_param = [0u8; 8];
win_prob_param[1..].copy_from_slice(&winning_probability);
let win_prob_param = u64::from_be_bytes(win_prob_param);
info!(
"Setting the global minimum winning probability to {:?} ({:?} in uint56 format)",
winning_probability, win_prob_param
);
hopr_win_prob
.set_win_prob(win_prob_param)
.send()
.await
.map_err(|e| HelperErrors::MiddlewareError(format!("Failed in broadcasting transactions {:?}", e)))?
.await
.map_err(|e| HelperErrors::MiddlewareError(format!("Failed in getting receipt {:?}", e)))?;
Ok(())
}
pub async fn execute_get_win_prob(network_provider: NetworkProviderArgs) -> Result<f64, HelperErrors> {
let rpc_provider = network_provider.get_provider_without_signer().await?;
let contract_addresses = network_provider.get_network_details_from_name()?;
let hopr_win_prob = HoprWinningProbabilityOracle::new(
contract_addresses.addresses.winning_probability_oracle,
rpc_provider.clone(),
);
let current_win_prob = hopr_win_prob
.current_win_prob()
.await
.map_err(|e| HelperErrors::MiddlewareError(format!("Failed to get current winning probability: {}", e)))?;
let mut tmp = [0u8; 7];
tmp.copy_from_slice(¤t_win_prob.to_be_bytes()[1..]);
let current_win_prob_f64 = win_prob_to_f64(&tmp);
info!(
"Current global minimum winning probability is {:?} ({:?} in uint56 format)",
current_win_prob_f64, current_win_prob
);
Ok(current_win_prob_f64)
}
}
impl Cmd for WinProbSubcommands {
fn run(self) -> Result<(), HelperErrors> {
Ok(())
}
async fn async_run(self) -> Result<(), HelperErrors> {
match self {
WinProbSubcommands::Set {
network_provider,
winning_probability,
private_key,
} => {
WinProbSubcommands::execute_set_win_prob(network_provider, winning_probability, private_key).await?;
}
WinProbSubcommands::Get { network_provider } => {
let win_prob = WinProbSubcommands::execute_get_win_prob(network_provider).await?;
debug!("Current global minimum winning probability is: {}", win_prob);
}
}
Ok(())
}
}