Skip to content

Commit

Permalink
Merge pull request #325 from Alesfatalis/i316_claimable_rewards_metrics
Browse files Browse the repository at this point in the history
Add claimable reward token metrics
  • Loading branch information
kushti authored Feb 5, 2024
2 parents 30034d9 + 389e837 commit 3a22293
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
15 changes: 15 additions & 0 deletions core/src/box_kind/oracle_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,21 @@ impl CollectedOracleBox {
&self.ergo_box
}

pub fn reward_token(&self) -> SpecToken<RewardTokenId> {
let token = self
.get_box()
.tokens
.as_ref()
.unwrap()
.get(1)
.unwrap()
.clone();
SpecToken {
token_id: RewardTokenId::from_token_id_unchecked(token.token_id),
amount: token.amount,
}
}

pub fn public_key(&self) -> EcPoint {
self.ergo_box
.get_register(NonMandatoryRegisterId::R4.into())
Expand Down
70 changes: 68 additions & 2 deletions core/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use prometheus::TextEncoder;
use reqwest::StatusCode;
use tower_http::cors::CorsLayer;

use crate::box_kind::PoolBox;
use crate::box_kind::{OracleBox, PoolBox};
use crate::monitor::check_oracle_health;
use crate::monitor::check_pool_health;
use crate::monitor::OracleHealth;
Expand Down Expand Up @@ -127,6 +127,21 @@ static MY_ORACLE_BOX_HEIGHT: Lazy<IntGaugeVec> = Lazy::new(|| {
m
});

static MY_ORACLE_CLAIMABLE_REWARDS: Lazy<IntGauge> = Lazy::new(|| {
let m = IntGauge::with_opts(
Opts::new(
"oracle_claimable_rewards",
"The amount of claimable rewards for this oracle",
)
.namespace("ergo")
.subsystem("oracle"),
)
.unwrap();

prometheus::register(Box::new(m.clone())).expect("Failed to register");
m
});

static ALL_ORACLE_BOX_HEIGHT: Lazy<IntGaugeVec> = Lazy::new(|| {
let m = IntGaugeVec::new(
Opts::new(
Expand All @@ -142,6 +157,21 @@ static ALL_ORACLE_BOX_HEIGHT: Lazy<IntGaugeVec> = Lazy::new(|| {
m
});

static ALL_ORACLE_CLAIMABLE_REWARDS: Lazy<IntGaugeVec> = Lazy::new(|| {
let m = IntGaugeVec::new(
Opts::new(
"all_oracle_claimable_rewards",
"The amount of claimable rewards for all oracles",
)
.namespace("ergo")
.subsystem("oracle"),
&["oracle_address"],
)
.unwrap();
prometheus::register(Box::new(m.clone())).expect("Failed to register");
m
});

static ACTIVE_ORACLE_BOX_HEIGHT: Lazy<IntGaugeVec> = Lazy::new(|| {
let m = IntGaugeVec::new(
Opts::new(
Expand Down Expand Up @@ -270,6 +300,40 @@ fn update_reward_tokens_in_buyback_box(oracle_pool: Arc<OraclePool>) {
}
}

fn update_oracle_claimable_reward_tokens(pool_health: &PoolHealth) {
for oracle in &pool_health.details.all_oracle_boxes {
let reward_tokens = oracle.reward_tokens;

if reward_tokens > 0 {
let claimable_tokens = reward_tokens - 1;
ALL_ORACLE_CLAIMABLE_REWARDS
.with_label_values(&[&oracle.address.to_base58()])
.set(claimable_tokens as i64);
} else {
ALL_ORACLE_CLAIMABLE_REWARDS
.with_label_values(&[&oracle.address.to_base58()])
.set(0);
}
}
}

fn update_my_claimable_reward_tokens(oracle_pool: Arc<OraclePool>) {
if let Some(oracle_box) = oracle_pool
.get_local_datapoint_box_source()
.get_local_oracle_datapoint_box()
.ok()
.flatten()
{
let num_tokens = *oracle_box.reward_token().amount.as_u64();
if num_tokens == 0 {
MY_ORACLE_CLAIMABLE_REWARDS.set(num_tokens as i64)
} else {
let claimable_tokens = num_tokens - 1;
MY_ORACLE_CLAIMABLE_REWARDS.set(claimable_tokens as i64)
}
}
}

pub fn update_metrics(oracle_pool: Arc<OraclePool>) -> Result<(), anyhow::Error> {
let node_api = NodeApi::new(
ORACLE_SECRETS.node_api_key.clone(),
Expand Down Expand Up @@ -302,7 +366,9 @@ pub fn update_metrics(oracle_pool: Arc<OraclePool>) -> Result<(), anyhow::Error>
let wallet_balance: i64 = node_api.node.wallet_nano_ergs_balance()? as i64;
ORACLE_NODE_WALLET_BALANCE.set(wallet_balance);
POOL_BOX_REWARD_TOKEN_AMOUNT.set(pool_box.reward_token().amount.into());
update_reward_tokens_in_buyback_box(oracle_pool);
update_reward_tokens_in_buyback_box(oracle_pool.clone());
update_my_claimable_reward_tokens(oracle_pool);
update_oracle_claimable_reward_tokens(&pool_health);
Ok(())
}

Expand Down
7 changes: 5 additions & 2 deletions core/src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub struct PoolHealthDetails {
pub struct OracleDetails {
pub address: NetworkAddress,
pub box_height: OracleBoxDetails,
pub reward_tokens: u64,
}

#[derive(Debug, serde::Serialize)]
Expand Down Expand Up @@ -124,14 +125,16 @@ pub fn get_all_oracle_boxes(
for b in posted_boxes {
let detail = OracleDetails {
address: NetworkAddress::new(network_prefix, &Address::P2Pk(b.public_key().into())),
box_height: b.into(),
box_height: b.clone().into(),
reward_tokens: *b.reward_token().amount.as_u64(),
};
oracle_details.push(detail);
}
for b in collected_boxes {
let detail = OracleDetails {
address: NetworkAddress::new(network_prefix, &Address::P2Pk(b.public_key().into())),
box_height: b.into(),
box_height: b.clone().into(),
reward_tokens: *b.reward_token().amount.as_u64(),
};
oracle_details.push(detail);
}
Expand Down

0 comments on commit 3a22293

Please sign in to comment.