Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do no use Rinkeby as a default, change defaults to be goerli or none if possible #2654

Merged
merged 15 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/payment-driver/erc20/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, NetworkConfig> {
Expand Down
31 changes: 25 additions & 6 deletions core/payment-driver/erc20/src/driver/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use chrono::Utc;

// Workspace uses
use ya_payment_driver::driver::PaymentDriver;
use ya_payment_driver::{
bus,
db::models::Network,
Expand All @@ -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
)));
}
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


bus::register_account(driver, &msg.address(), &network.to_string(), &token, mode).await?;

log::info!(
Expand Down Expand Up @@ -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<String, GenericError> {
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;
Expand Down
2 changes: 1 addition & 1 deletion core/payment-driver/erc20/src/driver/cron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
9 changes: 3 additions & 6 deletions core/payment-driver/erc20/src/erc20/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand Down Expand Up @@ -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.
Expand Down
41 changes: 21 additions & 20 deletions core/payment-driver/erc20/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,10 @@ pub fn platform_to_network_token(platform: String) -> Result<(DbNetwork, String)
}

pub fn network_token_to_platform(
network: Option<DbNetwork>,
network: DbNetwork,
token: Option<String>,
) -> Result<String, GenericError> {
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 {
Expand Down Expand Up @@ -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>) -> 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<String>,
) -> Result<String, GenericError> {
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<String>) -> 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
))),
}
}
4 changes: 2 additions & 2 deletions core/payment-driver/zksync/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -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) => {
Expand Down
8 changes: 4 additions & 4 deletions core/payment-driver/zksync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 20 additions & 30 deletions core/payment-driver/zksync/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Network> = 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! {
Expand All @@ -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: {}",
Expand All @@ -42,22 +32,10 @@ pub fn platform_to_network_token(platform: String) -> Result<(DbNetwork, String)
}

pub fn network_token_to_platform(
network: Option<DbNetwork>,
network: DbNetwork,
token: Option<String>,
) -> Result<String, GenericError> {
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 {
Expand All @@ -72,9 +50,21 @@ pub fn network_token_to_platform(
Ok(platform.to_string())
}

pub fn get_network_token(network: DbNetwork, token: Option<String>) -> 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<String>,
) -> Result<String, GenericError> {
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
))),
}
}
16 changes: 8 additions & 8 deletions core/payment-driver/zksync/src/zksync/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub async fn account_balance(address: &str, network: Network) -> Result<BigDecim
.await
.map_err(GenericError::new)?;
// TODO: implement tokens, replace None
let token = get_network_token(network, None);
let token = get_network_token(network, None)?;
let balance_com = acc_info
.committed
.balances
Expand All @@ -63,7 +63,7 @@ pub async fn init_wallet(msg: &Init) -> 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) {
Expand Down Expand Up @@ -91,7 +91,7 @@ pub async fn exit(msg: &Exit) -> Result<String, GenericError> {
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?;
Expand Down Expand Up @@ -134,7 +134,7 @@ pub async fn enter(msg: Enter) -> Result<String, GenericError> {
}

pub async fn get_tx_fee(address: &str, network: Network) -> Result<BigDecimal, GenericError> {
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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -346,7 +346,7 @@ async fn unlock_wallet<S: EthereumSigner + Clone, P: Provider + Clone>(
.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 {
Expand Down Expand Up @@ -389,7 +389,7 @@ pub async fn withdraw<S: EthereumSigner + Clone, P: Provider + Clone>(
amount: Option<BigDecimal>,
recipient: Option<String>,
) -> Result<SyncTransactionHandle<P>, 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",
Expand Down Expand Up @@ -505,7 +505,7 @@ pub async fn deposit<S: EthereumSigner + Clone, P: Provider + Clone>(
network: Network,
amount: BigDecimal,
) -> Result<H256, GenericError> {
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();

Expand Down
17 changes: 13 additions & 4 deletions core/payment/src/accounts.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use futures::future::join_all;
use serde::{Deserialize, Serialize};
use std::env;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -52,10 +53,18 @@ pub async fn init_accounts(data_dir: &Path) -> anyhow::Result<()> {
let text = fs::read(accounts_path).await?;
let accounts: Vec<Account> = 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(())
}

Expand Down
2 changes: 1 addition & 1 deletion core/payment/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ impl DriverRegistry {
network: Option<String>,
) -> 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,
Expand Down
Loading