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