Skip to content

Commit fdbe6e5

Browse files
committed
retry cashu mint after payment with NWC
1 parent 915bdfd commit fdbe6e5

File tree

1 file changed

+44
-16
lines changed

1 file changed

+44
-16
lines changed

ndk-wallet/src/wallets/nwc/index.ts

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import NDK, { NDKPool, LnPaymentInfo, NDKPaymentConfirmationCashu, NDKPaymentCon
44
import { NutPayment } from "../cashu/pay/nut.js";
55
import { sendReq } from "./req.js";
66
import createDebug from "debug";
7-
import { NDKNWCGetInfoResult, NDKNWCRequestMap, NDKNWCResponseBase, NDKNWCResponseMap } from "./types.js";
7+
import { NDKNWCGetInfoResult, NDKNWCMakeInvoiceParams, NDKNWCRequestMap, NDKNWCResponseBase, NDKNWCResponseMap } from "./types.js";
88
import { CashuMint, CashuWallet, MintQuoteResponse } from "@cashu/cashu-ts";
99

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

129129
try {
130130
const res = await this.req("pay_invoice", { invoice: quote.request });
131+
console.log('NWC cashuPay res', res);
131132
d('cashuPay res', res);
132133
} catch (e: any) {
133134
const message = e?.error?.message || e?.message || 'unknown error';
134135
console.error('error paying invoice', e, {message});
135136
throw new Error(message);
136137
}
137138

139+
const mintTokenAttempt = (resolve: (value: any) => void, reject: (reason?: any) => void, attempt: number) => {
140+
// mint the tokens
141+
console.log('minting tokens', {attempt, amount, quote: quote.quote, pubkey: payment.p2pk, mint });
142+
143+
wallet.mintProofs(amount, quote.quote, { pubkey: payment.p2pk }).then(mintProofs => {
144+
console.log('minted tokens', mintProofs);
145+
d('minted tokens', mintProofs);
146+
147+
resolve({
148+
proofs: mintProofs,
149+
mint: mint
150+
});
151+
}).catch(e => {
152+
attempt++;
153+
if (attempt <= 3) {
154+
console.error('error minting tokens', e);
155+
setTimeout(() => mintTokenAttempt(resolve, reject, attempt), attempt * 1500);
156+
} else {
157+
reject(e);
158+
}
159+
});
160+
}
161+
138162
this.updateBalance();
139163

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

142-
try {
143-
// mint the tokens
144-
const mintProofs = await wallet.mintProofs(amount, quote.quote, {
145-
pubkey: payment.p2pk
146-
});
147-
d('minted tokens', mintProofs);
148-
149-
return {
150-
proofs: mintProofs,
151-
mint: mint
152-
};
153-
} catch (e) {
154-
console.error('error minting tokens', e);
155-
throw e;
156-
}
166+
return new Promise((resolve, reject) => {
167+
mintTokenAttempt(resolve, reject, 0);
168+
});
157169
}
158170
}
159171

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

204216
return res.result;
205217
}
218+
219+
async listTransactions() {
220+
const res = await this.req("list_transactions", {});
221+
222+
if (!res.result) throw new Error("Failed to list transactions");
223+
224+
return res.result;
225+
}
226+
227+
async makeInvoice(amount: number, description: string): Promise<NDKNWCMakeInvoiceParams> {
228+
const res = await this.req("make_invoice", { amount, description });
229+
230+
if (!res.result) throw new Error("Failed to make invoice");
231+
232+
return res.result;
233+
}
206234
}

0 commit comments

Comments
 (0)