Skip to content

Commit

Permalink
track pools
Browse files Browse the repository at this point in the history
  • Loading branch information
pablof7z committed Jan 13, 2025
1 parent af92e4a commit 620c078
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 21 deletions.
1 change: 0 additions & 1 deletion ndk-mobile/src/cache-adapter/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ export const migrations = [
await db.execAsync(`CREATE INDEX IF NOT EXISTS idx_profiles_pubkey ON profiles (pubkey);`);
await db.execAsync(`DROP INDEX IF EXISTS idx_event_tags_tag;`);
await db.execAsync(`CREATE INDEX IF NOT EXISTS idx_event_tags_tag ON event_tags (tag, value);`);
console.log('migration 2 done');
},
},
{
Expand Down
2 changes: 2 additions & 0 deletions ndk/src/ndk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ export class NDK extends EventEmitter<{

public publishingFailureHandled = false;

public pools: NDKPool[] = [];

/**
* Default relay-auth policy that will be used when a relay requests authentication,
* if no other policy is specified for that relay.
Expand Down
25 changes: 15 additions & 10 deletions ndk/src/relay/pool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export class NDKPool extends EventEmitter<{
this.relayUrls = relayUrls;

this.blacklistRelayUrls = new Set(blacklistedRelayUrls);

this.ndk.pools.push(this);
}

get relays() {
Expand Down Expand Up @@ -340,20 +342,23 @@ export class NDKPool extends EventEmitter<{
}
}

const maybeEmitConnect = () => {
const allConnected = this.stats().connected === this.relays.size;
const someConnected = this.stats().connected > 0;

if (!allConnected && someConnected) {
this.emit("connect");
}
};

// If we are running with a timeout, check if we need to emit a `connect` event
// in case some, but not all, relays were connected
if (timeoutMs) {
setTimeout(() => {
const allConnected = this.stats().connected === this.relays.size;
const someConnected = this.stats().connected > 0;

if (!allConnected && someConnected) {
this.emit("connect");
}
}, timeoutMs);
}
if (timeoutMs)
setTimeout(maybeEmitConnect, timeoutMs);

await Promise.all(promises);

maybeEmitConnect();
}

private checkOnFlappingRelays() {
Expand Down
2 changes: 1 addition & 1 deletion ndk/src/relay/sub-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type { NDKSubscriptionManager } from "../subscription/manager";
*/
export class NDKRelaySubscriptionManager {
private relay: NDKRelay;
private subscriptions: Map<NDKFilterFingerprint, NDKRelaySubscription[]>;
public subscriptions: Map<NDKFilterFingerprint, NDKRelaySubscription[]>;
private generalSubManager: NDKSubscriptionManager;

/**
Expand Down
11 changes: 9 additions & 2 deletions ndk/src/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import { follows } from "./follows.js";
import { type NDKUserProfile, profileFromEvent, serializeProfile } from "./profile.js";
import { getNip05For } from "./nip05.js";
import type {
NDKFilter,
NDKRelay,
NDKSigner,
NDKZapDetails,
NDKZapMethod,
NDKZapMethodInfo,
} from "../index.js";
Expand Down Expand Up @@ -127,6 +126,14 @@ export class NDKUser {
this._pubkey = pubkey;
}

/**
* Equivalent to NDKEvent.filters().
* @returns {NDKFilter}
*/
public filters(): NDKFilter {
return {"#p": [this.pubkey]}
}

/**
* Gets NIP-57 and NIP-61 information that this user has signaled
*
Expand Down
28 changes: 21 additions & 7 deletions ndk/src/zapper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ export type NDKZapDetails<T> = T & {
* Description of the payment for the sender's record
*/
paymentDescription?: string;

/**
* If this payment is for a nip57 zap, this will contain the zap request.
*/
nip57ZapRequest?: NDKEvent;
};

export type NDKZapConfirmation = NDKZapConfirmationLN | NDKZapConfirmationCashu;
Expand Down Expand Up @@ -194,7 +199,7 @@ class NDKZapper extends EventEmitter<{
try {
result = await this.zapSplit(split);
} catch (e: any) {
result = e;
result = new Error(e.message);
}

this.emit("split:complete", split, result);
Expand Down Expand Up @@ -244,6 +249,7 @@ class NDKZapper extends EventEmitter<{
pr,
amount: split.amount,
unit: this.unit,
nip57ZapRequest: zapRequest
}
);
}
Expand Down Expand Up @@ -441,7 +447,11 @@ class NDKZapper extends EventEmitter<{
* @param pubkey
* @returns
*/
async getZapMethods(ndk: NDK, recipient: Hexpubkey): Promise<NDKZapMethodInfo[]> {
async getZapMethods(
ndk: NDK,
recipient: Hexpubkey,
timeout = 1500
): Promise<NDKZapMethodInfo[]> {
const methods: NDKZapMethod[] = [];

if (this.cashuPay) methods.push("nip61");
Expand All @@ -450,12 +460,16 @@ class NDKZapper extends EventEmitter<{
// if there are no methods available, return an empty array
if (methods.length === 0) throw new Error("There are no payment methods available! Please set at least one of lnPay or cashuPay");

const user = ndk.getUser({ pubkey: recipient });
const zapInfo = await user.getZapInfo(false, methods);

d("Zap info for %s: %o", user.npub, zapInfo);
const timeoutPromise = new Promise<NDKZapMethodInfo[]>((_, reject) => setTimeout(() => reject(new Error("Timeout getting information to zap")), timeout));
const getPromise = new Promise<NDKZapMethodInfo[]>((resolve) => {
const user = ndk.getUser({ pubkey: recipient });
user.getZapInfo(false, methods).then(resolve)
});

return zapInfo;
const res = await Promise.race([
timeoutPromise, getPromise
])
return res;
}

/**
Expand Down

0 comments on commit 620c078

Please sign in to comment.