use ethers_core::types::{
BlockId,
H256,
};
use ethers_providers::{
Http,
Provider,
};
use eyre::Result;
use serde::{
Deserialize,
Serialize,
};
use crate::errors::RollupNodeError;
#[derive(Debug, Clone, Default)]
pub struct RollupNode {
pub client: Option<Provider<Http>>,
}
impl TryFrom<String> for RollupNode {
type Error = RollupNodeError;
fn try_from(url: String) -> Result<Self, Self::Error> {
Self::new(url.as_ref()).map_err(|_| Self::Error::RollupNodeInvalidUrl(url))
}
}
impl RollupNode {
pub fn new(l2_url: &str) -> Result<Self> {
let client = Provider::<Http>::try_from(l2_url)?;
Ok(Self {
client: Some(client),
})
}
pub async fn output_at_block(&self, block_num: u64) -> Result<OutputResponse> {
let output = self
.client
.as_ref()
.unwrap()
.request("optimism_outputAtBlock", vec![block_num])
.await?;
Ok(output)
}
pub async fn sync_status(&self) -> Result<SyncStatus> {
let empty_params: Vec<String> = Vec::new();
let sync_status = self
.client
.as_ref()
.unwrap()
.request("optimism_syncStatus", empty_params)
.await?;
Ok(sync_status)
}
pub async fn rollup_config(&self) -> Result<serde_json::Value> {
let empty_params: Vec<String> = Vec::new();
let config = self
.client
.as_ref()
.unwrap()
.request("optimism_rollupConfig", empty_params)
.await?;
Ok(config)
}
pub async fn version(&self) -> Result<String> {
let empty_params: Vec<String> = Vec::new();
let version = self
.client
.as_ref()
.unwrap()
.request("optimism_version", empty_params)
.await?;
Ok(version)
}
}
#[derive(
Debug,
Clone,
Serialize,
Deserialize,
Default,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
)]
pub struct SyncStatus {
pub current_l1: u64,
pub current_l1_finalized: u64,
pub head_l1: u64,
pub safe_l1: u64,
pub finalized_l1: u64,
pub unsafe_l2: u64,
pub safe_l2: u64,
pub finalized_l2: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct OutputResponse {
pub version: Vec<u8>,
pub output_root: Vec<u8>,
pub block_ref: L2BlockRef,
pub withdrawal_storage_root: H256,
pub state_root: H256,
pub sync_status: SyncStatus,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct L2BlockRef {
pub hash: H256,
pub number: u64,
pub parent_hash: H256,
pub time: u64,
#[serde(rename = "l1origin")]
pub l1_origin: BlockId,
pub sequence_number: u64,
}