Skip to content

Commit

Permalink
add new feature native balance
Browse files Browse the repository at this point in the history
  • Loading branch information
tommady committed Aug 16, 2022
1 parent b874886 commit 27cc530
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ This exporter has below custom metrics right now!
| NetworkFunctional | subtraction of seconds of the latest block time with the current time |
| TotalCountOfValidators | the total number of validators from the consensus network |
| TotalBalanceOfRelayers | the total balance value of relayers from the specific bridge ( this metric displays with 6 digits as the fractional part so need to divide the metric value by 10 to the power of 6 ) |
| BridgedBalance | the token balance of reserving safe on source chain ( this metric displays with 6 digits as the fractional part so need to divide the metric value by 10 to the power of 6 ) |
| BridgedBalance | the specific token balance of reserving safe on source chain ( this metric displays with 6 digits as the fractional part so need to divide the metric value by 10 to the power of 6 ) |
| BridgedSupply | the token supply total minted on the destination chain ( this metric displays with 6 digits as the fractional part so need to divide the metric value by 10 to the power of 6 ) |
| NavtiveBalance | the native balance of reserving safe on source chain ( this metric displays with 6 digits as the fractional part so need to divide the metric value by 10 to the power of 6 ) |

## Installation

Expand Down
5 changes: 5 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub(crate) enum TaskName {
TotalBalanceOfRelayers,
BridgedBalance,
BridgedSupply,
NativeBalance,
}

impl Default for TaskName {
Expand All @@ -98,6 +99,10 @@ pub(crate) enum ExtraOpts {
token_address: String,
decimal: usize,
},
NativeBalance {
native_address: String,
decimal: usize,
},
}

#[derive(Debug, Default, Serialize, Deserialize)]
Expand Down
7 changes: 7 additions & 0 deletions src/crawler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ impl Crawler {
target.extra_opts.clone(),
crate::tasks::bridged_supply,
),
TaskName::NativeBalance => Task::new(
"native_balance".to_string(),
target.host_addr.clone(),
metric,
target.extra_opts.clone(),
crate::tasks::native_balance,
),
};

tasks.push(Arc::new(task));
Expand Down
5 changes: 5 additions & 0 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ where
"the token supply total minted on the destination chain",
)
.context("new bridged_supply failed")?,
TaskName::NativeBalance => GenericGauge::new(
"native_balance",
"the native balance of reserving safe on source chain",
)
.context("new native_balance failed")?,
};

registry
Expand Down
2 changes: 2 additions & 0 deletions src/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ mod bridged_balance;
pub(crate) use bridged_balance::bridged_balance;
mod bridged_supply;
pub(crate) use bridged_supply::bridged_supply;
mod native_balance;
pub(crate) use native_balance::native_balance;
103 changes: 103 additions & 0 deletions src/tasks/native_balance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use crate::config::ExtraOpts;
use crate::utils::{diff_of_decimal_18, toi64_div_10pow12};

use anyhow::{bail, Context, Result};
use prometheus::core::Number;
use serde_json::Value;

pub(crate) fn native_balance<N: Number>(addr: &str, opts: &Option<ExtraOpts>) -> Result<N> {
let (native_addr, decimal) = match opts {
Some(ExtraOpts::NativeBalance {
native_address,
decimal,
}) => (native_address, decimal),
_ => {
bail!(
"expecting extra_opts: native_address and decimal, addr:{:?}",
addr
)
}
};

let data: Value = ureq::post(addr)
.send_json(ureq::json!({
"method":"eth_getBalance",
"jsonrpc":"2.0",
"id":0,
"params":[
native_addr,
"latest"
],
}))
.with_context(|| {
format!(
"requesting eth_getBalance ureq call failed, addr:{:?}, opts:{:?}",
addr, opts
)
})?
.into_json()
.with_context(|| {
format!(
"requesting eth_getBalance ureq json failed, addr:{:?}, opts:{:?}",
addr, opts
)
})?;

let balance = &data["result"];
if balance.is_null() {
bail!("the balance is null, addr:{:?}, opts:{:?}", addr, opts)
}

let balance = match balance.as_str() {
Some(v) => {
let mut b = v.trim_start_matches("0x");
if b.is_empty() {
b = "0"
};
u128::from_str_radix(b, 16).with_context(|| {
format!(
"balance parse hex failed: {}, addr:{:?}, opts:{:?}",
v, addr, opts
)
})?
}
None => bail!(
"the balance result is not a str: {}, addr:{:?}, opts:{:?}",
balance,
addr,
opts
),
};

Ok(N::from_i64(toi64_div_10pow12(
balance,
diff_of_decimal_18(decimal),
)))
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_native_balance() {
println!(
"{:?}",
native_balance::<u64>(
"https://data-seed-prebsc-1-s1.binance.org:8545",
&Some(ExtraOpts::NativeBalance {
native_address: "0xae13d989dac2f0debff460ac112a837c89baa7cd".to_string(),
decimal: 18,
}),
)
);
assert!(native_balance::<u64>(
"https://data-seed-prebsc-1-s1.binance.org:8545",
&Some(ExtraOpts::NativeBalance {
native_address: "0xae13d989dac2f0debff460ac112a837c89baa7cd".to_string(),
decimal: 18,
}),
)
.is_ok())
}
}

0 comments on commit 27cc530

Please sign in to comment.