sui_bridge/sui_bridge_watchdog/
total_supplies.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The SuiBridgeStatus observable monitors whether the Sui Bridge is paused.
5
6use crate::sui_bridge_watchdog::Observable;
7use async_trait::async_trait;
8use prometheus::IntGaugeVec;
9use std::{collections::BTreeMap, sync::Arc};
10use sui_sdk::SuiClient;
11
12use tokio::time::Duration;
13use tracing::{error, info};
14
15pub struct TotalSupplies {
16    sui_client: Arc<SuiClient>,
17    coins: BTreeMap<String, String>,
18    metric: IntGaugeVec,
19}
20
21impl TotalSupplies {
22    pub fn new(
23        sui_client: Arc<SuiClient>,
24        coins: BTreeMap<String, String>,
25        metric: IntGaugeVec,
26    ) -> Self {
27        Self {
28            sui_client,
29            coins,
30            metric,
31        }
32    }
33}
34
35#[async_trait]
36impl Observable for TotalSupplies {
37    fn name(&self) -> &str {
38        "TotalSupplies"
39    }
40
41    async fn observe_and_report(&self) {
42        for (coin_name, coin_type) in &self.coins {
43            let resp = self
44                .sui_client
45                .coin_read_api()
46                .get_total_supply(coin_type.clone())
47                .await;
48            match resp {
49                Ok(supply) => {
50                    self.metric
51                        .with_label_values(&[coin_name])
52                        .set(supply.value as i64);
53                    info!("Total supply for {coin_type}: {}", supply.value);
54                }
55                Err(e) => {
56                    error!("Error getting total supply for coin {coin_type}: {:?}", e);
57                }
58            }
59        }
60    }
61
62    fn interval(&self) -> Duration {
63        Duration::from_secs(10)
64    }
65}