Skip to content

Commit

Permalink
add fee on wallet change
Browse files Browse the repository at this point in the history
  • Loading branch information
pablof7z committed Dec 18, 2024
1 parent 71dd758 commit 779df14
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
18 changes: 17 additions & 1 deletion ndk-wallet/src/wallets/cashu/pay/nut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions ndk-wallet/src/wallets/cashu/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,12 @@ export class NDKCashuWallet extends EventEmitter<NDKWalletEvents & {
filters[2] = { ...filters[2], ...this.event.filter() }; // add to CashuQuote filter
}

console.log("filters %j", filters);

this.sub = this.ndk.subscribe(filters, opts, this.relaySet, false);

this.sub.on("event", eventHandler.bind(this));
// this.sub.on("eose");
this.sub.on("eose", () => {
this.emit("ready");
});
this.sub.start();
}

Expand Down Expand Up @@ -536,6 +536,8 @@ export class NDKCashuWallet extends EventEmitter<NDKWalletEvents & {
historyEvent.direction = "out";
historyEvent.amount = payment.amount;
historyEvent.unit = payment.unit || this.unit;
historyEvent.mint = createResult.mint;
if (createResult.fee) historyEvent.fee = createResult.fee;
if (payment.target) {
// tag the target if there is one
historyEvent.tags.push(payment.target.tagReference());
Expand Down
6 changes: 3 additions & 3 deletions ndk/src/zapper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export type NDKZapDetails<T> = T & {

export type NDKZapConfirmation = NDKZapConfirmationLN | NDKZapConfirmationCashu;

export type NDKPaymentConfirmation = NDKPaymentConfirmationLN | NDKPaymentConfirmationCashu;
export type NDKPaymentConfirmation = NDKPaymentConfirmationLN | NDKNutzap;

export type NDKZapSplit = {
pubkey: string;
Expand Down Expand Up @@ -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<NDKPaymentConfirmation | undefined> {
async zapNip61(split: NDKZapSplit, data: CashuPaymentInfo): Promise<NDKNutzap | Error | undefined> {
if (!this.cashuPay) throw new Error("No cashuPay function available");

let ret: NDKPaymentConfirmationCashu | undefined;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 779df14

Please sign in to comment.