Skip to content

Commit

Permalink
feat: show paid and returned fees in cli
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Feb 19, 2024
1 parent 87fb8cd commit 8a7eadc
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 39 deletions.
8 changes: 6 additions & 2 deletions integrationtests/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use moksha_core::primitives::PaymentMethod;
use moksha_core::primitives::{CurrencyUnit, PaymentMethod};

use moksha_wallet::client::CashuClient;
use moksha_wallet::http::CrossPlatformHttpClient;
Expand Down Expand Up @@ -116,7 +116,11 @@ pub fn test_integration() -> anyhow::Result<()> {

// pay ln-invoice
let invoice_1000 = read_fixture("invoice_1000.txt").unwrap();
let result_pay_invoice = wallet.pay_invoice(invoice_1000).await;
let quote = wallet
.get_melt_quote_bolt11(invoice_1000.clone(), CurrencyUnit::Sat)
.await
.expect("Could not get melt quote");
let result_pay_invoice = wallet.pay_invoice(&quote, invoice_1000).await;
if result_pay_invoice.is_err() {
println!("error in pay_invoice{:?}", result_pay_invoice);
}
Expand Down
58 changes: 37 additions & 21 deletions moksha-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::{Parser, Subcommand};
use dialoguer::{theme::ColorfulTheme, Confirm, Select};
use moksha_core::primitives::{
PaymentMethod, PostMeltOnchainResponse, PostMintQuoteBolt11Response,
CurrencyUnit, PaymentMethod, PostMeltOnchainResponse, PostMintQuoteBolt11Response,
PostMintQuoteOnchainResponse,
};
use moksha_wallet::http::CrossPlatformHttpClient;
Expand All @@ -26,34 +26,24 @@ struct Opts {
#[derive(Subcommand, Clone)]
enum Command {
/// Mint tokens
Mint {
amount: u64,
},
Mint { amount: u64 },

/// Pay Lightning invoice
Pay {
invoice: String,
},
Pay { invoice: String },

/// Pay bitcoin onchain
PayOnchain {
address: String,
amount: u64,
},
/// Pay Bitcoin on chain
PayOnchain { address: String, amount: u64 },

/// Send tokens
Send {
amount: u64,
},
Send { amount: u64 },

/// Receive tokens
Receive {
token: String,
},
Receive { token: String },

/// Show local balance
Balance,

/// Show version and configuration
Info,
}

Expand Down Expand Up @@ -122,11 +112,35 @@ async fn main() -> anyhow::Result<()> {
println!("Balance: {balance} (sat)");
}
Command::Pay { invoice } => {
let response = wallet.pay_invoice(invoice).await?;
let quote = wallet
.get_melt_quote_bolt11(invoice.clone(), CurrencyUnit::Sat)
.await?;

let pay_confirmed = Confirm::new()
.with_prompt(format!(
"Pay lightning invoice: amount {} + fee {} = {} (sat)?",
quote.amount,
quote.fee_reserve,
quote.amount + quote.fee_reserve
))
.interact()
.unwrap();

if !pay_confirmed {
return Ok(());
}

let response = wallet.pay_invoice(&quote, invoice).await?;

// FIXME handle not enough tokens error

if response.paid {
if response.0.paid {
if response.1 > 0 {
println!(
"Returned fees {} (sat)",
response.1.to_formatted_string(&Locale::en)
);
}
println!(
"\nInvoice has been paid: Tokens melted successfully\nNew balance: {} (sat)",
wallet.get_balance().await?.to_formatted_string(&Locale::en)
Expand All @@ -143,7 +157,9 @@ async fn main() -> anyhow::Result<()> {
return Ok(());
}

let quotes = wallet.pay_onchain_quote(address.clone(), amount).await?;
let quotes = wallet
.get_melt_quote_btconchain(address.clone(), amount)
.await?;

if quotes.is_empty() {
println!("Error: No quotes found");
Expand Down
45 changes: 29 additions & 16 deletions moksha-wallet/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use moksha_core::{
keyset::V1Keyset,
primitives::{
CurrencyUnit, KeyResponse, MintInfoResponse, PaymentMethod, PostMeltBolt11Response,
PostMeltOnchainResponse, PostMeltQuoteOnchainResponse, PostMintQuoteBolt11Response,
PostMintQuoteOnchainResponse,
PostMeltOnchainResponse, PostMeltQuoteBolt11Response, PostMeltQuoteOnchainResponse,
PostMintQuoteBolt11Response, PostMintQuoteOnchainResponse,
},
proof::{Proof, Proofs},
token::TokenV3,
Expand Down Expand Up @@ -264,17 +264,23 @@ where
.await
}

pub async fn get_melt_quote_bolt11(
&self,
invoice: String,
currency: CurrencyUnit,
) -> Result<PostMeltQuoteBolt11Response, MokshaWalletError> {
self.client
.post_melt_quote_bolt11(&self.mint_url, invoice.clone(), currency)
.await
}

pub async fn pay_invoice(
&self,
melt_quote: &PostMeltQuoteBolt11Response,
invoice: String,
) -> Result<PostMeltBolt11Response, MokshaWalletError> {
) -> Result<(PostMeltBolt11Response, u64), MokshaWalletError> {
let all_proofs = self.localstore.get_proofs().await?;

let melt_quote = self
.client
.post_melt_quote_bolt11(&self.mint_url, invoice.clone(), CurrencyUnit::Sat)
.await?;

let ln_amount = Self::get_invoice_amount(&invoice)? + melt_quote.fee_reserve;

if ln_amount > all_proofs.total_amount() {
Expand Down Expand Up @@ -314,7 +320,7 @@ where
.collect::<Vec<(BlindedMessage, SecretKey)>>();

match self
.melt_token(melt_quote.quote, ln_amount, &total_proofs, msgs)
.melt_token(melt_quote.to_owned().quote, ln_amount, &total_proofs, msgs)
.await
{
Ok(response) => {
Expand All @@ -328,7 +334,7 @@ where
)?;
self.localstore.add_proofs(&change_proofs).await?;

Ok(response)
Ok((response, change_proofs.total_amount()))
}
Err(e) => {
self.localstore.add_proofs(&total_proofs).await?;
Expand All @@ -337,7 +343,7 @@ where
}
}

pub async fn pay_onchain_quote(
pub async fn get_melt_quote_btconchain(
&self,
address: String,
amount: u64,
Expand Down Expand Up @@ -858,8 +864,12 @@ mod tests {
// 21 sats
let invoice = "lnbcrt210n1pjg6mqhpp5pza5wzh0csjjuvfpjpv4zdjmg30vedj9ycv5tyfes9x7dp8axy0sdqqcqzzsxqyz5vqsp5vtxg4c5tw2s2zxxya2a7an0psn9mcfmlqctxzntm3sngnpyk3muq9qyyssqf8z5f90yu3wrmsufnnza25qjlnvc6ukdr094ckzn63ktcy6z5fw5mxf9skndpg2p4648gfjfvvx4qg2lqvlryyycg5k7x9h4dw70t4qq37pegm".to_string();

let result = wallet.pay_invoice(invoice).await?;
assert!(result.paid);
let quote = wallet
.get_melt_quote_bolt11(invoice.clone(), CurrencyUnit::Sat)
.await?;

let result = wallet.pay_invoice(&quote, invoice).await?;
assert!(result.0.paid);
Ok(())
}

Expand Down Expand Up @@ -909,10 +919,13 @@ mod tests {
// 21 sats
let invoice = "lnbcrt210n1pjg6mqhpp5pza5wzh0csjjuvfpjpv4zdjmg30vedj9ycv5tyfes9x7dp8axy0sdqqcqzzsxqyz5vqsp5vtxg4c5tw2s2zxxya2a7an0psn9mcfmlqctxzntm3sngnpyk3muq9qyyssqf8z5f90yu3wrmsufnnza25qjlnvc6ukdr094ckzn63ktcy6z5fw5mxf9skndpg2p4648gfjfvvx4qg2lqvlryyycg5k7x9h4dw70t4qq37pegm".to_string();

let result = wallet.pay_invoice(invoice).await?;
assert!(!result.paid);
let quote = wallet
.get_melt_quote_bolt11(invoice.clone(), CurrencyUnit::Sat)
.await?;
let result = wallet.pay_invoice(&quote, invoice).await?;
assert!(!result.0.paid);
assert_eq!(64, localstore.get_proofs().await?.total_amount());
assert!(!result.paid);
assert!(!result.0.paid);
Ok(())
}
}

0 comments on commit 8a7eadc

Please sign in to comment.