Override conversion rate when computing message fee (#1261)

* override conversion rate when message is sent

* spelling + fmt

* add --conversion-rate-override cli option

* try to read conversion rate from cmd output

* fix output

* fmt
This commit is contained in:
Svyatoslav Nikolsky
2022-03-02 17:36:26 +03:00
committed by Bastian Köcher
parent 0ef401ae53
commit cfdb9fe7f6
4 changed files with 167 additions and 18 deletions
@@ -16,7 +16,7 @@
//! Substrate relay helpers
use relay_utils::metrics::{FloatJsonValueMetric, PrometheusError};
use relay_utils::metrics::{FloatJsonValueMetric, PrometheusError, StandaloneMetric};
/// Creates standalone token price metric.
pub fn token_price_metric(token_id: &str) -> Result<FloatJsonValueMetric, PrometheusError> {
@@ -27,3 +27,28 @@ pub fn token_price_metric(token_id: &str) -> Result<FloatJsonValueMetric, Promet
format!("Rate used to convert from {} to some BASE tokens", token_id.to_uppercase()),
)
}
/// Compute conversion rate between two tokens immediately, without spawning any metrics.
pub async fn target_to_source_conversion_rate(
source_token_id: &str,
target_token_id: &str,
) -> anyhow::Result<f64> {
let source_token_metric = token_price_metric(source_token_id)?;
source_token_metric.update().await;
let target_token_metric = token_price_metric(target_token_id)?;
target_token_metric.update().await;
let source_token_value = *source_token_metric.shared_value_ref().read().await;
let target_token_value = *target_token_metric.shared_value_ref().read().await;
// `FloatJsonValueMetric` guarantees that the value is positive && normal, so no additional
// checks required here
match (source_token_value, target_token_value) {
(Some(source_token_value), Some(target_token_value)) =>
Ok(target_token_value / source_token_value),
_ => Err(anyhow::format_err!(
"Failed to compute conversion rate from {} to {}",
target_token_id,
source_token_id,
)),
}
}