Skip to content

Commit

Permalink
Merge branch 'refs/heads/master' into scx1332/update_payment7
Browse files Browse the repository at this point in the history
# Conflicts:
#	core/payment/src/cli.rs
  • Loading branch information
scx1332 committed Oct 1, 2024
2 parents 3e2a74e + 1418de5 commit c6a60ed
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 34 deletions.
4 changes: 3 additions & 1 deletion core/model/src/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,9 @@ pub mod local {
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct GetDrivers {}
pub struct GetDrivers {
pub ignore_legacy_networks: bool,
}

#[derive(Clone, Debug, Serialize, Deserialize, thiserror::Error)]
pub enum GetDriversError {
Expand Down
69 changes: 42 additions & 27 deletions core/payment/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod rpc;

use std::collections::BTreeMap;
use std::collections::HashMap;
// External crates
use bigdecimal::BigDecimal;
use chrono::{DateTime, Utc};
Expand Down Expand Up @@ -863,41 +863,56 @@ Typically operation should take less than 1 minute.
)))
}
DriverSubcommand::List => {
let drivers = bus::service(pay::BUS_ID).call(pay::GetDrivers {}).await??;
let drivers: HashMap<String, DriverDetails> = bus::service(pay::BUS_ID)
.call(pay::GetDrivers {
ignore_legacy_networks: false,
})
.await??;
if ctx.json_output {
return CommandOutput::object(drivers);
}
Ok(ResponseTable {
columns: vec![
"driver".to_owned(),
"network".to_owned(),
"default?".to_owned(),
"token".to_owned(),
"platform".to_owned(),
],
values: drivers
.iter()
.flat_map(|(driver, dd)| {
dd.networks
.iter()
.flat_map(|(network, n)| {
n.tokens
.iter()
.map(|(token, platform)|
serde_json::json! {[

let mut values: Vec<Value> = drivers
.iter()
.flat_map(|(driver, dd)| {
dd.networks
.iter()
.flat_map(|(network, n)| {
n.tokens
.iter()
.map(|(token, platform)|
serde_json::json! {[
driver,
network,
if &dd.default_network == network { "X" } else { "" },
token,
platform,
]}
)
.collect::<Vec<serde_json::Value>>()
})
.collect::<Vec<serde_json::Value>>()
})
.collect(),
}.into())
)
.collect::<Vec<serde_json::Value>>()
})
.collect::<Vec<serde_json::Value>>()
})
.collect();

values.sort_by(|a, b| {
//sort by index 4 (which means platform, be careful when changing these values)
let left_str = a.as_array().unwrap()[4].as_str().unwrap();
let right_str = b.as_array().unwrap()[4].as_str().unwrap();
left_str.cmp(right_str)
});

Ok(ResponseTable {
columns: vec![
"driver".to_owned(), //index 0
"network".to_owned(), //index 1
"default?".to_owned(), //index 2
"token".to_owned(), //index 3
"platform".to_owned(), //index 4 - we are sorting by platform, be careful when changing these values
],
values,
}
.into())
}
},
PaymentCli::ReleaseAllocations => {
Expand Down
21 changes: 17 additions & 4 deletions core/payment/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,18 @@ impl DriverRegistry {
}
}

pub fn get_drivers(&self) -> HashMap<String, DriverDetails> {
self.drivers.clone()
pub fn get_drivers(&self, ignore_legacy_networks: bool) -> HashMap<String, DriverDetails> {
let mut drivers = self.drivers.clone();

let legacy_networks = ["mumbai", "goerli", "rinkeby"];
if ignore_legacy_networks {
drivers.values_mut().for_each(|driver| {
driver
.networks
.retain(|name, _| !legacy_networks.contains(&name.as_str()))
})
}
drivers
}

pub fn get_network(
Expand Down Expand Up @@ -435,11 +445,14 @@ impl PaymentProcessor {
.map_err(|_| GetAccountsError::InternalTimeout)
}

pub async fn get_drivers(&self) -> Result<HashMap<String, DriverDetails>, GetDriversError> {
pub async fn get_drivers(
&self,
ignore_legacy_networks: bool,
) -> Result<HashMap<String, DriverDetails>, GetDriversError> {
self.registry
.timeout_read(REGISTRY_LOCK_TIMEOUT)
.await
.map(|registry| registry.get_drivers())
.map(|registry| registry.get_drivers(ignore_legacy_networks))
.map_err(|_| GetDriversError::InternalTimeout)
}

Expand Down
9 changes: 7 additions & 2 deletions core/payment/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ mod local {
_caller: String,
msg: GetDrivers,
) -> Result<HashMap<String, DriverDetails>, GetDriversError> {
processor.get_drivers().await
processor.get_drivers(msg.ignore_legacy_networks).await
}

async fn payment_driver_status(
Expand All @@ -511,7 +511,12 @@ mod local {
None => {
#[allow(clippy::iter_kv_map)]
// Unwrap is provably safe because NoError can't be instanciated
match service(PAYMENT_BUS_ID).call(GetDrivers {}).await {
match service(PAYMENT_BUS_ID)
.call(GetDrivers {
ignore_legacy_networks: false,
})
.await
{
Ok(drivers) => drivers,
Err(e) => return Err(PaymentDriverStatusError::Internal(e.to_string())),
}
Expand Down

0 comments on commit c6a60ed

Please sign in to comment.