change user address commands
This commit is contained in:
parent
c8d4e1a653
commit
67e4959e34
@ -288,7 +288,7 @@ mod tests {
|
|||||||
public_requests_per_period: Some(1_000_000),
|
public_requests_per_period: Some(1_000_000),
|
||||||
response_cache_max_bytes: 10_usize.pow(7),
|
response_cache_max_bytes: 10_usize.pow(7),
|
||||||
redirect_public_url: Some("example.com/".to_string()),
|
redirect_public_url: Some("example.com/".to_string()),
|
||||||
redirect_rpc_key_url: Some("example.com/{rpc_key_id}".to_string()),
|
redirect_rpc_key_url: Some("example.com/{{rpc_key_id}}".to_string()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
balanced_rpcs: HashMap::from([
|
balanced_rpcs: HashMap::from([
|
||||||
|
56
web3_proxy/src/bin/web3_proxy_cli/change_user_address.rs
Normal file
56
web3_proxy/src/bin/web3_proxy_cli/change_user_address.rs
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
use anyhow::Context;
|
||||||
|
use argh::FromArgs;
|
||||||
|
use entities::user;
|
||||||
|
use ethers::types::Address;
|
||||||
|
use log::{debug, info};
|
||||||
|
use migration::sea_orm::{
|
||||||
|
self, ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, IntoActiveModel,
|
||||||
|
QueryFilter,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// change a user's address.
|
||||||
|
#[derive(FromArgs, PartialEq, Eq, Debug)]
|
||||||
|
#[argh(subcommand, name = "change_user_address")]
|
||||||
|
pub struct ChangeUserAddressCommand {
|
||||||
|
/// the address of the user you want to change
|
||||||
|
#[argh(positional)]
|
||||||
|
old_address: String,
|
||||||
|
|
||||||
|
/// the address of the user you want to change
|
||||||
|
#[argh(positional)]
|
||||||
|
new_address: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ChangeUserAddressCommand {
|
||||||
|
pub async fn main(self, db_conn: &DatabaseConnection) -> anyhow::Result<()> {
|
||||||
|
let old_address: Address = self.old_address.parse()?;
|
||||||
|
let new_address: Address = self.new_address.parse()?;
|
||||||
|
|
||||||
|
let old_address: Vec<u8> = old_address.to_fixed_bytes().into();
|
||||||
|
let new_address: Vec<u8> = new_address.to_fixed_bytes().into();
|
||||||
|
|
||||||
|
let u = user::Entity::find()
|
||||||
|
.filter(user::Column::Address.eq(old_address))
|
||||||
|
.one(db_conn)
|
||||||
|
.await?
|
||||||
|
.context("No user found with that address")?;
|
||||||
|
|
||||||
|
debug!("initial user: {:#?}", u);
|
||||||
|
|
||||||
|
if u.address == new_address {
|
||||||
|
info!("user already has this address");
|
||||||
|
} else {
|
||||||
|
let mut u = u.into_active_model();
|
||||||
|
|
||||||
|
u.address = sea_orm::Set(new_address);
|
||||||
|
|
||||||
|
let u = u.save(db_conn).await?;
|
||||||
|
|
||||||
|
info!("changed user address");
|
||||||
|
|
||||||
|
debug!("updated user: {:#?}", u);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,67 @@
|
|||||||
|
use anyhow::Context;
|
||||||
|
use argh::FromArgs;
|
||||||
|
use entities::{rpc_key, user};
|
||||||
|
use ethers::types::Address;
|
||||||
|
use log::{debug, info};
|
||||||
|
use migration::sea_orm::{
|
||||||
|
self, ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, IntoActiveModel,
|
||||||
|
QueryFilter,
|
||||||
|
};
|
||||||
|
use uuid::Uuid;
|
||||||
|
use web3_proxy::frontend::authorization::RpcSecretKey;
|
||||||
|
|
||||||
|
/// change a user's tier.
|
||||||
|
#[derive(FromArgs, PartialEq, Eq, Debug)]
|
||||||
|
#[argh(subcommand, name = "change_user_address_by_key")]
|
||||||
|
pub struct ChangeUserAddressByKeyCommand {
|
||||||
|
#[argh(positional)]
|
||||||
|
/// the RPC key owned by the user you want to change.
|
||||||
|
rpc_secret_key: RpcSecretKey,
|
||||||
|
|
||||||
|
/// the new address for the user.
|
||||||
|
#[argh(positional)]
|
||||||
|
new_address: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ChangeUserAddressByKeyCommand {
|
||||||
|
pub async fn main(self, db_conn: &DatabaseConnection) -> anyhow::Result<()> {
|
||||||
|
let rpc_secret_key: Uuid = self.rpc_secret_key.into();
|
||||||
|
|
||||||
|
let new_address: Address = self.new_address.parse()?;
|
||||||
|
|
||||||
|
let new_address: Vec<u8> = new_address.to_fixed_bytes().into();
|
||||||
|
|
||||||
|
let uk = rpc_key::Entity::find()
|
||||||
|
.filter(rpc_key::Column::SecretKey.eq(rpc_secret_key))
|
||||||
|
.one(db_conn)
|
||||||
|
.await?
|
||||||
|
.context("No key found")?;
|
||||||
|
|
||||||
|
debug!("user key: {:#?}", uk);
|
||||||
|
|
||||||
|
// use the rpc secret key to get the user
|
||||||
|
// TODO: get this with a join on rpc_key
|
||||||
|
let u = user::Entity::find_by_id(uk.user_id)
|
||||||
|
.one(db_conn)
|
||||||
|
.await?
|
||||||
|
.context("No user found with that key")?;
|
||||||
|
|
||||||
|
debug!("user: {:#?}", u);
|
||||||
|
|
||||||
|
if u.address == new_address {
|
||||||
|
info!("user already has that address");
|
||||||
|
} else {
|
||||||
|
let mut u = u.into_active_model();
|
||||||
|
|
||||||
|
u.address = sea_orm::Set(new_address);
|
||||||
|
|
||||||
|
let u = u.save(db_conn).await?;
|
||||||
|
|
||||||
|
debug!("user: {:#?}", u);
|
||||||
|
|
||||||
|
info!("user's address changed");
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
@ -73,7 +73,7 @@ impl CheckConfigSubCommand {
|
|||||||
warn!("app.redirect_user_url is None. Registered users will get an error page instead of a redirect")
|
warn!("app.redirect_user_url is None. Registered users will get an error page instead of a redirect")
|
||||||
}
|
}
|
||||||
Some(x) => {
|
Some(x) => {
|
||||||
if !x.contains("{rpc_key_id}") {
|
if !x.contains("{{rpc_key_id}}") {
|
||||||
num_errors += 1;
|
num_errors += 1;
|
||||||
error!("redirect_user_url user url must contain \"{{rpc_key_id}}\"")
|
error!("redirect_user_url user url must contain \"{{rpc_key_id}}\"")
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
mod change_user_address;
|
||||||
|
mod change_user_address_by_key;
|
||||||
mod change_user_tier;
|
mod change_user_tier;
|
||||||
mod change_user_tier_by_key;
|
mod change_user_tier_by_key;
|
||||||
mod check_config;
|
mod check_config;
|
||||||
@ -37,6 +39,8 @@ pub struct CliConfig {
|
|||||||
#[derive(FromArgs, PartialEq, Debug)]
|
#[derive(FromArgs, PartialEq, Debug)]
|
||||||
#[argh(subcommand)]
|
#[argh(subcommand)]
|
||||||
enum SubCommand {
|
enum SubCommand {
|
||||||
|
ChangeUserAddress(change_user_address::ChangeUserAddressCommand),
|
||||||
|
ChangeUserAddressByKey(change_user_address_by_key::ChangeUserAddressByKeyCommand),
|
||||||
ChangeUserTier(change_user_tier::ChangeUserTierCommand),
|
ChangeUserTier(change_user_tier::ChangeUserTierCommand),
|
||||||
ChangeUserTierByKey(change_user_tier_by_key::ChangeUserTierByKeyCommand),
|
ChangeUserTierByKey(change_user_tier_by_key::ChangeUserTierByKeyCommand),
|
||||||
CheckConfig(check_config::CheckConfigSubCommand),
|
CheckConfig(check_config::CheckConfigSubCommand),
|
||||||
@ -80,6 +84,16 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
match cli_config.sub_command {
|
match cli_config.sub_command {
|
||||||
|
SubCommand::ChangeUserAddress(x) => {
|
||||||
|
let db_conn = get_db(cli_config.db_url, 1, 1).await?;
|
||||||
|
|
||||||
|
x.main(&db_conn).await
|
||||||
|
}
|
||||||
|
SubCommand::ChangeUserAddressByKey(x) => {
|
||||||
|
let db_conn = get_db(cli_config.db_url, 1, 1).await?;
|
||||||
|
|
||||||
|
x.main(&db_conn).await
|
||||||
|
}
|
||||||
SubCommand::ChangeUserTier(x) => {
|
SubCommand::ChangeUserTier(x) => {
|
||||||
let db_conn = get_db(cli_config.db_url, 1, 1).await?;
|
let db_conn = get_db(cli_config.db_url, 1, 1).await?;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user