Skip to content

Commit

Permalink
refactor a bunch ndk-wallet stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
pablof7z committed Sep 24, 2024
1 parent eef9911 commit 4cd11cb
Show file tree
Hide file tree
Showing 20 changed files with 477 additions and 233 deletions.
5 changes: 4 additions & 1 deletion ndk-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,21 @@
"homepage": "https://github.com/nostr-dev-kit/ndk",
"dependencies": {
"@cashu/cashu-ts": "1.0.0-rc.9",
"@getalby/sdk": "^3.6.1",
"@nostr-dev-kit/ndk": "workspace:*",
"debug": "^4.3.4",
"light-bolt11-decoder": "^3.0.0",
"tseep": "^1.1.1",
"typescript": "^5.4.4"
"typescript": "^5.4.4",
"webln": "^0.3.2"
},
"devDependencies": {
"@nostr-dev-kit/eslint-config-custom": "workspace:*",
"@nostr-dev-kit/tsconfig": "workspace:*",
"@types/debug": "^4.1.7",
"@types/jest": "^29.5.5",
"@types/node": "^18.15.11",
"@webbtc/webln-types": "^3.0.0",
"jest": "^29.7.0",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
Expand Down
2 changes: 1 addition & 1 deletion ndk-wallet/src/cashu/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class NDKCashuDeposit extends EventEmitter<{
try {
this.finalized = true;

const tokenEvent = new NDKCashuToken(this.wallet.ndk);
const tokenEvent = new NDKCashuToken(this.wallet.event.ndk);
tokenEvent.proofs = ret.proofs;
tokenEvent.mint = this.mint;
tokenEvent.wallet = this.wallet;
Expand Down
34 changes: 0 additions & 34 deletions ndk-wallet/src/cashu/pay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,37 +72,3 @@ export class NDKCashuPay {
public payNut = payNut.bind(this);
public payLn = payLn.bind(this);
}

/**
* Finds mints in common in the intersection of the arrays of mints
* @example
* const user1Mints = ["mint1", "mint2"];
* const user2Mints = ["mint2", "mint3"];
* const user3Mints = ["mint1", "mint2"];
*
* findMintsInCommon([user1Mints, user2Mints, user3Mints]);
*
* // returns ["mint2"]
*/
export function findMintsInCommon(mintCollections: string[][]) {
const mintCounts = new Map<string, number>();

for (const mints of mintCollections) {
for (const mint of mints) {
if (!mintCounts.has(mint)) {
mintCounts.set(mint, 1);
} else {
mintCounts.set(mint, mintCounts.get(mint)! + 1);
}
}
}

const commonMints: string[] = [];
for (const [mint, count] of mintCounts.entries()) {
if (count === mintCollections.length) {
commonMints.push(mint);
}
}

return commonMints;
}
37 changes: 35 additions & 2 deletions ndk-wallet/src/cashu/pay/nut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { Proof } from "@cashu/cashu-ts";
import { CashuMint, CashuWallet } from "@cashu/cashu-ts";
import type { MintUrl } from "../mint/utils";
import type { NDKCashuPay } from "../pay";
import { findMintsInCommon } from "../pay";
import { chooseProofsForAmount, rollOverProofs } from "../proofs";

export type NutPayment = { amount: number; unit: string; mints: MintUrl[]; p2pk?: string };
Expand Down Expand Up @@ -33,7 +32,7 @@ export async function payNut(
try {
const res = await payNutWithMintBalance(this, mintsInCommon);
return res;
} catch (e) {
} catch (e: any) {
this.debug("failed to pay with mints in common: %s %o", e.message, mintsInCommon);
}
} else {
Expand Down Expand Up @@ -138,3 +137,37 @@ async function payNutWithMintBalance(
pay.debug("failed to pay with any mint");
throw new Error("failed to find a mint with enough balance");
}

/**
* Finds mints in common in the intersection of the arrays of mints
* @example
* const user1Mints = ["mint1", "mint2"];
* const user2Mints = ["mint2", "mint3"];
* const user3Mints = ["mint1", "mint2"];
*
* findMintsInCommon([user1Mints, user2Mints, user3Mints]);
*
* // returns ["mint2"]
*/
export function findMintsInCommon(mintCollections: string[][]) {
const mintCounts = new Map<string, number>();

for (const mints of mintCollections) {
for (const mint of mints) {
if (!mintCounts.has(mint)) {
mintCounts.set(mint, 1);
} else {
mintCounts.set(mint, mintCounts.get(mint)! + 1);
}
}
}

const commonMints: string[] = [];
for (const [mint, count] of mintCounts.entries()) {
if (count === mintCollections.length) {
commonMints.push(mint);
}
}

return commonMints;
}
2 changes: 1 addition & 1 deletion ndk-wallet/src/cashu/proofs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export async function rollOverProofs(
if (proofs.usedTokens.length > 0) {
console.trace("rolling over proofs for mint %s %d tokens", mint, proofs.usedTokens.length);

const deleteEvent = new NDKEvent(wallet.ndk);
const deleteEvent = new NDKEvent(wallet.event.ndk);
deleteEvent.kind = NDKKind.EventDeletion;
deleteEvent.tags = [["k", NDKKind.CashuToken.toString()]];

Expand Down
2 changes: 1 addition & 1 deletion ndk-wallet/src/cashu/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,6 @@ export class NDKCashuWalletKey extends NDKEvent {
}

set wallet(wallet: NDKCashuWallet) {
this.dTag = wallet.dTag;
this.dTag = wallet.walletId;
}
}
Loading

0 comments on commit 4cd11cb

Please sign in to comment.