From e22209f695c00a55d253ef078fe92c30dbcd87ba Mon Sep 17 00:00:00 2001 From: scx1332 Date: Tue, 14 Nov 2023 19:06:14 +0100 Subject: [PATCH] added balance option --- Cargo.lock | 1 + .../erc20_payment_lib/src/account_balance.rs | 57 ++++++++++++------- crates/erc20_payment_lib/src/lib.rs | 2 +- crates/erc20_payment_lib/src/runtime.rs | 16 +++++- crates/erc20_payment_lib_test/Cargo.toml | 1 + .../erc20_payment_lib_test/src/get_balance.rs | 5 +- src/main.rs | 7 ++- src/options.rs | 4 +- 8 files changed, 63 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a7960fdf..12a6c00f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1053,6 +1053,7 @@ dependencies = [ "env_logger", "erc20_payment_lib", "erc20_payment_lib_extra", + "erc20_rpc_pool", "fastrand", "futures", "futures-util", diff --git a/crates/erc20_payment_lib/src/account_balance.rs b/crates/erc20_payment_lib/src/account_balance.rs index bfcb66c4..b469a723 100644 --- a/crates/erc20_payment_lib/src/account_balance.rs +++ b/crates/erc20_payment_lib/src/account_balance.rs @@ -1,5 +1,7 @@ use crate::error::PaymentError; use crate::eth::get_balance; +use crate::runtime::SharedState; +use crate::setup::PaymentSetup; use crate::utils::U256ConvExt; use crate::{config, err_custom_create}; use erc20_rpc_pool::{Web3RpcParams, Web3RpcPool}; @@ -55,6 +57,8 @@ pub struct BalanceResult { } pub async fn account_balance( + _shared_state: Option>>, + payment_setup: Option, account_balance_options: BalanceOptions, config: &config::Config, ) -> Result, PaymentError> { @@ -66,25 +70,40 @@ pub async fn account_balance( account_balance_options.chain_name ))?; - let web3_pool = Arc::new(Web3RpcPool::new( - chain_cfg.chain_id as u64, - chain_cfg - .rpc_endpoints - .iter() - .map(|rpc| Web3RpcParams { - chain_id: chain_cfg.chain_id as u64, - endpoint: rpc.endpoint.clone(), - skip_validation: rpc.skip_validation.unwrap_or(false), - backup_level: rpc.backup_level.unwrap_or(0), - name: rpc.name.clone(), - verify_interval_secs: rpc.verify_interval_secs.unwrap_or(120), - max_response_time_ms: rpc.max_timeout_ms.unwrap_or(10000), - max_head_behind_secs: rpc.allowed_head_behind_secs, - max_number_of_consecutive_errors: rpc.max_consecutive_errors.unwrap_or(5), - min_interval_requests_ms: rpc.min_interval_ms, - }) - .collect(), - )); + config + .chain + .get(&account_balance_options.chain_name) + .ok_or(err_custom_create!( + "Chain {} not found in config file", + account_balance_options.chain_name + ))?; + let web3_pool = if let Some(ps) = payment_setup { + ps.get_provider(chain_cfg.chain_id as i64).unwrap() + } else { + let web3_pool = Arc::new(Web3RpcPool::new( + chain_cfg.chain_id as u64, + chain_cfg + .rpc_endpoints + .iter() + .map(|rpc| Web3RpcParams { + chain_id: chain_cfg.chain_id as u64, + endpoint: rpc.endpoint.clone(), + skip_validation: rpc.skip_validation.unwrap_or(false), + backup_level: rpc.backup_level.unwrap_or(0), + name: rpc.name.clone(), + verify_interval_secs: rpc.verify_interval_secs.unwrap_or(120), + max_response_time_ms: rpc.max_timeout_ms.unwrap_or(10000), + max_head_behind_secs: rpc.allowed_head_behind_secs, + max_number_of_consecutive_errors: rpc.max_consecutive_errors.unwrap_or(5), + min_interval_requests_ms: rpc.min_interval_ms, + }) + .collect(), + )); + web3_pool + }; + + //shared_state. + //Vec::new(RwLock::new()); let token = if !account_balance_options.hide_token { Some(chain_cfg.token.address) diff --git a/crates/erc20_payment_lib/src/lib.rs b/crates/erc20_payment_lib/src/lib.rs index 95e5da3d..66a1b603 100644 --- a/crates/erc20_payment_lib/src/lib.rs +++ b/crates/erc20_payment_lib/src/lib.rs @@ -11,8 +11,8 @@ pub mod setup; pub mod transaction; pub mod utils; //@todo - add feature +pub mod account_balance; pub mod faucet_client; mod sender; pub mod server; pub mod signer; -pub mod account_balance; diff --git a/crates/erc20_payment_lib/src/runtime.rs b/crates/erc20_payment_lib/src/runtime.rs index 85dfcfd9..78ef2107 100644 --- a/crates/erc20_payment_lib/src/runtime.rs +++ b/crates/erc20_payment_lib/src/runtime.rs @@ -21,6 +21,7 @@ use secp256k1::SecretKey; use sqlx::SqlitePool; use tokio::sync::mpsc::Sender; +use crate::account_balance::{account_balance, BalanceOptions}; use crate::config::AdditionalOptions; use crate::db::model::{AllowanceDao, TokenTransferDao, TxDao}; use crate::sender::service_loop; @@ -34,7 +35,6 @@ use std::sync::{Arc, RwLock}; use tokio::sync::{Mutex, Notify}; use tokio::task::JoinHandle; use web3::types::{Address, H256, U256}; -use crate::account_balance::{account_balance, BalanceOptions}; #[derive(Debug, Clone, Serialize)] pub struct SharedInfoTx { @@ -463,7 +463,9 @@ impl PaymentRuntime { let extra_testing_ = extra_testing.clone(); let config_ = config.clone(); let jh = tokio::task::spawn(async move { - if let Some(balance_check_loop) = extra_testing_.clone().and_then(|e| e.balance_check_loop) { + if let Some(balance_check_loop) = + extra_testing_.clone().and_then(|e| e.balance_check_loop) + { let balance_options = BalanceOptions { chain_name: "mumbai".to_string(), //dead address @@ -475,7 +477,15 @@ impl PaymentRuntime { interval: Some(2.0), debug_loop: Some(balance_check_loop), }; - let _ = account_balance(balance_options, &config_).await; + let _ = account_balance( + Some(shared_state_clone), + Some(ps.clone()), + balance_options, + &config_, + ) + .await; + log::warn!("Balance debug loop finished"); + return; } if options.skip_service_loop && options.keep_running { log::warn!("Started with skip_service_loop and keep_running, no transaction will be sent or processed"); diff --git a/crates/erc20_payment_lib_test/Cargo.toml b/crates/erc20_payment_lib_test/Cargo.toml index c15d3a5f..4e022823 100644 --- a/crates/erc20_payment_lib_test/Cargo.toml +++ b/crates/erc20_payment_lib_test/Cargo.toml @@ -38,3 +38,4 @@ env_logger = { workspace = true } erc20_payment_lib = { path = "../erc20_payment_lib", version = "0.3.2" } erc20_payment_lib_extra = { path = "../erc20_payment_lib_extra", version = "0.3.2" } web3_test_proxy_client = { path = "../web3_test_proxy_client", version = "0.3.2" } +erc20_rpc_pool = { path = "../erc20_rpc_pool", version = "0.3.2" } \ No newline at end of file diff --git a/crates/erc20_payment_lib_test/src/get_balance.rs b/crates/erc20_payment_lib_test/src/get_balance.rs index aa18d51d..6aecce44 100644 --- a/crates/erc20_payment_lib_test/src/get_balance.rs +++ b/crates/erc20_payment_lib_test/src/get_balance.rs @@ -1,7 +1,7 @@ use crate::config_setup::create_default_config_setup; +use erc20_payment_lib::account_balance::{account_balance, BalanceOptions, BalanceResult}; use erc20_payment_lib::error::PaymentError; use std::collections::BTreeMap; -use erc20_payment_lib::account_balance::{account_balance, BalanceOptions, BalanceResult}; pub async fn test_get_balance( proxy_url_base: &str, @@ -18,5 +18,6 @@ pub async fn test_get_balance( interval: Some(0.001), debug_loop: None, }; - account_balance(account_balance_options.clone(), &config_check).await + + account_balance(None, None, account_balance_options.clone(), &config_check).await } diff --git a/src/main.rs b/src/main.rs index 5e077b52..7b906af8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,8 +32,9 @@ use erc20_payment_lib::runtime::{ use erc20_payment_lib::service::transaction_from_chain_and_into_db; use erc20_payment_lib::setup::PaymentSetup; use erc20_payment_lib::transaction::import_erc20_txs; -use erc20_payment_lib_extra::{generate_test_payments}; +use erc20_payment_lib_extra::generate_test_payments; +use erc20_payment_lib::account_balance::account_balance; use erc20_payment_lib::faucet_client::faucet_donate; use erc20_payment_lib::misc::gen_private_keys; use erc20_payment_lib::utils::{DecimalConvExt, StringConvExt}; @@ -43,7 +44,6 @@ use std::sync::Arc; use structopt::StructOpt; use tokio::sync::Mutex; use web3::ethabi::ethereum_types::Address; -use erc20_payment_lib::account_balance::account_balance; fn check_address_name(n: &str) -> String { match n { @@ -487,7 +487,8 @@ async fn main_internal() -> Result<(), PaymentError> { .join(","), ); } - let result = account_balance(account_balance_options, &config).await?; + + let result = account_balance(None, None, account_balance_options, &config).await?; println!( "{}", serde_json::to_string_pretty(&result).map_err(|err| err_custom_create!( diff --git a/src/options.rs b/src/options.rs index 569114c3..5813004d 100644 --- a/src/options.rs +++ b/src/options.rs @@ -1,9 +1,9 @@ use std::{fmt::Debug, path::PathBuf}; -use erc20_payment_lib_extra::{GenerateOptions}; +use erc20_payment_lib::account_balance::BalanceOptions; +use erc20_payment_lib_extra::GenerateOptions; use structopt::StructOpt; use web3::types::Address; -use erc20_payment_lib::account_balance::BalanceOptions; #[derive(StructOpt)] #[structopt(about = "Payment admin tool - run options")]