Skip to content

Commit 3a22293

Browse files
authored
Merge pull request #325 from Alesfatalis/i316_claimable_rewards_metrics
Add claimable reward token metrics
2 parents 30034d9 + 389e837 commit 3a22293

File tree

3 files changed

+88
-4
lines changed

3 files changed

+88
-4
lines changed

core/src/box_kind/oracle_box.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,21 @@ impl CollectedOracleBox {
276276
&self.ergo_box
277277
}
278278

279+
pub fn reward_token(&self) -> SpecToken<RewardTokenId> {
280+
let token = self
281+
.get_box()
282+
.tokens
283+
.as_ref()
284+
.unwrap()
285+
.get(1)
286+
.unwrap()
287+
.clone();
288+
SpecToken {
289+
token_id: RewardTokenId::from_token_id_unchecked(token.token_id),
290+
amount: token.amount,
291+
}
292+
}
293+
279294
pub fn public_key(&self) -> EcPoint {
280295
self.ergo_box
281296
.get_register(NonMandatoryRegisterId::R4.into())

core/src/metrics.rs

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use prometheus::TextEncoder;
1616
use reqwest::StatusCode;
1717
use tower_http::cors::CorsLayer;
1818

19-
use crate::box_kind::PoolBox;
19+
use crate::box_kind::{OracleBox, PoolBox};
2020
use crate::monitor::check_oracle_health;
2121
use crate::monitor::check_pool_health;
2222
use crate::monitor::OracleHealth;
@@ -127,6 +127,21 @@ static MY_ORACLE_BOX_HEIGHT: Lazy<IntGaugeVec> = Lazy::new(|| {
127127
m
128128
});
129129

130+
static MY_ORACLE_CLAIMABLE_REWARDS: Lazy<IntGauge> = Lazy::new(|| {
131+
let m = IntGauge::with_opts(
132+
Opts::new(
133+
"oracle_claimable_rewards",
134+
"The amount of claimable rewards for this oracle",
135+
)
136+
.namespace("ergo")
137+
.subsystem("oracle"),
138+
)
139+
.unwrap();
140+
141+
prometheus::register(Box::new(m.clone())).expect("Failed to register");
142+
m
143+
});
144+
130145
static ALL_ORACLE_BOX_HEIGHT: Lazy<IntGaugeVec> = Lazy::new(|| {
131146
let m = IntGaugeVec::new(
132147
Opts::new(
@@ -142,6 +157,21 @@ static ALL_ORACLE_BOX_HEIGHT: Lazy<IntGaugeVec> = Lazy::new(|| {
142157
m
143158
});
144159

160+
static ALL_ORACLE_CLAIMABLE_REWARDS: Lazy<IntGaugeVec> = Lazy::new(|| {
161+
let m = IntGaugeVec::new(
162+
Opts::new(
163+
"all_oracle_claimable_rewards",
164+
"The amount of claimable rewards for all oracles",
165+
)
166+
.namespace("ergo")
167+
.subsystem("oracle"),
168+
&["oracle_address"],
169+
)
170+
.unwrap();
171+
prometheus::register(Box::new(m.clone())).expect("Failed to register");
172+
m
173+
});
174+
145175
static ACTIVE_ORACLE_BOX_HEIGHT: Lazy<IntGaugeVec> = Lazy::new(|| {
146176
let m = IntGaugeVec::new(
147177
Opts::new(
@@ -270,6 +300,40 @@ fn update_reward_tokens_in_buyback_box(oracle_pool: Arc<OraclePool>) {
270300
}
271301
}
272302

303+
fn update_oracle_claimable_reward_tokens(pool_health: &PoolHealth) {
304+
for oracle in &pool_health.details.all_oracle_boxes {
305+
let reward_tokens = oracle.reward_tokens;
306+
307+
if reward_tokens > 0 {
308+
let claimable_tokens = reward_tokens - 1;
309+
ALL_ORACLE_CLAIMABLE_REWARDS
310+
.with_label_values(&[&oracle.address.to_base58()])
311+
.set(claimable_tokens as i64);
312+
} else {
313+
ALL_ORACLE_CLAIMABLE_REWARDS
314+
.with_label_values(&[&oracle.address.to_base58()])
315+
.set(0);
316+
}
317+
}
318+
}
319+
320+
fn update_my_claimable_reward_tokens(oracle_pool: Arc<OraclePool>) {
321+
if let Some(oracle_box) = oracle_pool
322+
.get_local_datapoint_box_source()
323+
.get_local_oracle_datapoint_box()
324+
.ok()
325+
.flatten()
326+
{
327+
let num_tokens = *oracle_box.reward_token().amount.as_u64();
328+
if num_tokens == 0 {
329+
MY_ORACLE_CLAIMABLE_REWARDS.set(num_tokens as i64)
330+
} else {
331+
let claimable_tokens = num_tokens - 1;
332+
MY_ORACLE_CLAIMABLE_REWARDS.set(claimable_tokens as i64)
333+
}
334+
}
335+
}
336+
273337
pub fn update_metrics(oracle_pool: Arc<OraclePool>) -> Result<(), anyhow::Error> {
274338
let node_api = NodeApi::new(
275339
ORACLE_SECRETS.node_api_key.clone(),
@@ -302,7 +366,9 @@ pub fn update_metrics(oracle_pool: Arc<OraclePool>) -> Result<(), anyhow::Error>
302366
let wallet_balance: i64 = node_api.node.wallet_nano_ergs_balance()? as i64;
303367
ORACLE_NODE_WALLET_BALANCE.set(wallet_balance);
304368
POOL_BOX_REWARD_TOKEN_AMOUNT.set(pool_box.reward_token().amount.into());
305-
update_reward_tokens_in_buyback_box(oracle_pool);
369+
update_reward_tokens_in_buyback_box(oracle_pool.clone());
370+
update_my_claimable_reward_tokens(oracle_pool);
371+
update_oracle_claimable_reward_tokens(&pool_health);
306372
Ok(())
307373
}
308374

core/src/monitor.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub struct PoolHealthDetails {
5252
pub struct OracleDetails {
5353
pub address: NetworkAddress,
5454
pub box_height: OracleBoxDetails,
55+
pub reward_tokens: u64,
5556
}
5657

5758
#[derive(Debug, serde::Serialize)]
@@ -124,14 +125,16 @@ pub fn get_all_oracle_boxes(
124125
for b in posted_boxes {
125126
let detail = OracleDetails {
126127
address: NetworkAddress::new(network_prefix, &Address::P2Pk(b.public_key().into())),
127-
box_height: b.into(),
128+
box_height: b.clone().into(),
129+
reward_tokens: *b.reward_token().amount.as_u64(),
128130
};
129131
oracle_details.push(detail);
130132
}
131133
for b in collected_boxes {
132134
let detail = OracleDetails {
133135
address: NetworkAddress::new(network_prefix, &Address::P2Pk(b.public_key().into())),
134-
box_height: b.into(),
136+
box_height: b.clone().into(),
137+
reward_tokens: *b.reward_token().amount.as_u64(),
135138
};
136139
oracle_details.push(detail);
137140
}

0 commit comments

Comments
 (0)