From 779df143a88c85f0d3453d1908f45bc3da6f9c47 Mon Sep 17 00:00:00 2001 From: pablof7z Date: Wed, 18 Dec 2024 14:32:43 +0000 Subject: [PATCH] add fee on wallet change --- ndk-wallet/src/wallets/cashu/pay/nut.ts | 18 +++++++++++++++++- ndk-wallet/src/wallets/cashu/wallet.ts | 8 +++++--- ndk/src/zapper/index.ts | 6 +++--- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/ndk-wallet/src/wallets/cashu/pay/nut.ts b/ndk-wallet/src/wallets/cashu/pay/nut.ts index 17a032ea..524f38fd 100644 --- a/ndk-wallet/src/wallets/cashu/pay/nut.ts +++ b/ndk-wallet/src/wallets/cashu/pay/nut.ts @@ -37,6 +37,22 @@ export async function createToken( console.log('updating wallet state'); const isP2pk = (p: Proof) => p.secret.startsWith('["P2PK"'); const isNotP2pk = (p: Proof) => !isP2pk(p); + + // fee could be calculated here with the difference between the + const totalSent = res.send.reduce((acc, p) => acc + p.amount, 0); + const totalChange = res.keep.reduce((acc, p) => acc + p.amount, 0); + const fee = totalSent - amount - totalChange; + + console.log("fee for mint payment calculated", { + fee, + totalSent, + totalChange, + amount, + }); + + if (fee > 0) { + res.fee = fee; + } wallet.updateState({ reserve: res.send.filter(isNotP2pk), @@ -147,7 +163,7 @@ async function createTokenForPaymentWithMintTransfer( return { keep: [], send: proofs, mint }; } -type TokenWithMint = SendResponse & { mint: MintUrl }; +type TokenWithMint = SendResponse & { mint: MintUrl, fee?: number }; /** * Finds mints in common in the intersection of the arrays of mints diff --git a/ndk-wallet/src/wallets/cashu/wallet.ts b/ndk-wallet/src/wallets/cashu/wallet.ts index deceaa44..2e740edb 100644 --- a/ndk-wallet/src/wallets/cashu/wallet.ts +++ b/ndk-wallet/src/wallets/cashu/wallet.ts @@ -259,12 +259,12 @@ export class NDKCashuWallet extends EventEmitter { + this.emit("ready"); + }); this.sub.start(); } @@ -536,6 +536,8 @@ export class NDKCashuWallet extends EventEmitter = T & { export type NDKZapConfirmation = NDKZapConfirmationLN | NDKZapConfirmationCashu; -export type NDKPaymentConfirmation = NDKPaymentConfirmationLN | NDKPaymentConfirmationCashu; +export type NDKPaymentConfirmation = NDKPaymentConfirmationLN | NDKNutzap; export type NDKZapSplit = { pubkey: string; @@ -240,7 +240,7 @@ class NDKZapper extends EventEmitter<{ * (note that the cashuPay function can use any method to create the proofs, including using lightning * to mint proofs in the specified mint, the responsibility of minting the proofs is delegated to the caller (e.g. ndk-wallet)) */ - async zapNip61(split: NDKZapSplit, data: CashuPaymentInfo): Promise { + async zapNip61(split: NDKZapSplit, data: CashuPaymentInfo): Promise { if (!this.cashuPay) throw new Error("No cashuPay function available"); let ret: NDKPaymentConfirmationCashu | undefined; @@ -280,7 +280,7 @@ class NDKZapper extends EventEmitter<{ nutzap.unit = this.unit; nutzap.recipientPubkey = split.pubkey; await nutzap.sign(this.signer); - await nutzap.publish(relaySet); + nutzap.publish(relaySet); // mark that we have zapped return nutzap;