Skip to content

Commit

Permalink
chore: remove get_melt_onchain by txid
Browse files Browse the repository at this point in the history
fixes #313
  • Loading branch information
ngutech21 committed Jul 31, 2024
1 parent 76dbc53 commit f7f2097
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 88 deletions.
15 changes: 15 additions & 0 deletions integrationtests/src/lnd_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,21 @@ impl LndClient {
Ok(amount_in_sat as u64)
}

pub async fn is_transaction_paid(&self, txid: &str) -> anyhow::Result<bool> {
let request = ListUnspentRequest {
min_confs: 0,
max_confs: i32::MAX,
..Default::default()
};

let response = self.wallet_lock().await?.list_unspent(request).await?;

Ok(response.get_ref().utxos.iter().any(|utxo| {
utxo.outpoint.clone().expect("No outpoint found").txid_str == txid
&& utxo.confirmations > 0
}))
}

pub async fn connect_to_peer(&self, peer_pubkey: &str, host_port: &str) -> anyhow::Result<()> {
self.wait_for_node_sync().await?;
let mut client = self.client_lock().await?;
Expand Down
4 changes: 3 additions & 1 deletion integrationtests/tests/tests_lnd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ async fn test_btc_onchain_mint_melt() -> anyhow::Result<()> {

let txid = result.txid.expect("No txid returned from mint");

let is_tx_paid = wallet.is_onchain_tx_paid(&mint_url, txid).await?;
let lnd_client = LndClient::new_mint_lnd().await?;

let is_tx_paid = lnd_client.is_transaction_paid(&txid).await?;
assert!(is_tx_paid);

Ok(())
Expand Down
8 changes: 5 additions & 3 deletions moksha-cli/src/bin/moksha-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,11 @@ async fn main() -> anyhow::Result<()> {
loop {
tokio::time::sleep(std::time::Duration::from_millis(2_000)).await;

let txid = txid.clone();

if paid || txid.as_ref().is_some() && wallet.is_onchain_tx_paid(&mint_url, txid.expect("invalid txid")).await? {
if paid
|| wallet
.is_onchain_paid(&mint_url, quote.quote.clone())
.await?
{
progress_bar.finish_with_message("\nTokens melted successfully\n");
cli::show_total_balance(&wallet).await?;
break;
Expand Down
5 changes: 0 additions & 5 deletions moksha-core/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,6 @@ impl From<BtcOnchainMeltQuote> for PostMeltQuoteBtcOnchainResponse {
}
}

#[derive(Deserialize, Serialize, Debug, Clone, ToSchema)]
pub struct GetMeltBtcOnchainResponse {
pub paid: bool,
}

#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, ToSchema)]
pub struct Nuts {
/// Minting tokens
Expand Down
39 changes: 8 additions & 31 deletions moksha-mint/src/routes/btconchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use axum::{
Json,
};
use moksha_core::primitives::{
BtcOnchainMeltQuote, BtcOnchainMintQuote, CurrencyUnit, GetMeltBtcOnchainResponse,
PaymentMethod, PostMeltBtcOnchainRequest, PostMeltBtcOnchainResponse,
PostMeltQuoteBtcOnchainRequest, PostMeltQuoteBtcOnchainResponse, PostMintBtcOnchainRequest,
PostMintBtcOnchainResponse, PostMintQuoteBtcOnchainRequest, PostMintQuoteBtcOnchainResponse,
BtcOnchainMeltQuote, BtcOnchainMintQuote, CurrencyUnit, PaymentMethod,
PostMeltBtcOnchainRequest, PostMeltBtcOnchainResponse, PostMeltQuoteBtcOnchainRequest,
PostMeltQuoteBtcOnchainResponse, PostMintBtcOnchainRequest, PostMintBtcOnchainResponse,
PostMintQuoteBtcOnchainRequest, PostMintQuoteBtcOnchainResponse,
};
use tracing::{info, instrument};
use uuid::Uuid;
Expand Down Expand Up @@ -286,33 +286,10 @@ pub async fn post_melt_btconchain(
.await?;
tx.commit().await?;

Ok(Json(PostMeltBtcOnchainResponse { paid, txid:Some(txid) }))
}

#[utoipa::path(
get,
path = "/v1/melt/btconchain/{tx_id}",
responses(
(status = 200, description = "is transaction paid", body = [GetMeltOnchainResponse])
),
params(
("tx_id" = String, Path, description = "Bitcoin onchain transaction-id"),
)
)]
#[instrument(name = "get_melt_btconchain", skip(mint), err)]
pub async fn get_melt_btconchain(
Path(tx_id): Path<String>,
State(mint): State<Mint>,
) -> Result<Json<GetMeltBtcOnchainResponse>, MokshaMintError> {
info!("is transaction paid: {}", tx_id);
let paid = mint
.onchain
.as_ref()
.expect("onchain not set")
.is_transaction_paid(&tx_id)
.await?;

Ok(Json(GetMeltBtcOnchainResponse { paid }))
Ok(Json(PostMeltBtcOnchainResponse {
paid,
txid: Some(txid),
}))
}

async fn is_onchain_paid(
Expand Down
25 changes: 10 additions & 15 deletions moksha-mint/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::routes::btconchain::{
get_melt_btconchain, get_melt_quote_btconchain, get_mint_quote_btconchain,
post_melt_btconchain, post_melt_quote_btconchain, post_mint_btconchain,
post_mint_quote_btconchain,
get_melt_quote_btconchain, get_mint_quote_btconchain, post_melt_btconchain,
post_melt_quote_btconchain, post_mint_btconchain, post_mint_quote_btconchain,
};
use crate::routes::default::{
get_info, get_keys, get_keys_by_id, get_keysets, get_melt_quote_bolt11, get_mint_quote_bolt11,
Expand All @@ -25,15 +24,14 @@ use crate::mint::Mint;
use moksha_core::blind::BlindedMessage;
use moksha_core::blind::BlindedSignature;
use moksha_core::primitives::{
ContactInfoResponse, CurrencyUnit, GetMeltBtcOnchainResponse, KeyResponse, KeysResponse,
MintInfoResponse, Nut10, Nut11, Nut12, Nut13, Nut14, Nut15, Nut16, Nut17, Nut18, Nut19, Nut4,
Nut5, Nut7, Nut8, Nut9, Nuts, PaymentMethod, PaymentMethodConfig,
PaymentMethodConfigBtcOnchainMelt, PaymentMethodConfigBtcOnchainMint, PostMeltBolt11Request,
PostMeltBolt11Response, PostMeltQuoteBolt11Request, PostMeltQuoteBolt11Response,
PostMeltQuoteBtcOnchainRequest, PostMeltQuoteBtcOnchainResponse, PostMintBolt11Request,
PostMintBolt11Response, PostMintQuoteBolt11Request, PostMintQuoteBolt11Response,
PostMintQuoteBtcOnchainRequest, PostMintQuoteBtcOnchainResponse, PostSwapRequest,
PostSwapResponse,
ContactInfoResponse, CurrencyUnit, KeyResponse, KeysResponse, MintInfoResponse, Nut10, Nut11,
Nut12, Nut13, Nut14, Nut15, Nut16, Nut17, Nut18, Nut19, Nut4, Nut5, Nut7, Nut8, Nut9, Nuts,
PaymentMethod, PaymentMethodConfig, PaymentMethodConfigBtcOnchainMelt,
PaymentMethodConfigBtcOnchainMint, PostMeltBolt11Request, PostMeltBolt11Response,
PostMeltQuoteBolt11Request, PostMeltQuoteBolt11Response, PostMeltQuoteBtcOnchainRequest,
PostMeltQuoteBtcOnchainResponse, PostMintBolt11Request, PostMintBolt11Response,
PostMintQuoteBolt11Request, PostMintQuoteBolt11Response, PostMintQuoteBtcOnchainRequest,
PostMintQuoteBtcOnchainResponse, PostSwapRequest, PostSwapResponse,
};

use tower_http::services::ServeDir;
Expand Down Expand Up @@ -112,7 +110,6 @@ pub async fn run_server(mint: Mint) -> anyhow::Result<()> {
crate::routes::btconchain::post_melt_quote_btconchain,
crate::routes::btconchain::get_melt_quote_btconchain,
crate::routes::btconchain::post_melt_btconchain,
crate::routes::btconchain::get_melt_btconchain
),
components(schemas(
MintInfoResponse,
Expand Down Expand Up @@ -157,7 +154,6 @@ pub async fn run_server(mint: Mint) -> anyhow::Result<()> {
PostMintQuoteBtcOnchainResponse,
PostMeltQuoteBtcOnchainRequest,
PostMeltQuoteBtcOnchainResponse,
GetMeltBtcOnchainResponse,
ContactInfoResponse,
PaymentMethodConfig,
PaymentMethodConfigBtcOnchainMint,
Expand Down Expand Up @@ -201,7 +197,6 @@ fn app(mint: Mint) -> Router {
get(get_melt_quote_btconchain),
)
.route("/v1/melt/btconchain", post(post_melt_btconchain))
.route("/v1/melt/btconchain/:txid", get(get_melt_btconchain))
} else {
Router::new()
};
Expand Down
21 changes: 6 additions & 15 deletions moksha-wallet/src/client/crossplatform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use moksha_core::{
blind::BlindedMessage,
keyset::Keysets,
primitives::{
CurrencyUnit, GetMeltBtcOnchainResponse, KeysResponse, MintInfoResponse,
PostMeltBolt11Request, PostMeltBolt11Response, PostMeltBtcOnchainRequest,
PostMeltBtcOnchainResponse, PostMeltQuoteBolt11Request, PostMeltQuoteBolt11Response,
PostMeltQuoteBtcOnchainRequest, PostMeltQuoteBtcOnchainResponse, PostMintBolt11Request,
PostMintBolt11Response, PostMintBtcOnchainRequest, PostMintBtcOnchainResponse,
PostMintQuoteBolt11Request, PostMintQuoteBolt11Response, PostMintQuoteBtcOnchainRequest,
CurrencyUnit, KeysResponse, MintInfoResponse, PostMeltBolt11Request,
PostMeltBolt11Response, PostMeltBtcOnchainRequest, PostMeltBtcOnchainResponse,
PostMeltQuoteBolt11Request, PostMeltQuoteBolt11Response, PostMeltQuoteBtcOnchainRequest,
PostMeltQuoteBtcOnchainResponse, PostMintBolt11Request, PostMintBolt11Response,
PostMintBtcOnchainRequest, PostMintBtcOnchainResponse, PostMintQuoteBolt11Request,
PostMintQuoteBolt11Response, PostMintQuoteBtcOnchainRequest,
PostMintQuoteBtcOnchainResponse, PostSwapRequest, PostSwapResponse,
},
proof::Proofs,
Expand Down Expand Up @@ -203,13 +203,4 @@ impl CashuClient for CrossPlatformHttpClient {
self.do_get(&mint_url.join(&format!("/v1/melt/quote/btconchain/{quote}"))?)
.await
}

async fn get_melt_onchain(
&self,
mint_url: &Url,
txid: String,
) -> Result<GetMeltBtcOnchainResponse, MokshaWalletError> {
self.do_get(&mint_url.join(&format!("/v1/melt/btconchain/{txid}"))?)
.await
}
}
14 changes: 4 additions & 10 deletions moksha-wallet/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use moksha_core::{
blind::BlindedMessage,
keyset::Keysets,
primitives::{
CurrencyUnit, GetMeltBtcOnchainResponse, KeysResponse, MintInfoResponse,
PostMeltBolt11Response, PostMeltBtcOnchainResponse, PostMeltQuoteBolt11Response,
PostMeltQuoteBtcOnchainResponse, PostMintBolt11Response, PostMintBtcOnchainResponse,
PostMintQuoteBolt11Response, PostMintQuoteBtcOnchainResponse, PostSwapResponse,
CurrencyUnit, KeysResponse, MintInfoResponse, PostMeltBolt11Response,
PostMeltBtcOnchainResponse, PostMeltQuoteBolt11Response, PostMeltQuoteBtcOnchainResponse,
PostMintBolt11Response, PostMintBtcOnchainResponse, PostMintQuoteBolt11Response,
PostMintQuoteBtcOnchainResponse, PostSwapResponse,
},
proof::Proofs,
};
Expand Down Expand Up @@ -125,10 +125,4 @@ pub trait CashuClient {
mint_url: &Url,
quote: String,
) -> Result<PostMeltQuoteBtcOnchainResponse, MokshaWalletError>;

async fn get_melt_onchain(
&self,
mint_url: &Url,
txid: String,
) -> Result<GetMeltBtcOnchainResponse, MokshaWalletError>;
}
8 changes: 0 additions & 8 deletions moksha-wallet/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,6 @@ where
.paid)
}

pub async fn is_onchain_tx_paid(
&self,
mint_url: &Url,
txid: String,
) -> Result<bool, MokshaWalletError> {
Ok(self.client.get_melt_onchain(mint_url, txid).await?.paid)
}

pub async fn get_wallet_keysets(&self) -> Result<Vec<WalletKeyset>, MokshaWalletError> {
let mut tx = self.localstore.begin_tx().await?;
let keysets = self.localstore.get_keysets(&mut tx).await?;
Expand Down

0 comments on commit f7f2097

Please sign in to comment.