use crate::{
InternalError,
dto::fleet_funding::{FleetRootFundingRequest, FleetRootFundingResponse},
};
use async_trait::async_trait;
use std::sync::OnceLock;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct RootFundingRuntimeConfig {
pub request_threshold: u128,
pub cooldown_secs: u64,
}
#[async_trait]
pub trait RootFundingRuntime: Send + Sync {
fn config(&self) -> Result<RootFundingRuntimeConfig, InternalError>;
fn current_request(&self) -> Result<Option<FleetRootFundingRequest>, InternalError>;
fn prepare_request(&self) -> Result<FleetRootFundingRequest, InternalError>;
async fn request(
&self,
request: FleetRootFundingRequest,
) -> Result<FleetRootFundingResponse, InternalError>;
fn record_response(
&self,
response: FleetRootFundingResponse,
) -> Result<FleetRootFundingResponse, InternalError>;
}
static ROOT_FUNDING_RUNTIME: OnceLock<&'static dyn RootFundingRuntime> = OnceLock::new();
pub struct RootFundingRuntimeApi;
impl RootFundingRuntimeApi {
pub fn register(runtime: &'static dyn RootFundingRuntime) {
let _ = ROOT_FUNDING_RUNTIME.set(runtime);
}
pub(crate) fn config() -> Result<RootFundingRuntimeConfig, InternalError> {
runtime()?.config()
}
pub(crate) fn current_request() -> Result<Option<FleetRootFundingRequest>, InternalError> {
runtime()?.current_request()
}
pub(crate) fn prepare_request() -> Result<FleetRootFundingRequest, InternalError> {
runtime()?.prepare_request()
}
pub(crate) async fn request(
request: FleetRootFundingRequest,
) -> Result<FleetRootFundingResponse, InternalError> {
runtime()?.request(request).await
}
pub(crate) fn record_response(
response: FleetRootFundingResponse,
) -> Result<FleetRootFundingResponse, InternalError> {
runtime()?.record_response(response)
}
}
fn runtime() -> Result<&'static dyn RootFundingRuntime, InternalError> {
ROOT_FUNDING_RUNTIME
.get()
.copied()
.ok_or_else(InternalError::lifecycle_failure)
}