From cee4603b554c9a422e6b00f91f9194c2adbc1c34 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Tue, 8 Aug 2023 16:18:07 -0700 Subject: [PATCH] add check_balance command --- Cargo.lock | 4 +- web3_proxy/Cargo.toml | 2 +- web3_proxy_cli/Cargo.toml | 2 +- web3_proxy_cli/src/main.rs | 10 +++++ .../src/sub_commands/check_balance.rs | 38 +++++++++++++++++++ web3_proxy_cli/src/sub_commands/mod.rs | 2 + 6 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 web3_proxy_cli/src/sub_commands/check_balance.rs diff --git a/Cargo.lock b/Cargo.lock index e1bec6fd..84848214 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7243,7 +7243,7 @@ dependencies = [ [[package]] name = "web3_proxy" -version = "1.42.4" +version = "1.42.5" dependencies = [ "anyhow", "arc-swap", @@ -7324,7 +7324,7 @@ dependencies = [ [[package]] name = "web3_proxy_cli" -version = "1.42.4" +version = "1.42.5" dependencies = [ "env_logger", "parking_lot", diff --git a/web3_proxy/Cargo.toml b/web3_proxy/Cargo.toml index ff76f665..ee688573 100644 --- a/web3_proxy/Cargo.toml +++ b/web3_proxy/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "web3_proxy" -version = "1.42.4" +version = "1.42.5" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/web3_proxy_cli/Cargo.toml b/web3_proxy_cli/Cargo.toml index 82540aa1..899ee14e 100644 --- a/web3_proxy_cli/Cargo.toml +++ b/web3_proxy_cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "web3_proxy_cli" -version = "1.42.4" +version = "1.42.5" edition = "2021" default-run = "web3_proxy_cli" diff --git a/web3_proxy_cli/src/main.rs b/web3_proxy_cli/src/main.rs index 36174149..8354b244 100644 --- a/web3_proxy_cli/src/main.rs +++ b/web3_proxy_cli/src/main.rs @@ -65,6 +65,7 @@ enum SubCommand { ChangeUserTier(sub_commands::ChangeUserTierSubCommand), ChangeUserTierByAddress(sub_commands::ChangeUserTierByAddressSubCommand), ChangeUserTierByKey(sub_commands::ChangeUserTierByKeySubCommand), + CheckBalance(sub_commands::CheckBalanceSubCommand), CheckConfig(sub_commands::CheckConfigSubCommand), CountUsers(sub_commands::CountUsersSubCommand), CreateKey(sub_commands::CreateKeySubCommand), @@ -349,6 +350,15 @@ fn main() -> anyhow::Result<()> { x.main(&db_conn).await } + SubCommand::CheckBalance(x) => { + let db_url = cli_config + .db_url + .expect("'--config' (with a db) or '--db-url' is required to run change_user_addres"); + + let db_conn = connect_db(db_url, 1, 1).await?; + + x.main(&db_conn).await + } SubCommand::CheckConfig(x) => x.main().await, SubCommand::CreateKey(x) => { let db_url = cli_config diff --git a/web3_proxy_cli/src/sub_commands/check_balance.rs b/web3_proxy_cli/src/sub_commands/check_balance.rs new file mode 100644 index 00000000..6ad7f23d --- /dev/null +++ b/web3_proxy_cli/src/sub_commands/check_balance.rs @@ -0,0 +1,38 @@ +use web3_proxy::balance::Balance; +use web3_proxy::prelude::anyhow::{self, Context}; +use web3_proxy::prelude::argh::{self, FromArgs}; +use web3_proxy::prelude::entities::user; +use web3_proxy::prelude::ethers::types::Address; +use web3_proxy::prelude::migration::sea_orm::{ + ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter, +}; +use web3_proxy::prelude::serde_json::json; +use web3_proxy::prelude::tracing::debug; + +/// change a user's tier. +#[derive(FromArgs, PartialEq, Eq, Debug)] +#[argh(subcommand, name = "check_balance")] +pub struct CheckBalanceSubCommand { + #[argh(positional)] + /// the address of the user you want to check. + user_address: Address, +} + +impl CheckBalanceSubCommand { + pub async fn main(self, db_conn: &DatabaseConnection) -> anyhow::Result<()> { + // use the address to get the user + let user = user::Entity::find() + .filter(user::Column::Address.eq(self.user_address.as_bytes())) + .one(db_conn) + .await? + .context("No user found with that key")?; + + // TODO: don't serialize the rpc key + debug!("user: {:#}", json!(&user)); + + let balance = Balance::try_from_db(db_conn, user.id).await?; + debug!("balance: {:#}", json!(&balance)); + + Ok(()) + } +} diff --git a/web3_proxy_cli/src/sub_commands/mod.rs b/web3_proxy_cli/src/sub_commands/mod.rs index 3e8489a8..f4fd242a 100644 --- a/web3_proxy_cli/src/sub_commands/mod.rs +++ b/web3_proxy_cli/src/sub_commands/mod.rs @@ -3,6 +3,7 @@ mod change_user_address; mod change_user_tier; mod change_user_tier_by_address; mod change_user_tier_by_key; +mod check_balance; mod check_config; mod count_users; mod create_key; @@ -26,6 +27,7 @@ pub use self::change_user_address::ChangeUserAddressSubCommand; pub use self::change_user_tier::ChangeUserTierSubCommand; pub use self::change_user_tier_by_address::ChangeUserTierByAddressSubCommand; pub use self::change_user_tier_by_key::ChangeUserTierByKeySubCommand; +pub use self::check_balance::CheckBalanceSubCommand; pub use self::check_config::CheckConfigSubCommand; pub use self::count_users::CountUsersSubCommand; pub use self::create_key::CreateKeySubCommand;