@canton-network/core-splice-client 1.8.0 → 1.9.0

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/dist/index.cjs CHANGED
@@ -1,417 +1,438 @@
1
- 'use strict';
2
-
3
- var createClient = require('openapi-fetch');
4
-
5
- function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
6
-
7
- var createClient__default = /*#__PURE__*/_interopDefault(createClient);
8
-
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region \0rolldown/runtime.js
3
+ var __create = Object.create;
9
4
  var __defProp = Object.defineProperty;
10
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
12
- var _ScanClient = class _ScanClient {
13
- constructor(baseUrl, logger, accessTokenProvider) {
14
- __publicField(this, "client");
15
- __publicField(this, "logger");
16
- __publicField(this, "accessTokenProvider");
17
- __publicField(this, "baseUrlHref");
18
- this.logger = logger;
19
- this.baseUrlHref = baseUrl.href;
20
- this.accessTokenProvider = accessTokenProvider;
21
- this.logger.debug({ baseUrl }, "ScanClient initialized");
22
- this.client = createClient__default.default({
23
- baseUrl: baseUrl.href,
24
- fetch: async (url, options = {}) => {
25
- const accessToken = await this.accessTokenProvider.getAccessToken();
26
- return fetch(url, {
27
- ...options,
28
- headers: {
29
- ...options.headers || {},
30
- ...accessToken ? { Authorization: `Bearer ${accessToken}` } : {},
31
- "Content-Type": "application/json"
32
- }
33
- });
34
- }
35
- });
36
- }
37
- async post(path, body, params) {
38
- const options = { body, params };
39
- this.logger.debug({ requestBody: body }, `POST ${path}`);
40
- const resp = await this.client.POST(path, options);
41
- this.logger.debug({ requestBody: body, response: resp }, `POST ${path}`);
42
- return this.valueOrError(resp);
43
- }
44
- async get(path, params) {
45
- const options = { params };
46
- const resp = await this.client.GET(path, options);
47
- this.logger.debug(
48
- { path, params, response: resp },
49
- `GET ${path}`
50
- );
51
- return this.valueOrError(resp);
52
- }
53
- async valueOrError(response) {
54
- if (response.data === void 0) {
55
- return Promise.reject(response.error);
56
- } else {
57
- return Promise.resolve(response.data);
58
- }
59
- }
60
- async getAmuletSynchronizerId() {
61
- const dsoInfo = await this.get("/v0/dso");
62
- const payloadObj = JSON.parse(
63
- JSON.stringify(dsoInfo.amulet_rules.contract.payload)
64
- );
65
- const initActiveSynchronizer = payloadObj?.configSchedule?.initialValue?.decentralizedSynchronizer?.activeSynchronizer;
66
- const futureValues = payloadObj?.configSchedule?.futureValues;
67
- if (futureValues.length > 0) {
68
- let updatedValue = void 0;
69
- for (const value of futureValues) {
70
- const parsed = JSON.parse(JSON.stringify(value));
71
- if (parsed?.decentralizedSynchronizer?.activeSynchronizer) {
72
- updatedValue = parsed.decentralizedSynchronizer.activeSynchronizer;
73
- }
74
- }
75
- return updatedValue ?? initActiveSynchronizer;
76
- } else return initActiveSynchronizer;
77
- }
78
- async fetchAmuletRulesOnce() {
79
- const resp = await this.post("/v0/amulet-rules", {});
80
- const contract = resp.amulet_rules_update.contract;
81
- if (!contract?.contract_id || !contract?.template_id) {
82
- throw new Error("Malformed AmuletRules response");
83
- }
84
- _ScanClient.amuletRulesCache.set(this.baseUrlHref, contract);
85
- return contract;
86
- }
87
- async getAmuletRules() {
88
- const key = this.baseUrlHref;
89
- const cached = _ScanClient.amuletRulesCache.get(key);
90
- if (cached) return structuredClone(cached);
91
- let inflight = _ScanClient.amuletRulesInflight.get(key);
92
- if (!inflight) {
93
- inflight = this.fetchAmuletRulesOnce().finally(() => {
94
- _ScanClient.amuletRulesInflight.delete(key);
95
- });
96
- _ScanClient.amuletRulesInflight.set(key, inflight);
97
- }
98
- const contract = await inflight;
99
- return structuredClone(contract);
100
- }
101
- static invalidateAmuletRulesCache(baseUrl) {
102
- const key = baseUrl.href;
103
- this.amuletRulesCache.delete(key);
104
- this.amuletRulesInflight.delete(key);
105
- }
106
- computeNextChangeAt(rounds) {
107
- const now = Date.now();
108
- let next = Number.POSITIVE_INFINITY;
109
- for (const round of rounds) {
110
- const { opensAt, targetClosesAt } = round.payload ?? {};
111
- const openMs = opensAt ? Number(new Date(opensAt)) : NaN;
112
- const closeMs = targetClosesAt ? Number(new Date(targetClosesAt)) : NaN;
113
- if (Number.isFinite(openMs) && openMs > now && openMs < next) {
114
- next = openMs;
115
- }
116
- if (Number.isFinite(closeMs) && closeMs > now && closeMs < next) {
117
- next = closeMs;
118
- }
119
- }
120
- return Number.isFinite(next) ? next : now;
121
- }
122
- async fetchOpenMiningRoundsOnce() {
123
- const resp = await this.post("/v0/open-and-issuing-mining-rounds", {
124
- cached_open_mining_round_contract_ids: [],
125
- cached_issuing_round_contract_ids: []
126
- });
127
- const rounds = Object.values(resp.open_mining_rounds).flatMap(
128
- (x) => x.contract ? [x.contract] : []
129
- );
130
- const key = this.baseUrlHref;
131
- _ScanClient.roundsCache.set(key, rounds);
132
- _ScanClient.roundsNextChangeAt.set(key, this.computeNextChangeAt(rounds));
133
- return rounds;
134
- }
135
- async refreshRounds(key) {
136
- let inflight = _ScanClient.roundsInflight.get(key);
137
- if (!inflight) {
138
- inflight = this.fetchOpenMiningRoundsOnce().finally(() => {
139
- _ScanClient.roundsInflight.delete(key);
140
- });
141
- _ScanClient.roundsInflight.set(key, inflight);
142
- }
143
- return inflight;
144
- }
145
- async getOpenMiningRounds() {
146
- const key = this.baseUrlHref;
147
- const now = Date.now();
148
- const cached = _ScanClient.roundsCache.get(key);
149
- const next = _ScanClient.roundsNextChangeAt.get(key);
150
- if (cached && next !== void 0 && now < next) {
151
- return structuredClone(cached);
152
- }
153
- const fresh = await this.refreshRounds(key);
154
- return structuredClone(fresh);
155
- }
156
- async getActiveOpenMiningRound() {
157
- const pickActive = (rounds2, timestamp) => rounds2.filter((round) => {
158
- const { opensAt, targetClosesAt } = round.payload;
159
- const openMs = opensAt ? Number(new Date(opensAt)) : NaN;
160
- const closeMs = targetClosesAt ? Number(new Date(targetClosesAt)) : NaN;
161
- return Number.isFinite(openMs) && Number.isFinite(closeMs) && openMs <= timestamp && timestamp < closeMs;
162
- }).sort((a, b) => a.payload.opensAt - b.payload.opensAt).at(-1) ?? null;
163
- const now = Date.now();
164
- const rounds = await this.getOpenMiningRounds();
165
- const active = pickActive(rounds, now);
166
- return active ? structuredClone(active) : null;
167
- }
168
- static invalidateOpenMiningRoundsCache(baseUrl) {
169
- const key = baseUrl.href;
170
- this.roundsCache.delete(key);
171
- this.roundsInflight.delete(key);
172
- this.roundsNextChangeAt.delete(key);
173
- }
174
- async isDevNet() {
175
- const amuletRules = await this.getAmuletRules();
176
- const payload = amuletRules.payload;
177
- return payload?.isDevNet ?? false;
178
- }
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
13
+ get: ((k) => from[k]).bind(null, key),
14
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
+ });
16
+ }
17
+ return to;
179
18
  };
180
- __publicField(_ScanClient, "amuletRulesCache", /* @__PURE__ */ new Map());
181
- __publicField(_ScanClient, "roundsCache", /* @__PURE__ */ new Map());
182
- // one in-flight fetch per baseUrl for rules/rounds
183
- __publicField(_ScanClient, "amuletRulesInflight", /* @__PURE__ */ new Map());
184
- __publicField(_ScanClient, "roundsInflight", /* @__PURE__ */ new Map());
185
- // time after surpassing which mining rounds should be refreshed
186
- __publicField(_ScanClient, "roundsNextChangeAt", /* @__PURE__ */ new Map());
187
- var ScanClient = _ScanClient;
188
- var _ScanProxyClient = class _ScanProxyClient {
189
- constructor(baseUrl, logger, accessTokenProvider) {
190
- __publicField(this, "client");
191
- __publicField(this, "logger");
192
- __publicField(this, "accessTokenProvider");
193
- __publicField(this, "baseUrlHref");
194
- this.baseUrlHref = baseUrl.href;
195
- this.accessTokenProvider = accessTokenProvider;
196
- this.logger = logger;
197
- this.logger.debug({ baseUrl }, "ScanProxyClient initialized");
198
- this.client = createClient__default.default({
199
- baseUrl: baseUrl.href,
200
- fetch: async (url, options = {}) => {
201
- const accessToken = await this.accessTokenProvider.getAccessToken();
202
- return fetch(url, {
203
- ...options,
204
- headers: {
205
- ...options.headers || {},
206
- ...accessToken ? { Authorization: `Bearer ${accessToken}` } : {},
207
- "Content-Type": "application/json"
208
- }
209
- });
210
- }
211
- });
212
- }
213
- async post(path, body, params) {
214
- const options = { body, params };
215
- this.logger.debug({ requestBody: body }, `POST ${path}`);
216
- const resp = await this.client.POST(path, options);
217
- this.logger.debug({ requestBody: body, response: resp }, `POST ${path}`);
218
- return this.valueOrError(resp);
219
- }
220
- async get(path, params) {
221
- const options = { params };
222
- const resp = await this.client.GET(path, options);
223
- this.logger.debug(
224
- { path, params, response: resp },
225
- `GET ${path}`
226
- );
227
- return this.valueOrError(resp);
228
- }
229
- async valueOrError(response) {
230
- if (response.data === void 0) {
231
- return Promise.reject(response.error);
232
- } else {
233
- return Promise.resolve(response.data);
234
- }
235
- }
236
- async fetchAmuletRulesOnce() {
237
- const resp = await this.get("/v0/scan-proxy/amulet-rules");
238
- const contract = resp?.amulet_rules?.contract;
239
- if (!contract?.contract_id || !contract?.template_id) {
240
- throw new Error("Malformed AmuletRules response");
241
- }
242
- _ScanProxyClient.amuletRulesCache.set(this.baseUrlHref, contract);
243
- return contract;
244
- }
245
- async getAmuletRules() {
246
- const key = this.baseUrlHref;
247
- const cached = _ScanProxyClient.amuletRulesCache.get(key);
248
- if (cached) return structuredClone(cached);
249
- let inflight = _ScanProxyClient.amuletRulesInflight.get(key);
250
- if (!inflight) {
251
- inflight = this.fetchAmuletRulesOnce().finally(() => {
252
- _ScanProxyClient.amuletRulesInflight.delete(key);
253
- });
254
- _ScanProxyClient.amuletRulesInflight.set(key, inflight);
255
- }
256
- const contract = await inflight;
257
- return structuredClone(contract);
258
- }
259
- static invalidateAmuletRulesCache(baseUrl) {
260
- const key = baseUrl.href;
261
- this.amuletRulesCache.delete(key);
262
- this.amuletRulesInflight.delete(key);
263
- }
264
- computeNextChangeAt(rounds) {
265
- const now = Date.now();
266
- let next = Number.POSITIVE_INFINITY;
267
- for (const round of rounds) {
268
- const { opensAt, targetClosesAt } = round.payload ?? {};
269
- const openMs = opensAt ? Number(new Date(opensAt)) : NaN;
270
- const closeMs = targetClosesAt ? Number(new Date(targetClosesAt)) : NaN;
271
- if (Number.isFinite(openMs) && openMs > now && openMs < next) {
272
- next = openMs;
273
- }
274
- if (Number.isFinite(closeMs) && closeMs > now && closeMs < next) {
275
- next = closeMs;
276
- }
277
- }
278
- return Number.isFinite(next) ? next : now;
279
- }
280
- async fetchOpenMiningRoundsOnce() {
281
- const resp = await this.get(
282
- "/v0/scan-proxy/open-and-issuing-mining-rounds"
283
- );
284
- const rounds = (resp.open_mining_rounds ?? []).map(
285
- (x) => x.contract
286
- );
287
- const key = this.baseUrlHref;
288
- _ScanProxyClient.roundsCache.set(key, rounds);
289
- _ScanProxyClient.roundsNextChangeAt.set(
290
- key,
291
- this.computeNextChangeAt(rounds)
292
- );
293
- return rounds;
294
- }
295
- async refreshRounds(key) {
296
- let inflight = _ScanProxyClient.roundsInflight.get(key);
297
- if (!inflight) {
298
- inflight = this.fetchOpenMiningRoundsOnce().finally(() => {
299
- _ScanProxyClient.roundsInflight.delete(key);
300
- });
301
- _ScanProxyClient.roundsInflight.set(key, inflight);
302
- }
303
- return inflight;
304
- }
305
- async getOpenMiningRounds() {
306
- const key = this.baseUrlHref;
307
- const now = Date.now();
308
- const cached = _ScanProxyClient.roundsCache.get(key);
309
- const next = _ScanProxyClient.roundsNextChangeAt.get(key);
310
- if (cached && next !== void 0 && now < next) {
311
- return structuredClone(cached);
312
- }
313
- const fresh = await this.refreshRounds(key);
314
- return structuredClone(fresh);
315
- }
316
- async getActiveOpenMiningRound() {
317
- const pickActive = (rounds2, timestamp) => rounds2.filter((round) => {
318
- const { opensAt, targetClosesAt } = round.payload;
319
- const openMs = opensAt ? Number(new Date(opensAt)) : NaN;
320
- const closeMs = targetClosesAt ? Number(new Date(targetClosesAt)) : NaN;
321
- return Number.isFinite(openMs) && Number.isFinite(closeMs) && openMs <= timestamp && timestamp < closeMs;
322
- }).sort((a, b) => a.payload.opensAt - b.payload.opensAt).at(-1) ?? null;
323
- const now = Date.now();
324
- const rounds = await this.getOpenMiningRounds();
325
- const active = pickActive(rounds, now);
326
- return active ? structuredClone(active) : null;
327
- }
328
- static invalidateOpenMiningRoundsCache(baseUrl) {
329
- const key = baseUrl.href;
330
- this.roundsCache.delete(key);
331
- this.roundsInflight.delete(key);
332
- this.roundsNextChangeAt.delete(key);
333
- }
334
- async getAmuletSynchronizerId() {
335
- const amuletRules = await this.getAmuletRules();
336
- const payloadObj = amuletRules.payload;
337
- const initActiveSynchronizer = payloadObj?.configSchedule?.initialValue?.decentralizedSynchronizer?.activeSynchronizer;
338
- const futureValues = payloadObj?.configSchedule?.futureValues;
339
- if (Array.isArray(futureValues) && futureValues.length > 0) {
340
- let updatedValue = void 0;
341
- for (const value of futureValues) {
342
- const parsed = JSON.parse(JSON.stringify(value));
343
- if (parsed?.decentralizedSynchronizer?.activeSynchronizer) {
344
- updatedValue = parsed.decentralizedSynchronizer.activeSynchronizer;
345
- }
346
- }
347
- return updatedValue ?? initActiveSynchronizer;
348
- } else return initActiveSynchronizer;
349
- }
350
- async isDevNet() {
351
- const amuletRules = await this.getAmuletRules();
352
- const payload = amuletRules.payload;
353
- return payload?.isDevNet ?? false;
354
- }
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
20
+ value: mod,
21
+ enumerable: true
22
+ }) : target, mod));
23
+ //#endregion
24
+ let openapi_fetch = require("openapi-fetch");
25
+ openapi_fetch = __toESM(openapi_fetch, 1);
26
+ //#region \0@oxc-project+runtime@0.142.0/helpers/esm/typeof.js
27
+ function _typeof(o) {
28
+ "@babel/helpers - typeof";
29
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
30
+ return typeof o;
31
+ } : function(o) {
32
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
33
+ }, _typeof(o);
34
+ }
35
+ //#endregion
36
+ //#region \0@oxc-project+runtime@0.142.0/helpers/esm/toPrimitive.js
37
+ function toPrimitive(t, r) {
38
+ if ("object" != _typeof(t) || !t) return t;
39
+ var e = t[Symbol.toPrimitive];
40
+ if (void 0 !== e) {
41
+ var i = e.call(t, r || "default");
42
+ if ("object" != _typeof(i)) return i;
43
+ throw new TypeError("@@toPrimitive must return a primitive value.");
44
+ }
45
+ return ("string" === r ? String : Number)(t);
46
+ }
47
+ //#endregion
48
+ //#region \0@oxc-project+runtime@0.142.0/helpers/esm/toPropertyKey.js
49
+ function toPropertyKey(t) {
50
+ var i = toPrimitive(t, "string");
51
+ return "symbol" == _typeof(i) ? i : i + "";
52
+ }
53
+ //#endregion
54
+ //#region \0@oxc-project+runtime@0.142.0/helpers/esm/defineProperty.js
55
+ function _defineProperty(e, r, t) {
56
+ return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
57
+ value: t,
58
+ enumerable: !0,
59
+ configurable: !0,
60
+ writable: !0
61
+ }) : e[r] = t, e;
62
+ }
63
+ //#endregion
64
+ //#region src/scan-client.ts
65
+ var ScanClient = class ScanClient {
66
+ constructor(baseUrl, logger, accessTokenProvider) {
67
+ _defineProperty(this, "client", void 0);
68
+ _defineProperty(this, "logger", void 0);
69
+ _defineProperty(this, "accessTokenProvider", void 0);
70
+ _defineProperty(this, "baseUrlHref", void 0);
71
+ this.logger = logger;
72
+ this.baseUrlHref = baseUrl.href;
73
+ this.accessTokenProvider = accessTokenProvider;
74
+ this.logger.debug({ baseUrl }, "ScanClient initialized");
75
+ this.client = (0, openapi_fetch.default)({
76
+ baseUrl: baseUrl.href,
77
+ fetch: async (url, options = {}) => {
78
+ const accessToken = await this.accessTokenProvider.getAccessToken();
79
+ return fetch(url, {
80
+ ...options,
81
+ headers: {
82
+ ...options.headers || {},
83
+ ...accessToken ? { Authorization: `Bearer ${accessToken}` } : {},
84
+ "Content-Type": "application/json"
85
+ }
86
+ });
87
+ }
88
+ });
89
+ }
90
+ async post(path, body, params) {
91
+ const options = {
92
+ body,
93
+ params
94
+ };
95
+ this.logger.debug({ requestBody: body }, `POST ${path}`);
96
+ const resp = await this.client.POST(path, options);
97
+ this.logger.debug({
98
+ requestBody: body,
99
+ response: resp
100
+ }, `POST ${path}`);
101
+ return this.valueOrError(resp);
102
+ }
103
+ async get(path, params) {
104
+ const options = { params };
105
+ const resp = await this.client.GET(path, options);
106
+ this.logger.debug({
107
+ path,
108
+ params,
109
+ response: resp
110
+ }, `GET ${path}`);
111
+ return this.valueOrError(resp);
112
+ }
113
+ async valueOrError(response) {
114
+ if (response.data === void 0) return Promise.reject(response.error);
115
+ else return Promise.resolve(response.data);
116
+ }
117
+ async getAmuletSynchronizerId() {
118
+ const dsoInfo = await this.get("/v0/dso");
119
+ const payloadObj = JSON.parse(JSON.stringify(dsoInfo.amulet_rules.contract.payload));
120
+ const initActiveSynchronizer = payloadObj?.configSchedule?.initialValue?.decentralizedSynchronizer?.activeSynchronizer;
121
+ const futureValues = payloadObj?.configSchedule?.futureValues;
122
+ if (futureValues.length > 0) {
123
+ let updatedValue = void 0;
124
+ for (const value of futureValues) {
125
+ const parsed = JSON.parse(JSON.stringify(value));
126
+ if (parsed?.decentralizedSynchronizer?.activeSynchronizer) updatedValue = parsed.decentralizedSynchronizer.activeSynchronizer;
127
+ }
128
+ return updatedValue ?? initActiveSynchronizer;
129
+ } else return initActiveSynchronizer;
130
+ }
131
+ async fetchAmuletRulesOnce() {
132
+ const contract = (await this.post("/v0/amulet-rules", {})).amulet_rules_update.contract;
133
+ if (!contract?.contract_id || !contract?.template_id) throw new Error("Malformed AmuletRules response");
134
+ ScanClient.amuletRulesCache.set(this.baseUrlHref, contract);
135
+ return contract;
136
+ }
137
+ async getAmuletRules() {
138
+ const key = this.baseUrlHref;
139
+ const cached = ScanClient.amuletRulesCache.get(key);
140
+ if (cached) return structuredClone(cached);
141
+ let inflight = ScanClient.amuletRulesInflight.get(key);
142
+ if (!inflight) {
143
+ inflight = this.fetchAmuletRulesOnce().finally(() => {
144
+ ScanClient.amuletRulesInflight.delete(key);
145
+ });
146
+ ScanClient.amuletRulesInflight.set(key, inflight);
147
+ }
148
+ const contract = await inflight;
149
+ return structuredClone(contract);
150
+ }
151
+ static invalidateAmuletRulesCache(baseUrl) {
152
+ const key = baseUrl.href;
153
+ this.amuletRulesCache.delete(key);
154
+ this.amuletRulesInflight.delete(key);
155
+ }
156
+ computeNextChangeAt(rounds) {
157
+ const now = Date.now();
158
+ let next = Number.POSITIVE_INFINITY;
159
+ for (const round of rounds) {
160
+ const { opensAt, targetClosesAt } = round.payload ?? {};
161
+ const openMs = opensAt ? Number(new Date(opensAt)) : NaN;
162
+ const closeMs = targetClosesAt ? Number(new Date(targetClosesAt)) : NaN;
163
+ if (Number.isFinite(openMs) && openMs > now && openMs < next) next = openMs;
164
+ if (Number.isFinite(closeMs) && closeMs > now && closeMs < next) next = closeMs;
165
+ }
166
+ return Number.isFinite(next) ? next : now;
167
+ }
168
+ async fetchOpenMiningRoundsOnce() {
169
+ const resp = await this.post("/v0/open-and-issuing-mining-rounds", {
170
+ cached_open_mining_round_contract_ids: [],
171
+ cached_issuing_round_contract_ids: []
172
+ });
173
+ const rounds = Object.values(resp.open_mining_rounds).flatMap((x) => x.contract ? [x.contract] : []);
174
+ const key = this.baseUrlHref;
175
+ ScanClient.roundsCache.set(key, rounds);
176
+ ScanClient.roundsNextChangeAt.set(key, this.computeNextChangeAt(rounds));
177
+ return rounds;
178
+ }
179
+ async refreshRounds(key) {
180
+ let inflight = ScanClient.roundsInflight.get(key);
181
+ if (!inflight) {
182
+ inflight = this.fetchOpenMiningRoundsOnce().finally(() => {
183
+ ScanClient.roundsInflight.delete(key);
184
+ });
185
+ ScanClient.roundsInflight.set(key, inflight);
186
+ }
187
+ return inflight;
188
+ }
189
+ async getOpenMiningRounds() {
190
+ const key = this.baseUrlHref;
191
+ const now = Date.now();
192
+ const cached = ScanClient.roundsCache.get(key);
193
+ const next = ScanClient.roundsNextChangeAt.get(key);
194
+ if (cached && next !== void 0 && now < next) return structuredClone(cached);
195
+ const fresh = await this.refreshRounds(key);
196
+ return structuredClone(fresh);
197
+ }
198
+ async getActiveOpenMiningRound() {
199
+ const pickActive = (rounds, timestamp) => rounds.filter((round) => {
200
+ const { opensAt, targetClosesAt } = round.payload;
201
+ const openMs = opensAt ? Number(new Date(opensAt)) : NaN;
202
+ const closeMs = targetClosesAt ? Number(new Date(targetClosesAt)) : NaN;
203
+ return Number.isFinite(openMs) && Number.isFinite(closeMs) && openMs <= timestamp && timestamp < closeMs;
204
+ }).sort((a, b) => a.payload.opensAt - b.payload.opensAt).at(-1) ?? null;
205
+ const now = Date.now();
206
+ const active = pickActive(await this.getOpenMiningRounds(), now);
207
+ return active ? structuredClone(active) : null;
208
+ }
209
+ static invalidateOpenMiningRoundsCache(baseUrl) {
210
+ const key = baseUrl.href;
211
+ this.roundsCache.delete(key);
212
+ this.roundsInflight.delete(key);
213
+ this.roundsNextChangeAt.delete(key);
214
+ }
215
+ async isDevNet() {
216
+ return (await this.getAmuletRules()).payload?.isDevNet ?? false;
217
+ }
355
218
  };
356
- // shared caches for all instances of ScanProxyClient
357
- __publicField(_ScanProxyClient, "amuletRulesCache", /* @__PURE__ */ new Map());
358
- __publicField(_ScanProxyClient, "roundsCache", /* @__PURE__ */ new Map());
359
- // one in-flight fetch per baseUrl for rules/rounds
360
- __publicField(_ScanProxyClient, "amuletRulesInflight", /* @__PURE__ */ new Map());
361
- __publicField(_ScanProxyClient, "roundsInflight", /* @__PURE__ */ new Map());
362
- // time after surpassing which mining rounds should be refreshed
363
- __publicField(_ScanProxyClient, "roundsNextChangeAt", /* @__PURE__ */ new Map());
364
- var ScanProxyClient = _ScanProxyClient;
219
+ _defineProperty(ScanClient, "amuletRulesCache", /* @__PURE__ */ new Map());
220
+ _defineProperty(ScanClient, "roundsCache", /* @__PURE__ */ new Map());
221
+ _defineProperty(ScanClient, "amuletRulesInflight", /* @__PURE__ */ new Map());
222
+ _defineProperty(ScanClient, "roundsInflight", /* @__PURE__ */ new Map());
223
+ _defineProperty(ScanClient, "roundsNextChangeAt", /* @__PURE__ */ new Map());
224
+ //#endregion
225
+ //#region src/scan-proxy-client.ts
226
+ var ScanProxyClient = class ScanProxyClient {
227
+ constructor(baseUrl, logger, accessTokenProvider) {
228
+ _defineProperty(this, "client", void 0);
229
+ _defineProperty(this, "logger", void 0);
230
+ _defineProperty(this, "accessTokenProvider", void 0);
231
+ _defineProperty(this, "baseUrlHref", void 0);
232
+ this.baseUrlHref = baseUrl.href;
233
+ this.accessTokenProvider = accessTokenProvider;
234
+ this.logger = logger;
235
+ this.logger.debug({ baseUrl }, "ScanProxyClient initialized");
236
+ this.client = (0, openapi_fetch.default)({
237
+ baseUrl: baseUrl.href,
238
+ fetch: async (url, options = {}) => {
239
+ const accessToken = await this.accessTokenProvider.getAccessToken();
240
+ return fetch(url, {
241
+ ...options,
242
+ headers: {
243
+ ...options.headers || {},
244
+ ...accessToken ? { Authorization: `Bearer ${accessToken}` } : {},
245
+ "Content-Type": "application/json"
246
+ }
247
+ });
248
+ }
249
+ });
250
+ }
251
+ async post(path, body, params) {
252
+ const options = {
253
+ body,
254
+ params
255
+ };
256
+ this.logger.debug({ requestBody: body }, `POST ${path}`);
257
+ const resp = await this.client.POST(path, options);
258
+ this.logger.debug({
259
+ requestBody: body,
260
+ response: resp
261
+ }, `POST ${path}`);
262
+ return this.valueOrError(resp);
263
+ }
264
+ async get(path, params) {
265
+ const options = { params };
266
+ const resp = await this.client.GET(path, options);
267
+ this.logger.debug({
268
+ path,
269
+ params,
270
+ response: resp
271
+ }, `GET ${path}`);
272
+ return this.valueOrError(resp);
273
+ }
274
+ async valueOrError(response) {
275
+ if (response.data === void 0) return Promise.reject(response.error);
276
+ else return Promise.resolve(response.data);
277
+ }
278
+ async fetchAmuletRulesOnce() {
279
+ const contract = (await this.get("/v0/scan-proxy/amulet-rules"))?.amulet_rules?.contract;
280
+ if (!contract?.contract_id || !contract?.template_id) throw new Error("Malformed AmuletRules response");
281
+ ScanProxyClient.amuletRulesCache.set(this.baseUrlHref, contract);
282
+ return contract;
283
+ }
284
+ async getAmuletRules() {
285
+ const key = this.baseUrlHref;
286
+ const cached = ScanProxyClient.amuletRulesCache.get(key);
287
+ if (cached) return structuredClone(cached);
288
+ let inflight = ScanProxyClient.amuletRulesInflight.get(key);
289
+ if (!inflight) {
290
+ inflight = this.fetchAmuletRulesOnce().finally(() => {
291
+ ScanProxyClient.amuletRulesInflight.delete(key);
292
+ });
293
+ ScanProxyClient.amuletRulesInflight.set(key, inflight);
294
+ }
295
+ const contract = await inflight;
296
+ return structuredClone(contract);
297
+ }
298
+ static invalidateAmuletRulesCache(baseUrl) {
299
+ const key = baseUrl.href;
300
+ this.amuletRulesCache.delete(key);
301
+ this.amuletRulesInflight.delete(key);
302
+ }
303
+ computeNextChangeAt(rounds) {
304
+ const now = Date.now();
305
+ let next = Number.POSITIVE_INFINITY;
306
+ for (const round of rounds) {
307
+ const { opensAt, targetClosesAt } = round.payload ?? {};
308
+ const openMs = opensAt ? Number(new Date(opensAt)) : NaN;
309
+ const closeMs = targetClosesAt ? Number(new Date(targetClosesAt)) : NaN;
310
+ if (Number.isFinite(openMs) && openMs > now && openMs < next) next = openMs;
311
+ if (Number.isFinite(closeMs) && closeMs > now && closeMs < next) next = closeMs;
312
+ }
313
+ return Number.isFinite(next) ? next : now;
314
+ }
315
+ async fetchOpenMiningRoundsOnce() {
316
+ const rounds = ((await this.get("/v0/scan-proxy/open-and-issuing-mining-rounds")).open_mining_rounds ?? []).map((x) => x.contract);
317
+ const key = this.baseUrlHref;
318
+ ScanProxyClient.roundsCache.set(key, rounds);
319
+ ScanProxyClient.roundsNextChangeAt.set(key, this.computeNextChangeAt(rounds));
320
+ return rounds;
321
+ }
322
+ async refreshRounds(key) {
323
+ let inflight = ScanProxyClient.roundsInflight.get(key);
324
+ if (!inflight) {
325
+ inflight = this.fetchOpenMiningRoundsOnce().finally(() => {
326
+ ScanProxyClient.roundsInflight.delete(key);
327
+ });
328
+ ScanProxyClient.roundsInflight.set(key, inflight);
329
+ }
330
+ return inflight;
331
+ }
332
+ async getOpenMiningRounds() {
333
+ const key = this.baseUrlHref;
334
+ const now = Date.now();
335
+ const cached = ScanProxyClient.roundsCache.get(key);
336
+ const next = ScanProxyClient.roundsNextChangeAt.get(key);
337
+ if (cached && next !== void 0 && now < next) return structuredClone(cached);
338
+ const fresh = await this.refreshRounds(key);
339
+ return structuredClone(fresh);
340
+ }
341
+ async getActiveOpenMiningRound() {
342
+ const pickActive = (rounds, timestamp) => rounds.filter((round) => {
343
+ const { opensAt, targetClosesAt } = round.payload;
344
+ const openMs = opensAt ? Number(new Date(opensAt)) : NaN;
345
+ const closeMs = targetClosesAt ? Number(new Date(targetClosesAt)) : NaN;
346
+ return Number.isFinite(openMs) && Number.isFinite(closeMs) && openMs <= timestamp && timestamp < closeMs;
347
+ }).sort((a, b) => a.payload.opensAt - b.payload.opensAt).at(-1) ?? null;
348
+ const now = Date.now();
349
+ const active = pickActive(await this.getOpenMiningRounds(), now);
350
+ return active ? structuredClone(active) : null;
351
+ }
352
+ static invalidateOpenMiningRoundsCache(baseUrl) {
353
+ const key = baseUrl.href;
354
+ this.roundsCache.delete(key);
355
+ this.roundsInflight.delete(key);
356
+ this.roundsNextChangeAt.delete(key);
357
+ }
358
+ async getAmuletSynchronizerId() {
359
+ const payloadObj = (await this.getAmuletRules()).payload;
360
+ const initActiveSynchronizer = payloadObj?.configSchedule?.initialValue?.decentralizedSynchronizer?.activeSynchronizer;
361
+ const futureValues = payloadObj?.configSchedule?.futureValues;
362
+ if (Array.isArray(futureValues) && futureValues.length > 0) {
363
+ let updatedValue = void 0;
364
+ for (const value of futureValues) {
365
+ const parsed = JSON.parse(JSON.stringify(value));
366
+ if (parsed?.decentralizedSynchronizer?.activeSynchronizer) updatedValue = parsed.decentralizedSynchronizer.activeSynchronizer;
367
+ }
368
+ return updatedValue ?? initActiveSynchronizer;
369
+ } else return initActiveSynchronizer;
370
+ }
371
+ async isDevNet() {
372
+ return (await this.getAmuletRules()).payload?.isDevNet ?? false;
373
+ }
374
+ };
375
+ _defineProperty(ScanProxyClient, "amuletRulesCache", /* @__PURE__ */ new Map());
376
+ _defineProperty(ScanProxyClient, "roundsCache", /* @__PURE__ */ new Map());
377
+ _defineProperty(ScanProxyClient, "amuletRulesInflight", /* @__PURE__ */ new Map());
378
+ _defineProperty(ScanProxyClient, "roundsInflight", /* @__PURE__ */ new Map());
379
+ _defineProperty(ScanProxyClient, "roundsNextChangeAt", /* @__PURE__ */ new Map());
380
+ //#endregion
381
+ //#region src/validator-internal-client.ts
365
382
  var ValidatorInternalClient = class {
366
- constructor(baseUrl, logger, accessTokenProvider) {
367
- __publicField(this, "client");
368
- __publicField(this, "logger");
369
- __publicField(this, "accessTokenProvider");
370
- this.accessTokenProvider = accessTokenProvider;
371
- this.logger = logger;
372
- this.logger.debug({ baseUrl }, "ValidatorInternalClient initialized");
373
- this.client = createClient__default.default({
374
- baseUrl: baseUrl.href,
375
- fetch: async (url, options = {}) => {
376
- const accessToken = await this.accessTokenProvider.getAccessToken();
377
- return fetch(url, {
378
- ...options,
379
- headers: {
380
- ...options.headers || {},
381
- ...accessToken ? { Authorization: `Bearer ${accessToken}` } : {},
382
- "Content-Type": "application/json"
383
- }
384
- });
385
- }
386
- });
387
- }
388
- async post(path, body, params) {
389
- const options = { body, params };
390
- this.logger.debug({ requestBody: body }, `POST ${path}`);
391
- const resp = await this.client.POST(path, options);
392
- this.logger.debug({ requestBody: body, response: resp }, `POST ${path}`);
393
- return this.valueOrError(resp);
394
- }
395
- async get(path, params) {
396
- const options = { params };
397
- const resp = await this.client.GET(path, options);
398
- this.logger.debug(
399
- { path, params, response: resp },
400
- `GET ${path}`
401
- );
402
- return this.valueOrError(resp);
403
- }
404
- async valueOrError(response) {
405
- if (response.data === void 0) {
406
- return Promise.reject(response.error);
407
- } else {
408
- return Promise.resolve(response.data);
409
- }
410
- }
383
+ constructor(baseUrl, logger, accessTokenProvider) {
384
+ _defineProperty(this, "client", void 0);
385
+ _defineProperty(this, "logger", void 0);
386
+ _defineProperty(this, "accessTokenProvider", void 0);
387
+ this.accessTokenProvider = accessTokenProvider;
388
+ this.logger = logger;
389
+ this.logger.debug({ baseUrl }, "ValidatorInternalClient initialized");
390
+ this.client = (0, openapi_fetch.default)({
391
+ baseUrl: baseUrl.href,
392
+ fetch: async (url, options = {}) => {
393
+ const accessToken = await this.accessTokenProvider.getAccessToken();
394
+ return fetch(url, {
395
+ ...options,
396
+ headers: {
397
+ ...options.headers || {},
398
+ ...accessToken ? { Authorization: `Bearer ${accessToken}` } : {},
399
+ "Content-Type": "application/json"
400
+ }
401
+ });
402
+ }
403
+ });
404
+ }
405
+ async post(path, body, params) {
406
+ const options = {
407
+ body,
408
+ params
409
+ };
410
+ this.logger.debug({ requestBody: body }, `POST ${path}`);
411
+ const resp = await this.client.POST(path, options);
412
+ this.logger.debug({
413
+ requestBody: body,
414
+ response: resp
415
+ }, `POST ${path}`);
416
+ return this.valueOrError(resp);
417
+ }
418
+ async get(path, params) {
419
+ const options = { params };
420
+ const resp = await this.client.GET(path, options);
421
+ this.logger.debug({
422
+ path,
423
+ params,
424
+ response: resp
425
+ }, `GET ${path}`);
426
+ return this.valueOrError(resp);
427
+ }
428
+ async valueOrError(response) {
429
+ if (response.data === void 0) return Promise.reject(response.error);
430
+ else return Promise.resolve(response.data);
431
+ }
411
432
  };
412
-
433
+ //#endregion
413
434
  exports.ScanClient = ScanClient;
414
435
  exports.ScanProxyClient = ScanProxyClient;
415
436
  exports.ValidatorInternalClient = ValidatorInternalClient;
416
- //# sourceMappingURL=index.cjs.map
437
+
417
438
  //# sourceMappingURL=index.cjs.map