|
| 1 | +// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Horizon (V2) contract detection utilities |
| 5 | +//! |
| 6 | +//! This module provides functionality to detect if Horizon (V2) contracts are active |
| 7 | +//! in the network by calling the HorizonStaking contract directly. |
| 8 | +
|
| 9 | +use alloy::{ |
| 10 | + contract::{Contract, Interface}, |
| 11 | + primitives::{Address, U256}, |
| 12 | + providers::{Provider, ProviderBuilder}, |
| 13 | + rpc::types::TransactionRequest, |
| 14 | + sol, |
| 15 | + transports::http::{Client, Http}, |
| 16 | +}; |
| 17 | +use anyhow::Result; |
| 18 | + |
| 19 | +sol! { |
| 20 | + #[sol(rpc)] |
| 21 | + interface HorizonStaking { |
| 22 | + function getMaxThawingPeriod() external view returns (uint64); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +/// Detects if Horizon (V2) contracts are active in the network. |
| 27 | +/// |
| 28 | +/// This function calls the `getMaxThawingPeriod()` method on the HorizonStaking contract. |
| 29 | +/// If the method returns a value greater than 0, the network is considered to have upgraded to Horizon. |
| 30 | +/// If the call fails or returns 0, the network is considered to be in legacy mode. |
| 31 | +/// |
| 32 | +/// This approach matches the proven TypeScript implementation and is more reliable than |
| 33 | +/// subgraph queries as it directly queries the source of truth (the contracts). |
| 34 | +/// |
| 35 | +/// # Arguments |
| 36 | +/// * `rpc_url` - The RPC endpoint to connect to |
| 37 | +/// * `horizon_staking_address` - The address of the HorizonStaking contract |
| 38 | +/// |
| 39 | +/// # Returns |
| 40 | +/// * `Ok(true)` if Horizon contracts are active (getMaxThawingPeriod() > 0) |
| 41 | +/// * `Ok(false)` if only legacy (V1) contracts are active |
| 42 | +/// * `Err(...)` if there was an error calling the contract |
| 43 | +pub async fn is_horizon_active(rpc_url: &str, horizon_staking_address: Address) -> Result<bool> { |
| 44 | + tracing::debug!( |
| 45 | + "Checking if Horizon (V2) contracts are active by calling HorizonStaking.getMaxThawingPeriod() at {}", |
| 46 | + horizon_staking_address |
| 47 | + ); |
| 48 | + |
| 49 | + // Create HTTP provider using alloy |
| 50 | + let provider = ProviderBuilder::new().on_http(rpc_url.parse()?); |
| 51 | + |
| 52 | + // Create contract instance |
| 53 | + let contract = HorizonStaking::new(horizon_staking_address, &provider); |
| 54 | + |
| 55 | + // Call getMaxThawingPeriod() |
| 56 | + match contract.getMaxThawingPeriod().call().await { |
| 57 | + Ok(max_thawing_period) => { |
| 58 | + let max_thawing_period = max_thawing_period._0; |
| 59 | + let horizon_active = max_thawing_period > 0; |
| 60 | + |
| 61 | + if horizon_active { |
| 62 | + tracing::info!( |
| 63 | + "Horizon (V2) contracts detected - HorizonStaking.getMaxThawingPeriod() returned {}", |
| 64 | + max_thawing_period |
| 65 | + ); |
| 66 | + } else { |
| 67 | + tracing::info!( |
| 68 | + "No Horizon (V2) contracts found - getMaxThawingPeriod() returned 0" |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + Ok(horizon_active) |
| 73 | + } |
| 74 | + Err(e) => { |
| 75 | + tracing::warn!( |
| 76 | + "Failed to call HorizonStaking.getMaxThawingPeriod() at {}: {}. Assuming legacy mode.", |
| 77 | + horizon_staking_address, |
| 78 | + e |
| 79 | + ); |
| 80 | + // On error, assume legacy mode (similar to TypeScript implementation) |
| 81 | + Ok(false) |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments