From 84517ed5a593402c7ed9d41451b64af92f179e56 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Wed, 28 Dec 2022 08:43:44 -0800 Subject: [PATCH] add change_user_tier_by_address subcommand --- .../change_user_tier_by_address.rs | 62 +++++++++++++++++++ web3_proxy/src/bin/web3_proxy_cli/main.rs | 9 ++- 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 web3_proxy/src/bin/web3_proxy_cli/change_user_tier_by_address.rs diff --git a/web3_proxy/src/bin/web3_proxy_cli/change_user_tier_by_address.rs b/web3_proxy/src/bin/web3_proxy_cli/change_user_tier_by_address.rs new file mode 100644 index 00000000..a38e350e --- /dev/null +++ b/web3_proxy/src/bin/web3_proxy_cli/change_user_tier_by_address.rs @@ -0,0 +1,62 @@ +use anyhow::Context; +use argh::FromArgs; +use entities::{rpc_key, user, user_tier}; +use log::{debug, info}; +use migration::sea_orm::{ + self, ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, IntoActiveModel, + QueryFilter, +}; +use uuid::Uuid; +use ethers::types::Address; + +/// change a user's tier. +#[derive(FromArgs, PartialEq, Eq, Debug)] +#[argh(subcommand, name = "change_user_tier_by_address")] +pub struct ChangeUserTierByAddressCommand { + #[argh(positional)] + /// the address of the user you want to change. + user_address: Address, + + /// the title of the desired user tier. + #[argh(positional)] + user_tier_title: String, +} + +impl ChangeUserTierByAddressCommand { + pub async fn main(self, db_conn: &DatabaseConnection) -> anyhow::Result<()> { + let address: Vec = self.user_address.to_fixed_bytes().into(); + + // use the address to get the user + let user = user::Entity::find() + .filter(user::Column::Address.eq(address)) + .one(db_conn) + .await? + .context("No user found with that key")?; + + // TODO: don't serialize the rpc key + debug!("user: {:#?}", user); + + // use the title to get the user tier + let user_tier = user_tier::Entity::find() + .filter(user_tier::Column::Title.eq(self.user_tier_title)) + .one(db_conn) + .await? + .context("No user tier found with that name")?; + + debug!("user_tier: {:#?}", user_tier); + + if user.user_tier_id == user_tier.id { + info!("user already has that tier"); + } else { + let mut user = user.into_active_model(); + + user.user_tier_id = sea_orm::Set(user_tier.id); + + user.save(db_conn).await?; + + info!("user's tier changed"); + } + + Ok(()) + } +} diff --git a/web3_proxy/src/bin/web3_proxy_cli/main.rs b/web3_proxy/src/bin/web3_proxy_cli/main.rs index 07696398..28782625 100644 --- a/web3_proxy/src/bin/web3_proxy_cli/main.rs +++ b/web3_proxy/src/bin/web3_proxy_cli/main.rs @@ -1,6 +1,7 @@ mod change_user_address; mod change_user_address_by_key; mod change_user_tier; +mod change_user_tier_by_address; mod change_user_tier_by_key; mod check_config; mod cost_calculator; @@ -43,9 +44,10 @@ enum SubCommand { ChangeUserAddress(change_user_address::ChangeUserAddressCommand), ChangeUserAddressByKey(change_user_address_by_key::ChangeUserAddressByKeyCommand), ChangeUserTier(change_user_tier::ChangeUserTierCommand), + ChangeUserTierByAddress(change_user_tier_by_address::ChangeUserTierByAddressCommand), ChangeUserTierByKey(change_user_tier_by_key::ChangeUserTierByKeyCommand), - CostCalculatorCommand(cost_calculator::CostCalculatorCommand), CheckConfig(check_config::CheckConfigSubCommand), + CostCalculatorCommand(cost_calculator::CostCalculatorCommand), CreateUser(create_user::CreateUserSubCommand), DropMigrationLock(drop_migration_lock::DropMigrationLockSubCommand), HealthCompass(health_compass::HealthCompassSubCommand), @@ -101,6 +103,11 @@ async fn main() -> anyhow::Result<()> { x.main(&db_conn).await } + SubCommand::ChangeUserTierByAddress(x) => { + let db_conn = get_db(cli_config.db_url, 1, 1).await?; + + x.main(&db_conn).await + } SubCommand::ChangeUserTierByKey(x) => { let db_conn = get_db(cli_config.db_url, 1, 1).await?;