From 1e8b68df3687ee328f15e805e9acbad6b5bbac90 Mon Sep 17 00:00:00 2001 From: maaktweluit <10008353+maaktweluit@users.noreply.github.com> Date: Mon, 18 Jan 2021 11:29:41 +0100 Subject: [PATCH] Add enter / exit / transfer commands to CLI --- core/model/src/driver.rs | 89 ++++++++++++++++++++++++ core/payment-driver/base/src/bus.rs | 13 +++- core/payment-driver/base/src/driver.rs | 17 +++++ core/payment-driver/zksync/src/driver.rs | 30 ++++++++ core/payment/src/cli.rs | 63 +++++++++++++++-- core/payment/src/lib.rs | 1 + core/payment/src/wallet.rs | 40 +++++++++++ 7 files changed, 244 insertions(+), 9 deletions(-) create mode 100644 core/payment/src/wallet.rs diff --git a/core/model/src/driver.rs b/core/model/src/driver.rs index 1ff3601efa..a530dbc4f0 100644 --- a/core/model/src/driver.rs +++ b/core/model/src/driver.rs @@ -236,3 +236,92 @@ impl RpcMessage for ValidateAllocation { type Item = bool; type Error = GenericError; } + +// ************************** ENTER ************************** + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Enter { + amount: String, + network: Option, + token: Option, +} + +impl Enter { + pub fn new(amount: String, network: Option, token: Option) -> Enter { + Enter { + amount, + network, + token, + } + } +} + +impl RpcMessage for Enter { + const ID: &'static str = "Enter"; + type Item = String; // Transaction Identifier + type Error = GenericError; +} + +// ************************** EXIT ************************** + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Exit { + to: Option, + amount: Option, + network: Option, + token: Option, +} + +impl Exit { + pub fn new( + to: Option, + amount: Option, + network: Option, + token: Option, + ) -> Exit { + Exit { + to, + amount, + network, + token, + } + } +} + +impl RpcMessage for Exit { + const ID: &'static str = "Exit"; + type Item = String; // Transaction Identifier + type Error = GenericError; +} + +// ************************** TRANSFER ************************** + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Transfer { + to: String, + amount: String, + network: Option, + token: Option, +} + +impl Transfer { + pub fn new( + to: String, + amount: String, + network: Option, + token: Option, + ) -> Transfer { + Transfer { + to, + amount, + network, + token, + } + } +} + +impl RpcMessage for Transfer { + const ID: &'static str = "Transfer"; + type Item = String; // Transaction Identifier + type Error = GenericError; +} diff --git a/core/payment-driver/base/src/bus.rs b/core/payment-driver/base/src/bus.rs index 8e9bc81815..99953edff8 100644 --- a/core/payment-driver/base/src/bus.rs +++ b/core/payment-driver/base/src/bus.rs @@ -39,10 +39,13 @@ pub async fn bind_service( #[rustfmt::skip] // Keep move's neatly aligned ServiceBinder::new(&bus_id, db, driver.clone()) .bind_with_processor( - move |db, dr, c, m| async move { dr.init(db, c, m).await } + move |db, dr, c, m| async move { dr.account_event(db, c, m).await } ) .bind_with_processor( - move |db, dr, c, m| async move { dr.account_event(db, c, m).await } + move |db, dr, c, m| async move { dr.enter(db, c, m).await } + ) + .bind_with_processor( + move |db, dr, c, m| async move { dr.exit(db, c, m).await } ) .bind_with_processor( move |db, dr, c, m| async move { dr.get_account_balance(db, c, m).await } @@ -50,6 +53,12 @@ pub async fn bind_service( .bind_with_processor( move |db, dr, c, m| async move { dr.get_transaction_balance(db, c, m).await } ) + .bind_with_processor( + move |db, dr, c, m| async move { dr.init(db, c, m).await } + ) + .bind_with_processor( + move |db, dr, c, m| async move { dr.transfer(db, c, m).await } + ) .bind_with_processor( move |db, dr, c, m| async move { dr.schedule_payment(db, c, m).await } ) diff --git a/core/payment-driver/base/src/driver.rs b/core/payment-driver/base/src/driver.rs index 5ecb755216..dad6988570 100644 --- a/core/payment-driver/base/src/driver.rs +++ b/core/payment-driver/base/src/driver.rs @@ -32,6 +32,16 @@ pub trait PaymentDriver { msg: GetAccountBalance, ) -> Result; + async fn enter( + &self, + db: DbExecutor, + caller: String, + msg: Enter, + ) -> Result; + + async fn exit(&self, db: DbExecutor, caller: String, msg: Exit) + -> Result; + // used by bus to bind service fn get_name(&self) -> String; fn get_platform(&self) -> String; @@ -46,6 +56,13 @@ pub trait PaymentDriver { async fn init(&self, db: DbExecutor, caller: String, msg: Init) -> Result; + async fn transfer( + &self, + db: DbExecutor, + caller: String, + msg: Transfer, + ) -> Result; + async fn schedule_payment( &self, db: DbExecutor, diff --git a/core/payment-driver/zksync/src/driver.rs b/core/payment-driver/zksync/src/driver.rs index 9cf8dd678f..8a2ec45eea 100644 --- a/core/payment-driver/zksync/src/driver.rs +++ b/core/payment-driver/zksync/src/driver.rs @@ -108,6 +108,26 @@ impl PaymentDriver for ZksyncDriver { Ok(()) } + async fn enter( + &self, + _db: DbExecutor, + _caller: String, + msg: Enter, + ) -> Result { + log::info!("ENTER = Not Implemented: {:?}", msg); + Ok("NOT_IMPLEMENTED".to_string()) + } + + async fn exit( + &self, + _db: DbExecutor, + _caller: String, + msg: Exit, + ) -> Result { + log::info!("EXIT = Not Implemented: {:?}", msg); + Ok("NOT_IMPLEMENTED".to_string()) + } + async fn get_account_balance( &self, _db: DbExecutor, @@ -173,6 +193,16 @@ impl PaymentDriver for ZksyncDriver { Ok(Ack {}) } + async fn transfer( + &self, + _db: DbExecutor, + _caller: String, + msg: Transfer, + ) -> Result { + log::info!("TRANSFER = Not Implemented: {:?}", msg); + Ok("NOT_IMPLEMENTED".to_string()) + } + async fn schedule_payment( &self, _db: DbExecutor, diff --git a/core/payment/src/cli.rs b/core/payment/src/cli.rs index f3db1daee1..adb24dd050 100644 --- a/core/payment/src/cli.rs +++ b/core/payment/src/cli.rs @@ -1,5 +1,5 @@ use crate::accounts::{init_account, Account}; -use crate::{DEFAULT_PAYMENT_DRIVER, DEFAULT_PAYMENT_PLATFORM}; +use crate::{wallet, DEFAULT_PAYMENT_DRIVER, DEFAULT_PAYMENT_PLATFORM}; use chrono::Utc; use structopt::*; use ya_core_model::{identity as id_api, payment::local as pay}; @@ -9,6 +9,26 @@ use ya_service_bus::{typed as bus, RpcEndpoint}; /// Payment management. #[derive(StructOpt, Debug)] pub enum PaymentCli { + Accounts, + Enter { + amount: String, + #[structopt(long, default_value = DEFAULT_PAYMENT_DRIVER)] + driver: String, + #[structopt(long, short)] + network: Option, + #[structopt(long, short)] + token: Option, + }, + Exit { + to: Option, + amount: Option, + #[structopt(long, default_value = DEFAULT_PAYMENT_DRIVER)] + driver: String, + #[structopt(long, short)] + network: Option, + #[structopt(long, short)] + token: Option, + }, Init { address: Option, #[structopt(long, short)] @@ -18,17 +38,26 @@ pub enum PaymentCli { #[structopt(long, default_value = DEFAULT_PAYMENT_DRIVER)] driver: String, }, - Status { - address: Option, - #[structopt(long, short)] - platform: Option, - }, - Accounts, Invoice { address: Option, #[structopt(subcommand)] command: InvoiceCommand, }, + Transfer { + amount: String, + to: String, + #[structopt(long, default_value = DEFAULT_PAYMENT_DRIVER)] + driver: String, + #[structopt(long, short)] + network: Option, + #[structopt(long, short)] + token: Option, + }, + Status { + address: Option, + #[structopt(long, short)] + platform: Option, + }, } #[derive(StructOpt, Debug)] @@ -112,6 +141,26 @@ impl PaymentCli { .await??, ) } + PaymentCli::Enter { + amount, + driver, + network, + token, + } => CommandOutput::object(wallet::enter(amount, driver, network, token).await?), + PaymentCli::Exit { + to, + amount, + driver, + network, + token, + } => CommandOutput::object(wallet::exit(to, amount, driver, network, token).await?), + PaymentCli::Transfer { + to, + amount, + driver, + network, + token, + } => CommandOutput::object(wallet::transfer(to, amount, driver, network, token).await?), } } } diff --git a/core/payment/src/lib.rs b/core/payment/src/lib.rs index bed54b9fd8..0d8dd402f5 100644 --- a/core/payment/src/lib.rs +++ b/core/payment/src/lib.rs @@ -17,6 +17,7 @@ pub mod processor; pub mod schema; pub mod service; pub mod utils; +mod wallet; pub mod migrations { #[derive(diesel_migrations::EmbedMigrations)] diff --git a/core/payment/src/wallet.rs b/core/payment/src/wallet.rs new file mode 100644 index 0000000000..8847856262 --- /dev/null +++ b/core/payment/src/wallet.rs @@ -0,0 +1,40 @@ +use ya_core_model::driver::{driver_bus_id, Enter, Exit, Transfer}; +use ya_service_bus::typed as bus; + +pub async fn enter( + amount: String, + driver: String, + network: Option, + token: Option, +) -> anyhow::Result { + let driver_id = driver_bus_id(driver); + let message = Enter::new(amount, network, token); + let tx_id = bus::service(driver_id).call(message).await??; + Ok(tx_id) +} + +pub async fn exit( + to: Option, + amount: Option, + driver: String, + network: Option, + token: Option, +) -> anyhow::Result { + let driver_id = driver_bus_id(driver); + let message = Exit::new(to, amount, network, token); + let tx_id = bus::service(driver_id).call(message).await??; + Ok(tx_id) +} + +pub async fn transfer( + to: String, + amount: String, + driver: String, + network: Option, + token: Option, +) -> anyhow::Result { + let driver_id = driver_bus_id(driver); + let message = Transfer::new(to, amount, network, token); + let tx_id = bus::service(driver_id).call(message).await??; + Ok(tx_id) +}