From 041ee9bbf906ef2afbfedc3b931d65da9b39f04b Mon Sep 17 00:00:00 2001 From: scx1332 Date: Tue, 12 Mar 2024 11:53:19 +0100 Subject: [PATCH] Refactor deposit commands --- .github/workflows/deposit.yml | 18 ++-- src/actions.rs | 5 +- src/actions/deposit.rs | 4 + .../{close_deposit.rs => deposit/close.rs} | 2 +- .../{create_deposit.rs => deposit/create.rs} | 2 +- .../details.rs} | 2 +- .../terminate.rs} | 2 +- src/main.rs | 90 +++++++++---------- src/options.rs | 45 ++++++---- 9 files changed, 90 insertions(+), 80 deletions(-) create mode 100644 src/actions/deposit.rs rename src/actions/{close_deposit.rs => deposit/close.rs} (97%) rename src/actions/{create_deposit.rs => deposit/create.rs} (99%) rename src/actions/{deposit_details.rs => deposit/details.rs} (98%) rename src/actions/{terminate_deposit.rs => deposit/terminate.rs} (98%) diff --git a/.github/workflows/deposit.yml b/.github/workflows/deposit.yml index 28327974..40d5298d 100644 --- a/.github/workflows/deposit.yml +++ b/.github/workflows/deposit.yml @@ -35,9 +35,9 @@ jobs: - name: Create random deposit id run: | - echo DEPOSIT_NONCE0=$(shuf -i 0-2000000000 -n 1) >> $GITHUB_ENV - echo DEPOSIT_NONCE1=$(shuf -i 0-2000000000 -n 1) >> $GITHUB_ENV - echo DEPOSIT_NONCE2=$(shuf -i 0-2000000000 -n 1) >> $GITHUB_ENV + echo DEPOSIT_NONCE0=$(shuf -i 0-2000000000000000000 -n 1) >> $GITHUB_ENV + echo DEPOSIT_NONCE1=$(shuf -i 0-2000000000000000000 -n 1) >> $GITHUB_ENV + echo DEPOSIT_NONCE2=$(shuf -i 0-2000000000000000000 -n 1) >> $GITHUB_ENV - name: Show created addresses run: | @@ -69,14 +69,14 @@ jobs: - name: Create deposit run: | set -x - erc20_processor create-deposit --account-no 1 --amount 1 --fee-amount 0.1 --block-for 0 --spender $ETH_ADDRESS_2 --deposit-nonce $DEPOSIT_NONCE0 - erc20_processor create-deposit --account-no 1 --amount 1 --fee-amount 0.1 --block-for 1000 --spender $ETH_ADDRESS_2 --deposit-nonce $DEPOSIT_NONCE1 + erc20_processor deposit create --account-no 1 --amount 1 --fee-amount 0.1 --block-for 0 --spender $ETH_ADDRESS_2 --deposit-nonce $DEPOSIT_NONCE0 + erc20_processor deposit create --account-no 1 --amount 1 --fee-amount 0.1 --block-for 1000 --spender $ETH_ADDRESS_2 --deposit-nonce $DEPOSIT_NONCE1 erc20_processor run - name: Get Deposit ID from funder and nonce id run: | - echo DEPOSIT_ID0=$(erc20_processor check-deposit --deposit-nonce $DEPOSIT_NONCE0 --deposit-funder=$ETH_ADDRESS_1 | jq -r ".depositId") >> $GITHUB_ENV - echo DEPOSIT_ID1=$(erc20_processor check-deposit --deposit-nonce $DEPOSIT_NONCE1 --deposit-funder=$ETH_ADDRESS_1 | jq -r ".depositId") >> $GITHUB_ENV + echo DEPOSIT_ID0=$(erc20_processor deposit check --deposit-nonce $DEPOSIT_NONCE0 --deposit-funder=$ETH_ADDRESS_1 | jq -r ".depositId") >> $GITHUB_ENV + echo DEPOSIT_ID1=$(erc20_processor deposit check --deposit-nonce $DEPOSIT_NONCE1 --deposit-funder=$ETH_ADDRESS_1 | jq -r ".depositId") >> $GITHUB_ENV - name: Make single transfer from deposit run: | @@ -111,8 +111,8 @@ jobs: - name: Free deposit run: | set -x - erc20_processor close-deposit --deposit-id $DEPOSIT_ID0 --account-no 2 - erc20_processor close-deposit --deposit-id $DEPOSIT_ID1 --account-no 2 + erc20_processor deposit close --deposit-id $DEPOSIT_ID0 --account-no 2 + erc20_processor deposit close --deposit-id $DEPOSIT_ID1 --account-no 2 erc20_processor run - name: Transfer all left ETH tokens diff --git a/src/actions.rs b/src/actions.rs index 1e5207f7..04981680 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -3,11 +3,8 @@ use std::str::FromStr; use web3::types::Address; pub mod check_rpc; -pub mod close_deposit; -pub mod create_deposit; -pub mod deposit_details; +pub mod deposit; pub mod scan_chain; -pub mod terminate_deposit; pub fn check_address_name(n: &str) -> Result { match n { diff --git a/src/actions/deposit.rs b/src/actions/deposit.rs new file mode 100644 index 00000000..507320e4 --- /dev/null +++ b/src/actions/deposit.rs @@ -0,0 +1,4 @@ +pub mod close; +pub mod create; +pub mod details; +pub mod terminate; diff --git a/src/actions/close_deposit.rs b/src/actions/deposit/close.rs similarity index 97% rename from src/actions/close_deposit.rs rename to src/actions/deposit/close.rs index 55a1ede3..fcc6c180 100644 --- a/src/actions/close_deposit.rs +++ b/src/actions/deposit/close.rs @@ -9,7 +9,7 @@ use structopt::StructOpt; use web3::types::{Address, U256}; #[derive(StructOpt)] -#[structopt(about = "Allocate funds for use by payer")] +#[structopt(about = "Close deposit if you are spender")] pub struct CloseDepositOptions { #[structopt(short = "c", long = "chain-name", default_value = "holesky")] pub chain_name: String, diff --git a/src/actions/create_deposit.rs b/src/actions/deposit/create.rs similarity index 99% rename from src/actions/create_deposit.rs rename to src/actions/deposit/create.rs index 7bac0694..bdf7f940 100644 --- a/src/actions/create_deposit.rs +++ b/src/actions/deposit/create.rs @@ -17,7 +17,7 @@ use structopt::StructOpt; use web3::types::{Address, U256}; #[derive(StructOpt)] -#[structopt(about = "Allocate funds for use by payer")] +#[structopt(about = "Create deposit for use by spender")] pub struct CreateDepositOptions { #[structopt(short = "c", long = "chain-name", default_value = "holesky")] pub chain_name: String, diff --git a/src/actions/deposit_details.rs b/src/actions/deposit/details.rs similarity index 98% rename from src/actions/deposit_details.rs rename to src/actions/deposit/details.rs index 896ab535..aa432af1 100644 --- a/src/actions/deposit_details.rs +++ b/src/actions/deposit/details.rs @@ -9,7 +9,7 @@ use structopt::StructOpt; use web3::types::{Address, U256}; #[derive(StructOpt)] -#[structopt(about = "Allocate funds for use by payer")] +#[structopt(about = "Show details of given deposit")] pub struct CheckDepositOptions { #[structopt(short = "c", long = "chain-name", default_value = "holesky")] pub chain_name: String, diff --git a/src/actions/terminate_deposit.rs b/src/actions/deposit/terminate.rs similarity index 98% rename from src/actions/terminate_deposit.rs rename to src/actions/deposit/terminate.rs index 4189309e..a0657ca1 100644 --- a/src/actions/terminate_deposit.rs +++ b/src/actions/deposit/terminate.rs @@ -10,7 +10,7 @@ use structopt::StructOpt; use web3::types::{Address, U256}; #[derive(StructOpt)] -#[structopt(about = "Allocate funds for use by payer")] +#[structopt(about = "Terminate deposit if you are funder")] pub struct TerminateDepositOptions { #[structopt(short = "c", long = "chain-name", default_value = "holesky")] pub chain_name: String, diff --git a/src/main.rs b/src/main.rs index fd980f07..ca5526a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ mod actions; mod options; mod stats; -use crate::options::{PaymentCommands, PaymentOptions}; +use crate::options::{DepositCommands, PaymentCommands, PaymentOptions}; use actix_web::Scope; use actix_web::{web, App, HttpServer}; use csv::ReaderBuilder; @@ -26,10 +26,10 @@ use std::str::FromStr; use crate::actions::check_address_name; use crate::actions::check_rpc::check_rpc_local; -use crate::actions::close_deposit::close_deposit_local; -use crate::actions::create_deposit::make_deposit_local; -use crate::actions::deposit_details::deposit_details_local; -use crate::actions::terminate_deposit::terminate_deposit_local; +use crate::actions::deposit::close::close_deposit_local; +use crate::actions::deposit::create::make_deposit_local; +use crate::actions::deposit::details::deposit_details_local; +use crate::actions::deposit::terminate::terminate_deposit_local; use crate::stats::{export_stats, run_stats}; use erc20_payment_lib::faucet_client::faucet_donate; use erc20_payment_lib::misc::gen_private_keys; @@ -72,10 +72,7 @@ async fn main_internal() -> Result<(), PaymentError> { PaymentCommands::CheckRpc { .. } => {} PaymentCommands::GetDevEth { .. } => {} PaymentCommands::MintTestTokens { .. } => {} - PaymentCommands::CreateDeposit { .. } => {} - PaymentCommands::CloseDeposit { .. } => {} - PaymentCommands::TerminateDeposit { .. } => {} - PaymentCommands::CheckDeposit { .. } => {} + PaymentCommands::Deposit { .. } => {} PaymentCommands::Transfer { .. } => {} PaymentCommands::Balance { .. } => {} PaymentCommands::ImportPayments { .. } => {} @@ -352,45 +349,48 @@ async fn main_internal() -> Result<(), PaymentError> { ) .await?; } - PaymentCommands::CreateDeposit { - make_deposit_options, - } => { - make_deposit_local( - conn.clone().unwrap(), + PaymentCommands::Deposit { deposit } => match deposit { + DepositCommands::Create { make_deposit_options, - config, - &public_addrs, - signer, - ) - .await?; - } - PaymentCommands::CloseDeposit { - close_deposit_options, - } => { - close_deposit_local( - conn.clone().unwrap(), + } => { + make_deposit_local( + conn.clone().unwrap(), + make_deposit_options, + config, + &public_addrs, + signer, + ) + .await?; + } + DepositCommands::Close { close_deposit_options, - config, - &public_addrs, - ) - .await?; - } - PaymentCommands::TerminateDeposit { - terminate_deposit_options, - } => { - terminate_deposit_local( - conn.clone().unwrap(), + } => { + close_deposit_local( + conn.clone().unwrap(), + close_deposit_options, + config, + &public_addrs, + ) + .await?; + } + DepositCommands::Terminate { terminate_deposit_options, - config, - &public_addrs, - ) - .await?; - } - PaymentCommands::CheckDeposit { - check_deposit_options, - } => { - deposit_details_local(check_deposit_options, config).await?; - } + } => { + terminate_deposit_local( + conn.clone().unwrap(), + terminate_deposit_options, + config, + &public_addrs, + ) + .await?; + } + DepositCommands::Check { + check_deposit_options, + } => { + deposit_details_local(check_deposit_options, config).await?; + } + }, + PaymentCommands::GenerateKey { generate_key_options, } => { diff --git a/src/options.rs b/src/options.rs index 5602c278..c47767b9 100644 --- a/src/options.rs +++ b/src/options.rs @@ -1,9 +1,9 @@ use std::{fmt::Debug, path::PathBuf}; -use crate::actions::close_deposit::CloseDepositOptions; -use crate::actions::create_deposit::CreateDepositOptions; -use crate::actions::deposit_details::CheckDepositOptions; -use crate::actions::terminate_deposit::TerminateDepositOptions; +use crate::actions::deposit::close::CloseDepositOptions; +use crate::actions::deposit::create::CreateDepositOptions; +use crate::actions::deposit::details::CheckDepositOptions; +use crate::actions::deposit::terminate::TerminateDepositOptions; use erc20_payment_lib_extra::{BalanceOptions, GenerateOptions}; use structopt::StructOpt; use web3::types::Address; @@ -355,6 +355,27 @@ pub struct CleanupOptions { pub remove_tx_unsafe: bool, } +#[derive(StructOpt)] +#[structopt(about = "Commands for deposit management")] +pub enum DepositCommands { + Create { + #[structopt(flatten)] + make_deposit_options: CreateDepositOptions, + }, + Close { + #[structopt(flatten)] + close_deposit_options: CloseDepositOptions, + }, + Terminate { + #[structopt(flatten)] + terminate_deposit_options: TerminateDepositOptions, + }, + Check { + #[structopt(flatten)] + check_deposit_options: CheckDepositOptions, + }, +} + #[derive(StructOpt)] #[structopt(about = "Payment admin tool")] pub enum PaymentCommands { @@ -383,21 +404,9 @@ pub enum PaymentCommands { #[structopt(flatten)] mint_test_tokens_options: MintTestTokensOptions, }, - CreateDeposit { - #[structopt(flatten)] - make_deposit_options: CreateDepositOptions, - }, - CloseDeposit { - #[structopt(flatten)] - close_deposit_options: CloseDepositOptions, - }, - TerminateDeposit { - #[structopt(flatten)] - terminate_deposit_options: TerminateDepositOptions, - }, - CheckDeposit { + Deposit { #[structopt(flatten)] - check_deposit_options: CheckDepositOptions, + deposit: DepositCommands, }, Transfer { #[structopt(flatten)]