Skip to content

Commit

Permalink
added balance option
Browse files Browse the repository at this point in the history
  • Loading branch information
scx1332 committed Nov 14, 2023
1 parent 6857246 commit e22209f
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 30 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 38 additions & 19 deletions crates/erc20_payment_lib/src/account_balance.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -55,6 +57,8 @@ pub struct BalanceResult {
}

pub async fn account_balance(
_shared_state: Option<Arc<tokio::sync::Mutex<SharedState>>>,
payment_setup: Option<PaymentSetup>,
account_balance_options: BalanceOptions,
config: &config::Config,
) -> Result<BTreeMap<String, BalanceResult>, PaymentError> {
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion crates/erc20_payment_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
16 changes: 13 additions & 3 deletions crates/erc20_payment_lib/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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");
Expand Down
1 change: 1 addition & 0 deletions crates/erc20_payment_lib_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
5 changes: 3 additions & 2 deletions crates/erc20_payment_lib_test/src/get_balance.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
}
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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 {
Expand Down Expand Up @@ -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!(
Expand Down
4 changes: 2 additions & 2 deletions src/options.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down

0 comments on commit e22209f

Please sign in to comment.