diff --git a/core/payment-driver/erc20/src/driver.rs b/core/payment-driver/erc20/src/driver.rs index 43c01c4804..9dd15fd487 100644 --- a/core/payment-driver/erc20/src/driver.rs +++ b/core/payment-driver/erc20/src/driver.rs @@ -24,7 +24,7 @@ use ya_payment_driver::{ }; // Local uses -use crate::{dao::Erc20Dao, network::SUPPORTED_NETWORKS, DRIVER_NAME, RINKEBY_NETWORK}; +use crate::{dao::Erc20Dao, network::SUPPORTED_NETWORKS, DRIVER_NAME, GOERLI_NETWORK}; mod api; mod cli; @@ -139,7 +139,7 @@ impl PaymentDriver for Erc20Driver { } fn get_default_network(&self) -> String { - RINKEBY_NETWORK.to_string() + GOERLI_NETWORK.to_string() } fn get_networks(&self) -> HashMap { diff --git a/core/payment-driver/erc20/src/driver/cli.rs b/core/payment-driver/erc20/src/driver/cli.rs index 59ecbd8ba3..5869fbd5ba 100644 --- a/core/payment-driver/erc20/src/driver/cli.rs +++ b/core/payment-driver/erc20/src/driver/cli.rs @@ -7,6 +7,7 @@ use chrono::Utc; // Workspace uses +use ya_payment_driver::driver::PaymentDriver; use ya_payment_driver::{ bus, db::models::Network, @@ -32,13 +33,31 @@ pub async fn init(driver: &Erc20Driver, msg: Init) -> Result<(), GenericError> { driver.is_account_active(&address)? } - wallet::init_wallet(&msg) - .timeout(Some(30)) - .await - .map_err(GenericError::new)??; + wallet::init_wallet( + msg.address(), + msg.network() + .unwrap_or_else(|| driver.get_default_network()), + ) + .timeout(Some(30)) + .await + .map_err(GenericError::new)??; let network = network::network_like_to_network(msg.network()); - let token = network::get_network_token(network, msg.token()); + let token = match network::get_network_token(network, msg.token()) { + Ok(token) => token, + Err(e) => { + return Err(GenericError::new(format!( + "Failed to initialize payment account. mode={:?}, address={}, driver={}, network={}, token={}: {}", + mode, + &msg.address(), + DRIVER_NAME, + network, + msg.token().unwrap_or_default(), + e + ))); + } + }; + bus::register_account(driver, &msg.address(), &network.to_string(), &token, mode).await?; log::info!( @@ -111,7 +130,7 @@ To be able to use mainnet Ethereum driver please send some GLM tokens and ETH fo pub async fn transfer(dao: &Erc20Dao, msg: Transfer) -> Result { log::debug!("transfer: {:?}", msg); let network = network::network_like_to_network(msg.network); - let token = network::get_network_token(network, None); + let token = network::get_network_token(network, None)?; let sender = msg.sender; let sender_h160 = utils::str_to_addr(&sender)?; let recipient = msg.to; diff --git a/core/payment-driver/erc20/src/driver/cron.rs b/core/payment-driver/erc20/src/driver/cron.rs index 506c457966..24a7f71106 100644 --- a/core/payment-driver/erc20/src/driver/cron.rs +++ b/core/payment-driver/erc20/src/driver/cron.rs @@ -241,7 +241,7 @@ pub async fn confirm_payments(dao: &Erc20Dao, name: &str, network_key: &str) { .map(|payment| payment.order_id.clone()) .collect(); - let platform = match network::network_token_to_platform(Some(network), None) { + let platform = match network::network_token_to_platform(network, None) { Ok(platform) => platform, Err(e) => { log::error!( diff --git a/core/payment-driver/erc20/src/erc20/wallet.rs b/core/payment-driver/erc20/src/erc20/wallet.rs index 8d16f2a360..d615a4f8e2 100644 --- a/core/payment-driver/erc20/src/erc20/wallet.rs +++ b/core/payment-driver/erc20/src/erc20/wallet.rs @@ -21,7 +21,7 @@ use web3::types::{H160, H256, U256, U64}; // Workspace uses use ya_payment_driver::{ db::models::{Network, TransactionEntity, TxType}, - model::{GenericError, Init, PaymentDetails}, + model::{GenericError, PaymentDetails}, }; // Local uses @@ -35,7 +35,6 @@ use crate::{ convert_u256_gas_to_float, str_to_addr, topic_to_str_address, u256_to_big_dec, }, }, - RINKEBY_NETWORK, }; use ya_payment_driver::db::models::TransactionStatus; @@ -70,10 +69,8 @@ pub async fn account_gas_balance( Ok(balance) } -pub async fn init_wallet(msg: &Init) -> Result<(), GenericError> { - log::debug!("init_wallet. msg={:?}", msg); - let address = msg.address(); - let network = msg.network().unwrap_or_else(|| RINKEBY_NETWORK.to_string()); +pub async fn init_wallet(address: String, network: String) -> Result<(), GenericError> { + log::debug!("init_wallet. address={}, network={}", address, network); let network = Network::from_str(&network).map_err(GenericError::new)?; // Validate address and that checking balance of GLM and ETH works. diff --git a/core/payment-driver/erc20/src/network.rs b/core/payment-driver/erc20/src/network.rs index d54ac9f419..130889ba1a 100644 --- a/core/payment-driver/erc20/src/network.rs +++ b/core/payment-driver/erc20/src/network.rs @@ -74,21 +74,10 @@ pub fn platform_to_network_token(platform: String) -> Result<(DbNetwork, String) } pub fn network_token_to_platform( - network: Option, + network: DbNetwork, token: Option, ) -> Result { - let network = network.unwrap_or(*RINKEBY_DB_NETWORK); - let network_config = (*SUPPORTED_NETWORKS).get(&(network.to_string())); - let network_config = match network_config { - Some(nc) => nc, - None => { - return Err(GenericError::new(format!( - "Unable to find platform for network={}", - network - ))) - } - }; - + let network_config = get_network_config(&network)?; let token = token.unwrap_or_else(|| network_config.default_token.clone()); let platform = network_config.tokens.get(&token); let platform = match platform { @@ -132,16 +121,28 @@ pub fn platform_to_currency(platform: String) -> Result<(String, String), Generi } } -pub fn get_network_token(network: DbNetwork, token: Option) -> String { - // Fetch network config, safe as long as all DbNetwork entries are in SUPPORTED_NETWORKS - let network_config = (*SUPPORTED_NETWORKS).get(&(network.to_string())).unwrap(); - // TODO: Check if token in network.tokens - token.unwrap_or_else(|| network_config.default_token.clone()) +pub fn get_network_token( + network: DbNetwork, + token: Option, +) -> Result { + let network_config = get_network_config(&network)?; + Ok(token.unwrap_or_else(|| network_config.default_token.clone())) } pub fn network_like_to_network(network_like: Option) -> DbNetwork { match network_like { - Some(n) => DbNetwork::from_str(&n).unwrap_or(*RINKEBY_DB_NETWORK), - None => *RINKEBY_DB_NETWORK, + Some(n) => DbNetwork::from_str(&n).unwrap_or(*GOERLI_DB_NETWORK), + None => *GOERLI_DB_NETWORK, + } +} + +pub fn get_network_config(network: &DbNetwork) -> Result<&Network, GenericError> { + let network_config = (*SUPPORTED_NETWORKS).get(&(network.to_string())); + match network_config { + Some(network_config) => Ok(network_config), + None => Err(GenericError::new(format!( + "Network {} is not supported", + network + ))), } } diff --git a/core/payment-driver/zksync/src/driver.rs b/core/payment-driver/zksync/src/driver.rs index 9066fd0721..1efa3c24cf 100644 --- a/core/payment-driver/zksync/src/driver.rs +++ b/core/payment-driver/zksync/src/driver.rs @@ -267,7 +267,7 @@ impl PaymentDriver for ZksyncDriver { let token = get_network_token( DbNetwork::from_str(&network).map_err(GenericError::new)?, msg.token(), - ); + )?; bus::register_account(self, &address, &network, &token, mode).await?; log::info!( @@ -513,7 +513,7 @@ impl PaymentDriverCron for ZksyncDriver { } // TODO: Add token support - let platform = network_token_to_platform(Some(network), None).unwrap(); // TODO: Catch error? + let platform = network_token_to_platform(network, None).unwrap(); // TODO: Catch error? let details = match wallet::verify_tx(tx_hash, network).await { Ok(a) => a, Err(e) => { diff --git a/core/payment-driver/zksync/src/lib.rs b/core/payment-driver/zksync/src/lib.rs index 2787a9cf97..4a6f2fc190 100644 --- a/core/payment-driver/zksync/src/lib.rs +++ b/core/payment-driver/zksync/src/lib.rs @@ -7,14 +7,14 @@ // Public pub const DRIVER_NAME: &str = "zksync"; -pub const DEFAULT_NETWORK: &str = "rinkeby"; -pub const DEFAULT_TOKEN: &str = "tGLM"; -pub const DEFAULT_PLATFORM: &str = "zksync-rinkeby-tglm"; - pub const MAINNET_NETWORK: &str = "mainnet"; pub const MAINNET_TOKEN: &str = "GLM"; pub const MAINNET_PLATFORM: &str = "zksync-mainnet-glm"; +pub const DEFAULT_NETWORK: &str = MAINNET_NETWORK; +pub const DEFAULT_TOKEN: &str = MAINNET_TOKEN; +pub const DEFAULT_PLATFORM: &str = MAINNET_PLATFORM; + pub use service::ZksyncService as PaymentDriverService; // Private diff --git a/core/payment-driver/zksync/src/network.rs b/core/payment-driver/zksync/src/network.rs index c92a9a2024..69c6256dff 100644 --- a/core/payment-driver/zksync/src/network.rs +++ b/core/payment-driver/zksync/src/network.rs @@ -6,19 +6,10 @@ use std::str::FromStr; use ya_payment_driver::{db::models::Network as DbNetwork, driver::Network, model::GenericError}; // Local uses -use crate::{ - DEFAULT_NETWORK, DEFAULT_PLATFORM, DEFAULT_TOKEN, MAINNET_NETWORK, MAINNET_PLATFORM, - MAINNET_TOKEN, -}; +use crate::{DEFAULT_NETWORK, MAINNET_NETWORK, MAINNET_PLATFORM, MAINNET_TOKEN}; lazy_static::lazy_static! { pub static ref SUPPORTED_NETWORKS: HashMap = hashmap! { - DEFAULT_NETWORK.to_string() => Network { - default_token: DEFAULT_TOKEN.to_string(), - tokens: hashmap! { - DEFAULT_TOKEN.to_string() => DEFAULT_PLATFORM.to_string() - } - }, MAINNET_NETWORK.to_string() => Network { default_token: MAINNET_TOKEN.to_string(), tokens: hashmap! { @@ -32,7 +23,6 @@ lazy_static::lazy_static! { pub fn platform_to_network_token(platform: String) -> Result<(DbNetwork, String), GenericError> { match platform.as_str() { - DEFAULT_PLATFORM => Ok((*DEFAULT_DB_NETWORK, DEFAULT_TOKEN.to_owned())), MAINNET_PLATFORM => Ok((*MAINNET_DB_NETWORK, MAINNET_TOKEN.to_owned())), other => Err(GenericError::new(format!( "Unable to find network for platform: {}", @@ -42,22 +32,10 @@ pub fn platform_to_network_token(platform: String) -> Result<(DbNetwork, String) } pub fn network_token_to_platform( - network: Option, + network: DbNetwork, token: Option, ) -> Result { - let network = - network.unwrap_or(DbNetwork::from_str(DEFAULT_NETWORK).map_err(GenericError::new)?); - let network_config = (*SUPPORTED_NETWORKS).get(&(network.to_string())); - let network_config = match network_config { - Some(nc) => nc, - None => { - return Err(GenericError::new(format!( - "Unable to find platform for network={}", - network - ))) - } - }; - + let network_config = get_network_config(&network)?; let token = token.unwrap_or_else(|| network_config.default_token.clone()); let platform = network_config.tokens.get(&token); let platform = match platform { @@ -72,9 +50,21 @@ pub fn network_token_to_platform( Ok(platform.to_string()) } -pub fn get_network_token(network: DbNetwork, token: Option) -> String { - // Fetch network config, safe as long as all DbNetwork entries are in SUPPORTED_NETWORKS - let network_config = (*SUPPORTED_NETWORKS).get(&(network.to_string())).unwrap(); - // TODO: Check if token in network.tokens - token.unwrap_or_else(|| network_config.default_token.clone()) +pub fn get_network_token( + network: DbNetwork, + token: Option, +) -> Result { + let network_config = get_network_config(&network)?; + Ok(token.unwrap_or_else(|| network_config.default_token.clone())) +} + +pub fn get_network_config(network: &DbNetwork) -> Result<&Network, GenericError> { + let network_config = (*SUPPORTED_NETWORKS).get(&(network.to_string())); + match network_config { + Some(network_config) => Ok(network_config), + None => Err(GenericError::new(format!( + "Network {} is not supported", + network + ))), + } } diff --git a/core/payment-driver/zksync/src/zksync/wallet.rs b/core/payment-driver/zksync/src/zksync/wallet.rs index 3d4c8e2e08..62c944d137 100644 --- a/core/payment-driver/zksync/src/zksync/wallet.rs +++ b/core/payment-driver/zksync/src/zksync/wallet.rs @@ -42,7 +42,7 @@ pub async fn account_balance(address: &str, network: Network) -> Result Result<(), GenericError> { log::debug!("init_wallet. msg={:?}", msg); let mode = msg.mode(); let address = msg.address().clone(); - let network = msg.network().unwrap_or_else(|| DEFAULT_NETWORK.to_string()); + let network = msg.network().unwrap_or_default(); let network = Network::from_str(&network).map_err(GenericError::new)?; if mode.contains(AccountMode::SEND) { @@ -91,7 +91,7 @@ pub async fn exit(msg: &Exit) -> Result { let network = Network::from_str(&network).map_err(GenericError::new)?; let wallet = get_wallet(&msg.sender(), network).await?; - let token = get_network_token(network, None); + let token = get_network_token(network, None)?; let balance = get_balance(&wallet, &token).await?; let unlock_fee = get_unlock_fee(&wallet, &token).await?; let withdraw_fee = get_withdraw_fee(&wallet, &token).await?; @@ -134,7 +134,7 @@ pub async fn enter(msg: Enter) -> Result { } pub async fn get_tx_fee(address: &str, network: Network) -> Result { - let token = get_network_token(network, None); + let token = get_network_token(network, None)?; let wallet = get_wallet(address, network).await?; let tx_fee = wallet .provider @@ -184,7 +184,7 @@ pub async fn make_transfer( let sender = details.sender.clone(); let wallet = get_wallet(&sender, network).await?; - let token = get_network_token(network, None); + let token = get_network_token(network, None)?; let balance = get_balance(&wallet, &token).await?; log::debug!("balance before transfer={}", balance); @@ -346,7 +346,7 @@ async fn unlock_wallet( .map_err(GenericError::new)? { log::info!("Unlocking wallet... address = {}", wallet.signer.address); - let token = get_network_token(network, None); + let token = get_network_token(network, None)?; let balance = get_balance(wallet, &token).await?; let unlock_fee = get_unlock_fee(wallet, &token).await?; if unlock_fee > balance { @@ -389,7 +389,7 @@ pub async fn withdraw( amount: Option, recipient: Option, ) -> Result, GenericError> { - let token = get_network_token(network, None); + let token = get_network_token(network, None)?; let balance = get_balance(&wallet, &token).await?; info!( "Wallet funded with {} {} available for withdrawal", @@ -505,7 +505,7 @@ pub async fn deposit( network: Network, amount: BigDecimal, ) -> Result { - let token = get_network_token(network, None); + let token = get_network_token(network, None)?; let amount = base_utils::big_dec_to_u256(&amount); let address = wallet.address(); diff --git a/core/payment/src/accounts.rs b/core/payment/src/accounts.rs index e3c5c0ead7..bde36516a8 100644 --- a/core/payment/src/accounts.rs +++ b/core/payment/src/accounts.rs @@ -1,3 +1,4 @@ +use futures::future::join_all; use serde::{Deserialize, Serialize}; use std::env; use std::path::{Path, PathBuf}; @@ -52,10 +53,18 @@ pub async fn init_accounts(data_dir: &Path) -> anyhow::Result<()> { let text = fs::read(accounts_path).await?; let accounts: Vec = serde_json::from_slice(&text)?; - for account in accounts { - init_account(account).await?; - } - log::debug!("Payment accounts initialized."); + let init_results = join_all( + accounts + .into_iter() + .map(|account| async move { init_account(account).await.is_ok() }), + ) + .await; + + log::debug!( + "Successfully initialized {} / {} payment accounts.", + init_results.iter().filter(|&r| *r).count(), + init_results.len() + ); Ok(()) } diff --git a/core/payment/src/processor.rs b/core/payment/src/processor.rs index 4dcc88bff4..f68e8550b6 100644 --- a/core/payment/src/processor.rs +++ b/core/payment/src/processor.rs @@ -224,7 +224,7 @@ impl DriverRegistry { network: Option, ) -> Result<(String, Network), RegisterAccountError> { let driver_details = self.get_driver(&driver)?; - let network_name = network.unwrap_or_else(|| driver_details.default_network.to_owned()); + let network_name = network.unwrap_or_default(); match driver_details.networks.get(&network_name) { None => Err(RegisterAccountError::UnsupportedNetwork( network_name,