Skip to content

Commit

Permalink
better history tagging
Browse files Browse the repository at this point in the history
  • Loading branch information
pablof7z committed Oct 28, 2024
1 parent 2c1e9ad commit e4f27c7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
6 changes: 6 additions & 0 deletions ndk-wallet/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @nostr-dev-kit/ndk-cache-redis

## 0.3.6

### Patch Changes

- bump

## 0.3.5

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion ndk-wallet/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nostr-dev-kit/ndk-wallet",
"version": "0.3.5",
"version": "0.3.6",
"description": "NDK Wallet",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
20 changes: 17 additions & 3 deletions ndk-wallet/src/cashu/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
CashuPaymentInfo,
NDKEventId,
NDKNutzap,
NDKPaymentConfirmationCashu,
NDKPaymentConfirmationLN,
NDKSubscription,
Expand All @@ -25,6 +26,12 @@ import { chooseProofsForAmounts, rollOverProofs } from "./proofs.js";

const d = createDebug("ndk-wallet:cashu:wallet");

interface SaveProofsOptions {
nutzap?: NDKNutzap;
direction?: "in" | "out";
amount?: number;
}

/**
* This class tracks state of a NIP-60 wallet
*/
Expand Down Expand Up @@ -318,7 +325,8 @@ export class NDKCashuWallet extends EventEmitter<NDKWalletEvents> implements NDK
const mint = getDecodedToken(token).token[0].mint
const wallet = new CashuWallet(new CashuMint(mint));
const proofs = await wallet.receive(token);
await this.saveProofs(proofs, mint);
const amount = proofsTotalBalance(proofs);
await this.saveProofs(proofs, mint, { direction: "in", amount });

const tokenEvent = new NDKCashuToken(this.event.ndk);
tokenEvent.proofs = proofs;
Expand Down Expand Up @@ -411,7 +419,7 @@ export class NDKCashuWallet extends EventEmitter<NDKWalletEvents> implements NDK
* @param nutzap Nutzap event if these proofs are redeemed from a nutzap
* @returns
*/
async saveProofs(proofs: Proof[], mint: MintUrl, nutzap?: NDKEvent) {
async saveProofs(proofs: Proof[], mint: MintUrl, { nutzap, direction, amount } : SaveProofsOptions = {}) {
const tokenEvent = new NDKCashuToken(this.event.ndk);
tokenEvent.proofs = proofs;
tokenEvent.mint = mint;
Expand All @@ -426,10 +434,16 @@ export class NDKCashuWallet extends EventEmitter<NDKWalletEvents> implements NDK
});

const historyEvent = new NDKWalletChange(this.event.ndk);
historyEvent.tag(this.event);
historyEvent.tags.push(this.event.tagReference());

if (nutzap) {
historyEvent.addRedeemedNutzap(nutzap);
historyEvent.direction = "in";

historyEvent.amount = nutzap.amount;
} else {
historyEvent.direction = direction;
if (amount) historyEvent.amount = amount;
}

historyEvent.tag(tokenEvent, NDKWalletChange.MARKERS.CREATED);
Expand Down

0 comments on commit e4f27c7

Please sign in to comment.