Browse Source

collected fee

0xfirefist 1 year ago
parent
commit
75fb482de7
2 changed files with 48 additions and 0 deletions
  1. 37 0
      apps/fortuna/src/command/run.rs
  2. 11 0
      apps/fortuna/src/metrics.rs

+ 37 - 0
apps/fortuna/src/command/run.rs

@@ -262,6 +262,11 @@ pub async fn run(opts: &RunOptions) -> Result<()> {
         opts.provider.clone(),
         metrics_registry.clone(),
     ));
+    spawn(track_collected_fee(
+        config.clone(),
+        opts.provider.clone(),
+        metrics_registry.clone(),
+    ));
     run_api(opts.addr.clone(), chains, metrics_registry.clone(), rx_exit).await?;
 
     Ok(())
@@ -297,6 +302,38 @@ pub async fn track_balance(
     }
 }
 
+pub async fn track_collected_fee(
+    config: Config,
+    provider_address: Address,
+    metrics_registry: Arc<metrics::Metrics>,
+) {
+    loop {
+        for (chain_id, chain_config) in &config.chains {
+            let contract = match PythContract::from_config(chain_config) {
+                Ok(r) => r,
+                Err(_e) => continue,
+            };
+
+            let provider_info = match contract.get_provider_info(provider_address).call().await {
+                Ok(info) => info,
+                Err(_e) => {
+                    time::sleep(Duration::from_secs(5)).await;
+                    continue;
+                }
+            };
+            let collected_fee = provider_info.accrued_fees_in_wei as f64 / 1e18;
+
+            metrics_registry
+                .collected_fee
+                .get_or_create(&ProviderLabel {
+                    chain_id: chain_id.clone(),
+                    address:  provider_address.to_string(),
+                })
+                .set(collected_fee);
+        }
+    }
+}
+
 
 pub async fn track_hashchain(
     config: Config,

+ 11 - 0
apps/fortuna/src/metrics.rs

@@ -38,6 +38,7 @@ pub struct Metrics {
     pub current_sequence_number: Family<ProviderLabel, Gauge>,
     pub end_sequence_number:     Family<ProviderLabel, Gauge>,
     pub balance:                 Family<ProviderLabel, Gauge<f64, AtomicU64>>,
+    pub collected_fee:           Family<ProviderLabel, Gauge<f64, AtomicU64>>,
     //
     pub requests:                Family<ProviderLabel, Counter>,
     pub requests_processed:      Family<ProviderLabel, Counter>,
@@ -119,6 +120,15 @@ impl Metrics {
             balance.clone(),
         );
 
+        let collected_fee = Family::<ProviderLabel, Gauge<f64, AtomicU64>>::default();
+        metrics_registry.register(
+            // With the metric name.
+            "collected_fee",
+            // And the metric help text.
+            "Collected fee on the contract",
+            collected_fee.clone(),
+        );
+
         Metrics {
             registry: RwLock::new(metrics_registry),
             request_counter: http_requests,
@@ -128,6 +138,7 @@ impl Metrics {
             requests_processed,
             reveals,
             balance,
+            collected_fee,
         }
     }
 }