@canton-network/core-splice-client 1.7.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 -299
- 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 -294
- package/dist/index.js.map +1 -1
- package/dist/scan-client.d.ts +15 -0
- package/dist/scan-client.d.ts.map +1 -1
- package/package.json +11 -11
package/dist/index.cjs
CHANGED
|
@@ -1,308 +1,438 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var
|
|
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
|
|
11
|
-
var
|
|
12
|
-
var
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
}
|
|
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;
|
|
78
18
|
};
|
|
79
|
-
var
|
|
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
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
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
|
-
|
|
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
|
+
}
|
|
246
218
|
};
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
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
|
|
256
382
|
var ValidatorInternalClient = class {
|
|
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
|
-
|
|
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
|
+
}
|
|
302
432
|
};
|
|
303
|
-
|
|
433
|
+
//#endregion
|
|
304
434
|
exports.ScanClient = ScanClient;
|
|
305
435
|
exports.ScanProxyClient = ScanProxyClient;
|
|
306
436
|
exports.ValidatorInternalClient = ValidatorInternalClient;
|
|
307
|
-
|
|
437
|
+
|
|
308
438
|
//# sourceMappingURL=index.cjs.map
|