Skip to content

Commit

Permalink
Add enter / exit / transfer commands to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
maaktweluit committed Jan 18, 2021
1 parent dde6dc9 commit 1e8b68d
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 9 deletions.
89 changes: 89 additions & 0 deletions core/model/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
token: Option<String>,
}

impl Enter {
pub fn new(amount: String, network: Option<String>, token: Option<String>) -> 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<String>,
amount: Option<String>,
network: Option<String>,
token: Option<String>,
}

impl Exit {
pub fn new(
to: Option<String>,
amount: Option<String>,
network: Option<String>,
token: Option<String>,
) -> 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<String>,
token: Option<String>,
}

impl Transfer {
pub fn new(
to: String,
amount: String,
network: Option<String>,
token: Option<String>,
) -> Transfer {
Transfer {
to,
amount,
network,
token,
}
}
}

impl RpcMessage for Transfer {
const ID: &'static str = "Transfer";
type Item = String; // Transaction Identifier
type Error = GenericError;
}
13 changes: 11 additions & 2 deletions core/payment-driver/base/src/bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,26 @@ pub async fn bind_service<Driver: PaymentDriver + 'static>(
#[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 }
)
.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 }
)
Expand Down
17 changes: 17 additions & 0 deletions core/payment-driver/base/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ pub trait PaymentDriver {
msg: GetAccountBalance,
) -> Result<BigDecimal, GenericError>;

async fn enter(
&self,
db: DbExecutor,
caller: String,
msg: Enter,
) -> Result<String, GenericError>;

async fn exit(&self, db: DbExecutor, caller: String, msg: Exit)
-> Result<String, GenericError>;

// used by bus to bind service
fn get_name(&self) -> String;
fn get_platform(&self) -> String;
Expand All @@ -46,6 +56,13 @@ pub trait PaymentDriver {

async fn init(&self, db: DbExecutor, caller: String, msg: Init) -> Result<Ack, GenericError>;

async fn transfer(
&self,
db: DbExecutor,
caller: String,
msg: Transfer,
) -> Result<String, GenericError>;

async fn schedule_payment(
&self,
db: DbExecutor,
Expand Down
30 changes: 30 additions & 0 deletions core/payment-driver/zksync/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,26 @@ impl PaymentDriver for ZksyncDriver {
Ok(())
}

async fn enter(
&self,
_db: DbExecutor,
_caller: String,
msg: Enter,
) -> Result<String, GenericError> {
log::info!("ENTER = Not Implemented: {:?}", msg);
Ok("NOT_IMPLEMENTED".to_string())
}

async fn exit(
&self,
_db: DbExecutor,
_caller: String,
msg: Exit,
) -> Result<String, GenericError> {
log::info!("EXIT = Not Implemented: {:?}", msg);
Ok("NOT_IMPLEMENTED".to_string())
}

async fn get_account_balance(
&self,
_db: DbExecutor,
Expand Down Expand Up @@ -173,6 +193,16 @@ impl PaymentDriver for ZksyncDriver {
Ok(Ack {})
}

async fn transfer(
&self,
_db: DbExecutor,
_caller: String,
msg: Transfer,
) -> Result<String, GenericError> {
log::info!("TRANSFER = Not Implemented: {:?}", msg);
Ok("NOT_IMPLEMENTED".to_string())
}

async fn schedule_payment(
&self,
_db: DbExecutor,
Expand Down
63 changes: 56 additions & 7 deletions core/payment/src/cli.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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<String>,
#[structopt(long, short)]
token: Option<String>,
},
Exit {
to: Option<String>,
amount: Option<String>,
#[structopt(long, default_value = DEFAULT_PAYMENT_DRIVER)]
driver: String,
#[structopt(long, short)]
network: Option<String>,
#[structopt(long, short)]
token: Option<String>,
},
Init {
address: Option<String>,
#[structopt(long, short)]
Expand All @@ -18,17 +38,26 @@ pub enum PaymentCli {
#[structopt(long, default_value = DEFAULT_PAYMENT_DRIVER)]
driver: String,
},
Status {
address: Option<String>,
#[structopt(long, short)]
platform: Option<String>,
},
Accounts,
Invoice {
address: Option<String>,
#[structopt(subcommand)]
command: InvoiceCommand,
},
Transfer {
amount: String,
to: String,
#[structopt(long, default_value = DEFAULT_PAYMENT_DRIVER)]
driver: String,
#[structopt(long, short)]
network: Option<String>,
#[structopt(long, short)]
token: Option<String>,
},
Status {
address: Option<String>,
#[structopt(long, short)]
platform: Option<String>,
},
}

#[derive(StructOpt, Debug)]
Expand Down Expand Up @@ -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?),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions core/payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
40 changes: 40 additions & 0 deletions core/payment/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
token: Option<String>,
) -> anyhow::Result<String> {
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<String>,
amount: Option<String>,
driver: String,
network: Option<String>,
token: Option<String>,
) -> anyhow::Result<String> {
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<String>,
token: Option<String>,
) -> anyhow::Result<String> {
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)
}

0 comments on commit 1e8b68d

Please sign in to comment.