Skip to content

Commit

Permalink
Add historical balance command
Browse files Browse the repository at this point in the history
  • Loading branch information
madninja committed Sep 7, 2023
1 parent 0b99fd8 commit e12db2a
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
74 changes: 74 additions & 0 deletions src/cmd/balance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use crate::{
cmd::{Format, Opts},
BlockSpan, Result,
};
use chrono::NaiveDate;
use futures::stream::{self, StreamExt, TryStreamExt};
use sqlx::postgres::PgPool;
use std::result::Result as StdResult;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
/// Gets the balance of the given account at the given date
pub struct Cmd {
/// The wallet address to look up the balance for
account: String,

/// One or more end dates (exclusive) to run the report over (in UTC). The
/// end time is at the beginning midnight of the given date (00:00:00).
end: Vec<NaiveDate>,

#[structopt(long, default_value)]
format: Format,
}

#[derive(Debug, serde::Serialize, sqlx::FromRow)]
pub struct Balance {
#[sqlx(default)]
#[serde(skip_serializing_if = "Option::is_none")]
date: Option<NaiveDate>,
block: i64,
hnt: f64,
staked_hnt: f64,
}

const BALANCE_QUERY: &str = r#"
select
$2 as block,
greatest(0, max(balance))::float8 / 100000000 as hnt,
greatest(0, max(staked_balance))::float8 / 100000000 as staked_hnt
from accounts
where
address = $1 and
block > $2 and
block <= $3
"#;

impl Cmd {
pub async fn run(&self, pool: &PgPool, _opts: Opts) -> Result {
let supplies = stream::iter(self.end.clone())
.map(|end| Ok(fetch_balance(pool, &self.account, end)))
.try_buffered(10)
.boxed();

self.format.output(std::io::stdout(), supplies).await?;
Ok(())
}
}

async fn fetch_balance(
pool: &PgPool,
account: &str,
end: NaiveDate,
) -> StdResult<Balance, sqlx::Error> {
let start = end - chrono::Duration::days(1);
let blockspan = BlockSpan::for_date_range(pool, start, end).await?;
let mut balance: Balance = sqlx::query_as::<_, Balance>(BALANCE_QUERY)
.bind(account)
.bind(blockspan.low)
.bind(blockspan.high)
.fetch_one(pool)
.await?;
balance.date = Some(end);
Ok(balance)
}
1 change: 1 addition & 0 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use structopt::StructOpt;

pub mod blocks;
// pub mod flow;
pub mod balance;
pub mod hotspots;
pub mod rewards;
pub mod supply;
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use etl_exporter::{
cmd::{blocks, hotspots, rewards, supply, Opts},
cmd::{balance, blocks, hotspots, rewards, supply, Opts},
Result,
};
use sqlx::postgres::PgPool;
Expand All @@ -21,6 +21,7 @@ pub enum Cmd {
Rewards(rewards::Cmd),
Hotspots(hotspots::Cmd),
Supply(supply::Cmd),
Balance(balance::Cmd),
}

#[tokio::main]
Expand All @@ -45,5 +46,6 @@ async fn run(cli: Cli) -> Result {
Cmd::Rewards(cmd) => cmd.run(&pool, cli.opts).await,
Cmd::Hotspots(cmd) => cmd.run(&pool, cli.opts).await,
Cmd::Supply(cmd) => cmd.run(&pool, cli.opts).await,
Cmd::Balance(cmd) => cmd.run(&pool, cli.opts).await,
}
}

0 comments on commit e12db2a

Please sign in to comment.