Skip to content

Commit

Permalink
retry cashu mint after payment with NWC
Browse files Browse the repository at this point in the history
  • Loading branch information
pablof7z committed Feb 4, 2025
1 parent 915bdfd commit fdbe6e5
Showing 1 changed file with 44 additions and 16 deletions.
60 changes: 44 additions & 16 deletions ndk-wallet/src/wallets/nwc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import NDK, { NDKPool, LnPaymentInfo, NDKPaymentConfirmationCashu, NDKPaymentCon
import { NutPayment } from "../cashu/pay/nut.js";
import { sendReq } from "./req.js";
import createDebug from "debug";
import { NDKNWCGetInfoResult, NDKNWCRequestMap, NDKNWCResponseBase, NDKNWCResponseMap } from "./types.js";
import { NDKNWCGetInfoResult, NDKNWCMakeInvoiceParams, NDKNWCRequestMap, NDKNWCResponseBase, NDKNWCResponseMap } from "./types.js";
import { CashuMint, CashuWallet, MintQuoteResponse } from "@cashu/cashu-ts";

const d = createDebug("ndk-wallet:nwc");
Expand Down Expand Up @@ -128,32 +128,44 @@ export class NDKNWCWallet extends EventEmitter<NDKNWCWalletEvents> implements ND

try {
const res = await this.req("pay_invoice", { invoice: quote.request });
console.log('NWC cashuPay res', res);
d('cashuPay res', res);
} catch (e: any) {
const message = e?.error?.message || e?.message || 'unknown error';
console.error('error paying invoice', e, {message});
throw new Error(message);
}

const mintTokenAttempt = (resolve: (value: any) => void, reject: (reason?: any) => void, attempt: number) => {
// mint the tokens
console.log('minting tokens', {attempt, amount, quote: quote.quote, pubkey: payment.p2pk, mint });

wallet.mintProofs(amount, quote.quote, { pubkey: payment.p2pk }).then(mintProofs => {
console.log('minted tokens', mintProofs);
d('minted tokens', mintProofs);

resolve({
proofs: mintProofs,
mint: mint
});
}).catch(e => {
attempt++;
if (attempt <= 3) {
console.error('error minting tokens', e);
setTimeout(() => mintTokenAttempt(resolve, reject, attempt), attempt * 1500);
} else {
reject(e);
}
});
}

this.updateBalance();

// todo check that the amount of the invoice matches the amount we want to pay

try {
// mint the tokens
const mintProofs = await wallet.mintProofs(amount, quote.quote, {
pubkey: payment.p2pk
});
d('minted tokens', mintProofs);

return {
proofs: mintProofs,
mint: mint
};
} catch (e) {
console.error('error minting tokens', e);
throw e;
}
return new Promise((resolve, reject) => {
mintTokenAttempt(resolve, reject, 0);
});
}
}

Expand Down Expand Up @@ -203,4 +215,20 @@ export class NDKNWCWallet extends EventEmitter<NDKNWCWalletEvents> implements ND

return res.result;
}

async listTransactions() {
const res = await this.req("list_transactions", {});

if (!res.result) throw new Error("Failed to list transactions");

return res.result;
}

async makeInvoice(amount: number, description: string): Promise<NDKNWCMakeInvoiceParams> {
const res = await this.req("make_invoice", { amount, description });

if (!res.result) throw new Error("Failed to make invoice");

return res.result;
}
}

0 comments on commit fdbe6e5

Please sign in to comment.