@arkade-os/swap 0.1.10 → 0.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -0
- package/dist/advanced.cjs +1058 -414
- package/dist/advanced.d.cts +7 -64
- package/dist/advanced.d.ts +7 -64
- package/dist/advanced.js +29 -5
- package/dist/{chunk-AUJDREZN.js → chunk-CIDVLDDM.js} +1 -1
- package/dist/{chunk-FWRDAL73.js → chunk-F3BGKWIB.js} +758 -104
- package/dist/{chunk-LCO6AJOS.js → chunk-ISKJIONJ.js} +58 -285
- package/dist/{chunk-2HQZLIVX.js → chunk-JBTU7Y6M.js} +1 -1
- package/dist/chunk-UXK7YDA4.js +568 -0
- package/dist/{chunk-6RJVT65W.js → chunk-VPVSSBY2.js} +2 -4
- package/dist/index.cjs +1045 -414
- package/dist/index.d.cts +6 -4
- package/dist/index.d.ts +6 -4
- package/dist/index.js +15 -5
- package/dist/{lockupContract-BTTRVwOt.d.cts → lockupContract-B1mr672c.d.cts} +1 -1
- package/dist/{lockupContract-aFarhlxj.d.ts → lockupContract-BRpk0NxT.d.ts} +1 -1
- package/dist/node/index.d.cts +18 -18
- package/dist/node/index.d.ts +18 -18
- package/dist/nostr.cjs +276 -302
- package/dist/nostr.d.cts +20 -2
- package/dist/nostr.d.ts +20 -2
- package/dist/nostr.js +21 -357
- package/dist/{offerFillPlan-CJC5tnEd.d.ts → offerFillPlan-BMZXiVMB.d.ts} +213 -28
- package/dist/{offerFillPlan-FmyaQoYT.d.cts → offerFillPlan-CqJerwXd.d.cts} +213 -28
- package/dist/openRfq-Dx-vxOzl.d.cts +32 -0
- package/dist/openRfq-Dx-vxOzl.d.ts +32 -0
- package/dist/openRfqTransport-MQZ7JWYN.js +9 -0
- package/dist/protocol.cjs +301 -322
- package/dist/protocol.d.cts +5 -5
- package/dist/protocol.d.ts +5 -5
- package/dist/protocol.js +6 -8
- package/dist/repositories/realm/index.d.cts +1 -1
- package/dist/repositories/realm/index.d.ts +1 -1
- package/dist/repositories/sqlite/index.d.cts +1 -1
- package/dist/repositories/sqlite/index.d.ts +1 -1
- package/dist/{repository-CwwhIAEd.d.cts → repository-CFC0fHAy.d.cts} +26 -26
- package/dist/{repository-DO6QKxBx.d.ts → repository-suGldzOK.d.ts} +26 -26
- package/dist/rfq-contract.cjs +3 -6
- package/dist/rfq-contract.d.cts +3 -4
- package/dist/rfq-contract.d.ts +3 -4
- package/dist/rfq-contract.js +3 -5
- package/dist/transportErrors-DZuZqnF4.d.cts +8 -0
- package/dist/transportErrors-DZuZqnF4.d.ts +8 -0
- package/dist/{verify-ENO36VPK.js → verify-HU5COTNO.js} +3 -3
- package/dist/{watch-_fZSgx_l.d.ts → watch-C2z6Onak.d.ts} +1 -1
- package/dist/{watch-DpDSP2tu.d.cts → watch-CpYjItYX.d.cts} +1 -1
- package/package.json +3 -3
package/dist/nostr.d.cts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { TransportCardFetcher } from '@arkade-os/solver-discovery';
|
|
2
2
|
import { m as RfqTransport } from './rfq-nMGywkqI.cjs';
|
|
3
|
+
import { a as SolverUnavailable } from './transportErrors-DZuZqnF4.cjs';
|
|
3
4
|
import { SimplePool } from 'nostr-tools';
|
|
5
|
+
import { O as OpenRfqBid, a as OpenRfqRequest } from './openRfq-Dx-vxOzl.cjs';
|
|
6
|
+
export { R as RFQ_BROADCAST_KIND, o as openRfqPayload, p as parseOpenRfqBid, r as rankOpenRfqBids } from './openRfq-Dx-vxOzl.cjs';
|
|
4
7
|
import '@arkade-os/sdk';
|
|
5
8
|
import './rfq-contract.cjs';
|
|
6
9
|
|
|
@@ -35,6 +38,21 @@ declare const sharedNostrPool: () => SimplePool;
|
|
|
35
38
|
*/
|
|
36
39
|
declare const closeSharedNostrPool: () => void;
|
|
37
40
|
|
|
41
|
+
interface CollectOpenRfqOptions {
|
|
42
|
+
readonly secretKey: Uint8Array;
|
|
43
|
+
readonly relays: readonly string[];
|
|
44
|
+
readonly windowMs?: number;
|
|
45
|
+
readonly signal?: AbortSignal;
|
|
46
|
+
readonly onBids?: (bids: readonly OpenRfqBid[]) => void;
|
|
47
|
+
readonly pool?: NostrPool;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Raw bids, authenticated by their Nostr event signature only. Card validation, registry policy
|
|
51
|
+
* and routing preferences live in the swap client (`openBidCandidates`); a direct caller must
|
|
52
|
+
* apply equivalent checks before treating a bid as a solver's quote.
|
|
53
|
+
*/
|
|
54
|
+
declare const collectOpenRfqBids: (request: OpenRfqRequest, options: CollectOpenRfqOptions) => Promise<readonly OpenRfqBid[]>;
|
|
55
|
+
|
|
38
56
|
/**
|
|
39
57
|
* The Nostr RFQ transport — the PRODUCTION one; `httpTransport` and the dev-broker
|
|
40
58
|
* `relayTransport` in `rfq.ts` are not what deployed solvers listen on. The kind, NIP-44 framing
|
|
@@ -60,7 +78,7 @@ declare const RFQ_DIRECTED_KIND = 24859;
|
|
|
60
78
|
* Every relay dropped the subscription, so no reply can arrive on it. Distinct from a timeout:
|
|
61
79
|
* that says the solver did not respond, this says we could never have heard it.
|
|
62
80
|
*/
|
|
63
|
-
declare class RelayUnavailable extends
|
|
81
|
+
declare class RelayUnavailable extends SolverUnavailable {
|
|
64
82
|
readonly reasons: string[];
|
|
65
83
|
constructor(reasons: string[]);
|
|
66
84
|
}
|
|
@@ -98,4 +116,4 @@ interface NostrCardFetcherOptions {
|
|
|
98
116
|
}
|
|
99
117
|
declare const nostrCardFetcher: (options?: NostrCardFetcherOptions) => TransportCardFetcher;
|
|
100
118
|
|
|
101
|
-
export { type NostrCardFetcherOptions, type NostrPool, type NostrRfqOptions, RFQ_DIRECTED_KIND, RelayUnavailable, TransportClosed, closeSharedNostrPool, createNostrPool, nostrCardFetcher, nostrRfqTransport, sharedNostrPool };
|
|
119
|
+
export { type CollectOpenRfqOptions, type NostrCardFetcherOptions, type NostrPool, type NostrRfqOptions, OpenRfqBid, OpenRfqRequest, RFQ_DIRECTED_KIND, RelayUnavailable, TransportClosed, closeSharedNostrPool, collectOpenRfqBids, createNostrPool, nostrCardFetcher, nostrRfqTransport, sharedNostrPool };
|
package/dist/nostr.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { TransportCardFetcher } from '@arkade-os/solver-discovery';
|
|
2
2
|
import { m as RfqTransport } from './rfq-CYxOyniL.js';
|
|
3
|
+
import { a as SolverUnavailable } from './transportErrors-DZuZqnF4.js';
|
|
3
4
|
import { SimplePool } from 'nostr-tools';
|
|
5
|
+
import { O as OpenRfqBid, a as OpenRfqRequest } from './openRfq-Dx-vxOzl.js';
|
|
6
|
+
export { R as RFQ_BROADCAST_KIND, o as openRfqPayload, p as parseOpenRfqBid, r as rankOpenRfqBids } from './openRfq-Dx-vxOzl.js';
|
|
4
7
|
import '@arkade-os/sdk';
|
|
5
8
|
import './rfq-contract.js';
|
|
6
9
|
|
|
@@ -35,6 +38,21 @@ declare const sharedNostrPool: () => SimplePool;
|
|
|
35
38
|
*/
|
|
36
39
|
declare const closeSharedNostrPool: () => void;
|
|
37
40
|
|
|
41
|
+
interface CollectOpenRfqOptions {
|
|
42
|
+
readonly secretKey: Uint8Array;
|
|
43
|
+
readonly relays: readonly string[];
|
|
44
|
+
readonly windowMs?: number;
|
|
45
|
+
readonly signal?: AbortSignal;
|
|
46
|
+
readonly onBids?: (bids: readonly OpenRfqBid[]) => void;
|
|
47
|
+
readonly pool?: NostrPool;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Raw bids, authenticated by their Nostr event signature only. Card validation, registry policy
|
|
51
|
+
* and routing preferences live in the swap client (`openBidCandidates`); a direct caller must
|
|
52
|
+
* apply equivalent checks before treating a bid as a solver's quote.
|
|
53
|
+
*/
|
|
54
|
+
declare const collectOpenRfqBids: (request: OpenRfqRequest, options: CollectOpenRfqOptions) => Promise<readonly OpenRfqBid[]>;
|
|
55
|
+
|
|
38
56
|
/**
|
|
39
57
|
* The Nostr RFQ transport — the PRODUCTION one; `httpTransport` and the dev-broker
|
|
40
58
|
* `relayTransport` in `rfq.ts` are not what deployed solvers listen on. The kind, NIP-44 framing
|
|
@@ -60,7 +78,7 @@ declare const RFQ_DIRECTED_KIND = 24859;
|
|
|
60
78
|
* Every relay dropped the subscription, so no reply can arrive on it. Distinct from a timeout:
|
|
61
79
|
* that says the solver did not respond, this says we could never have heard it.
|
|
62
80
|
*/
|
|
63
|
-
declare class RelayUnavailable extends
|
|
81
|
+
declare class RelayUnavailable extends SolverUnavailable {
|
|
64
82
|
readonly reasons: string[];
|
|
65
83
|
constructor(reasons: string[]);
|
|
66
84
|
}
|
|
@@ -98,4 +116,4 @@ interface NostrCardFetcherOptions {
|
|
|
98
116
|
}
|
|
99
117
|
declare const nostrCardFetcher: (options?: NostrCardFetcherOptions) => TransportCardFetcher;
|
|
100
118
|
|
|
101
|
-
export { type NostrCardFetcherOptions, type NostrPool, type NostrRfqOptions, RFQ_DIRECTED_KIND, RelayUnavailable, TransportClosed, closeSharedNostrPool, createNostrPool, nostrCardFetcher, nostrRfqTransport, sharedNostrPool };
|
|
119
|
+
export { type CollectOpenRfqOptions, type NostrCardFetcherOptions, type NostrPool, type NostrRfqOptions, OpenRfqBid, OpenRfqRequest, RFQ_DIRECTED_KIND, RelayUnavailable, TransportClosed, closeSharedNostrPool, collectOpenRfqBids, createNostrPool, nostrCardFetcher, nostrRfqTransport, sharedNostrPool };
|
package/dist/nostr.js
CHANGED
|
@@ -1,369 +1,33 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
RFQ_BROADCAST_KIND,
|
|
3
|
+
RFQ_DIRECTED_KIND,
|
|
4
|
+
RelayUnavailable,
|
|
5
|
+
TransportClosed,
|
|
6
|
+
closeSharedNostrPool,
|
|
7
|
+
collectOpenRfqBids,
|
|
8
|
+
createNostrPool,
|
|
9
|
+
nostrCardFetcher,
|
|
10
|
+
nostrRfqTransport,
|
|
11
|
+
openRfqPayload,
|
|
12
|
+
parseOpenRfqBid,
|
|
13
|
+
rankOpenRfqBids,
|
|
14
|
+
sharedNostrPool
|
|
15
|
+
} from "./chunk-UXK7YDA4.js";
|
|
16
|
+
import "./chunk-ISKJIONJ.js";
|
|
17
|
+
import "./chunk-VPVSSBY2.js";
|
|
6
18
|
import "./chunk-A3SHHRX4.js";
|
|
7
|
-
|
|
8
|
-
// src/nostr.ts
|
|
9
|
-
import {
|
|
10
|
-
SOLVER_CARD_KIND
|
|
11
|
-
} from "@arkade-os/solver-discovery";
|
|
12
|
-
|
|
13
|
-
// src/nostrPool.ts
|
|
14
|
-
import { SimplePool } from "nostr-tools";
|
|
15
|
-
import { normalizeURL } from "nostr-tools/utils";
|
|
16
|
-
|
|
17
|
-
// src/closeOnceWebSocket.ts
|
|
18
|
-
var CloseOnceWebSocket = typeof WebSocket === "undefined" ? void 0 : class CloseOnceWebSocket2 extends WebSocket {
|
|
19
|
-
#closed = false;
|
|
20
|
-
close(code, reason) {
|
|
21
|
-
if (this.#closed) return;
|
|
22
|
-
this.#closed = true;
|
|
23
|
-
super.close(code, reason);
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
// src/nostrPool.ts
|
|
28
|
-
var shared;
|
|
29
|
-
var IDLE_CLOSE_MS = 3e4;
|
|
30
|
-
var NEVER_IDLE_MS = 2 ** 31 - 1;
|
|
31
|
-
var PUBLISH_TIMED_OUT = "publish timed out";
|
|
32
|
-
var POOL_DESTROYED = "nostr pool destroyed";
|
|
33
|
-
var urlKey = (url) => {
|
|
34
|
-
try {
|
|
35
|
-
return normalizeURL(url);
|
|
36
|
-
} catch {
|
|
37
|
-
return url;
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
var SharedPool = class extends SimplePool {
|
|
41
|
-
#users = /* @__PURE__ */ new Map();
|
|
42
|
-
#idle = /* @__PURE__ */ new Map();
|
|
43
|
-
#destroyed = false;
|
|
44
|
-
/** Count one user on each relay; the returned release is idempotent. */
|
|
45
|
-
#hold(urls) {
|
|
46
|
-
const held = [...new Set(urls.map(urlKey))];
|
|
47
|
-
for (const url of held) {
|
|
48
|
-
clearTimeout(this.#idle.get(url));
|
|
49
|
-
this.#idle.delete(url);
|
|
50
|
-
this.#users.set(url, (this.#users.get(url) ?? 0) + 1);
|
|
51
|
-
}
|
|
52
|
-
let released = false;
|
|
53
|
-
return () => {
|
|
54
|
-
if (released) return;
|
|
55
|
-
released = true;
|
|
56
|
-
for (const url of held) {
|
|
57
|
-
const left = (this.#users.get(url) ?? 1) - 1;
|
|
58
|
-
if (left > 0) {
|
|
59
|
-
this.#users.set(url, left);
|
|
60
|
-
continue;
|
|
61
|
-
}
|
|
62
|
-
this.#users.delete(url);
|
|
63
|
-
const timer = setTimeout(() => {
|
|
64
|
-
this.#idle.delete(url);
|
|
65
|
-
if (!this.#users.has(url)) this.close([url]);
|
|
66
|
-
}, IDLE_CLOSE_MS);
|
|
67
|
-
timer.unref?.();
|
|
68
|
-
this.#idle.set(url, timer);
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* A destroyed pool would otherwise reconnect for anyone still holding it (a transport that
|
|
74
|
-
* outlived closeSharedNostrPool()), opening a socket no one can close. Refused here, a
|
|
75
|
-
* subscription reports every relay lost and a publish a connection failure.
|
|
76
|
-
*/
|
|
77
|
-
async ensureRelay(...args) {
|
|
78
|
-
if (this.#destroyed) throw new Error(POOL_DESTROYED);
|
|
79
|
-
return super.ensureRelay(...args);
|
|
80
|
-
}
|
|
81
|
-
subscribeMap(...[requests, params]) {
|
|
82
|
-
const release = this.#hold(requests.map((request) => request.url));
|
|
83
|
-
const closer = super.subscribeMap(requests, {
|
|
84
|
-
...params,
|
|
85
|
-
// Every relay dropped it: nothing is left to hold the sockets for.
|
|
86
|
-
onclose: (reasons) => {
|
|
87
|
-
release();
|
|
88
|
-
params.onclose?.(reasons);
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
let closed = false;
|
|
92
|
-
return {
|
|
93
|
-
close: (reason) => {
|
|
94
|
-
if (closed) return;
|
|
95
|
-
closed = true;
|
|
96
|
-
release();
|
|
97
|
-
closer.close(reason);
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
publish(...[relays, event, params]) {
|
|
102
|
-
const release = this.#hold(relays);
|
|
103
|
-
const sent = super.publish(relays, event, params);
|
|
104
|
-
sent.forEach((publish, i) => {
|
|
105
|
-
publish.catch((error) => {
|
|
106
|
-
if (error instanceof Error && error.message === PUBLISH_TIMED_OUT)
|
|
107
|
-
this.close([relays[i]]);
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
void Promise.allSettled(sent).then(release);
|
|
111
|
-
return sent;
|
|
112
|
-
}
|
|
113
|
-
destroy() {
|
|
114
|
-
this.#destroyed = true;
|
|
115
|
-
for (const timer of this.#idle.values()) clearTimeout(timer);
|
|
116
|
-
this.#idle.clear();
|
|
117
|
-
this.#users.clear();
|
|
118
|
-
super.destroy();
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
var createNostrPool = () => (
|
|
122
|
-
// SimplePool forwards every option at runtime; its declared options just omit most of them.
|
|
123
|
-
new SharedPool({
|
|
124
|
-
enablePing: true,
|
|
125
|
-
idleTimeout: NEVER_IDLE_MS,
|
|
126
|
-
...CloseOnceWebSocket ? { websocketImplementation: CloseOnceWebSocket } : {}
|
|
127
|
-
})
|
|
128
|
-
);
|
|
129
|
-
var sharedNostrPool = () => shared ??= createNostrPool();
|
|
130
|
-
var closeSharedNostrPool = () => {
|
|
131
|
-
const pool = shared;
|
|
132
|
-
shared = void 0;
|
|
133
|
-
if (!pool) return;
|
|
134
|
-
try {
|
|
135
|
-
pool.destroy();
|
|
136
|
-
} catch {
|
|
137
|
-
}
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
// src/nostr.ts
|
|
141
|
-
import { assertTimerMs } from "@arkade-os/sdk";
|
|
142
|
-
import {
|
|
143
|
-
finalizeEvent,
|
|
144
|
-
generateSecretKey,
|
|
145
|
-
getPublicKey,
|
|
146
|
-
nip44,
|
|
147
|
-
verifyEvent
|
|
148
|
-
} from "nostr-tools";
|
|
149
|
-
var RFQ_DIRECTED_KIND = 24859;
|
|
150
|
-
var DEFAULT_TIMEOUT_MS = 3e4;
|
|
151
|
-
var CONNECTION_FAILURE = "connection failure";
|
|
152
|
-
var CARD_FETCH_TIMEOUT_MS = 5e3;
|
|
153
|
-
var RelayUnavailable = class extends Error {
|
|
154
|
-
reasons;
|
|
155
|
-
constructor(reasons) {
|
|
156
|
-
super(`lost every relay connection: ${reasons.join("; ") || "connection closed"}`);
|
|
157
|
-
this.name = "RelayUnavailable";
|
|
158
|
-
this.reasons = reasons;
|
|
159
|
-
}
|
|
160
|
-
};
|
|
161
|
-
var TransportClosed = class extends Error {
|
|
162
|
-
constructor() {
|
|
163
|
-
super("transport closed before the solver replied");
|
|
164
|
-
this.name = "TransportClosed";
|
|
165
|
-
}
|
|
166
|
-
};
|
|
167
|
-
var closeReasons = (raw) => raw.map((entry) => {
|
|
168
|
-
if (typeof entry === "string") return entry;
|
|
169
|
-
const { url, reason } = entry ?? {};
|
|
170
|
-
return url ? `${url}: ${reason ?? "closed"}` : reason ?? "closed";
|
|
171
|
-
});
|
|
172
|
-
var nostrRfqTransport = (options) => {
|
|
173
|
-
assertTimerMs("timeoutMs", options.timeoutMs);
|
|
174
|
-
const relays = options.relays;
|
|
175
|
-
const solverPubkey = options.solverPubkey;
|
|
176
|
-
const secretKey = options.secretKey ?? generateSecretKey();
|
|
177
|
-
const pubkey = getPublicKey(secretKey);
|
|
178
|
-
const pool = options.pool ?? sharedNostrPool();
|
|
179
|
-
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
180
|
-
const conversationKey = nip44.v2.utils.getConversationKey(secretKey, solverPubkey);
|
|
181
|
-
let closed = false;
|
|
182
|
-
let lost;
|
|
183
|
-
const waiters = /* @__PURE__ */ new Map();
|
|
184
|
-
const subscription = pool.subscribeMany(
|
|
185
|
-
relays,
|
|
186
|
-
{ kinds: [RFQ_DIRECTED_KIND], "#p": [pubkey], authors: [solverPubkey] },
|
|
187
|
-
{
|
|
188
|
-
onevent(event) {
|
|
189
|
-
if (event.pubkey !== solverPubkey || !verifyEvent(event)) return;
|
|
190
|
-
let payload;
|
|
191
|
-
try {
|
|
192
|
-
payload = JSON.parse(nip44.v2.decrypt(event.content, conversationKey));
|
|
193
|
-
} catch {
|
|
194
|
-
return;
|
|
195
|
-
}
|
|
196
|
-
const rfqId = payload?.rfq_id;
|
|
197
|
-
if (!rfqId) return;
|
|
198
|
-
waiters.get(rfqId)?.resolve(payload);
|
|
199
|
-
},
|
|
200
|
-
// Called once every relay has closed: fail now with the real cause, not at timeout.
|
|
201
|
-
onclose(reasons) {
|
|
202
|
-
if (closed) return;
|
|
203
|
-
const error = new RelayUnavailable(closeReasons(reasons));
|
|
204
|
-
lost = error;
|
|
205
|
-
for (const waiter of waiters.values()) waiter.reject(error);
|
|
206
|
-
waiters.clear();
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
);
|
|
210
|
-
const send = async (payload) => {
|
|
211
|
-
const event = finalizeEvent(
|
|
212
|
-
{
|
|
213
|
-
kind: RFQ_DIRECTED_KIND,
|
|
214
|
-
created_at: Math.floor(Date.now() / 1e3),
|
|
215
|
-
tags: [["p", solverPubkey]],
|
|
216
|
-
content: nip44.v2.encrypt(JSON.stringify(payload), conversationKey)
|
|
217
|
-
},
|
|
218
|
-
secretKey
|
|
219
|
-
);
|
|
220
|
-
const results = await Promise.allSettled(pool.publish(relays, event));
|
|
221
|
-
const reasons = results.map(
|
|
222
|
-
(r) => r.status === "rejected" ? String(r.reason) : String(r.value)
|
|
223
|
-
);
|
|
224
|
-
const accepted = results.some(
|
|
225
|
-
(r) => r.status === "fulfilled" && !String(r.value).startsWith(CONNECTION_FAILURE)
|
|
226
|
-
);
|
|
227
|
-
if (!accepted) throw new Error(`no relay accepted the RFQ message: ${reasons.join("; ")}`);
|
|
228
|
-
};
|
|
229
|
-
const awaitReply = (rfqId) => {
|
|
230
|
-
const reply = new Promise((resolve, reject) => {
|
|
231
|
-
const timer = setTimeout(() => {
|
|
232
|
-
waiters.delete(rfqId);
|
|
233
|
-
reject(new Error(`no solver reply within ${timeoutMs}ms`));
|
|
234
|
-
}, timeoutMs);
|
|
235
|
-
waiters.set(rfqId, {
|
|
236
|
-
resolve: (payload) => {
|
|
237
|
-
clearTimeout(timer);
|
|
238
|
-
waiters.delete(rfqId);
|
|
239
|
-
resolve(payload);
|
|
240
|
-
},
|
|
241
|
-
reject: (error) => {
|
|
242
|
-
clearTimeout(timer);
|
|
243
|
-
waiters.delete(rfqId);
|
|
244
|
-
reject(error);
|
|
245
|
-
}
|
|
246
|
-
});
|
|
247
|
-
});
|
|
248
|
-
reply.catch(() => {
|
|
249
|
-
});
|
|
250
|
-
return reply;
|
|
251
|
-
};
|
|
252
|
-
const ask = async (rfqId, payload) => {
|
|
253
|
-
if (closed) throw new TransportClosed();
|
|
254
|
-
if (lost) throw lost;
|
|
255
|
-
const reply = awaitReply(rfqId);
|
|
256
|
-
try {
|
|
257
|
-
await send(payload);
|
|
258
|
-
} catch (error) {
|
|
259
|
-
waiters.get(rfqId)?.reject(error);
|
|
260
|
-
throw error;
|
|
261
|
-
}
|
|
262
|
-
return reply;
|
|
263
|
-
};
|
|
264
|
-
return {
|
|
265
|
-
async requestQuote(payload) {
|
|
266
|
-
const rfqId = String(payload.rfq_id);
|
|
267
|
-
return expectQuote(await ask(rfqId, payload), rfqId, pairOf(payload));
|
|
268
|
-
},
|
|
269
|
-
async status(rfqId) {
|
|
270
|
-
const reply = ask(rfqId, { v: 1, type: "rfq_status_request", rfq_id: rfqId });
|
|
271
|
-
const payload = await reply;
|
|
272
|
-
if (payload?.type !== "rfq_status") return null;
|
|
273
|
-
return payload;
|
|
274
|
-
},
|
|
275
|
-
async close() {
|
|
276
|
-
if (closed) return;
|
|
277
|
-
closed = true;
|
|
278
|
-
for (const waiter of waiters.values()) waiter.reject(new TransportClosed());
|
|
279
|
-
waiters.clear();
|
|
280
|
-
subscription.close();
|
|
281
|
-
}
|
|
282
|
-
};
|
|
283
|
-
};
|
|
284
|
-
var isNewer = (event, held) => !held || event.created_at > held.created_at || event.created_at === held.created_at && event.id < held.id;
|
|
285
|
-
var nostrCardFetcher = (options = {}) => {
|
|
286
|
-
assertTimerMs("timeoutMs", options.timeoutMs);
|
|
287
|
-
if (options.timeoutMs === 0) throw new Error("timeoutMs must be positive");
|
|
288
|
-
const seen = /* @__PURE__ */ new Map();
|
|
289
|
-
const inflight = /* @__PURE__ */ new Map();
|
|
290
|
-
return {
|
|
291
|
-
async fetch(solvers, network) {
|
|
292
|
-
const asked = new Set(solvers.map((solver) => solver.discovery_pubkey));
|
|
293
|
-
const authors = [...asked];
|
|
294
|
-
const relays = [
|
|
295
|
-
...new Set(solvers.flatMap((solver) => solver.transports?.nostr?.relays ?? []))
|
|
296
|
-
];
|
|
297
|
-
if (authors.length === 0 || relays.length === 0) return [];
|
|
298
|
-
const key = `${network}:${[...asked].sort().join(",")}@${[...relays].sort().join(",")}`;
|
|
299
|
-
const pending = inflight.get(key);
|
|
300
|
-
if (pending) return pending;
|
|
301
|
-
const task = (async () => {
|
|
302
|
-
const timeoutMs = options.timeoutMs ?? CARD_FETCH_TIMEOUT_MS;
|
|
303
|
-
const pool = options.pool ?? sharedNostrPool();
|
|
304
|
-
const latest = /* @__PURE__ */ new Map();
|
|
305
|
-
let subscription;
|
|
306
|
-
let timer;
|
|
307
|
-
try {
|
|
308
|
-
await new Promise((resolve) => {
|
|
309
|
-
timer = setTimeout(resolve, timeoutMs);
|
|
310
|
-
subscription = pool.subscribeMany(
|
|
311
|
-
relays,
|
|
312
|
-
{ kinds: [SOLVER_CARD_KIND], authors, "#d": [network] },
|
|
313
|
-
{
|
|
314
|
-
// Relays are untrusted: re-check the filter and the signature, so a stray
|
|
315
|
-
// or forged later event cannot shadow the real card.
|
|
316
|
-
onevent(event) {
|
|
317
|
-
if (event.kind !== SOLVER_CARD_KIND || !asked.has(event.pubkey))
|
|
318
|
-
return;
|
|
319
|
-
if (!verifyEvent(event)) return;
|
|
320
|
-
if (event.tags.find((tag) => tag[0] === "d")?.[1] !== network)
|
|
321
|
-
return;
|
|
322
|
-
if (isNewer(event, latest.get(event.pubkey)))
|
|
323
|
-
latest.set(event.pubkey, event);
|
|
324
|
-
},
|
|
325
|
-
oneose: () => resolve(),
|
|
326
|
-
onclose: () => resolve()
|
|
327
|
-
}
|
|
328
|
-
);
|
|
329
|
-
});
|
|
330
|
-
} finally {
|
|
331
|
-
clearTimeout(timer);
|
|
332
|
-
subscription?.close();
|
|
333
|
-
}
|
|
334
|
-
for (const author of authors) {
|
|
335
|
-
const seenKey = `${network}:${author}`;
|
|
336
|
-
const event = latest.get(author);
|
|
337
|
-
if (event && isNewer(event, seen.get(seenKey))) seen.set(seenKey, event);
|
|
338
|
-
const best = seen.get(seenKey);
|
|
339
|
-
if (best) latest.set(author, best);
|
|
340
|
-
}
|
|
341
|
-
const cards = [];
|
|
342
|
-
for (const event of latest.values()) {
|
|
343
|
-
try {
|
|
344
|
-
const card = JSON.parse(event.content);
|
|
345
|
-
cards.push({ pubkey: event.pubkey, createdAt: event.created_at, card });
|
|
346
|
-
} catch {
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
return cards;
|
|
350
|
-
})();
|
|
351
|
-
inflight.set(key, task);
|
|
352
|
-
try {
|
|
353
|
-
return await task;
|
|
354
|
-
} finally {
|
|
355
|
-
if (inflight.get(key) === task) inflight.delete(key);
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
};
|
|
359
|
-
};
|
|
360
19
|
export {
|
|
20
|
+
RFQ_BROADCAST_KIND,
|
|
361
21
|
RFQ_DIRECTED_KIND,
|
|
362
22
|
RelayUnavailable,
|
|
363
23
|
TransportClosed,
|
|
364
24
|
closeSharedNostrPool,
|
|
25
|
+
collectOpenRfqBids,
|
|
365
26
|
createNostrPool,
|
|
366
27
|
nostrCardFetcher,
|
|
367
28
|
nostrRfqTransport,
|
|
29
|
+
openRfqPayload,
|
|
30
|
+
parseOpenRfqBid,
|
|
31
|
+
rankOpenRfqBids,
|
|
368
32
|
sharedNostrPool
|
|
369
33
|
};
|