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;
10use sui_rpc::proto::sui::rpc::v2::GetCoinInfoRequest;
11
12use tokio::time::Duration;
13use tracing::{error, info};
14
15pub struct TotalSupplies {
16    sui_client: sui_rpc::Client,
17    coins: BTreeMap<String, String>,
18    metric: IntGaugeVec,
19}
20
21impl TotalSupplies {
22    pub fn new(
23        sui_client: sui_rpc::Client,
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                .clone()
46                .state_client()
47                .get_coin_info(GetCoinInfoRequest::default().with_coin_type(coin_type))
48                .await;
49            match resp {
50                Ok(resp) => {
51                    let supply = resp.into_inner().treasury().total_supply();
52                    self.metric
53                        .with_label_values(&[coin_name])
54                        .set(supply as i64);
55                    info!("Total supply for {coin_type}: {}", supply);
56                }
57                Err(e) => {
58                    error!("Error getting total supply for coin {coin_type}: {:?}", e);
59                }
60            }
61        }
62    }
63
64    fn interval(&self) -> Duration {
65        Duration::from_secs(10)
66    }
67}