Skip to content

Commit

Permalink
feat: onchain receive
Browse files Browse the repository at this point in the history
  • Loading branch information
sandipndev committed Jul 28, 2023
1 parent d9464ea commit 13bc2d9
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 19 deletions.
19 changes: 3 additions & 16 deletions src/app/operations/intraledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use anyhow::{Context, Result};

use rust_decimal::Decimal;

use crate::{
app::{errors::payment_error::PaymentError, App},
client::{queries::query_me::WalletCurrency, types::Wallet},
};
use crate::{app::App, client::types::Wallet};

impl App {
pub async fn intraledger_payment(
Expand All @@ -22,12 +19,7 @@ impl App {

match (wallet, sats, cents) {
(Wallet::Btc, Some(sats), _) => {
let btc_wallet_id = sender_wallets
.iter()
.find(|wallet| wallet.wallet_currency == WalletCurrency::BTC)
.map(|wallet| &wallet.id)
.ok_or_else(|| PaymentError::FailedToGetWallet("BTC".to_string()))
.map(|id| id.to_owned())?;
let btc_wallet_id = self.get_user_btc_wallet_id(sender_wallets)?;

self.client
.intraleger_send_btc(btc_wallet_id, recipient_wallet_id, sats, memo)
Expand All @@ -37,12 +29,7 @@ impl App {
println!("Successfully sent {} sats to username: {}", sats, username);
}
(Wallet::Usd, _, Some(cents)) => {
let usd_wallet_id = sender_wallets
.iter()
.find(|wallet| wallet.wallet_currency == WalletCurrency::USD)
.map(|wallet| &wallet.id)
.ok_or_else(|| PaymentError::FailedToGetWallet("USD".to_string()))
.map(|id| id.to_owned())?;
let usd_wallet_id = self.get_user_usd_wallet_id(sender_wallets)?;

self.client
.intraleger_send_usd(usd_wallet_id, recipient_wallet_id, cents, memo)
Expand Down
1 change: 1 addition & 0 deletions src/app/operations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod auth;
pub mod batch;
pub mod globals;
pub mod intraledger;
pub mod onchain;
pub mod request_phone_code;
pub mod user;
pub mod wallet;
33 changes: 31 additions & 2 deletions src/app/operations/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ use std::collections::HashSet;
use anyhow::{Context, Result};

use crate::{
app::App,
client::types::{Wallet, WalletBalance},
app::{errors::payment_error::PaymentError, App},
client::{
queries::query_me::{QueryMeMeDefaultAccountWallets, WalletCurrency},
types::{Wallet, WalletBalance},
},
};

impl App {
Expand Down Expand Up @@ -59,4 +62,30 @@ impl App {
println!("{}", balances_json);
Ok(())
}

pub fn get_user_btc_wallet_id(
&self,
wallets: Vec<QueryMeMeDefaultAccountWallets>,
) -> Result<String> {
let btc_wallet_id = wallets
.iter()
.find(|wallet| wallet.wallet_currency == WalletCurrency::BTC)
.map(|wallet| &wallet.id)
.ok_or_else(|| PaymentError::FailedToGetWallet("BTC".to_string()))
.map(|id| id.to_owned())?;
Ok(btc_wallet_id)
}

pub fn get_user_usd_wallet_id(
&self,
wallets: Vec<QueryMeMeDefaultAccountWallets>,
) -> Result<String> {
let usd_wallet_id = wallets
.iter()
.find(|wallet| wallet.wallet_currency == WalletCurrency::USD)
.map(|wallet| &wallet.id)
.ok_or_else(|| PaymentError::FailedToGetWallet("USD".to_string()))
.map(|id| id.to_owned())?;
Ok(usd_wallet_id)
}
}
9 changes: 8 additions & 1 deletion src/cli/commands.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::{Parser, Subcommand};

use crate::client::types::Wallet;
use crate::client::types::{ReceiveVia, Wallet};
use rust_decimal::Decimal;

#[derive(Parser)]
Expand Down Expand Up @@ -64,6 +64,13 @@ pub enum Command {
#[clap(short, long)]
memo: Option<String>,
},
/// Receive a Payment
Receive {
#[clap(short, long, value_parser)]
wallet: Wallet,
#[clap(short, long, value_parser)]
via: ReceiveVia,
},
/// execute a batch payment
Batch {
#[clap(short, long = "csv")]
Expand Down
3 changes: 3 additions & 0 deletions src/cli/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub async fn run() -> anyhow::Result<()> {
app.intraledger_payment(username, wallet, cents, sats, memo)
.await?;
}
Command::Receive { wallet, via } => {
app.receive(wallet, via).await?;
}
Command::Batch { file } => {
app.batch_payment(file).await?;
}
Expand Down
11 changes: 11 additions & 0 deletions src/client/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type SignedAmount = Decimal;
type SatAmount = Decimal;
type CentAmount = Decimal;
type Memo = String;
type OnChainAddress = String;

#[derive(GraphQLQuery)]
#[graphql(
Expand Down Expand Up @@ -137,3 +138,13 @@ pub(super) struct IntraLedgerUsdPaymentSend;
pub use self::intra_ledger_usd_payment_send::IntraLedgerUsdPaymentSendInput;

use super::errors::captcha_error::CaptchaError;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "src/client/gql/schema.gql",
query_path = "src/client/gql/mutations/onchain_address_current.gql",
response_derives = "Debug, Serialize"
)]
pub(super) struct OnChainAddressCurrent;
pub use self::on_chain_address_current::OnChainAddressCurrentInput;
pub use self::on_chain_address_current::OnChainAddressCurrentOnChainAddressCurrent;
1 change: 1 addition & 0 deletions src/client/requests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ mod default_wallet;
mod globals;
mod intraledger;
mod me;
mod onchain;
mod set_username;
5 changes: 5 additions & 0 deletions src/client/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ pub struct WalletBalance {
pub id: Option<String>,
pub default: bool,
}

#[derive(Debug, Clone, clap::ValueEnum, PartialEq, Eq)]
pub enum ReceiveVia {
Onchain,
}

0 comments on commit 13bc2d9

Please sign in to comment.