|
|
@@ -4,6 +4,7 @@ use {
|
|
|
api::ChainId, chain::ethereum::InstrumentedPythContract,
|
|
|
eth_utils::traced_client::TracedClient,
|
|
|
},
|
|
|
+ anyhow::{anyhow, Result},
|
|
|
ethers::middleware::Middleware,
|
|
|
ethers::{prelude::BlockNumber, providers::Provider, types::Address},
|
|
|
std::{
|
|
|
@@ -14,23 +15,17 @@ use {
|
|
|
};
|
|
|
|
|
|
/// tracks the balance of the given address on the given chain
|
|
|
-/// if there was an error, the function will just return
|
|
|
#[tracing::instrument(skip_all)]
|
|
|
pub async fn track_balance(
|
|
|
chain_id: String,
|
|
|
provider: Arc<Provider<TracedClient>>,
|
|
|
address: Address,
|
|
|
metrics: Arc<KeeperMetrics>,
|
|
|
-) {
|
|
|
- let balance = match provider.get_balance(address, None).await {
|
|
|
- // This conversion to u128 is fine as the total balance will never cross the limits
|
|
|
- // of u128 practically.
|
|
|
- Ok(r) => r.as_u128(),
|
|
|
- Err(e) => {
|
|
|
- tracing::error!("Error while getting balance. error: {:?}", e);
|
|
|
- return;
|
|
|
- }
|
|
|
- };
|
|
|
+) -> Result<()> {
|
|
|
+ let balance = provider.get_balance(address, None).await?;
|
|
|
+ // This conversion to u128 is fine as the total balance will never cross the limits
|
|
|
+ // of u128 practically.
|
|
|
+ let balance = balance.as_u128();
|
|
|
// The f64 conversion is made to be able to serve metrics within the constraints of Prometheus.
|
|
|
// The balance is in wei, so we need to divide by 1e18 to convert it to eth.
|
|
|
let balance = balance as f64 / 1e18;
|
|
|
@@ -42,6 +37,8 @@ pub async fn track_balance(
|
|
|
address: address.to_string(),
|
|
|
})
|
|
|
.set(balance);
|
|
|
+
|
|
|
+ Ok(())
|
|
|
}
|
|
|
|
|
|
/// Tracks the difference between the server timestamp and the latest block timestamp for each chain
|
|
|
@@ -50,53 +47,47 @@ pub async fn track_block_timestamp_lag(
|
|
|
chain_id: String,
|
|
|
provider: Arc<Provider<TracedClient>>,
|
|
|
metrics: Arc<KeeperMetrics>,
|
|
|
-) {
|
|
|
- const INF_LAG: i64 = 1000000; // value that definitely triggers an alert
|
|
|
- let lag = match provider.get_block(BlockNumber::Latest).await {
|
|
|
- Ok(block) => match block {
|
|
|
- Some(block) => {
|
|
|
- let block_timestamp = block.timestamp;
|
|
|
- let server_timestamp = SystemTime::now()
|
|
|
- .duration_since(UNIX_EPOCH)
|
|
|
- .unwrap()
|
|
|
- .as_secs();
|
|
|
- let lag: i64 = (server_timestamp as i64) - (block_timestamp.as_u64() as i64);
|
|
|
- lag
|
|
|
- }
|
|
|
- None => {
|
|
|
- tracing::error!("Block is None");
|
|
|
- INF_LAG
|
|
|
- }
|
|
|
- },
|
|
|
- Err(e) => {
|
|
|
- tracing::error!("Failed to get block - {:?}", e);
|
|
|
- INF_LAG
|
|
|
- }
|
|
|
+) -> Result<()> {
|
|
|
+ let label = ChainIdLabel {
|
|
|
+ chain_id: chain_id.clone(),
|
|
|
};
|
|
|
+
|
|
|
+ let block = provider.get_block(BlockNumber::Latest).await?;
|
|
|
+ let block = block.ok_or(anyhow!("block was none"))?;
|
|
|
+ let block_timestamp = block.timestamp.as_u64();
|
|
|
+ let block_timestamp = i64::try_from(block_timestamp)?;
|
|
|
+ let block_number = block
|
|
|
+ .number
|
|
|
+ .ok_or(anyhow!("block number was none"))?
|
|
|
+ .as_u64();
|
|
|
+
|
|
|
metrics
|
|
|
- .block_timestamp_lag
|
|
|
- .get_or_create(&ChainIdLabel {
|
|
|
- chain_id: chain_id.clone(),
|
|
|
- })
|
|
|
- .set(lag);
|
|
|
+ .latest_block_timestamp
|
|
|
+ .get_or_create(&label)
|
|
|
+ .set(block_timestamp);
|
|
|
+
|
|
|
+ metrics
|
|
|
+ .latest_block_number
|
|
|
+ .get_or_create(&label)
|
|
|
+ .set(block_number as i64);
|
|
|
+
|
|
|
+ let server_timestamp = i64::try_from(SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs())?;
|
|
|
+
|
|
|
+ let lag = server_timestamp - block_timestamp;
|
|
|
+ metrics.block_timestamp_lag.get_or_create(&label).set(lag);
|
|
|
+
|
|
|
+ Ok(())
|
|
|
}
|
|
|
|
|
|
/// tracks the collected fees and the hashchain data of the given provider address on the given chain
|
|
|
-/// if there is a error the function will just return
|
|
|
#[tracing::instrument(skip_all)]
|
|
|
pub async fn track_provider(
|
|
|
chain_id: ChainId,
|
|
|
contract: InstrumentedPythContract,
|
|
|
provider_address: Address,
|
|
|
metrics: Arc<KeeperMetrics>,
|
|
|
-) {
|
|
|
- let provider_info = match contract.get_provider_info(provider_address).call().await {
|
|
|
- Ok(info) => info,
|
|
|
- Err(e) => {
|
|
|
- tracing::error!("Error while getting provider info. error: {:?}", e);
|
|
|
- return;
|
|
|
- }
|
|
|
- };
|
|
|
+) -> Result<()> {
|
|
|
+ let provider_info = contract.get_provider_info(provider_address).call().await?;
|
|
|
|
|
|
// The f64 conversion is made to be able to serve metrics with the constraints of Prometheus.
|
|
|
// The fee is in wei, so we divide by 1e18 to convert it to eth.
|
|
|
@@ -150,23 +141,18 @@ pub async fn track_provider(
|
|
|
address: provider_address.to_string(),
|
|
|
})
|
|
|
.set(end_sequence_number as i64);
|
|
|
+
|
|
|
+ Ok(())
|
|
|
}
|
|
|
|
|
|
/// tracks the accrued pyth fees on the given chain
|
|
|
-/// if there is an error the function will just return
|
|
|
#[tracing::instrument(skip_all)]
|
|
|
pub async fn track_accrued_pyth_fees(
|
|
|
chain_id: ChainId,
|
|
|
contract: InstrumentedPythContract,
|
|
|
metrics: Arc<KeeperMetrics>,
|
|
|
-) {
|
|
|
- let accrued_pyth_fees = match contract.get_accrued_pyth_fees().call().await {
|
|
|
- Ok(fees) => fees,
|
|
|
- Err(e) => {
|
|
|
- tracing::error!("Error while getting accrued pyth fees. error: {:?}", e);
|
|
|
- return;
|
|
|
- }
|
|
|
- };
|
|
|
+) -> Result<()> {
|
|
|
+ let accrued_pyth_fees = contract.get_accrued_pyth_fees().call().await?;
|
|
|
|
|
|
// The f64 conversion is made to be able to serve metrics with the constraints of Prometheus.
|
|
|
// The fee is in wei, so we divide by 1e18 to convert it to eth.
|
|
|
@@ -178,4 +164,6 @@ pub async fn track_accrued_pyth_fees(
|
|
|
chain_id: chain_id.clone(),
|
|
|
})
|
|
|
.set(accrued_pyth_fees);
|
|
|
+
|
|
|
+ Ok(())
|
|
|
}
|