use alloy::primitives::{Address, B256};
use newton_core::cache::{PolicyContractCache, PolicySnapshot};
use crate::{
error::ChainIoError,
policy_client::{get_policy_address_for_client, get_policy_config_by_id},
version::{get_policy_data_version, get_policy_version},
};
pub async fn resolve_policy_snapshot(
cache: &PolicyContractCache,
policy_client: Address,
policy_id: B256,
chain_id: u64,
rpc_url: &str,
) -> Result<PolicySnapshot, ChainIoError> {
let rpc = rpc_url.to_string();
cache
.get_or_try_insert_snapshot(chain_id, policy_id, async {
let policy_address = get_policy_address_for_client(policy_client, rpc.clone()).await?;
let config = get_policy_config_by_id(policy_address, policy_id, &rpc).await?;
Ok::<_, ChainIoError>(PolicySnapshot {
policy_address,
policy_id,
policy_params: config.policyParams,
expire_after: config.expireAfter,
})
})
.await
.map_err(|e| ChainIoError::SignerError(eyre::eyre!("{}", e)))
}
pub async fn resolve_policy_version(
cache: &PolicyContractCache,
policy_address: Address,
chain_id: u64,
rpc_url: &str,
) -> Result<String, ChainIoError> {
let rpc = rpc_url.to_string();
cache
.get_or_try_insert_policy_version(chain_id, policy_address, async {
get_policy_version(policy_address, &rpc)
.await
.map_err(ChainIoError::SignerError)
})
.await
.map_err(|e| ChainIoError::SignerError(eyre::eyre!("{}", e)))
}
pub async fn resolve_policy_data_version(
cache: &PolicyContractCache,
policy_data_address: Address,
chain_id: u64,
rpc_url: &str,
) -> Result<String, ChainIoError> {
let rpc = rpc_url.to_string();
cache
.get_or_try_insert_policy_data_version(chain_id, policy_data_address, async {
get_policy_data_version(policy_data_address, &rpc)
.await
.map_err(ChainIoError::SignerError)
})
.await
.map_err(|e| ChainIoError::SignerError(eyre::eyre!("{}", e)))
}