Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add claimable reward token metrics #325

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(), &ORACLE_CONFIG.node_url);
let current_height = (node_api.node.current_block_height()? as u32).into();
Expand Down Expand Up @@ -298,7 +362,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
Loading