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

Payment status/yagna with header #948

Merged
merged 4 commits into from
Jan 21, 2021
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
67 changes: 63 additions & 4 deletions core/model/src/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,19 @@ pub mod local {
#[error("")]
pub struct NoError {} // This is needed because () doesn't implement Display

#[derive(Clone, Debug, Serialize, Deserialize, Default, derive_more::Display)]
#[display(fmt = "{}-{}-{}", driver, network, "token.to_lowercase()")]
pub struct Platform {
pub driver: String,
pub network: String,
pub token: String,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Network {
pub name: String,
pub default_token: String,
pub tokens: HashMap<String, String>, // token -> platform
pub tokens: HashMap<String, Platform>, // token -> platform
}

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -181,6 +190,16 @@ pub mod local {
pub mode: AccountMode,
}

impl From<RegisterAccount> for Platform {
fn from(msg: RegisterAccount) -> Self {
Self {
driver: msg.driver,
network: msg.network,
token: msg.token,
}
}
}

#[derive(Clone, Debug, Serialize, Deserialize, thiserror::Error)]
pub enum RegisterAccountError {
#[error("Account already registered: address={0}, driver={1}")]
Expand All @@ -189,8 +208,8 @@ pub mod local {
DriverNotRegistered(String),
#[error("Network not supported by driver: network={0}, driver={1}")]
UnsupportedNetwork(String, String),
#[error("Token not supported by driver: token={0}, network={1}, driver={2}")]
UnsupportedToken(String, String, String),
#[error("Platform not supported: token={}, network={}, driver={}", _0.token, _0.network, _0.driver)]
UnsupportedPlatform(Platform),
#[error("Error while registering account: {0}")]
Other(String),
}
Expand Down Expand Up @@ -233,7 +252,8 @@ pub mod local {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct GetStatus {
pub address: String,
pub driver: String,
pub platform: Option<String>,
pub driver: Option<String>,
pub network: Option<String>,
pub token: Option<String>,
}
Expand All @@ -246,6 +266,7 @@ pub mod local {

#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct StatusResult {
pub platform: Platform,
pub amount: BigDecimal,
pub reserved: BigDecimal,
pub outgoing: StatusNotes,
Expand Down Expand Up @@ -558,3 +579,41 @@ pub mod public {
type Error = SendError;
}
}

#[cfg(test)]
mod test {
use crate::payment::local::{Platform, RegisterAccountError};

#[test]
fn test_plarform_to_string() {
let p = Platform {
driver: "drv".into(),
network: "net".into(),
token: "tkn".into(),
};
assert_eq!(p.to_string(), "drv-net-tkn");
}

#[test]
fn test_plarform_to_string_uppercase_token() {
let p = Platform {
driver: "drv".into(),
network: "net".into(),
token: "TKN".into(),
};
assert_eq!(p.to_string(), "drv-net-tkn");
}

#[test]
fn test_unsupported_plarform() {
let e = RegisterAccountError::UnsupportedPlatform(Platform {
driver: "drv".into(),
network: "net".into(),
token: "TKN".into(),
});
assert_eq!(
e.to_string(),
"Platform not supported: token=TKN, network=net, driver=drv"
)
}
}
2 changes: 1 addition & 1 deletion core/payment-driver/base/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use bigdecimal::BigDecimal;
use std::collections::HashMap;
pub use ya_client_model::NodeId;
pub use ya_core_model::identity::{event::Event as IdentityEvent, Error as IdentityError};
pub use ya_core_model::payment::local::Network;
pub use ya_core_model::payment::local::{Network, Platform};

#[async_trait(?Send)]
pub trait PaymentDriver {
Expand Down
2 changes: 1 addition & 1 deletion core/payment-driver/dummy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod service;
pub const DRIVER_NAME: &'static str = "dummy";
pub const NETWORK_NAME: &'static str = "dummy";
pub const TOKEN_NAME: &'static str = "GLM";
pub const PLATFORM_NAME: &'static str = "dummy-glm";
pub const PLATFORM_NAME: &'static str = "dummy-dummy-glm"; // TODO: remove

pub struct PaymentDriverService;

Expand Down
10 changes: 8 additions & 2 deletions core/payment-driver/dummy/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use maplit::hashmap;
use std::str::FromStr;
use uuid::Uuid;
use ya_core_model::driver::*;
use ya_core_model::payment::local as payment_srv;
use ya_core_model::payment::local::{self as payment_srv, Platform};
use ya_service_bus::typed::service;
use ya_service_bus::{typed as bus, RpcEndpoint};

Expand All @@ -26,13 +26,19 @@ pub fn bind_service() {

pub async fn register_in_payment_service() -> anyhow::Result<()> {
log::debug!("Registering driver in payment service...");
let default_platform = Platform {
driver: DRIVER_NAME.to_string(),
network: NETWORK_NAME.to_string(),
token: TOKEN_NAME.to_string(),
};
let details = payment_srv::DriverDetails {
default_network: NETWORK_NAME.to_string(),
networks: hashmap! {
NETWORK_NAME.to_string() => payment_srv::Network {
name: NETWORK_NAME.to_string(),
default_token: TOKEN_NAME.to_string(),
tokens: hashmap! {
TOKEN_NAME.to_string() => PLATFORM_NAME.to_string()
TOKEN_NAME.to_string().to_lowercase() => default_platform
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion core/payment-driver/gnt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub const DRIVER_NAME: &'static str = "erc20";

pub const DEFAULT_NETWORK: &'static str = "rinkeby";
pub const DEFAULT_TOKEN: &'static str = "tGLM";
pub const DEFAULT_PLATFORM: &'static str = "erc20-rinkeby-tglm";
pub const DEFAULT_PLATFORM: &'static str = "erc20-rinkeby-tglm"; // TODO: remove

const ETH_FAUCET_MAX_WAIT: time::Duration = time::Duration::from_secs(180);

Expand Down
12 changes: 9 additions & 3 deletions core/payment-driver/gnt/src/service.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::processor::GNTDriverProcessor;
use crate::{DEFAULT_NETWORK, DEFAULT_PLATFORM, DEFAULT_TOKEN, DRIVER_NAME};
use crate::{DEFAULT_NETWORK, DEFAULT_TOKEN, DRIVER_NAME};
use bigdecimal::BigDecimal;
use maplit::hashmap;
use ya_core_model::driver::*;
use ya_core_model::payment::local as payment_srv;
use ya_core_model::payment::local::{self as payment_srv, Platform};
use ya_persistence::executor::DbExecutor;
use ya_service_bus::typed::service;
use ya_service_bus::{typed as bus, RpcEndpoint};
Expand Down Expand Up @@ -34,11 +34,17 @@ pub async fn subscribe_to_identity_events() -> anyhow::Result<()> {

pub async fn register_in_payment_service() -> anyhow::Result<()> {
log::debug!("Registering driver in payment service...");
let default_platform = Platform {
driver: DRIVER_NAME.to_string(),
network: DEFAULT_NETWORK.to_string(),
token: DEFAULT_TOKEN.to_string(),
};
let networks = hashmap! { // TODO: Implement multi-network support
DEFAULT_NETWORK.to_string() => payment_srv::Network {
name: DEFAULT_NETWORK.to_string(),
default_token: DEFAULT_TOKEN.to_string(),
tokens: hashmap! {
DEFAULT_TOKEN.to_string() => DEFAULT_PLATFORM.to_string()
DEFAULT_TOKEN.to_string().to_lowercase() => default_platform
}
}
};
Expand Down
12 changes: 10 additions & 2 deletions core/payment-driver/zksync/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ use ya_payment_driver::{
cron::PaymentDriverCron,
dao::DbExecutor,
db::models::PaymentEntity,
driver::{async_trait, BigDecimal, IdentityError, IdentityEvent, Network, PaymentDriver},
driver::{
async_trait, BigDecimal, IdentityError, IdentityEvent, Network, PaymentDriver, Platform,
},
model::*,
utils,
};
Expand Down Expand Up @@ -136,12 +138,18 @@ impl PaymentDriver for ZksyncDriver {

fn get_networks(&self) -> HashMap<String, Network> {
// TODO: Implement multi-network support
let default_platform = Platform {
driver: DRIVER_NAME.to_string(),
network: DEFAULT_NETWORK.to_string(),
token: DEFAULT_TOKEN.to_string(),
};

hashmap! {
DEFAULT_NETWORK.to_string() => Network {
name: DEFAULT_NETWORK.to_string(),
default_token: DEFAULT_TOKEN.to_string(),
tokens: hashmap! {
DEFAULT_TOKEN.to_string() => DEFAULT_PLATFORM.to_string()
DEFAULT_TOKEN.to_string().to_lowercase() => default_platform
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/payment-driver/zksync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub const ZKSYNC_TOKEN_NAME: &'static str = "GNT";

pub const DEFAULT_NETWORK: &'static str = "rinkeby";
pub const DEFAULT_TOKEN: &'static str = "tGLM";
pub const DEFAULT_PLATFORM: &'static str = "zksync-rinkeby-tglm";
pub const DEFAULT_PLATFORM: &'static str = "zksync-rinkeby-tglm"; // TODO: remove

pub use service::ZksyncService as PaymentDriverService;

Expand Down
5 changes: 4 additions & 1 deletion core/payment/examples/account_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ async fn main() -> anyhow::Result<()> {
for account in account_list.into_iter() {
let payer_status = bus::service(pay::BUS_ID)
.call(pay::GetStatus {
platform: account.platform.to_string(),
address: account.address.to_string(),
platform: Some(account.platform.to_string()),
driver: None,
network: None,
token: None,
})
.await??;

Expand Down
10 changes: 8 additions & 2 deletions core/payment/examples/cancel_invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,22 @@ async fn assert_requested_amount(
) -> anyhow::Result<()> {
let payer_status = bus::service(pay::BUS_ID)
.call(pay::GetStatus {
platform: payment_platform.to_string(),
address: payer_addr.to_string(),
platform: Some(payment_platform.to_string()),
driver: None,
network: None,
token: None,
})
.await??;
assert_eq!(&payer_status.outgoing.requested.total_amount, amount);

let payee_status = bus::service(pay::BUS_ID)
.call(pay::GetStatus {
platform: payment_platform.to_string(),
address: payee_addr.to_string(),
platform: Some(payment_platform.to_string()),
driver: None,
network: None,
token: None,
})
.await??;
assert_eq!(&payee_status.incoming.requested.total_amount, amount);
Expand Down
5 changes: 4 additions & 1 deletion core/payment/examples/validate_allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ async fn get_requestor_balance_and_platform() -> anyhow::Result<(BigDecimal, Str
if account.send {
let status = bus::service(pay::BUS_ID)
.call(pay::GetStatus {
platform: account.platform.clone(),
address: account.address.clone(),
platform: Some(account.platform.clone()),
driver: None,
network: None,
token: None,
})
.await??;
return Ok((status.amount, account.platform));
Expand Down
48 changes: 46 additions & 2 deletions core/payment/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub enum PaymentCli {
driver: String,
#[structopt(long)]
network: Option<String>,
#[structopt(long)]
platform: Option<String>,
},
Accounts,
Invoice {
Expand Down Expand Up @@ -71,17 +73,59 @@ impl PaymentCli {
account,
driver,
network,
platform,
} => {
let address = resolve_address(account).await?;
let status = bus::service(pay::BUS_ID)
.call(pay::GetStatus {
address,
driver,
platform,
driver: Some(driver),
network,
token: None,
})
.await??;
CommandOutput::object(status) // TODO: render as table
if ctx.json_output {
CommandOutput::object(status)
} else {
Ok(ResponseTable {
columns: vec![
"platform".to_owned(),
"total amount".to_owned(),
"reserved".to_owned(),
"amount".to_owned(),
"incoming".to_owned(),
"outgoing".to_owned(),
],
values: vec![
serde_json::json! {[
format!("driver: {}", status.platform.driver),
format!("{} {}", status.amount, status.platform.token),
format!("{} {}", status.reserved, status.platform.token),
"accepted",
status.incoming.accepted.total_amount,
status.outgoing.accepted.total_amount,
]},
serde_json::json! {[
format!("network: {}", status.platform.network),
"",
"",
"confirmed",
status.incoming.confirmed.total_amount,
status.outgoing.confirmed.total_amount,
]},
serde_json::json! {[
format!("token: {}", status.platform.token),
"",
"",
"requested",
status.incoming.requested.total_amount,
status.outgoing.requested.total_amount,
]},
],
}
.into())
}
}
PaymentCli::Accounts => {
let accounts = bus::service(pay::BUS_ID)
Expand Down
Loading