1- import { CashuWallet , CashuMint , Proof , MeltQuoteState } from "@cashu/cashu-ts" ;
1+ import { CashuWallet , CashuMint , Proof , MeltQuoteState , SendResponse } from "@cashu/cashu-ts" ;
22import { NDKEvent , NDKTag , NDKUser , NDKZapDetails , type LnPaymentInfo } from "@nostr-dev-kit/ndk" ;
3- import type { NDKCashuPay } from "../pay" ;
43import { rollOverProofs , type TokenSelection } from "../proofs" ;
54import type { MintUrl } from "../mint/utils" ;
65import { NDKCashuWallet } from "../wallet" ;
76import { NDKWalletChange } from "../history" ;
87
9- export async function payLn (
10- this : NDKCashuPay ,
11- payment : NDKZapDetails < LnPaymentInfo > ,
12- useMint ?: MintUrl ,
13- ) : Promise < string | undefined > {
14- this . debug ( "payLn %o" , this . info ) ;
15-
16- const mintBalances = this . wallet . mintBalances ;
17- const amount = this . getAmount ( ) / 1000 ; // convert msat to sat
18- const data = this . info as LnPaymentInfo ;
19- if ( ! data . pr ) throw new Error ( "missing pr" ) ;
20-
21- return new Promise < string > ( ( resolve , reject ) => {
22- const eligibleMints = getEligibleMints ( mintBalances , amount , useMint , this . debug ) ;
23-
24- if ( eligibleMints . length === 0 ) {
25- handleInsufficientBalance ( amount , data . pr , this . wallet , this . debug , reject ) ;
26- return ;
27- }
28-
29- attemptPaymentWithEligibleMints (
30- eligibleMints ,
31- data . pr ,
32- amount ,
33- this . wallet ,
34- payment ,
35- this . debug ,
36- resolve ,
37- reject
38- ) ;
39- } ) ;
40- }
41-
42- function getEligibleMints (
43- mintBalances : Record < string , number > ,
44- amount : number ,
45- useMint : MintUrl | undefined ,
46- debug : NDKCashuPay [ "debug" ]
47- ) : string [ ] {
48- return Object . entries ( mintBalances )
49- . filter ( ( [ mint , balance ] ) => {
50- if ( useMint && mint !== useMint ) return false ;
51- if ( balance < amount ) {
52- debug ( "mint %s has insufficient balance %d" , mint , balance , amount ) ;
53- return false ;
54- }
55- return true ;
56- } )
57- . map ( ( [ mint ] ) => mint ) ;
58- }
8+ type LNPaymentResult = SendResponse & { preimage : string , change : Proof [ ] , mint : MintUrl } ;
599
60- function handleInsufficientBalance (
10+ export async function payLn (
11+ wallet : NDKCashuWallet ,
6112 amount : number ,
6213 pr : string ,
63- wallet : NDKCashuPay [ "wallet" ] ,
64- debug : NDKCashuPay [ "debug" ] ,
65- reject : ( reason : string ) => void
66- ) : void {
67- wallet . emit ( "insufficient_balance" , { amount, pr } ) ;
68- debug ( "no mint with sufficient balance found" ) ;
69- reject ( "no mint with sufficient balance found" ) ;
70- }
71-
72- async function attemptPaymentWithEligibleMints (
73- eligibleMints : string [ ] ,
74- pr : string ,
75- amount : number ,
76- wallet : NDKCashuPay [ "wallet" ] ,
77- payment : NDKZapDetails < LnPaymentInfo > ,
78- debug : NDKCashuPay [ "debug" ] ,
79- resolve : ( value : string ) => void ,
80- reject : ( reason : string , errors : Record < string , string > ) => void
81- ) : Promise < void > {
82- const errors : Record < string , string > = { } ;
14+ ) : Promise < LNPaymentResult | undefined | null > {
15+ const eligibleMints = wallet . getMintsWithBalance ( amount ) ;
16+ console . log ( "eligible mints" , eligibleMints , { amount} ) ;
8317
8418 for ( const mint of eligibleMints ) {
8519 try {
86- const result = await executePayment ( mint , pr , amount , wallet , payment , debug ) ;
20+ const result = await executePayment ( mint , pr , amount , wallet ) ;
8721 if ( result ) {
88- resolve ( result ) ;
89- return ;
22+ return result ;
9023 }
9124 } catch ( error : any ) {
92- debug ( "Failed to execute payment for mint %s: %s" , mint , error ) ;
93- errors [ mint ] = error . message ;
25+ console . log ( "Failed to execute payment for mint %s: %s" , mint , error ) ;
9426 }
9527 }
96-
97- reject ( "Failed to pay with any mint" , errors ) ;
9828}
9929
10030/**
@@ -120,9 +50,8 @@ async function executePayment(
12050 pr : string ,
12151 amount : number ,
12252 wallet : NDKCashuWallet ,
123- payment : NDKZapDetails < LnPaymentInfo > ,
124- debug : NDKCashuPay [ "debug" ]
125- ) : Promise < string | undefined | null > {
53+ ) : Promise < LNPaymentResult | undefined | null > {
54+ console . log ( "executing payment from mint" , mint ) ;
12655 const _wallet = await wallet . walletForMint ( mint ) ;
12756 if ( ! _wallet ) throw new Error ( "unable to load wallet for mint " + mint ) ;
12857 const mintProofs = wallet . proofsForMint ( mint ) ;
@@ -135,10 +64,10 @@ async function executePayment(
13564 const meltQuote = await _wallet . createMeltQuote ( pr ) ;
13665 const amountToSend = meltQuote . amount + meltQuote . fee_reserve ;
13766
138- const res = _wallet . selectProofsToSend ( mintProofs , amountToSend ) ;
67+ const proofs = _wallet . selectProofsToSend ( mintProofs , amountToSend ) ;
13968
140- const meltResult = await _wallet . meltProofs ( meltQuote , mintProofs ) ;
141- debug ( "Melt result: %o" , meltResult ) ;
69+ const meltResult = await _wallet . meltProofs ( meltQuote , proofs . send ) ;
70+ console . log ( "Melt result: %o" , meltResult ) ;
14271
14372 const fee = calculateFee ( amount , mintProofs , meltResult . change ) ;
14473
@@ -151,59 +80,46 @@ async function executePayment(
15180
15281 // generate history event
15382 if ( meltResult . quote . state === MeltQuoteState . PAID && meltResult . quote . payment_preimage ) {
154- debug ( "Payment successful" ) ;
155- const { destroyedTokens, createdToken } = await rollOverProofs (
156- {
157- usedProofs : mintProofs ,
158- movedProofs : [
159- ...meltResult . keep ,
160- ...meltResult . change
161- ] ,
162- usedTokens : mintProofs ,
163- mint,
164- } ,
165- [ ] ,
166- mint ,
167- wallet
168- ) ;
169- const historyEvent = new NDKWalletChange ( wallet . ndk ) ;
170- historyEvent . destroyedTokens = destroyedTokens ;
171- if ( createdToken ) historyEvent . createdTokens = [ createdToken ] ;
172- if ( wallet . event ) historyEvent . tag ( wallet . event ) ;
173- historyEvent . direction = 'out' ;
174- historyEvent . description = payment . paymentDescription ;
83+ console . log ( "Payment successful" ) ;
84+
85+ // const historyEvent = new NDKWalletChange(wallet.ndk);
86+ // historyEvent.destroyedTokens = sendProofs;
87+ // historyEvent.createdTokens = meltResult.change;
88+ // if (wallet.event) historyEvent.tag(wallet.event);
89+ // historyEvent.direction = 'out';
90+ // historyEvent.description = payment.paymentDescription;
17591
176- if ( payment . target ) {
177- let tag : NDKTag | undefined ;
92+ // if (payment.target) {
93+ // let tag: NDKTag | undefined;
17894
179- if ( payment . target instanceof NDKEvent ) {
180- tag = payment . target . tagReference ( ) ;
181- } else if ( payment . target instanceof NDKUser && ! payment . recipientPubkey ) {
182- tag = [ 'p' , payment . target . pubkey ] ;
183- }
184-
185- if ( tag ) {
186- console . log ( "adding tag" , tag ) ;
187- historyEvent . tags . push ( tag ) ;
188- }
189- }
95+ // if (payment.target instanceof NDKEvent) {
96+ // tag = payment.target.tagReference();
97+ // } else if (payment.target instanceof NDKUser && !payment.recipientPubkey) {
98+ // tag = ['p', payment.target.pubkey];
99+ // }
100+
101+ // if (tag) {
102+ // console.log("adding tag", tag);
103+ // historyEvent.tags.push(tag);
104+ // }
105+ // }
190106
191- if ( payment . recipientPubkey ) {
192- historyEvent . tags . push ( [ 'p' , payment . recipientPubkey ] ) ;
193- }
107+ // if (payment.recipientPubkey) {
108+ // historyEvent.tags.push(['p', payment.recipientPubkey]);
109+ // }
194110
195- historyEvent . tags . push ( [ 'preimage' , meltResult . quote . payment_preimage ] ) ;
196- historyEvent . amount = meltResult . quote . amount ;
197- historyEvent . fee = fee ;
198- historyEvent . publish ( wallet . relaySet ) ;
111+ // historyEvent.tags.push(['preimage', meltResult.quote.payment_preimage]);
112+ // historyEvent.amount = meltResult.quote.amount;
113+ // historyEvent.fee = fee;
114+ // historyEvent.publish(wallet.relaySet);
199115
200- return meltResult . quote . payment_preimage ;
116+ return { preimage : meltResult . quote . payment_preimage , change : meltResult . change , ... proofs , mint } ;
201117 }
202118
203- return meltResult . quote . payment_preimage ;
119+ return null ;
204120 } catch ( e ) {
205121 if ( e instanceof Error ) {
206- debug ( "Failed to pay with mint %s" , e . message ) ;
122+ console . log ( "Failed to pay with mint %s" , e . message ) ;
207123 // if (e.message.match(/already spent/i)) {
208124 // debug("Proofs already spent, rolling over");
209125 // rollOverProofs(selection, [], selection.mint, wallet);
0 commit comments