newton-chainio 0.5.2

newton prover chainio
//! Version query utilities for policy and policy data contracts
//!
//! This module provides functions to query version information from
//! deployed policy and policy data contract instances via RPC calls.

use alloy::{primitives::Address, providers::ProviderBuilder, sol};
use eyre::Result;

// Import the ISemVerMixin interface
sol! {
    #[sol(rpc)]
    interface ISemVerMixin {
        function version() external view returns (string memory);
    }
}

/// Get the factory address for a given policy contract.
///
/// Used by the CLI migration tool to find which factory to redeploy through.
pub async fn get_policy_factory_for_policy(policy_address: Address, rpc_url: &str) -> Result<Address> {
    let provider = ProviderBuilder::new().connect_http(rpc_url.parse()?);

    sol! {
        #[sol(rpc)]
        interface INewtonPolicy {
            function factory() external view returns (address);
        }
    }

    let policy = INewtonPolicy::new(policy_address, provider);
    let result = policy.factory().call().await?;
    Ok(Address::from(result.0))
}

/// Get the factory address for a given policy data contract.
///
/// Used by the CLI migration tool to find which factory to redeploy through.
pub async fn get_policy_data_factory_for_policy_data(policy_data_address: Address, rpc_url: &str) -> Result<Address> {
    let provider = ProviderBuilder::new().connect_http(rpc_url.parse()?);

    sol! {
        #[sol(rpc)]
        interface INewtonPolicyData {
            function factory() external view returns (address);
        }
    }

    let policy_data = INewtonPolicyData::new(policy_data_address, provider);
    let result = policy_data.factory().call().await?;
    Ok(Address::from(result.0))
}

/// Get the version from a deployed policy contract instance.
///
/// Calls `version()` directly on the policy (not the factory).
/// Returns `Ok("0.0.0")` if the call reverts (pre-versioning implementation).
pub async fn get_policy_version(policy_address: Address, rpc_url: &str) -> Result<String> {
    let provider = ProviderBuilder::new().connect_http(rpc_url.parse()?);
    let sem_ver = ISemVerMixin::new(policy_address, provider);
    match sem_ver.version().call().await {
        Ok(version) => Ok(version),
        Err(_) => Ok("0.0.0".to_string()),
    }
}

/// Get the version from a deployed policy data contract instance.
///
/// Calls `version()` directly on the policy data (not the factory).
/// Returns `Ok("0.0.0")` if the call reverts (pre-versioning implementation).
pub async fn get_policy_data_version(policy_data_address: Address, rpc_url: &str) -> Result<String> {
    let provider = ProviderBuilder::new().connect_http(rpc_url.parse()?);
    let sem_ver = ISemVerMixin::new(policy_data_address, provider);
    match sem_ver.version().call().await {
        Ok(version) => Ok(version),
        Err(_) => Ok("0.0.0".to_string()),
    }
}