@elsoul/erpc-sdk 0.7.0 → 0.8.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/README.md +154 -5
- package/dist/index.cjs +2484 -101
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +235 -5
- package/dist/index.d.ts +235 -5
- package/dist/index.js +2470 -102
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -9,11 +9,88 @@ var credentialVariants = (credential) => {
|
|
|
9
9
|
componentEncoded = encodeURIComponent(credential);
|
|
10
10
|
} catch {
|
|
11
11
|
}
|
|
12
|
-
return [
|
|
12
|
+
return [
|
|
13
|
+
.../* @__PURE__ */ new Set([
|
|
14
|
+
credential,
|
|
15
|
+
componentEncoded,
|
|
16
|
+
queryEncoded
|
|
17
|
+
])
|
|
18
|
+
].filter((value) => Boolean(value)).sort((left, right) => right.length - left.length);
|
|
19
|
+
};
|
|
20
|
+
var directEndpointVariants = (endpoint) => {
|
|
21
|
+
const values = [];
|
|
22
|
+
const add = (value) => {
|
|
23
|
+
if (value) values.push(value);
|
|
24
|
+
};
|
|
25
|
+
for (const value of endpoint.searchParams.values()) add(value);
|
|
26
|
+
const rawQuery = endpoint.search.startsWith("?") ? endpoint.search.slice(1) : endpoint.search;
|
|
27
|
+
for (const part of rawQuery.split("&")) {
|
|
28
|
+
const separator = part.indexOf("=");
|
|
29
|
+
const rawValue = separator === -1 ? "" : part.slice(separator + 1);
|
|
30
|
+
add(rawValue);
|
|
31
|
+
if (rawValue) {
|
|
32
|
+
try {
|
|
33
|
+
add(decodeURIComponent(rawValue.replaceAll("+", " ")));
|
|
34
|
+
} catch {
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return values;
|
|
39
|
+
};
|
|
40
|
+
var headerVariants = (headers) => {
|
|
41
|
+
const values = [];
|
|
42
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
43
|
+
values.push(value);
|
|
44
|
+
if (name.toLowerCase() !== "authorization" && name.toLowerCase() !== "proxy-authorization") continue;
|
|
45
|
+
const match = /^\s*[^\s]+\s+(.+?)\s*$/u.exec(value);
|
|
46
|
+
if (match?.[1]) values.push(match[1]);
|
|
47
|
+
if (match?.[1] && /^basic$/iu.test(value.trim().split(/\s+/u, 1)[0] ?? "")) {
|
|
48
|
+
try {
|
|
49
|
+
const decoded = globalThis.atob(match[1]);
|
|
50
|
+
values.push(decoded);
|
|
51
|
+
const separator = decoded.indexOf(":");
|
|
52
|
+
if (separator !== -1) {
|
|
53
|
+
values.push(decoded.slice(0, separator), decoded.slice(separator + 1));
|
|
54
|
+
}
|
|
55
|
+
} catch {
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return values;
|
|
60
|
+
};
|
|
61
|
+
var directRedactionVariants = (endpoint, headers = {}) => [.../* @__PURE__ */ new Set([...directEndpointVariants(endpoint), ...headerVariants(headers)])].flatMap((value) => credentialVariants(value)).filter((value, index, all) => all.indexOf(value) === index).sort((left, right) => right.length - left.length);
|
|
62
|
+
var escapeRegex = (value) => value.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&");
|
|
63
|
+
var hexCharacterPattern = (value) => {
|
|
64
|
+
const lower = value.toLowerCase();
|
|
65
|
+
const upper = value.toUpperCase();
|
|
66
|
+
if (lower === upper) return escapeRegex(value);
|
|
67
|
+
return `[${lower}${upper}]`;
|
|
68
|
+
};
|
|
69
|
+
var percentTripletPattern = (value) => {
|
|
70
|
+
const pattern = /%([0-9a-f])([0-9a-f])/giu;
|
|
71
|
+
let result = "";
|
|
72
|
+
let offset = 0;
|
|
73
|
+
for (const match of value.matchAll(pattern)) {
|
|
74
|
+
const index = match.index ?? 0;
|
|
75
|
+
result += escapeRegex(value.slice(offset, index));
|
|
76
|
+
result += `%${hexCharacterPattern(match[1] ?? "")}${hexCharacterPattern(
|
|
77
|
+
match[2] ?? ""
|
|
78
|
+
)}`;
|
|
79
|
+
offset = index + match[0].length;
|
|
80
|
+
}
|
|
81
|
+
return result + escapeRegex(value.slice(offset));
|
|
13
82
|
};
|
|
14
83
|
var redactString = (value, variants) => {
|
|
15
84
|
let redacted = value;
|
|
16
|
-
for (const variant of variants)
|
|
85
|
+
for (const variant of variants) {
|
|
86
|
+
if (!variant) continue;
|
|
87
|
+
const pattern = percentTripletPattern(variant);
|
|
88
|
+
if (pattern === escapeRegex(variant)) {
|
|
89
|
+
redacted = redacted.replaceAll(variant, REDACTED);
|
|
90
|
+
} else {
|
|
91
|
+
redacted = redacted.replace(new RegExp(pattern, "gu"), REDACTED);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
17
94
|
return redacted;
|
|
18
95
|
};
|
|
19
96
|
var redactValue = (value, variants, seen, depth) => {
|
|
@@ -26,7 +103,7 @@ var redactValue = (value, variants, seen, depth) => {
|
|
|
26
103
|
}
|
|
27
104
|
const result = {};
|
|
28
105
|
for (const [key, item] of Object.entries(value)) {
|
|
29
|
-
Object.defineProperty(result, key, {
|
|
106
|
+
Object.defineProperty(result, redactString(key, variants), {
|
|
30
107
|
configurable: true,
|
|
31
108
|
enumerable: true,
|
|
32
109
|
value: redactValue(item, variants, seen, depth + 1),
|
|
@@ -35,9 +112,12 @@ var redactValue = (value, variants, seen, depth) => {
|
|
|
35
112
|
}
|
|
36
113
|
return result;
|
|
37
114
|
};
|
|
38
|
-
var redactJsonRpcError = (error, credential) => {
|
|
39
|
-
|
|
40
|
-
|
|
115
|
+
var redactJsonRpcError = (error, credential, additionalCredentials = []) => {
|
|
116
|
+
const variants = [.../* @__PURE__ */ new Set([
|
|
117
|
+
...credential ? credentialVariants(credential) : [],
|
|
118
|
+
...additionalCredentials
|
|
119
|
+
])].sort((left, right) => right.length - left.length);
|
|
120
|
+
if (variants.length === 0) return error;
|
|
41
121
|
const result = {
|
|
42
122
|
code: error.code,
|
|
43
123
|
message: redactString(error.message, variants)
|
|
@@ -61,6 +141,14 @@ var ErpcConfigError = class extends ErpcError {
|
|
|
61
141
|
this.name = "ErpcConfigError";
|
|
62
142
|
}
|
|
63
143
|
};
|
|
144
|
+
var ErpcNotConfiguredError = class extends ErpcError {
|
|
145
|
+
namespace;
|
|
146
|
+
constructor(namespace) {
|
|
147
|
+
super("ERPC_NOT_CONFIGURED", `ERPC namespace '${namespace}' is not configured`);
|
|
148
|
+
this.name = "ErpcNotConfiguredError";
|
|
149
|
+
this.namespace = namespace;
|
|
150
|
+
}
|
|
151
|
+
};
|
|
64
152
|
var ErpcTransportError = class extends ErpcError {
|
|
65
153
|
constructor(message, options) {
|
|
66
154
|
super("ERPC_TRANSPORT", message, options);
|
|
@@ -167,12 +255,14 @@ var RestTransport = class {
|
|
|
167
255
|
#fetch;
|
|
168
256
|
#headers;
|
|
169
257
|
#timeoutMs;
|
|
258
|
+
#unavailableNamespace;
|
|
170
259
|
constructor(config) {
|
|
171
|
-
this.#apiKey = config.apiKey;
|
|
260
|
+
this.#apiKey = config.apiKey ?? "";
|
|
172
261
|
this.#endpoint = new URL(config.endpoint);
|
|
173
262
|
this.#fetch = config.fetch;
|
|
174
263
|
this.#headers = config.headers;
|
|
175
264
|
this.#timeoutMs = config.timeoutMs;
|
|
265
|
+
this.#unavailableNamespace = config.unavailableNamespace;
|
|
176
266
|
const endpoint = new URL(config.endpoint);
|
|
177
267
|
endpoint.search = "";
|
|
178
268
|
endpoint.hash = "";
|
|
@@ -186,6 +276,9 @@ var RestTransport = class {
|
|
|
186
276
|
return url;
|
|
187
277
|
}
|
|
188
278
|
async get(path, query, options = {}) {
|
|
279
|
+
if (this.#unavailableNamespace !== void 0) {
|
|
280
|
+
throw new ErpcNotConfiguredError(this.#unavailableNamespace);
|
|
281
|
+
}
|
|
189
282
|
const controlled = controlledSignal(this.#timeoutMs, options.signal);
|
|
190
283
|
try {
|
|
191
284
|
const response = await this.#get(path, query, controlled.signal);
|
|
@@ -203,6 +296,9 @@ var RestTransport = class {
|
|
|
203
296
|
}
|
|
204
297
|
}
|
|
205
298
|
async stream(path, query, options = {}) {
|
|
299
|
+
if (this.#unavailableNamespace !== void 0) {
|
|
300
|
+
throw new ErpcNotConfiguredError(this.#unavailableNamespace);
|
|
301
|
+
}
|
|
206
302
|
const controlled = controlledSignal(this.#timeoutMs, options.signal);
|
|
207
303
|
try {
|
|
208
304
|
const response = await this.#get(path, query, controlled.signal);
|
|
@@ -219,13 +315,16 @@ var RestTransport = class {
|
|
|
219
315
|
}
|
|
220
316
|
}
|
|
221
317
|
#get(path, query, signal) {
|
|
318
|
+
const headers = {
|
|
319
|
+
...this.#headers,
|
|
320
|
+
accept: "application/json"
|
|
321
|
+
};
|
|
322
|
+
if (this.#unavailableNamespace === void 0) {
|
|
323
|
+
headers.authorization = `Bearer ${this.#apiKey}`;
|
|
324
|
+
}
|
|
222
325
|
return this.#fetch(this.url(path, query), {
|
|
223
326
|
method: "GET",
|
|
224
|
-
headers
|
|
225
|
-
...this.#headers,
|
|
226
|
-
authorization: `Bearer ${this.#apiKey}`,
|
|
227
|
-
accept: "application/json"
|
|
228
|
-
},
|
|
327
|
+
headers,
|
|
229
328
|
signal
|
|
230
329
|
});
|
|
231
330
|
}
|
|
@@ -2449,22 +2548,97 @@ var requireFetch = (provided) => {
|
|
|
2449
2548
|
}
|
|
2450
2549
|
return implementation;
|
|
2451
2550
|
};
|
|
2551
|
+
var normalizeLegacyEndpoint = (endpoint) => {
|
|
2552
|
+
endpoint.search = "";
|
|
2553
|
+
endpoint.hash = "";
|
|
2554
|
+
endpoint.pathname = endpoint.pathname.replace(/\/+$/, "") || "/";
|
|
2555
|
+
return endpoint;
|
|
2556
|
+
};
|
|
2557
|
+
var explicitAuthority = (value) => /^[a-z][a-z\d+.-]*:\/\//iu.test(value);
|
|
2558
|
+
var hasUserInfo = (value) => {
|
|
2559
|
+
const separator = value.indexOf("://");
|
|
2560
|
+
if (separator === -1) return false;
|
|
2561
|
+
const authorityAndPath = value.slice(separator + 3);
|
|
2562
|
+
const authority = authorityAndPath.split(/[/?#]/u, 1)[0] ?? "";
|
|
2563
|
+
return authority.length === 0 || authority.includes("@");
|
|
2564
|
+
};
|
|
2565
|
+
var parseDirectUrl = (value, namespace, kind) => {
|
|
2566
|
+
if (typeof value !== "string") {
|
|
2567
|
+
throw new ErpcConfigError(`${namespace} URL must be an absolute ${kind} URL`);
|
|
2568
|
+
}
|
|
2569
|
+
const input = value.trim();
|
|
2570
|
+
if (!input || !explicitAuthority(input) || input.includes("#") || hasUserInfo(input)) {
|
|
2571
|
+
throw new ErpcConfigError(`${namespace} URL must be an absolute ${kind} URL`);
|
|
2572
|
+
}
|
|
2573
|
+
let url;
|
|
2574
|
+
try {
|
|
2575
|
+
url = new URL(input);
|
|
2576
|
+
} catch {
|
|
2577
|
+
throw new ErpcConfigError(`${namespace} URL must be an absolute ${kind} URL`);
|
|
2578
|
+
}
|
|
2579
|
+
const allowed = kind === "HTTP(S)" ? url.protocol === "http:" || url.protocol === "https:" : url.protocol === "ws:" || url.protocol === "wss:";
|
|
2580
|
+
if (!allowed || !url.hostname || url.username || url.password) {
|
|
2581
|
+
throw new ErpcConfigError(`${namespace} URL must be an absolute ${kind} URL`);
|
|
2582
|
+
}
|
|
2583
|
+
return url;
|
|
2584
|
+
};
|
|
2585
|
+
var directHeaders = (value, namespace) => {
|
|
2586
|
+
if (value === void 0) return {};
|
|
2587
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
2588
|
+
throw new ErpcConfigError(`${namespace} headers must be string values`);
|
|
2589
|
+
}
|
|
2590
|
+
const result = {};
|
|
2591
|
+
for (const [name, header] of Object.entries(value)) {
|
|
2592
|
+
if (typeof header !== "string") {
|
|
2593
|
+
throw new ErpcConfigError(`${namespace} headers must be string values`);
|
|
2594
|
+
}
|
|
2595
|
+
result[name] = header;
|
|
2596
|
+
}
|
|
2597
|
+
return Object.freeze(result);
|
|
2598
|
+
};
|
|
2599
|
+
var resolveRpcEndpoint = (value, namespace) => {
|
|
2600
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
2601
|
+
throw new ErpcConfigError(`${namespace} must define an HTTP(S) URL`);
|
|
2602
|
+
}
|
|
2603
|
+
const config = value;
|
|
2604
|
+
const httpUrl = parseDirectUrl(config.httpUrl, `${namespace}.httpUrl`, "HTTP(S)");
|
|
2605
|
+
const webSocketUrl = config.webSocketUrl === void 0 ? void 0 : parseDirectUrl(
|
|
2606
|
+
config.webSocketUrl,
|
|
2607
|
+
`${namespace}.webSocketUrl`,
|
|
2608
|
+
"WS(S)"
|
|
2609
|
+
);
|
|
2610
|
+
const headers = directHeaders(config.headers, namespace);
|
|
2611
|
+
return {
|
|
2612
|
+
headers,
|
|
2613
|
+
httpUrl,
|
|
2614
|
+
...webSocketUrl === void 0 ? {} : { webSocketUrl }
|
|
2615
|
+
};
|
|
2616
|
+
};
|
|
2452
2617
|
var resolveConfig = (config) => {
|
|
2453
|
-
const
|
|
2454
|
-
|
|
2618
|
+
const configuredApiKey = config.apiKey === void 0 ? void 0 : typeof config.apiKey === "string" ? config.apiKey.trim() : (() => {
|
|
2619
|
+
throw new ErpcConfigError("apiKey must be a string");
|
|
2620
|
+
})();
|
|
2621
|
+
const solanaRpc = config.solanaRpc === void 0 ? void 0 : resolveRpcEndpoint(config.solanaRpc, "solanaRpc");
|
|
2622
|
+
const ethereumRpc = config.ethereumRpc === void 0 ? void 0 : resolveRpcEndpoint(config.ethereumRpc, "ethereumRpc");
|
|
2623
|
+
const avalancheCRpc = config.avalancheCRpc === void 0 ? void 0 : resolveRpcEndpoint(config.avalancheCRpc, "avalancheCRpc");
|
|
2624
|
+
const hasDirectRpc = solanaRpc !== void 0 || ethereumRpc !== void 0 || avalancheCRpc !== void 0;
|
|
2625
|
+
if (!configuredApiKey && !hasDirectRpc) {
|
|
2626
|
+
throw new ErpcConfigError("apiKey must not be empty");
|
|
2627
|
+
}
|
|
2628
|
+
const apiKey = configuredApiKey || void 0;
|
|
2455
2629
|
let endpoint;
|
|
2456
2630
|
let avalancheEndpoint;
|
|
2457
2631
|
let accountEndpoint;
|
|
2458
2632
|
let userEndpoint;
|
|
2459
2633
|
try {
|
|
2460
|
-
endpoint = new URL(config.endpoint ?? DEFAULT_ENDPOINT);
|
|
2461
|
-
avalancheEndpoint = new URL(
|
|
2634
|
+
endpoint = normalizeLegacyEndpoint(new URL(config.endpoint ?? DEFAULT_ENDPOINT));
|
|
2635
|
+
avalancheEndpoint = normalizeLegacyEndpoint(new URL(
|
|
2462
2636
|
config.avalancheEndpoint ?? DEFAULT_AVALANCHE_ENDPOINT
|
|
2463
|
-
);
|
|
2464
|
-
accountEndpoint = new URL(
|
|
2637
|
+
));
|
|
2638
|
+
accountEndpoint = normalizeLegacyEndpoint(new URL(
|
|
2465
2639
|
config.accountEndpoint ?? DEFAULT_ACCOUNT_ENDPOINT
|
|
2466
|
-
);
|
|
2467
|
-
userEndpoint = new URL(config.userEndpoint ?? DEFAULT_USER_ENDPOINT);
|
|
2640
|
+
));
|
|
2641
|
+
userEndpoint = normalizeLegacyEndpoint(new URL(config.userEndpoint ?? DEFAULT_USER_ENDPOINT));
|
|
2468
2642
|
} catch {
|
|
2469
2643
|
throw new ErpcConfigError("endpoint must be an absolute HTTP(S) URL");
|
|
2470
2644
|
}
|
|
@@ -2480,31 +2654,22 @@ var resolveConfig = (config) => {
|
|
|
2480
2654
|
if (userEndpoint.protocol !== "https:" && userEndpoint.protocol !== "http:") {
|
|
2481
2655
|
throw new ErpcConfigError("userEndpoint must use HTTP or HTTPS");
|
|
2482
2656
|
}
|
|
2483
|
-
endpoint.search = "";
|
|
2484
|
-
endpoint.hash = "";
|
|
2485
|
-
endpoint.pathname = endpoint.pathname.replace(/\/+$/, "") || "/";
|
|
2486
|
-
avalancheEndpoint.search = "";
|
|
2487
|
-
avalancheEndpoint.hash = "";
|
|
2488
|
-
avalancheEndpoint.pathname = avalancheEndpoint.pathname.replace(/\/+$/, "") || "/";
|
|
2489
|
-
accountEndpoint.search = "";
|
|
2490
|
-
accountEndpoint.hash = "";
|
|
2491
|
-
accountEndpoint.pathname = accountEndpoint.pathname.replace(/\/+$/, "") || "/";
|
|
2492
|
-
userEndpoint.search = "";
|
|
2493
|
-
userEndpoint.hash = "";
|
|
2494
|
-
userEndpoint.pathname = userEndpoint.pathname.replace(/\/+$/, "") || "/";
|
|
2495
2657
|
const timeoutMs = config.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
2496
2658
|
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
|
|
2497
2659
|
throw new ErpcConfigError("timeoutMs must be a positive finite number");
|
|
2498
2660
|
}
|
|
2499
2661
|
return {
|
|
2500
2662
|
accountEndpoint,
|
|
2501
|
-
apiKey,
|
|
2502
2663
|
avalancheEndpoint,
|
|
2503
2664
|
endpoint,
|
|
2504
2665
|
fetch: requireFetch(config.fetch),
|
|
2505
2666
|
headers: config.headers ?? {},
|
|
2506
2667
|
timeoutMs,
|
|
2507
2668
|
userEndpoint,
|
|
2669
|
+
...apiKey === void 0 ? {} : { apiKey },
|
|
2670
|
+
...avalancheCRpc === void 0 ? {} : { avalancheCRpc },
|
|
2671
|
+
...ethereumRpc === void 0 ? {} : { ethereumRpc },
|
|
2672
|
+
...solanaRpc === void 0 ? {} : { solanaRpc },
|
|
2508
2673
|
...config.webSocket === void 0 ? {} : { webSocket: config.webSocket }
|
|
2509
2674
|
};
|
|
2510
2675
|
};
|
|
@@ -2846,16 +3011,16 @@ var nativeNamespace = (transport, methodPrefix) => createRpcNamespace({
|
|
|
2846
3011
|
transport,
|
|
2847
3012
|
validateBatch: rejectNativeBatch
|
|
2848
3013
|
});
|
|
2849
|
-
var createAvalancheClient = (transport, indexTransports) => ({
|
|
3014
|
+
var createAvalancheClient = (transport, indexTransports, nativeTransport = transport) => ({
|
|
2850
3015
|
...createEthereumClient(transport),
|
|
2851
|
-
avax: nativeNamespace(
|
|
2852
|
-
xChain: nativeNamespace(
|
|
2853
|
-
pChain: nativeNamespace(
|
|
3016
|
+
avax: nativeNamespace(nativeTransport, "avax"),
|
|
3017
|
+
xChain: nativeNamespace(nativeTransport, "avm"),
|
|
3018
|
+
pChain: nativeNamespace(nativeTransport, "platform"),
|
|
2854
3019
|
proposerVm: nativeNamespace(
|
|
2855
|
-
|
|
3020
|
+
nativeTransport,
|
|
2856
3021
|
"proposervm"
|
|
2857
3022
|
),
|
|
2858
|
-
info: nativeNamespace(
|
|
3023
|
+
info: nativeNamespace(nativeTransport, "info"),
|
|
2859
3024
|
index: {
|
|
2860
3025
|
cChainBlocks: nativeNamespace(
|
|
2861
3026
|
indexTransports.cChainBlocks,
|
|
@@ -3030,7 +3195,9 @@ var WebSocketJsonRpcTransport = class {
|
|
|
3030
3195
|
#credential;
|
|
3031
3196
|
#listeners = /* @__PURE__ */ new Set();
|
|
3032
3197
|
#pending = /* @__PURE__ */ new Map();
|
|
3198
|
+
#redactionVariants;
|
|
3033
3199
|
#timeoutMs;
|
|
3200
|
+
#unavailableNamespace;
|
|
3034
3201
|
#webSocket;
|
|
3035
3202
|
#connectPromise;
|
|
3036
3203
|
#nextId = 1;
|
|
@@ -3042,21 +3209,38 @@ var WebSocketJsonRpcTransport = class {
|
|
|
3042
3209
|
this.#connectionUrl = config.endpoint.toString();
|
|
3043
3210
|
this.endpoint = publicEndpoint(config.endpoint);
|
|
3044
3211
|
this.maxBatchSize = config.maxBatchSize ?? 256;
|
|
3212
|
+
this.#redactionVariants = directRedactionVariants(
|
|
3213
|
+
config.endpoint,
|
|
3214
|
+
config.redactionHeaders
|
|
3215
|
+
);
|
|
3045
3216
|
this.#timeoutMs = config.timeoutMs;
|
|
3217
|
+
this.#unavailableNamespace = config.unavailableNamespace;
|
|
3046
3218
|
}
|
|
3047
3219
|
async connect() {
|
|
3048
3220
|
if (this.#socket?.readyState === 1) return;
|
|
3049
3221
|
if (this.#connectPromise) return this.#connectPromise;
|
|
3222
|
+
if (this.#unavailableNamespace !== void 0) {
|
|
3223
|
+
throw new ErpcNotConfiguredError(this.#unavailableNamespace);
|
|
3224
|
+
}
|
|
3050
3225
|
if (!this.#webSocket) {
|
|
3051
3226
|
throw new ErpcConfigError(
|
|
3052
3227
|
"A WebSocket implementation is required for subscriptions"
|
|
3053
3228
|
);
|
|
3054
3229
|
}
|
|
3055
3230
|
this.#connectPromise = new Promise((resolve, reject) => {
|
|
3056
|
-
|
|
3231
|
+
let socket;
|
|
3232
|
+
try {
|
|
3233
|
+
socket = new this.#webSocket(this.#connectionUrl);
|
|
3234
|
+
} catch {
|
|
3235
|
+
reject(new ErpcTransportError("Unable to connect to ERPC WebSocket"));
|
|
3236
|
+
return;
|
|
3237
|
+
}
|
|
3057
3238
|
this.#socket = socket;
|
|
3058
3239
|
const timeout = setTimeout(() => {
|
|
3059
|
-
|
|
3240
|
+
try {
|
|
3241
|
+
socket.close();
|
|
3242
|
+
} catch {
|
|
3243
|
+
}
|
|
3060
3244
|
reject(new ErpcTimeoutError(this.#timeoutMs));
|
|
3061
3245
|
}, this.#timeoutMs);
|
|
3062
3246
|
socket.addEventListener(
|
|
@@ -3110,6 +3294,9 @@ var WebSocketJsonRpcTransport = class {
|
|
|
3110
3294
|
return promise;
|
|
3111
3295
|
}
|
|
3112
3296
|
async batch(calls, options = {}) {
|
|
3297
|
+
if (this.#unavailableNamespace !== void 0) {
|
|
3298
|
+
throw new ErpcNotConfiguredError(this.#unavailableNamespace);
|
|
3299
|
+
}
|
|
3113
3300
|
if (calls.length === 0) return [];
|
|
3114
3301
|
if (calls.length > this.maxBatchSize) {
|
|
3115
3302
|
throw new ErpcInvalidResponseError(
|
|
@@ -3216,7 +3403,11 @@ var WebSocketJsonRpcTransport = class {
|
|
|
3216
3403
|
if (isRpcError(message.error)) {
|
|
3217
3404
|
pending.reject(
|
|
3218
3405
|
new ErpcJsonRpcError(
|
|
3219
|
-
redactJsonRpcError(
|
|
3406
|
+
redactJsonRpcError(
|
|
3407
|
+
message.error,
|
|
3408
|
+
this.#credential,
|
|
3409
|
+
this.#redactionVariants
|
|
3410
|
+
)
|
|
3220
3411
|
)
|
|
3221
3412
|
);
|
|
3222
3413
|
} else if ("result" in message) pending.resolve(message.result);
|
|
@@ -3907,6 +4098,13 @@ function normalizeEvmAddress2(address) {
|
|
|
3907
4098
|
return address.toLowerCase();
|
|
3908
4099
|
}
|
|
3909
4100
|
|
|
4101
|
+
// src/generated/swap_execution_capabilities.ts
|
|
4102
|
+
var SWAP_EXECUTION_CAPABILITIES_JSON = '[{"swapExecutionCapabilityId":"swap-execution-0001","chainId":"eip155:1","dexDeploymentId":"dex-deployment-0001","poolDefinitionId":"pool-0001","factoryAddress":"0x5c69bee701ef814a2b6a3edd4b1652cb9cc5aa6f","routerAddress":"0x7a250d5630b4cf539739df2c5dacb4c659f2488d","routerKind":"uniswap-v2-router02","adapterKind":"evm-constant-product-v2","token0DeploymentId":"deployment-0008","token0Address":"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48","token0Standard":"erc20","token1DeploymentId":"deployment-0002","token1Address":"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","token1Standard":"erc20","wrappedNativeTokenDeploymentId":"deployment-0002","wrappedNativeTokenAddress":"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","wrappedNativeFunctionSelector":"0xad5c4648","functionKind":"exact-input-erc20-to-erc20","functionSignature":"swapExactTokensForTokens(uint256,uint256,address[],address,uint256)","functionSelector":"0x38ed1739","status":"active"},{"swapExecutionCapabilityId":"swap-execution-0002","chainId":"eip155:43114","dexDeploymentId":"dex-deployment-0002","poolDefinitionId":"pool-0002","factoryAddress":"0x9ad6c38be94206ca50bb0d90783181662f0cfa10","routerAddress":"0x60ae616a2155ee3d9a68541ba4544862310933d4","routerKind":"joe-v1-router02","adapterKind":"evm-constant-product-v2","token0DeploymentId":"deployment-0004","token0Address":"0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7","token0Standard":"erc20","token1DeploymentId":"deployment-0009","token1Address":"0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e","token1Standard":"erc20","wrappedNativeTokenDeploymentId":"deployment-0004","wrappedNativeTokenAddress":"0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7","wrappedNativeFunctionSelector":"0x73b295c2","functionKind":"exact-input-erc20-to-erc20","functionSignature":"swapExactTokensForTokens(uint256,uint256,address[],address,uint256)","functionSelector":"0x38ed1739","status":"active"}]';
|
|
4103
|
+
var SWAP_EXECUTION_CAPABILITIES_CONTENT_DIGEST = "84424dc48687e2cb1dd0c9a2446f820efd2c311e71ce5acacfeb6e882811875f";
|
|
4104
|
+
var SWAP_EXECUTION_CAPABILITIES_AS_OF_DATE = "2026-09-16";
|
|
4105
|
+
var SWAP_EXECUTION_FUNCTION_SIGNATURE = "swapExactTokensForTokens(uint256,uint256,address[],address,uint256)";
|
|
4106
|
+
var SWAP_EXECUTION_FUNCTION_SELECTOR = "0x38ed1739";
|
|
4107
|
+
|
|
3910
4108
|
// src/swap.ts
|
|
3911
4109
|
var SWAP_QUOTE_ERROR_MESSAGES = Object.freeze({
|
|
3912
4110
|
SWAP_INVALID_ARGUMENT: "Swap request is invalid",
|
|
@@ -3933,6 +4131,22 @@ var SwapQuoteError = class extends Error {
|
|
|
3933
4131
|
this.code = code;
|
|
3934
4132
|
}
|
|
3935
4133
|
};
|
|
4134
|
+
var SWAP_EXECUTION_ERROR_MESSAGES = Object.freeze({
|
|
4135
|
+
SWAP_EXECUTION_INVALID_ARGUMENT: "Swap execution request is invalid",
|
|
4136
|
+
SWAP_UNSUPPORTED_EXECUTION: "Swap execution is unsupported for the selected records",
|
|
4137
|
+
SWAP_PROGRAM_MISMATCH: "Swap program does not match the selected records",
|
|
4138
|
+
SWAP_INSUFFICIENT_ALLOWANCE: "Swap allowance is insufficient",
|
|
4139
|
+
SWAP_SIMULATION_REVERTED: "Swap simulation reverted",
|
|
4140
|
+
SWAP_INVALID_SIMULATION: "Swap simulation result is invalid"
|
|
4141
|
+
});
|
|
4142
|
+
var SwapExecutionError = class extends Error {
|
|
4143
|
+
code;
|
|
4144
|
+
constructor(code) {
|
|
4145
|
+
super(SWAP_EXECUTION_ERROR_MESSAGES[code]);
|
|
4146
|
+
this.name = "SwapExecutionError";
|
|
4147
|
+
this.code = code;
|
|
4148
|
+
}
|
|
4149
|
+
};
|
|
3936
4150
|
var SUPPORTED_QUOTE_ADAPTER = "evm-constant-product-v2";
|
|
3937
4151
|
var SUPPORTED_QUOTE_CAPABILITIES = Object.freeze([
|
|
3938
4152
|
Object.freeze({
|
|
@@ -4025,9 +4239,15 @@ var PAIR_FACTORY_SELECTOR = "0xc45a0155";
|
|
|
4025
4239
|
var PAIR_TOKEN0_SELECTOR = "0x0dfe1681";
|
|
4026
4240
|
var PAIR_TOKEN1_SELECTOR = "0xd21220a7";
|
|
4027
4241
|
var PAIR_GET_RESERVES_SELECTOR = "0x0902f1ac";
|
|
4242
|
+
var ROUTER_FACTORY_SELECTOR = "0xc45a0155";
|
|
4243
|
+
var ROUTER_GET_AMOUNTS_OUT_SELECTOR = "0xd06ca61f";
|
|
4244
|
+
var ERC20_ALLOWANCE_SELECTOR = "0xdd62ed3e";
|
|
4028
4245
|
function swapFail(code) {
|
|
4029
4246
|
throw new SwapQuoteError(code);
|
|
4030
4247
|
}
|
|
4248
|
+
function executionFail(code) {
|
|
4249
|
+
throw new SwapExecutionError(code);
|
|
4250
|
+
}
|
|
4031
4251
|
var isRecord3 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
4032
4252
|
var isKnownChain2 = (value) => value === DEX_CHAIN_IDS.ethereum || value === DEX_CHAIN_IDS.solana || value === DEX_CHAIN_IDS.avalancheC;
|
|
4033
4253
|
var isOpaqueId = (value) => /^[A-Za-z0-9][A-Za-z0-9._:-]*$/u.test(value);
|
|
@@ -4217,6 +4437,43 @@ var uintWord = (value, maximum = UINT256_MAX) => {
|
|
|
4217
4437
|
if (parsed > maximum) swapFail("SWAP_INVALID_POOL_STATE");
|
|
4218
4438
|
return parsed;
|
|
4219
4439
|
};
|
|
4440
|
+
var parseExecutionHexBytes = (value, expectedBytes, code = "SWAP_INVALID_SIMULATION") => {
|
|
4441
|
+
if (typeof value !== "string") executionFail(code);
|
|
4442
|
+
if (!HEX_BYTES.test(value) || value.length % 2 !== 0 || expectedBytes !== void 0 && value.length !== expectedBytes * 2 + 2) {
|
|
4443
|
+
executionFail(code);
|
|
4444
|
+
}
|
|
4445
|
+
return value.toLowerCase();
|
|
4446
|
+
};
|
|
4447
|
+
var executionAddressWord = (value, code = "SWAP_INVALID_SIMULATION") => {
|
|
4448
|
+
const bytes = parseExecutionHexBytes(value, 32, code);
|
|
4449
|
+
const word = bytes.slice(2);
|
|
4450
|
+
if (!/^0{24}[0-9a-f]{40}$/u.test(word)) executionFail(code);
|
|
4451
|
+
return `0x${word.slice(24)}`;
|
|
4452
|
+
};
|
|
4453
|
+
var executionUintWord = (value, code = "SWAP_INVALID_SIMULATION") => {
|
|
4454
|
+
const bytes = parseExecutionHexBytes(value, 32, code);
|
|
4455
|
+
try {
|
|
4456
|
+
return BigInt(`0x${bytes.slice(2)}`);
|
|
4457
|
+
} catch {
|
|
4458
|
+
executionFail(code);
|
|
4459
|
+
}
|
|
4460
|
+
};
|
|
4461
|
+
var executionUintArrayOfTwo = (value) => {
|
|
4462
|
+
const bytes = parseExecutionHexBytes(value);
|
|
4463
|
+
const payload = bytes.slice(2);
|
|
4464
|
+
if (payload.length % 64 !== 0 || payload.length < 256) {
|
|
4465
|
+
executionFail("SWAP_INVALID_SIMULATION");
|
|
4466
|
+
}
|
|
4467
|
+
const offset = BigInt(`0x${payload.slice(0, 64)}`);
|
|
4468
|
+
const length = BigInt(`0x${payload.slice(64, 128)}`);
|
|
4469
|
+
if (offset !== 0x20n || length !== 2n || payload.length !== 256) {
|
|
4470
|
+
executionFail("SWAP_INVALID_SIMULATION");
|
|
4471
|
+
}
|
|
4472
|
+
return [
|
|
4473
|
+
executionUintWord(`0x${payload.slice(128, 192)}`),
|
|
4474
|
+
executionUintWord(`0x${payload.slice(192, 256)}`)
|
|
4475
|
+
];
|
|
4476
|
+
};
|
|
4220
4477
|
var reserveWords = (value) => {
|
|
4221
4478
|
const bytes = parseHexBytes(value, 96);
|
|
4222
4479
|
const payload = bytes.slice(2);
|
|
@@ -4262,8 +4519,101 @@ var currentClockSeconds = (clock) => {
|
|
|
4262
4519
|
if (!Number.isSafeInteger(value)) swapFail("SWAP_INVALID_ARGUMENT");
|
|
4263
4520
|
return value;
|
|
4264
4521
|
};
|
|
4265
|
-
var
|
|
4266
|
-
|
|
4522
|
+
var EXECUTION_REQUEST_KEYS = /* @__PURE__ */ new Set([
|
|
4523
|
+
"chainId",
|
|
4524
|
+
"poolDefinitionId",
|
|
4525
|
+
"inputTokenDeploymentId",
|
|
4526
|
+
"outputTokenDeploymentId",
|
|
4527
|
+
"amountIn",
|
|
4528
|
+
"freshness",
|
|
4529
|
+
"sender",
|
|
4530
|
+
"recipient",
|
|
4531
|
+
"slippageBps",
|
|
4532
|
+
"deadline"
|
|
4533
|
+
]);
|
|
4534
|
+
var snapshotExecutionRequest = (request) => {
|
|
4535
|
+
if (!isRecord3(request)) executionFail("SWAP_EXECUTION_INVALID_ARGUMENT");
|
|
4536
|
+
if (Object.keys(request).some((key) => !EXECUTION_REQUEST_KEYS.has(key))) {
|
|
4537
|
+
executionFail("SWAP_EXECUTION_INVALID_ARGUMENT");
|
|
4538
|
+
}
|
|
4539
|
+
const freshness = request.freshness;
|
|
4540
|
+
const freshnessSnapshot = isRecord3(freshness) ? { ...freshness } : freshness;
|
|
4541
|
+
const quoteRequest = {
|
|
4542
|
+
chainId: request.chainId,
|
|
4543
|
+
poolDefinitionId: request.poolDefinitionId,
|
|
4544
|
+
inputTokenDeploymentId: request.inputTokenDeploymentId,
|
|
4545
|
+
outputTokenDeploymentId: request.outputTokenDeploymentId,
|
|
4546
|
+
amountIn: request.amountIn,
|
|
4547
|
+
...freshnessSnapshot === void 0 ? {} : { freshness: freshnessSnapshot }
|
|
4548
|
+
};
|
|
4549
|
+
return {
|
|
4550
|
+
quoteRequest,
|
|
4551
|
+
sender: request.sender,
|
|
4552
|
+
recipient: request.recipient,
|
|
4553
|
+
slippageBps: request.slippageBps,
|
|
4554
|
+
deadline: request.deadline
|
|
4555
|
+
};
|
|
4556
|
+
};
|
|
4557
|
+
var normalizeExecutionAddress = (value) => {
|
|
4558
|
+
if (typeof value !== "string" || !/^0x[0-9a-fA-F]{40}$/u.test(value)) {
|
|
4559
|
+
executionFail("SWAP_EXECUTION_INVALID_ARGUMENT");
|
|
4560
|
+
}
|
|
4561
|
+
const normalized = value.toLowerCase();
|
|
4562
|
+
if (normalized === `0x${"0".repeat(40)}`) {
|
|
4563
|
+
executionFail("SWAP_EXECUTION_INVALID_ARGUMENT");
|
|
4564
|
+
}
|
|
4565
|
+
return normalized;
|
|
4566
|
+
};
|
|
4567
|
+
var normalizeExecutionSlippage = (value) => {
|
|
4568
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0 || value > 9999) {
|
|
4569
|
+
executionFail("SWAP_EXECUTION_INVALID_ARGUMENT");
|
|
4570
|
+
}
|
|
4571
|
+
return value;
|
|
4572
|
+
};
|
|
4573
|
+
var parseExecutionDeadline = (value) => {
|
|
4574
|
+
if (typeof value !== "string" || !UINT256_DECIMAL.test(value) || value.length > 78) {
|
|
4575
|
+
executionFail("SWAP_EXECUTION_INVALID_ARGUMENT");
|
|
4576
|
+
}
|
|
4577
|
+
let parsed;
|
|
4578
|
+
try {
|
|
4579
|
+
parsed = BigInt(value);
|
|
4580
|
+
} catch {
|
|
4581
|
+
executionFail("SWAP_EXECUTION_INVALID_ARGUMENT");
|
|
4582
|
+
}
|
|
4583
|
+
if (parsed <= 0n || parsed > UINT256_MAX) {
|
|
4584
|
+
executionFail("SWAP_EXECUTION_INVALID_ARGUMENT");
|
|
4585
|
+
}
|
|
4586
|
+
return { source: value, value: parsed };
|
|
4587
|
+
};
|
|
4588
|
+
var normalizeExecutionRequest = (request, clock) => {
|
|
4589
|
+
const snapshot = snapshotExecutionRequest(request);
|
|
4590
|
+
const sender = normalizeExecutionAddress(snapshot.sender);
|
|
4591
|
+
const recipient = normalizeExecutionAddress(snapshot.recipient);
|
|
4592
|
+
const slippageBps = normalizeExecutionSlippage(snapshot.slippageBps);
|
|
4593
|
+
const deadline = parseExecutionDeadline(snapshot.deadline);
|
|
4594
|
+
const initialClock = BigInt(currentClockSeconds(clock));
|
|
4595
|
+
if (deadline.value <= initialClock) {
|
|
4596
|
+
executionFail("SWAP_EXECUTION_INVALID_ARGUMENT");
|
|
4597
|
+
}
|
|
4598
|
+
let normalized;
|
|
4599
|
+
try {
|
|
4600
|
+
normalized = normalizeQuoteRequest(snapshot.quoteRequest);
|
|
4601
|
+
} catch (error) {
|
|
4602
|
+
if (error instanceof SwapQuoteError && (error.code === "SWAP_UNKNOWN_POOL" || error.code === "SWAP_UNSUPPORTED_ADAPTER" || error.code === "SWAP_UNSUPPORTED_TOKEN" || error.code === "SWAP_UNSUPPORTED_TOKEN_STANDARD")) {
|
|
4603
|
+
executionFail("SWAP_UNSUPPORTED_EXECUTION");
|
|
4604
|
+
}
|
|
4605
|
+
throw error;
|
|
4606
|
+
}
|
|
4607
|
+
return {
|
|
4608
|
+
normalized,
|
|
4609
|
+
sender,
|
|
4610
|
+
recipient,
|
|
4611
|
+
slippageBps,
|
|
4612
|
+
deadline: deadline.source,
|
|
4613
|
+
deadlineValue: deadline.value
|
|
4614
|
+
};
|
|
4615
|
+
};
|
|
4616
|
+
var assertFreshnessAtNow = (state, freshness, now) => {
|
|
4267
4617
|
const initialAge = now - state.initial.timestamp;
|
|
4268
4618
|
if (initialAge < -BigInt(freshness.maxClockSkewSeconds) || initialAge > BigInt(freshness.maxBlockAgeSeconds)) {
|
|
4269
4619
|
swapFail("SWAP_STATE_STALE");
|
|
@@ -4285,6 +4635,54 @@ var assertFreshness = (state, freshness, clock) => {
|
|
|
4285
4635
|
swapFail("SWAP_STATE_STALE");
|
|
4286
4636
|
}
|
|
4287
4637
|
};
|
|
4638
|
+
var assertFreshness = (state, freshness, clock) => {
|
|
4639
|
+
assertFreshnessAtNow(
|
|
4640
|
+
state,
|
|
4641
|
+
freshness,
|
|
4642
|
+
BigInt(currentClockSeconds(clock))
|
|
4643
|
+
);
|
|
4644
|
+
};
|
|
4645
|
+
var executionCapabilityFor = (normalized) => {
|
|
4646
|
+
const capability = EXECUTION_CAPABILITIES.find(
|
|
4647
|
+
(entry) => entry.poolDefinitionId === normalized.pool.poolDefinitionId
|
|
4648
|
+
);
|
|
4649
|
+
if (!capability || capability.status !== "active") {
|
|
4650
|
+
executionFail("SWAP_UNSUPPORTED_EXECUTION");
|
|
4651
|
+
}
|
|
4652
|
+
const token0 = getTokenDeployment(capability.token0DeploymentId);
|
|
4653
|
+
const token1 = getTokenDeployment(capability.token1DeploymentId);
|
|
4654
|
+
const wrapped = getTokenDeployment(capability.wrappedNativeTokenDeploymentId);
|
|
4655
|
+
const nativeWrap = NATIVE_WRAP_DEFINITIONS.find(
|
|
4656
|
+
(entry) => entry.chainId === capability.chainId && entry.wrappedTokenDeploymentId === capability.wrappedNativeTokenDeploymentId
|
|
4657
|
+
);
|
|
4658
|
+
const matchesCapabilityToken = (token, deploymentId, address, standard) => token?.deploymentId === deploymentId && token.chainId === capability.chainId && token.address === address && token.standard === standard && token.status === "active";
|
|
4659
|
+
const inputMatches = matchesCapabilityToken(
|
|
4660
|
+
normalized.input,
|
|
4661
|
+
capability.token0DeploymentId,
|
|
4662
|
+
capability.token0Address,
|
|
4663
|
+
capability.token0Standard
|
|
4664
|
+
) || matchesCapabilityToken(
|
|
4665
|
+
normalized.input,
|
|
4666
|
+
capability.token1DeploymentId,
|
|
4667
|
+
capability.token1Address,
|
|
4668
|
+
capability.token1Standard
|
|
4669
|
+
);
|
|
4670
|
+
const outputMatches = matchesCapabilityToken(
|
|
4671
|
+
normalized.output,
|
|
4672
|
+
capability.token0DeploymentId,
|
|
4673
|
+
capability.token0Address,
|
|
4674
|
+
capability.token0Standard
|
|
4675
|
+
) || matchesCapabilityToken(
|
|
4676
|
+
normalized.output,
|
|
4677
|
+
capability.token1DeploymentId,
|
|
4678
|
+
capability.token1Address,
|
|
4679
|
+
capability.token1Standard
|
|
4680
|
+
);
|
|
4681
|
+
if (capability.chainId !== normalized.pool.chainId || capability.dexDeploymentId !== normalized.pool.dexDeploymentId || capability.adapterKind !== SUPPORTED_QUOTE_ADAPTER || capability.functionKind !== "exact-input-erc20-to-erc20" || capability.functionSignature !== SWAP_EXECUTION_FUNCTION_SIGNATURE || capability.functionSelector !== SWAP_EXECUTION_FUNCTION_SELECTOR || normalized.dex.status !== "active" || normalized.dex.programAddress !== capability.factoryAddress || normalized.dex.adapterKind !== capability.adapterKind || normalized.pool.status !== "active" || normalized.pool.adapter.kind !== capability.adapterKind || normalized.pool.adapter.feeNumerator !== "3" || normalized.pool.adapter.feeDenominator !== "1000" || normalized.pool.token0DeploymentId !== capability.token0DeploymentId || normalized.pool.token1DeploymentId !== capability.token1DeploymentId || !inputMatches || !outputMatches || !token0 || !token1 || !wrapped || wrapped.chainId !== capability.chainId || wrapped.address !== capability.wrappedNativeTokenAddress || wrapped.standard !== "erc20" || wrapped.status !== "active" || !nativeWrap || nativeWrap.status !== "active" || nativeWrap.wrappedTokenDeploymentId !== capability.wrappedNativeTokenDeploymentId) {
|
|
4682
|
+
executionFail("SWAP_UNSUPPORTED_EXECUTION");
|
|
4683
|
+
}
|
|
4684
|
+
return capability;
|
|
4685
|
+
};
|
|
4288
4686
|
var readEvmState = async (transport, normalized, clock, options) => {
|
|
4289
4687
|
const { pool, dex } = normalized;
|
|
4290
4688
|
const chainId = await rpcRequest(transport, "eth_chainId", [], options);
|
|
@@ -4417,14 +4815,269 @@ var quoteResult = (normalized, state, result) => {
|
|
|
4417
4815
|
};
|
|
4418
4816
|
return deepFreeze5(output);
|
|
4419
4817
|
};
|
|
4818
|
+
var transportForNormalized = (normalized, options) => {
|
|
4819
|
+
const transport = normalized.pool.chainId === DEX_CHAIN_IDS.ethereum ? options.ethereum : normalized.pool.chainId === DEX_CHAIN_IDS.avalancheC ? options.avalanche : void 0;
|
|
4820
|
+
if (!transport) swapFail("SWAP_UNSUPPORTED_ADAPTER");
|
|
4821
|
+
return transport;
|
|
4822
|
+
};
|
|
4823
|
+
var encodeUint256Word = (value) => ensureUint256(value).toString(16).padStart(64, "0");
|
|
4824
|
+
var executionTokenAddress = (token) => {
|
|
4825
|
+
if (typeof token.address !== "string" || !EVM_ADDRESS.test(token.address)) {
|
|
4826
|
+
executionFail("SWAP_INVALID_SIMULATION");
|
|
4827
|
+
}
|
|
4828
|
+
return token.address.toLowerCase();
|
|
4829
|
+
};
|
|
4830
|
+
var executionGetAmountsOutData = (amountIn, inputAddress, outputAddress) => `${ROUTER_GET_AMOUNTS_OUT_SELECTOR}${encodeUint256Word(amountIn)}${encodeUint256Word(
|
|
4831
|
+
0x40n
|
|
4832
|
+
)}${encodeUint256Word(2n)}${encodeAddressArgument(
|
|
4833
|
+
inputAddress
|
|
4834
|
+
)}${encodeAddressArgument(outputAddress)}`;
|
|
4835
|
+
var executionSwapData = (amountIn, minimumAmountOut, inputAddress, outputAddress, recipient, deadline) => `${SWAP_EXECUTION_FUNCTION_SELECTOR}${encodeUint256Word(
|
|
4836
|
+
amountIn
|
|
4837
|
+
)}${encodeUint256Word(minimumAmountOut)}${encodeUint256Word(
|
|
4838
|
+
0xa0n
|
|
4839
|
+
)}${encodeAddressArgument(recipient)}${encodeUint256Word(
|
|
4840
|
+
deadline
|
|
4841
|
+
)}${encodeUint256Word(2n)}${encodeAddressArgument(
|
|
4842
|
+
inputAddress
|
|
4843
|
+
)}${encodeAddressArgument(outputAddress)}`;
|
|
4844
|
+
var buildExecutionPreparation = (execution, capability, quote) => {
|
|
4845
|
+
const inputAddress = executionTokenAddress(execution.normalized.input);
|
|
4846
|
+
const outputAddress = executionTokenAddress(execution.normalized.output);
|
|
4847
|
+
const quoteAmountOut = parseDecimalQuantity(quote.amountOut, "SWAP_ARITHMETIC");
|
|
4848
|
+
const minimumAmountOut = ensureUint256(
|
|
4849
|
+
quoteAmountOut * BigInt(1e4 - execution.slippageBps),
|
|
4850
|
+
"SWAP_ARITHMETIC"
|
|
4851
|
+
) / 10000n;
|
|
4852
|
+
if (minimumAmountOut <= 0n) {
|
|
4853
|
+
executionFail("SWAP_EXECUTION_INVALID_ARGUMENT");
|
|
4854
|
+
}
|
|
4855
|
+
const path = [
|
|
4856
|
+
{
|
|
4857
|
+
tokenDeploymentId: execution.normalized.input.deploymentId,
|
|
4858
|
+
address: inputAddress,
|
|
4859
|
+
standard: "erc20",
|
|
4860
|
+
representationKind: execution.normalized.input.representationKind
|
|
4861
|
+
},
|
|
4862
|
+
{
|
|
4863
|
+
tokenDeploymentId: execution.normalized.output.deploymentId,
|
|
4864
|
+
address: outputAddress,
|
|
4865
|
+
standard: "erc20",
|
|
4866
|
+
representationKind: execution.normalized.output.representationKind
|
|
4867
|
+
}
|
|
4868
|
+
];
|
|
4869
|
+
const transaction = {
|
|
4870
|
+
kind: "evm-unsigned-transaction",
|
|
4871
|
+
chainId: quote.chainId,
|
|
4872
|
+
from: execution.sender,
|
|
4873
|
+
to: capability.routerAddress,
|
|
4874
|
+
data: executionSwapData(
|
|
4875
|
+
execution.normalized.amountIn,
|
|
4876
|
+
minimumAmountOut,
|
|
4877
|
+
inputAddress,
|
|
4878
|
+
outputAddress,
|
|
4879
|
+
execution.recipient,
|
|
4880
|
+
execution.deadlineValue
|
|
4881
|
+
),
|
|
4882
|
+
value: "0"
|
|
4883
|
+
};
|
|
4884
|
+
return deepFreeze5({
|
|
4885
|
+
preparationKind: "evm-router-v2-exact-input",
|
|
4886
|
+
executionCapabilityId: capability.swapExecutionCapabilityId,
|
|
4887
|
+
executionCapabilityDigest: SWAP_EXECUTION_CAPABILITIES_CONTENT_DIGEST,
|
|
4888
|
+
quote,
|
|
4889
|
+
minimumAmountOut: minimumAmountOut.toString(10),
|
|
4890
|
+
slippageBps: execution.slippageBps,
|
|
4891
|
+
deadline: execution.deadline,
|
|
4892
|
+
recipient: execution.recipient,
|
|
4893
|
+
path,
|
|
4894
|
+
transaction,
|
|
4895
|
+
allowance: {
|
|
4896
|
+
tokenDeploymentId: execution.normalized.input.deploymentId,
|
|
4897
|
+
tokenAddress: inputAddress,
|
|
4898
|
+
owner: execution.sender,
|
|
4899
|
+
spender: capability.routerAddress,
|
|
4900
|
+
requiredAmount: quote.amountIn
|
|
4901
|
+
}
|
|
4902
|
+
});
|
|
4903
|
+
};
|
|
4904
|
+
var assertExecutionDeadline = (execution, quoteTimestamp, completionClock) => {
|
|
4905
|
+
if (execution.deadlineValue <= quoteTimestamp || execution.deadlineValue <= completionClock) {
|
|
4906
|
+
executionFail("SWAP_EXECUTION_INVALID_ARGUMENT");
|
|
4907
|
+
}
|
|
4908
|
+
};
|
|
4909
|
+
var prepareExecutionContext = async (execution, capability, transport, options, requestOptions) => {
|
|
4910
|
+
const state = await readEvmState(
|
|
4911
|
+
transport,
|
|
4912
|
+
execution.normalized,
|
|
4913
|
+
options.clock,
|
|
4914
|
+
requestOptions
|
|
4915
|
+
);
|
|
4916
|
+
assertFreshness(state, execution.normalized.freshness, options.clock);
|
|
4917
|
+
const quote = quoteResult(
|
|
4918
|
+
execution.normalized,
|
|
4919
|
+
state,
|
|
4920
|
+
calculateQuote(execution.normalized, state)
|
|
4921
|
+
);
|
|
4922
|
+
if (execution.deadlineValue <= state.initial.timestamp) {
|
|
4923
|
+
executionFail("SWAP_EXECUTION_INVALID_ARGUMENT");
|
|
4924
|
+
}
|
|
4925
|
+
const inputAddress = executionTokenAddress(execution.normalized.input);
|
|
4926
|
+
const outputAddress = executionTokenAddress(execution.normalized.output);
|
|
4927
|
+
const selector = rpcSelector(state.initial.hash);
|
|
4928
|
+
const routerCodeRaw = await rpcRequest(transport, "eth_getCode", [
|
|
4929
|
+
capability.routerAddress,
|
|
4930
|
+
selector
|
|
4931
|
+
], requestOptions);
|
|
4932
|
+
const routerCode = parseExecutionHexBytes(routerCodeRaw);
|
|
4933
|
+
if (routerCode.length <= 2) executionFail("SWAP_INVALID_SIMULATION");
|
|
4934
|
+
const routerFactoryRaw = await rpcRequest(transport, "eth_call", [
|
|
4935
|
+
abiCall(capability.routerAddress, ROUTER_FACTORY_SELECTOR),
|
|
4936
|
+
selector
|
|
4937
|
+
], requestOptions);
|
|
4938
|
+
const routerFactory = executionAddressWord(routerFactoryRaw);
|
|
4939
|
+
if (routerFactory !== capability.factoryAddress) {
|
|
4940
|
+
executionFail("SWAP_PROGRAM_MISMATCH");
|
|
4941
|
+
}
|
|
4942
|
+
const wrappedNativeRaw = await rpcRequest(transport, "eth_call", [
|
|
4943
|
+
abiCall(capability.routerAddress, capability.wrappedNativeFunctionSelector),
|
|
4944
|
+
selector
|
|
4945
|
+
], requestOptions);
|
|
4946
|
+
const wrappedNative = executionAddressWord(wrappedNativeRaw);
|
|
4947
|
+
if (wrappedNative !== capability.wrappedNativeTokenAddress) {
|
|
4948
|
+
executionFail("SWAP_PROGRAM_MISMATCH");
|
|
4949
|
+
}
|
|
4950
|
+
const amountsOutRaw = await rpcRequest(transport, "eth_call", [
|
|
4951
|
+
abiCall(
|
|
4952
|
+
capability.routerAddress,
|
|
4953
|
+
executionGetAmountsOutData(
|
|
4954
|
+
execution.normalized.amountIn,
|
|
4955
|
+
inputAddress,
|
|
4956
|
+
outputAddress
|
|
4957
|
+
)
|
|
4958
|
+
),
|
|
4959
|
+
selector
|
|
4960
|
+
], requestOptions);
|
|
4961
|
+
const amountsOut = executionUintArrayOfTwo(amountsOutRaw);
|
|
4962
|
+
const quoteAmountOut = parseDecimalQuantity(quote.amountOut, "SWAP_ARITHMETIC");
|
|
4963
|
+
if (amountsOut[0] !== execution.normalized.amountIn || amountsOut[1] !== quoteAmountOut) {
|
|
4964
|
+
executionFail("SWAP_INVALID_SIMULATION");
|
|
4965
|
+
}
|
|
4966
|
+
const latestAfterRouterReads = blockHeader(
|
|
4967
|
+
await rpcRequest(
|
|
4968
|
+
transport,
|
|
4969
|
+
"eth_getBlockByNumber",
|
|
4970
|
+
["latest", false],
|
|
4971
|
+
requestOptions
|
|
4972
|
+
)
|
|
4973
|
+
);
|
|
4974
|
+
const completionClock = BigInt(currentClockSeconds(options.clock));
|
|
4975
|
+
assertFreshnessAtNow(
|
|
4976
|
+
{
|
|
4977
|
+
initial: state.initial,
|
|
4978
|
+
latestAfterReads: latestAfterRouterReads
|
|
4979
|
+
},
|
|
4980
|
+
execution.normalized.freshness,
|
|
4981
|
+
completionClock
|
|
4982
|
+
);
|
|
4983
|
+
const preparation = buildExecutionPreparation(execution, capability, quote);
|
|
4984
|
+
assertExecutionDeadline(
|
|
4985
|
+
execution,
|
|
4986
|
+
state.initial.timestamp,
|
|
4987
|
+
BigInt(currentClockSeconds(options.clock))
|
|
4988
|
+
);
|
|
4989
|
+
return { normalized: execution, capability, transport, state, quote, preparation };
|
|
4990
|
+
};
|
|
4991
|
+
var isRecognizedExecutionRevert = (error) => {
|
|
4992
|
+
if (!(error instanceof ErpcJsonRpcError)) return false;
|
|
4993
|
+
if (![3, -32e3, -32015, -32603].includes(error.rpcCode)) return false;
|
|
4994
|
+
const message = error.message.toLowerCase();
|
|
4995
|
+
return message.includes("execution reverted") || message.includes("transaction reverted") || message.includes("vm execution error") || /^revert(?:ed)?(?:\b|:)/u.test(message);
|
|
4996
|
+
};
|
|
4997
|
+
var simulateExecutionContext = async (context, options, requestOptions) => {
|
|
4998
|
+
const { normalized, capability, transport, state, preparation } = context;
|
|
4999
|
+
const selector = rpcSelector(state.initial.hash);
|
|
5000
|
+
const inputAddress = preparation.allowance.tokenAddress;
|
|
5001
|
+
const allowanceData = `${ERC20_ALLOWANCE_SELECTOR}${encodeAddressArgument(
|
|
5002
|
+
normalized.sender
|
|
5003
|
+
)}${encodeAddressArgument(capability.routerAddress)}`;
|
|
5004
|
+
const allowanceRaw = await rpcRequest(transport, "eth_call", [
|
|
5005
|
+
abiCall(inputAddress, allowanceData),
|
|
5006
|
+
selector
|
|
5007
|
+
], requestOptions);
|
|
5008
|
+
const currentAllowance = executionUintWord(
|
|
5009
|
+
allowanceRaw,
|
|
5010
|
+
"SWAP_INSUFFICIENT_ALLOWANCE"
|
|
5011
|
+
);
|
|
5012
|
+
if (currentAllowance < normalized.normalized.amountIn) {
|
|
5013
|
+
executionFail("SWAP_INSUFFICIENT_ALLOWANCE");
|
|
5014
|
+
}
|
|
5015
|
+
let simulationRaw;
|
|
5016
|
+
try {
|
|
5017
|
+
simulationRaw = await rpcRequest(transport, "eth_call", [
|
|
5018
|
+
{
|
|
5019
|
+
from: preparation.transaction.from,
|
|
5020
|
+
to: preparation.transaction.to,
|
|
5021
|
+
data: preparation.transaction.data,
|
|
5022
|
+
value: "0x0"
|
|
5023
|
+
},
|
|
5024
|
+
selector
|
|
5025
|
+
], requestOptions);
|
|
5026
|
+
} catch (error) {
|
|
5027
|
+
if (isRecognizedExecutionRevert(error)) {
|
|
5028
|
+
executionFail("SWAP_SIMULATION_REVERTED");
|
|
5029
|
+
}
|
|
5030
|
+
throw error;
|
|
5031
|
+
}
|
|
5032
|
+
const latestAfterSimulation = blockHeader(
|
|
5033
|
+
await rpcRequest(
|
|
5034
|
+
transport,
|
|
5035
|
+
"eth_getBlockByNumber",
|
|
5036
|
+
["latest", false],
|
|
5037
|
+
requestOptions
|
|
5038
|
+
)
|
|
5039
|
+
);
|
|
5040
|
+
const completionClock = BigInt(currentClockSeconds(options.clock));
|
|
5041
|
+
assertFreshnessAtNow(
|
|
5042
|
+
{ initial: state.initial, latestAfterReads: latestAfterSimulation },
|
|
5043
|
+
normalized.normalized.freshness,
|
|
5044
|
+
completionClock
|
|
5045
|
+
);
|
|
5046
|
+
const amounts = executionUintArrayOfTwo(simulationRaw);
|
|
5047
|
+
const quoteAmountOut = parseDecimalQuantity(
|
|
5048
|
+
preparation.quote.amountOut,
|
|
5049
|
+
"SWAP_ARITHMETIC"
|
|
5050
|
+
);
|
|
5051
|
+
const minimumAmountOut = parseDecimalQuantity(
|
|
5052
|
+
preparation.minimumAmountOut,
|
|
5053
|
+
"SWAP_ARITHMETIC"
|
|
5054
|
+
);
|
|
5055
|
+
if (amounts[0] !== normalized.normalized.amountIn || amounts[1] !== quoteAmountOut || amounts[1] < minimumAmountOut) {
|
|
5056
|
+
executionFail("SWAP_INVALID_SIMULATION");
|
|
5057
|
+
}
|
|
5058
|
+
assertExecutionDeadline(
|
|
5059
|
+
normalized,
|
|
5060
|
+
state.initial.timestamp,
|
|
5061
|
+
BigInt(currentClockSeconds(options.clock))
|
|
5062
|
+
);
|
|
5063
|
+
return deepFreeze5({
|
|
5064
|
+
simulationKind: "evm-call",
|
|
5065
|
+
preparation,
|
|
5066
|
+
snapshot: preparation.quote.snapshot,
|
|
5067
|
+
currentAllowance: currentAllowance.toString(10),
|
|
5068
|
+
amounts: [
|
|
5069
|
+
amounts[0].toString(10),
|
|
5070
|
+
amounts[1].toString(10)
|
|
5071
|
+
],
|
|
5072
|
+
amountOut: amounts[1].toString(10)
|
|
5073
|
+
});
|
|
5074
|
+
};
|
|
4420
5075
|
var quoteExactInputWithTransports = async (request, options, requestOptions) => {
|
|
4421
5076
|
const normalized = normalizeQuoteRequest(request);
|
|
4422
5077
|
if (normalized.pool.adapter.kind !== SUPPORTED_QUOTE_ADAPTER) {
|
|
4423
5078
|
swapFail("SWAP_UNSUPPORTED_ADAPTER");
|
|
4424
5079
|
}
|
|
4425
|
-
const
|
|
4426
|
-
if (!transport) swapFail("SWAP_UNSUPPORTED_ADAPTER");
|
|
4427
|
-
const selectedTransport = transport;
|
|
5080
|
+
const selectedTransport = transportForNormalized(normalized, options);
|
|
4428
5081
|
const state = await readEvmState(
|
|
4429
5082
|
selectedTransport,
|
|
4430
5083
|
normalized,
|
|
@@ -4434,6 +5087,32 @@ var quoteExactInputWithTransports = async (request, options, requestOptions) =>
|
|
|
4434
5087
|
assertFreshness(state, normalized.freshness, options.clock);
|
|
4435
5088
|
return quoteResult(normalized, state, calculateQuote(normalized, state));
|
|
4436
5089
|
};
|
|
5090
|
+
var prepareExactInputSwapWithTransports = async (request, options, requestOptions) => {
|
|
5091
|
+
const normalized = normalizeExecutionRequest(request, options.clock);
|
|
5092
|
+
const capability = executionCapabilityFor(normalized.normalized);
|
|
5093
|
+
const transport = transportForNormalized(normalized.normalized, options);
|
|
5094
|
+
const context = await prepareExecutionContext(
|
|
5095
|
+
normalized,
|
|
5096
|
+
capability,
|
|
5097
|
+
transport,
|
|
5098
|
+
options,
|
|
5099
|
+
requestOptions
|
|
5100
|
+
);
|
|
5101
|
+
return context.preparation;
|
|
5102
|
+
};
|
|
5103
|
+
var simulateExactInputSwapWithTransports = async (request, options, requestOptions) => {
|
|
5104
|
+
const normalized = normalizeExecutionRequest(request, options.clock);
|
|
5105
|
+
const capability = executionCapabilityFor(normalized.normalized);
|
|
5106
|
+
const transport = transportForNormalized(normalized.normalized, options);
|
|
5107
|
+
const context = await prepareExecutionContext(
|
|
5108
|
+
normalized,
|
|
5109
|
+
capability,
|
|
5110
|
+
transport,
|
|
5111
|
+
options,
|
|
5112
|
+
requestOptions
|
|
5113
|
+
);
|
|
5114
|
+
return simulateExecutionContext(context, options, requestOptions);
|
|
5115
|
+
};
|
|
4437
5116
|
var deepFreeze5 = (value) => {
|
|
4438
5117
|
if (value && typeof value === "object" && !Object.isFrozen(value)) {
|
|
4439
5118
|
Object.freeze(value);
|
|
@@ -4443,8 +5122,13 @@ var deepFreeze5 = (value) => {
|
|
|
4443
5122
|
}
|
|
4444
5123
|
return value;
|
|
4445
5124
|
};
|
|
5125
|
+
var EXECUTION_CAPABILITIES = deepFreeze5(
|
|
5126
|
+
JSON.parse(SWAP_EXECUTION_CAPABILITIES_JSON)
|
|
5127
|
+
);
|
|
4446
5128
|
var createSwapClient = (options) => Object.freeze({
|
|
4447
|
-
quoteExactInput: (request, requestOptions) => quoteExactInputWithTransports(request, options, requestOptions)
|
|
5129
|
+
quoteExactInput: (request, requestOptions) => quoteExactInputWithTransports(request, options, requestOptions),
|
|
5130
|
+
prepareExactInputSwap: (request, requestOptions) => prepareExactInputSwapWithTransports(request, options, requestOptions),
|
|
5131
|
+
simulateExactInputSwap: (request, requestOptions) => simulateExactInputSwapWithTransports(request, options, requestOptions)
|
|
4448
5132
|
});
|
|
4449
5133
|
|
|
4450
5134
|
// src/transport/http.ts
|
|
@@ -4476,19 +5160,45 @@ var parseJson = (text) => {
|
|
|
4476
5160
|
throw new ErpcInvalidResponseError("ERPC returned malformed JSON");
|
|
4477
5161
|
}
|
|
4478
5162
|
};
|
|
5163
|
+
var directRequestHeaders = (headers) => {
|
|
5164
|
+
try {
|
|
5165
|
+
const result = new Headers(headers);
|
|
5166
|
+
result.set("accept", "application/json");
|
|
5167
|
+
result.set("content-type", "application/json");
|
|
5168
|
+
return result;
|
|
5169
|
+
} catch {
|
|
5170
|
+
const result = {};
|
|
5171
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
5172
|
+
const lowerName = name.toLowerCase();
|
|
5173
|
+
if (lowerName === "accept" || lowerName === "content-type") continue;
|
|
5174
|
+
result[name] = value;
|
|
5175
|
+
}
|
|
5176
|
+
result.accept = "application/json";
|
|
5177
|
+
result["content-type"] = "application/json";
|
|
5178
|
+
return result;
|
|
5179
|
+
}
|
|
5180
|
+
};
|
|
4479
5181
|
var HttpJsonRpcTransport = class {
|
|
4480
5182
|
endpoint;
|
|
4481
5183
|
maxBatchSize;
|
|
4482
5184
|
#apiKey;
|
|
5185
|
+
#direct;
|
|
5186
|
+
#requestEndpoint;
|
|
4483
5187
|
#fetch;
|
|
4484
5188
|
#headers;
|
|
5189
|
+
#redactionVariants;
|
|
4485
5190
|
#timeoutMs;
|
|
5191
|
+
#unavailableNamespace;
|
|
4486
5192
|
#nextId = 1;
|
|
4487
5193
|
constructor(config) {
|
|
4488
|
-
this.#apiKey = config.apiKey;
|
|
5194
|
+
this.#apiKey = config.apiKey ?? "";
|
|
5195
|
+
this.#direct = config.direct ?? false;
|
|
5196
|
+
this.#requestEndpoint = new URL(config.endpoint);
|
|
4489
5197
|
this.#fetch = config.fetch;
|
|
4490
5198
|
this.#headers = config.headers;
|
|
5199
|
+
this.#redactionVariants = this.#direct ? directRedactionVariants(this.#requestEndpoint, this.#headers) : [];
|
|
4491
5200
|
this.#timeoutMs = config.timeoutMs;
|
|
5201
|
+
this.#unavailableNamespace = config.unavailableNamespace;
|
|
4492
5202
|
this.maxBatchSize = config.maxBatchSize ?? 256;
|
|
4493
5203
|
const endpoint = new URL(config.endpoint);
|
|
4494
5204
|
endpoint.search = "";
|
|
@@ -4496,12 +5206,14 @@ var HttpJsonRpcTransport = class {
|
|
|
4496
5206
|
this.endpoint = endpoint.toString();
|
|
4497
5207
|
}
|
|
4498
5208
|
async request(method, params, options = {}) {
|
|
5209
|
+
this.#ensureConfigured();
|
|
4499
5210
|
const id = this.#id();
|
|
4500
5211
|
const request = this.#request(id, method, params);
|
|
4501
5212
|
const response = await this.#post(request, options);
|
|
4502
5213
|
return this.#unwrap(response, id);
|
|
4503
5214
|
}
|
|
4504
5215
|
async batch(calls, options = {}) {
|
|
5216
|
+
this.#ensureConfigured();
|
|
4505
5217
|
if (calls.length === 0) return [];
|
|
4506
5218
|
if (calls.length > this.maxBatchSize) {
|
|
4507
5219
|
throw new ErpcInvalidResponseError(
|
|
@@ -4515,7 +5227,13 @@ var HttpJsonRpcTransport = class {
|
|
|
4515
5227
|
const raw = await this.#post(requests, options);
|
|
4516
5228
|
if (!Array.isArray(raw)) {
|
|
4517
5229
|
if (isFailure(raw)) {
|
|
4518
|
-
throw new ErpcJsonRpcError(
|
|
5230
|
+
throw new ErpcJsonRpcError(
|
|
5231
|
+
redactJsonRpcError(
|
|
5232
|
+
raw.error,
|
|
5233
|
+
this.#direct ? void 0 : this.#apiKey,
|
|
5234
|
+
this.#redactionVariants
|
|
5235
|
+
)
|
|
5236
|
+
);
|
|
4519
5237
|
}
|
|
4520
5238
|
throw new ErpcInvalidResponseError("ERPC returned a non-array batch response");
|
|
4521
5239
|
}
|
|
@@ -4557,24 +5275,34 @@ var HttpJsonRpcTransport = class {
|
|
|
4557
5275
|
this.#nextId = id >= Number.MAX_SAFE_INTEGER ? 1 : id + 1;
|
|
4558
5276
|
return id;
|
|
4559
5277
|
}
|
|
5278
|
+
#ensureConfigured() {
|
|
5279
|
+
if (this.#unavailableNamespace !== void 0) {
|
|
5280
|
+
throw new ErpcNotConfiguredError(this.#unavailableNamespace);
|
|
5281
|
+
}
|
|
5282
|
+
}
|
|
4560
5283
|
#request(id, method, params) {
|
|
4561
5284
|
const request = { jsonrpc: "2.0", id, method };
|
|
4562
5285
|
if (params !== void 0) request.params = params;
|
|
4563
5286
|
return request;
|
|
4564
5287
|
}
|
|
4565
5288
|
async #post(body, options) {
|
|
4566
|
-
|
|
4567
|
-
url
|
|
5289
|
+
this.#ensureConfigured();
|
|
5290
|
+
const url = new URL(this.#requestEndpoint);
|
|
5291
|
+
if (!this.#direct) url.searchParams.set("api-key", this.#apiKey);
|
|
4568
5292
|
const controlled = controlledSignal2(this.#timeoutMs, options.signal);
|
|
4569
5293
|
try {
|
|
4570
5294
|
const response = await this.#fetch(url, {
|
|
4571
5295
|
method: "POST",
|
|
4572
|
-
headers: {
|
|
5296
|
+
headers: this.#direct ? directRequestHeaders(this.#headers) : {
|
|
4573
5297
|
...this.#headers,
|
|
4574
5298
|
"content-type": "application/json"
|
|
4575
5299
|
},
|
|
4576
5300
|
body: JSON.stringify(body),
|
|
4577
|
-
signal: controlled.signal
|
|
5301
|
+
signal: controlled.signal,
|
|
5302
|
+
...this.#direct ? {
|
|
5303
|
+
credentials: "omit",
|
|
5304
|
+
redirect: "manual"
|
|
5305
|
+
} : {}
|
|
4578
5306
|
});
|
|
4579
5307
|
const text = await response.text();
|
|
4580
5308
|
if (!response.ok) throw new ErpcHttpError(response.status);
|
|
@@ -4597,7 +5325,11 @@ var HttpJsonRpcTransport = class {
|
|
|
4597
5325
|
}
|
|
4598
5326
|
if (isFailure(response)) {
|
|
4599
5327
|
throw new ErpcJsonRpcError(
|
|
4600
|
-
redactJsonRpcError(
|
|
5328
|
+
redactJsonRpcError(
|
|
5329
|
+
response.error,
|
|
5330
|
+
this.#direct ? void 0 : this.#apiKey,
|
|
5331
|
+
this.#redactionVariants
|
|
5332
|
+
)
|
|
4601
5333
|
);
|
|
4602
5334
|
}
|
|
4603
5335
|
if (!("result" in response)) throw new ErpcInvalidResponseError();
|
|
@@ -4691,62 +5423,81 @@ var UsageClient = class {
|
|
|
4691
5423
|
};
|
|
4692
5424
|
|
|
4693
5425
|
// src/client.ts
|
|
5426
|
+
var unavailableEndpoint = (namespace) => new URL(
|
|
5427
|
+
`https://unconfigured.invalid/${namespace.replace(/[^a-z\d._-]+/giu, "-")}`
|
|
5428
|
+
);
|
|
4694
5429
|
var createErpcClient = (config) => {
|
|
4695
5430
|
const resolved = resolveConfig(config);
|
|
4696
|
-
const
|
|
4697
|
-
apiKey: resolved.apiKey,
|
|
5431
|
+
const legacyTransport = {
|
|
4698
5432
|
fetch: resolved.fetch,
|
|
4699
5433
|
headers: resolved.headers,
|
|
4700
|
-
timeoutMs: resolved.timeoutMs
|
|
5434
|
+
timeoutMs: resolved.timeoutMs,
|
|
5435
|
+
...resolved.apiKey === void 0 ? {} : { apiKey: resolved.apiKey }
|
|
4701
5436
|
};
|
|
4702
|
-
const
|
|
4703
|
-
|
|
4704
|
-
|
|
4705
|
-
|
|
4706
|
-
const ethereumEndpoint = endpointWithPath(resolved.endpoint, "/eth");
|
|
4707
|
-
const ethereumTransport = new HttpJsonRpcTransport({
|
|
4708
|
-
...sharedTransport,
|
|
4709
|
-
endpoint: ethereumEndpoint
|
|
4710
|
-
});
|
|
4711
|
-
const avalancheTransport = new HttpJsonRpcTransport({
|
|
4712
|
-
...sharedTransport,
|
|
4713
|
-
endpoint: endpointWithPath(resolved.avalancheEndpoint, "/ava")
|
|
4714
|
-
});
|
|
4715
|
-
const avalancheIndexTransport = (path) => new HttpJsonRpcTransport({
|
|
4716
|
-
...sharedTransport,
|
|
4717
|
-
endpoint: endpointWithPath(resolved.avalancheEndpoint, path)
|
|
4718
|
-
});
|
|
4719
|
-
const solanaWebSocket = new WebSocketJsonRpcTransport({
|
|
4720
|
-
endpoint: websocketUrl(resolved.endpoint, resolved.apiKey),
|
|
5437
|
+
const createUnavailableHttp = (namespace) => new HttpJsonRpcTransport({
|
|
5438
|
+
endpoint: unavailableEndpoint(namespace),
|
|
5439
|
+
fetch: resolved.fetch,
|
|
5440
|
+
headers: {},
|
|
4721
5441
|
timeoutMs: resolved.timeoutMs,
|
|
4722
|
-
|
|
5442
|
+
unavailableNamespace: namespace
|
|
4723
5443
|
});
|
|
4724
|
-
const
|
|
4725
|
-
endpoint:
|
|
5444
|
+
const createUnavailableWebSocket = (namespace) => new WebSocketJsonRpcTransport({
|
|
5445
|
+
endpoint: unavailableEndpoint(namespace),
|
|
4726
5446
|
timeoutMs: resolved.timeoutMs,
|
|
4727
|
-
|
|
5447
|
+
unavailableNamespace: namespace
|
|
4728
5448
|
});
|
|
4729
|
-
const
|
|
4730
|
-
|
|
4731
|
-
|
|
4732
|
-
|
|
4733
|
-
|
|
4734
|
-
|
|
5449
|
+
const createDirectHttp = (endpoint) => new HttpJsonRpcTransport({
|
|
5450
|
+
direct: true,
|
|
5451
|
+
endpoint: endpoint.httpUrl,
|
|
5452
|
+
fetch: resolved.fetch,
|
|
5453
|
+
headers: endpoint.headers,
|
|
5454
|
+
timeoutMs: resolved.timeoutMs
|
|
5455
|
+
});
|
|
5456
|
+
const createDirectWebSocket = (endpoint, namespace) => {
|
|
5457
|
+
if (endpoint.webSocketUrl === void 0) {
|
|
5458
|
+
return createUnavailableWebSocket(namespace);
|
|
5459
|
+
}
|
|
5460
|
+
return new WebSocketJsonRpcTransport({
|
|
5461
|
+
endpoint: endpoint.webSocketUrl,
|
|
5462
|
+
redactionHeaders: endpoint.headers,
|
|
5463
|
+
timeoutMs: resolved.timeoutMs,
|
|
5464
|
+
...resolved.webSocket === void 0 ? {} : { webSocket: resolved.webSocket }
|
|
5465
|
+
});
|
|
5466
|
+
};
|
|
5467
|
+
const createLegacyHttp = (endpoint) => new HttpJsonRpcTransport({ ...legacyTransport, endpoint });
|
|
5468
|
+
const createLegacyWebSocket = (endpoint) => new WebSocketJsonRpcTransport({
|
|
5469
|
+
endpoint,
|
|
4735
5470
|
timeoutMs: resolved.timeoutMs,
|
|
4736
5471
|
...resolved.webSocket === void 0 ? {} : { webSocket: resolved.webSocket }
|
|
4737
5472
|
});
|
|
4738
|
-
const
|
|
4739
|
-
|
|
4740
|
-
|
|
5473
|
+
const solanaTransport = resolved.solanaRpc === void 0 ? resolved.apiKey === void 0 ? createUnavailableHttp("solana.rpc") : createLegacyHttp(resolved.endpoint) : createDirectHttp(resolved.solanaRpc);
|
|
5474
|
+
const ethereumTransport = resolved.ethereumRpc === void 0 ? resolved.apiKey === void 0 ? createUnavailableHttp("ethereum.rpc") : createLegacyHttp(endpointWithPath(resolved.endpoint, "/eth")) : createDirectHttp(resolved.ethereumRpc);
|
|
5475
|
+
const avalancheTransport = resolved.avalancheCRpc === void 0 ? resolved.apiKey === void 0 ? createUnavailableHttp("avalanche.rpc") : createLegacyHttp(endpointWithPath(resolved.avalancheEndpoint, "/ava")) : createDirectHttp(resolved.avalancheCRpc);
|
|
5476
|
+
const avalancheNativeTransport = resolved.apiKey === void 0 ? createUnavailableHttp("avalanche.native") : createLegacyHttp(endpointWithPath(resolved.avalancheEndpoint, "/ava"));
|
|
5477
|
+
const avalancheIndexTransport = (path, namespace) => resolved.apiKey === void 0 ? createUnavailableHttp(namespace) : createLegacyHttp(endpointWithPath(resolved.avalancheEndpoint, path));
|
|
5478
|
+
const solanaWebSocket = resolved.solanaRpc === void 0 ? resolved.apiKey === void 0 ? createUnavailableWebSocket("solana.subscriptions") : createLegacyWebSocket(websocketUrl(resolved.endpoint, resolved.apiKey)) : createDirectWebSocket(resolved.solanaRpc, "solana.subscriptions");
|
|
5479
|
+
const ethereumWebSocket = resolved.ethereumRpc === void 0 ? resolved.apiKey === void 0 ? createUnavailableWebSocket("ethereum.subscriptions") : createLegacyWebSocket(
|
|
5480
|
+
websocketUrl(resolved.endpoint, resolved.apiKey, "/eth")
|
|
5481
|
+
) : createDirectWebSocket(resolved.ethereumRpc, "ethereum.subscriptions");
|
|
5482
|
+
const avalancheWebSocket = resolved.avalancheCRpc === void 0 ? resolved.apiKey === void 0 ? createUnavailableWebSocket("avalanche.subscriptions") : createLegacyWebSocket(
|
|
5483
|
+
websocketUrl(resolved.avalancheEndpoint, resolved.apiKey, "/ava-ws")
|
|
5484
|
+
) : createDirectWebSocket(
|
|
5485
|
+
resolved.avalancheCRpc,
|
|
5486
|
+
"avalanche.subscriptions"
|
|
5487
|
+
);
|
|
5488
|
+
const createUnavailableRest = (namespace) => new RestTransport({
|
|
5489
|
+
endpoint: unavailableEndpoint(namespace),
|
|
5490
|
+
fetch: resolved.fetch,
|
|
5491
|
+
headers: {},
|
|
5492
|
+
timeoutMs: resolved.timeoutMs,
|
|
5493
|
+
unavailableNamespace: namespace
|
|
4741
5494
|
});
|
|
4742
|
-
const
|
|
4743
|
-
|
|
5495
|
+
const priceTransport = resolved.apiKey === void 0 ? createUnavailableRest("price") : new RestTransport({ ...legacyTransport, endpoint: resolved.endpoint });
|
|
5496
|
+
const accountTransport = resolved.apiKey === void 0 ? createUnavailableRest("account") : new RestTransport({
|
|
5497
|
+
...legacyTransport,
|
|
4744
5498
|
endpoint: resolved.accountEndpoint
|
|
4745
5499
|
});
|
|
4746
|
-
const userTransport = new RestTransport({
|
|
4747
|
-
...sharedTransport,
|
|
4748
|
-
endpoint: resolved.userEndpoint
|
|
4749
|
-
});
|
|
5500
|
+
const userTransport = resolved.apiKey === void 0 ? createUnavailableRest("usage") : new RestTransport({ ...legacyTransport, endpoint: resolved.userEndpoint });
|
|
4750
5501
|
const solana = {
|
|
4751
5502
|
...createSolanaClient(solanaTransport),
|
|
4752
5503
|
subscriptions: new SolanaSubscriptions(solanaWebSocket)
|
|
@@ -4757,11 +5508,23 @@ var createErpcClient = (config) => {
|
|
|
4757
5508
|
};
|
|
4758
5509
|
const avalanche = {
|
|
4759
5510
|
...createAvalancheClient(avalancheTransport, {
|
|
4760
|
-
cChainBlocks: avalancheIndexTransport(
|
|
4761
|
-
|
|
4762
|
-
|
|
4763
|
-
|
|
4764
|
-
|
|
5511
|
+
cChainBlocks: avalancheIndexTransport(
|
|
5512
|
+
"/ava/ext/index/C/block",
|
|
5513
|
+
"avalanche.index.cChainBlocks"
|
|
5514
|
+
),
|
|
5515
|
+
pChainBlocks: avalancheIndexTransport(
|
|
5516
|
+
"/ava/ext/index/P/block",
|
|
5517
|
+
"avalanche.index.pChainBlocks"
|
|
5518
|
+
),
|
|
5519
|
+
xChainBlocks: avalancheIndexTransport(
|
|
5520
|
+
"/ava/ext/index/X/block",
|
|
5521
|
+
"avalanche.index.xChainBlocks"
|
|
5522
|
+
),
|
|
5523
|
+
xChainTransactions: avalancheIndexTransport(
|
|
5524
|
+
"/ava/ext/index/X/tx",
|
|
5525
|
+
"avalanche.index.xChainTransactions"
|
|
5526
|
+
)
|
|
5527
|
+
}, avalancheNativeTransport),
|
|
4765
5528
|
subscriptions: new EthereumSubscriptions(avalancheWebSocket)
|
|
4766
5529
|
};
|
|
4767
5530
|
const swap = createSwapClient({
|
|
@@ -5000,6 +5763,1611 @@ var createErpcCloudClient = (config) => {
|
|
|
5000
5763
|
};
|
|
5001
5764
|
};
|
|
5002
5765
|
|
|
5766
|
+
// src/generated/bridge_capabilities.ts
|
|
5767
|
+
var BRIDGE_CAPABILITIES_JSON = '[{"bridgeCapabilityId":"bridge-mayan-swift-v2-eurc-eth-sol","providerId":"mayan-swift-v2","capabilityKind":"external-provider-dynamic","sourceChainId":"eip155:1","destinationChainId":"solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp","sourceTokenDeploymentId":"deployment-0011","destinationTokenDeploymentId":"deployment-0013","sourceTokenAddress":"0x1abaea1f7c830bd89acc67ec4af516284b1bc33c","destinationTokenAddress":"HzwqbKZw8HxMN6bF2yFZNrht3c2iXXzpKcFu7uBEDKtr","sourceTokenStandard":"erc20","destinationTokenStandard":"spl-token","sourceTokenDecimals":6,"destinationTokenDecimals":6,"sourceProviderChainName":"ethereum","destinationProviderChainName":"solana","sourceProviderChainId":1,"destinationProviderChainId":0,"sourceWormholeChainId":2,"destinationWormholeChainId":1,"sourceUsdcDeploymentId":"deployment-0008","sourceUsdcAddress":"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48","sourceUsdcStandard":"erc20","sourceUsdcDecimals":6,"swiftContract":"0x40ffe85a28dc9993541449464d7529a922142960","forwarderAddress":"0x337685fdab40d39bd02028545a4ffa7d287cc3e2","forwarderFunctionSelector":"0x30dedc57","jupiterProgramAddress":null,"builderEndpoint":"https://tx-builder.mayan.finance","explorerEndpoint":"https://explorer-api.mayan.finance/v3","dependencies":["mayan-hosted-quote-api","mayan-hosted-transaction-builder","mayan-hosted-source-swap-builder","swift-auction-solvers","relayers","wormhole-guardian-messaging","mayan-explorer-indexer"],"status":"active"},{"bridgeCapabilityId":"bridge-mayan-swift-v2-eurc-sol-eth","providerId":"mayan-swift-v2","capabilityKind":"external-provider-dynamic","sourceChainId":"solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp","destinationChainId":"eip155:1","sourceTokenDeploymentId":"deployment-0013","destinationTokenDeploymentId":"deployment-0011","sourceTokenAddress":"HzwqbKZw8HxMN6bF2yFZNrht3c2iXXzpKcFu7uBEDKtr","destinationTokenAddress":"0x1abaea1f7c830bd89acc67ec4af516284b1bc33c","sourceTokenStandard":"spl-token","destinationTokenStandard":"erc20","sourceTokenDecimals":6,"destinationTokenDecimals":6,"sourceProviderChainName":"solana","destinationProviderChainName":"ethereum","sourceProviderChainId":0,"destinationProviderChainId":1,"sourceWormholeChainId":1,"destinationWormholeChainId":2,"sourceUsdcDeploymentId":"deployment-0010","sourceUsdcAddress":"EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v","sourceUsdcStandard":"spl-token","sourceUsdcDecimals":6,"swiftContract":"mayan34VedncxdK2XobtvWFDXQASUTBXhUVzt2kKgny","forwarderAddress":null,"forwarderFunctionSelector":null,"jupiterProgramAddress":"JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4","builderEndpoint":"https://tx-builder.mayan.finance","explorerEndpoint":"https://explorer-api.mayan.finance/v3","dependencies":["mayan-hosted-quote-api","mayan-hosted-transaction-builder","mayan-hosted-source-swap-builder","swift-auction-solvers","relayers","wormhole-guardian-messaging","mayan-explorer-indexer","jupiter-v6-source-swap"],"status":"active"}]';
|
|
5768
|
+
var BRIDGE_CAPABILITIES_CONTENT_DIGEST = "fe698f99218057f8cb40b3e24421d1d78e4e2cd7508cf6e3eb2457b329e5141b";
|
|
5769
|
+
var BRIDGE_CAPABILITIES_AS_OF_DATE = "2026-09-16";
|
|
5770
|
+
|
|
5771
|
+
// src/bridge.ts
|
|
5772
|
+
var BRIDGE_ERROR_MESSAGES = Object.freeze({
|
|
5773
|
+
BRIDGE_INVALID_ARGUMENT: "Bridge request is invalid",
|
|
5774
|
+
BRIDGE_UNSUPPORTED_ROUTE: "Bridge route is unsupported",
|
|
5775
|
+
BRIDGE_PROVIDER_AUTH_REQUIRED: "Bridge provider authentication is required",
|
|
5776
|
+
BRIDGE_PROVIDER_TRANSPORT: "Bridge provider transport failed",
|
|
5777
|
+
BRIDGE_PROVIDER_HTTP: "Bridge provider HTTP request failed",
|
|
5778
|
+
BRIDGE_PROVIDER_INVALID_RESPONSE: "Bridge provider response is invalid",
|
|
5779
|
+
BRIDGE_QUOTE_UNAVAILABLE: "Bridge quote is unavailable",
|
|
5780
|
+
BRIDGE_QUOTE_EXPIRED: "Bridge quote is expired",
|
|
5781
|
+
BRIDGE_QUOTE_MISMATCH: "Bridge quote does not match the request",
|
|
5782
|
+
BRIDGE_BUILD_INVALID: "Bridge provider build is invalid",
|
|
5783
|
+
BRIDGE_STATUS_NOT_FOUND: "Bridge status was not found",
|
|
5784
|
+
BRIDGE_TIMEOUT: "Bridge provider request timed out",
|
|
5785
|
+
BRIDGE_ABORTED: "Bridge provider request was aborted"
|
|
5786
|
+
});
|
|
5787
|
+
var BridgeError = class extends Error {
|
|
5788
|
+
code;
|
|
5789
|
+
status;
|
|
5790
|
+
constructor(code, status) {
|
|
5791
|
+
super(BRIDGE_ERROR_MESSAGES[code]);
|
|
5792
|
+
this.name = "BridgeError";
|
|
5793
|
+
this.code = code;
|
|
5794
|
+
if (status !== void 0) this.status = status;
|
|
5795
|
+
}
|
|
5796
|
+
};
|
|
5797
|
+
var ETHEREUM_CHAIN_ID = TOKEN_CHAIN_IDS.ethereumMainnet;
|
|
5798
|
+
var SOLANA_CHAIN_ID = TOKEN_CHAIN_IDS.solanaMainnet;
|
|
5799
|
+
var ETHEREUM_NAME = "ethereum";
|
|
5800
|
+
var SOLANA_NAME = "solana";
|
|
5801
|
+
var ETHEREUM_PROVIDER_CHAIN_ID = 1;
|
|
5802
|
+
var SOLANA_PROVIDER_CHAIN_ID = 0;
|
|
5803
|
+
var ETHEREUM_WORMHOLE_CHAIN_ID = 2;
|
|
5804
|
+
var SOLANA_WORMHOLE_CHAIN_ID = 1;
|
|
5805
|
+
var ETHEREUM_EURC_DEPLOYMENT_ID = "deployment-0011";
|
|
5806
|
+
var SOLANA_EURC_DEPLOYMENT_ID = "deployment-0013";
|
|
5807
|
+
var ETHEREUM_USDC_DEPLOYMENT_ID = "deployment-0008";
|
|
5808
|
+
var SOLANA_USDC_DEPLOYMENT_ID = "deployment-0010";
|
|
5809
|
+
var ETHEREUM_EURC_ADDRESS = "0x1abaea1f7c830bd89acc67ec4af516284b1bc33c";
|
|
5810
|
+
var SOLANA_EURC_ADDRESS = "HzwqbKZw8HxMN6bF2yFZNrht3c2iXXzpKcFu7uBEDKtr";
|
|
5811
|
+
var ETHEREUM_USDC_ADDRESS = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48";
|
|
5812
|
+
var SOLANA_USDC_ADDRESS = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";
|
|
5813
|
+
var ETHEREUM_SWIFT_CONTRACT = "0x40ffe85a28dc9993541449464d7529a922142960";
|
|
5814
|
+
var SOLANA_SWIFT_PROGRAM = "mayan34VedncxdK2XobtvWFDXQASUTBXhUVzt2kKgny";
|
|
5815
|
+
var ETHEREUM_FORWARDER = "0x337685fdab40d39bd02028545a4ffa7d287cc3e2";
|
|
5816
|
+
var SOLANA_JUPITER_V6 = "JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4";
|
|
5817
|
+
var ETHEREUM_FORWARDER_SELECTOR = "0x30dedc57";
|
|
5818
|
+
var DEFAULT_BUILDER_ENDPOINT = "https://tx-builder.mayan.finance";
|
|
5819
|
+
var DEFAULT_EXPLORER_ENDPOINT = "https://explorer-api.mayan.finance/v3";
|
|
5820
|
+
var DEFAULT_TIMEOUT_MS2 = 3e4;
|
|
5821
|
+
var MAX_RESPONSE_BYTES = 1024 * 1024;
|
|
5822
|
+
var MAX_RAW_QUOTE_BYTES = 256 * 1024;
|
|
5823
|
+
var MAX_JSON_DEPTH = 32;
|
|
5824
|
+
var MAX_QUOTES = 16;
|
|
5825
|
+
var UINT64_MAX = (1n << 64n) - 1n;
|
|
5826
|
+
var CANONICAL_UINT64 = /^(0|[1-9][0-9]*)$/u;
|
|
5827
|
+
var POSITIVE_UINT64 = /^[1-9][0-9]*$/u;
|
|
5828
|
+
var EVM_ADDRESS2 = /^0x[0-9a-fA-F]{40}$/u;
|
|
5829
|
+
var EVM_HASH = /^0x[0-9a-fA-F]{64}$/u;
|
|
5830
|
+
var EVM_SIGNATURE = /^0x[0-9a-fA-F]{130}$/u;
|
|
5831
|
+
var HEX_BYTES2 = /^0x[0-9a-fA-F]*$/u;
|
|
5832
|
+
var BASE64 = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/u;
|
|
5833
|
+
var BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
5834
|
+
var DEPENDENCIES = Object.freeze([
|
|
5835
|
+
"mayan-hosted-quote-api",
|
|
5836
|
+
"mayan-hosted-transaction-builder",
|
|
5837
|
+
"mayan-hosted-source-swap-builder",
|
|
5838
|
+
"swift-auction-solvers",
|
|
5839
|
+
"relayers",
|
|
5840
|
+
"wormhole-guardian-messaging",
|
|
5841
|
+
"mayan-explorer-indexer"
|
|
5842
|
+
]);
|
|
5843
|
+
var isRecord4 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
5844
|
+
var deepFreeze6 = (value) => {
|
|
5845
|
+
if (value && typeof value === "object" && !Object.isFrozen(value)) {
|
|
5846
|
+
Object.freeze(value);
|
|
5847
|
+
for (const child of Object.values(value)) deepFreeze6(child);
|
|
5848
|
+
}
|
|
5849
|
+
return value;
|
|
5850
|
+
};
|
|
5851
|
+
var parsedCapabilities = JSON.parse(BRIDGE_CAPABILITIES_JSON);
|
|
5852
|
+
var BRIDGE_CAPABILITIES = deepFreeze6(
|
|
5853
|
+
Array.isArray(parsedCapabilities) ? parsedCapabilities : isRecord4(parsedCapabilities) && Array.isArray(parsedCapabilities.capabilities) ? parsedCapabilities.capabilities : []
|
|
5854
|
+
);
|
|
5855
|
+
var fail = (code, status) => {
|
|
5856
|
+
throw new BridgeError(code, status);
|
|
5857
|
+
};
|
|
5858
|
+
var requireRecord = (value, code) => {
|
|
5859
|
+
if (!isRecord4(value)) fail(code);
|
|
5860
|
+
return value;
|
|
5861
|
+
};
|
|
5862
|
+
var requireString = (value, code, nonempty = true) => {
|
|
5863
|
+
if (typeof value !== "string" || nonempty && value.length === 0) fail(code);
|
|
5864
|
+
return value;
|
|
5865
|
+
};
|
|
5866
|
+
var normalizeEvmAddress3 = (value, code) => {
|
|
5867
|
+
const address = requireString(value, code);
|
|
5868
|
+
if (!EVM_ADDRESS2.test(address) || /^0x0{40}$/iu.test(address)) fail(code);
|
|
5869
|
+
return address.toLowerCase();
|
|
5870
|
+
};
|
|
5871
|
+
var normalizePositiveUint64 = (value, code) => {
|
|
5872
|
+
const source = requireString(value, code);
|
|
5873
|
+
if (!POSITIVE_UINT64.test(source) || source.length > 20) fail(code);
|
|
5874
|
+
let parsed;
|
|
5875
|
+
try {
|
|
5876
|
+
parsed = BigInt(source);
|
|
5877
|
+
} catch {
|
|
5878
|
+
return fail(code);
|
|
5879
|
+
}
|
|
5880
|
+
const valueParsed = parsed;
|
|
5881
|
+
if (valueParsed <= 0n || valueParsed > UINT64_MAX) fail(code);
|
|
5882
|
+
return { source, value: valueParsed };
|
|
5883
|
+
};
|
|
5884
|
+
var normalizeCanonicalUint64 = (value, code) => {
|
|
5885
|
+
const source = requireString(value, code);
|
|
5886
|
+
if (!CANONICAL_UINT64.test(source) || source.length > 20) fail(code);
|
|
5887
|
+
let parsed;
|
|
5888
|
+
try {
|
|
5889
|
+
parsed = BigInt(source);
|
|
5890
|
+
} catch {
|
|
5891
|
+
return fail(code);
|
|
5892
|
+
}
|
|
5893
|
+
const valueParsed = parsed;
|
|
5894
|
+
if (valueParsed > UINT64_MAX) fail(code);
|
|
5895
|
+
return { source, value: valueParsed };
|
|
5896
|
+
};
|
|
5897
|
+
var normalizeSlippage = (value, code) => {
|
|
5898
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0 || value > 500) {
|
|
5899
|
+
fail(code);
|
|
5900
|
+
}
|
|
5901
|
+
return value;
|
|
5902
|
+
};
|
|
5903
|
+
var providerAddressEquals = (value, expected) => typeof value === "string" && (EVM_ADDRESS2.test(value) ? value.toLowerCase() === expected.toLowerCase() : value === expected);
|
|
5904
|
+
var catalogTokenMatches = (token, expected) => token?.deploymentId === expected.deploymentId && token.chainId === expected.chainId && token.address !== null && (expected.chainId === ETHEREUM_CHAIN_ID ? token.address.toLowerCase() === expected.address.toLowerCase() : token.address === expected.address) && token.standard === expected.standard && token.decimals === expected.decimals && token.status === "active";
|
|
5905
|
+
var directionFacts = (sourceChainId, destinationChainId) => {
|
|
5906
|
+
if (sourceChainId === ETHEREUM_CHAIN_ID && destinationChainId === SOLANA_CHAIN_ID) {
|
|
5907
|
+
return {
|
|
5908
|
+
sourceChainId,
|
|
5909
|
+
destinationChainId,
|
|
5910
|
+
bridgeCapabilityId: "bridge-mayan-swift-v2-eurc-eth-sol",
|
|
5911
|
+
sourceTokenDeploymentId: ETHEREUM_EURC_DEPLOYMENT_ID,
|
|
5912
|
+
destinationTokenDeploymentId: SOLANA_EURC_DEPLOYMENT_ID,
|
|
5913
|
+
sourceTokenAddress: ETHEREUM_EURC_ADDRESS,
|
|
5914
|
+
destinationTokenAddress: SOLANA_EURC_ADDRESS,
|
|
5915
|
+
sourceTokenStandard: "erc20",
|
|
5916
|
+
destinationTokenStandard: "spl-token",
|
|
5917
|
+
sourceProviderChainId: ETHEREUM_PROVIDER_CHAIN_ID,
|
|
5918
|
+
destinationProviderChainId: SOLANA_PROVIDER_CHAIN_ID,
|
|
5919
|
+
sourceWormholeChainId: ETHEREUM_WORMHOLE_CHAIN_ID,
|
|
5920
|
+
destinationWormholeChainId: SOLANA_WORMHOLE_CHAIN_ID,
|
|
5921
|
+
sourceName: ETHEREUM_NAME,
|
|
5922
|
+
destinationName: SOLANA_NAME,
|
|
5923
|
+
sourceEurcMint: "",
|
|
5924
|
+
destinationEurcMint: SOLANA_EURC_ADDRESS,
|
|
5925
|
+
sourceUsdcDeploymentId: ETHEREUM_USDC_DEPLOYMENT_ID,
|
|
5926
|
+
sourceUsdcAddress: ETHEREUM_USDC_ADDRESS,
|
|
5927
|
+
sourceUsdcStandard: "erc20",
|
|
5928
|
+
swiftContract: ETHEREUM_SWIFT_CONTRACT
|
|
5929
|
+
};
|
|
5930
|
+
}
|
|
5931
|
+
if (sourceChainId === SOLANA_CHAIN_ID && destinationChainId === ETHEREUM_CHAIN_ID) {
|
|
5932
|
+
return {
|
|
5933
|
+
sourceChainId,
|
|
5934
|
+
destinationChainId,
|
|
5935
|
+
bridgeCapabilityId: "bridge-mayan-swift-v2-eurc-sol-eth",
|
|
5936
|
+
sourceTokenDeploymentId: SOLANA_EURC_DEPLOYMENT_ID,
|
|
5937
|
+
destinationTokenDeploymentId: ETHEREUM_EURC_DEPLOYMENT_ID,
|
|
5938
|
+
sourceTokenAddress: SOLANA_EURC_ADDRESS,
|
|
5939
|
+
destinationTokenAddress: ETHEREUM_EURC_ADDRESS,
|
|
5940
|
+
sourceTokenStandard: "spl-token",
|
|
5941
|
+
destinationTokenStandard: "erc20",
|
|
5942
|
+
sourceProviderChainId: SOLANA_PROVIDER_CHAIN_ID,
|
|
5943
|
+
destinationProviderChainId: ETHEREUM_PROVIDER_CHAIN_ID,
|
|
5944
|
+
sourceWormholeChainId: SOLANA_WORMHOLE_CHAIN_ID,
|
|
5945
|
+
destinationWormholeChainId: ETHEREUM_WORMHOLE_CHAIN_ID,
|
|
5946
|
+
sourceName: SOLANA_NAME,
|
|
5947
|
+
destinationName: ETHEREUM_NAME,
|
|
5948
|
+
sourceEurcMint: SOLANA_EURC_ADDRESS,
|
|
5949
|
+
destinationEurcMint: "",
|
|
5950
|
+
sourceUsdcDeploymentId: SOLANA_USDC_DEPLOYMENT_ID,
|
|
5951
|
+
sourceUsdcAddress: SOLANA_USDC_ADDRESS,
|
|
5952
|
+
sourceUsdcStandard: "spl-token",
|
|
5953
|
+
swiftContract: SOLANA_SWIFT_PROGRAM
|
|
5954
|
+
};
|
|
5955
|
+
}
|
|
5956
|
+
return fail("BRIDGE_UNSUPPORTED_ROUTE");
|
|
5957
|
+
};
|
|
5958
|
+
var findCapability = (facts) => {
|
|
5959
|
+
const capability = BRIDGE_CAPABILITIES.find(
|
|
5960
|
+
(entry) => entry.bridgeCapabilityId === facts.bridgeCapabilityId && entry.sourceChainId === facts.sourceChainId && entry.destinationChainId === facts.destinationChainId && entry.sourceTokenDeploymentId === facts.sourceTokenDeploymentId && entry.destinationTokenDeploymentId === facts.destinationTokenDeploymentId
|
|
5961
|
+
);
|
|
5962
|
+
if (!capability) fail("BRIDGE_UNSUPPORTED_ROUTE");
|
|
5963
|
+
return capability;
|
|
5964
|
+
};
|
|
5965
|
+
var validateCatalogDirection = (facts) => {
|
|
5966
|
+
const sourceToken = getTokenDeployment(facts.sourceTokenDeploymentId);
|
|
5967
|
+
const destinationToken = getTokenDeployment(facts.destinationTokenDeploymentId);
|
|
5968
|
+
const sourceUsdc = getTokenDeployment(facts.sourceUsdcDeploymentId);
|
|
5969
|
+
if (!catalogTokenMatches(sourceToken, {
|
|
5970
|
+
deploymentId: facts.sourceTokenDeploymentId,
|
|
5971
|
+
chainId: facts.sourceChainId,
|
|
5972
|
+
address: facts.sourceTokenAddress,
|
|
5973
|
+
standard: facts.sourceTokenStandard,
|
|
5974
|
+
decimals: 6
|
|
5975
|
+
}) || !catalogTokenMatches(destinationToken, {
|
|
5976
|
+
deploymentId: facts.destinationTokenDeploymentId,
|
|
5977
|
+
chainId: facts.destinationChainId,
|
|
5978
|
+
address: facts.destinationTokenAddress,
|
|
5979
|
+
standard: facts.destinationTokenStandard,
|
|
5980
|
+
decimals: 6
|
|
5981
|
+
}) || !catalogTokenMatches(sourceUsdc, {
|
|
5982
|
+
deploymentId: facts.sourceUsdcDeploymentId,
|
|
5983
|
+
chainId: facts.sourceChainId,
|
|
5984
|
+
address: facts.sourceUsdcAddress,
|
|
5985
|
+
standard: facts.sourceUsdcStandard,
|
|
5986
|
+
decimals: 6
|
|
5987
|
+
})) {
|
|
5988
|
+
fail("BRIDGE_UNSUPPORTED_ROUTE");
|
|
5989
|
+
}
|
|
5990
|
+
};
|
|
5991
|
+
var validateCapability = (capability, facts) => {
|
|
5992
|
+
const expected = {
|
|
5993
|
+
bridgeCapabilityId: facts.bridgeCapabilityId,
|
|
5994
|
+
providerId: "mayan-swift-v2",
|
|
5995
|
+
capabilityKind: "external-provider-dynamic",
|
|
5996
|
+
sourceChainId: facts.sourceChainId,
|
|
5997
|
+
destinationChainId: facts.destinationChainId,
|
|
5998
|
+
sourceTokenDeploymentId: facts.sourceTokenDeploymentId,
|
|
5999
|
+
destinationTokenDeploymentId: facts.destinationTokenDeploymentId,
|
|
6000
|
+
sourceTokenAddress: facts.sourceTokenAddress,
|
|
6001
|
+
destinationTokenAddress: facts.destinationTokenAddress,
|
|
6002
|
+
sourceTokenStandard: facts.sourceTokenStandard,
|
|
6003
|
+
destinationTokenStandard: facts.destinationTokenStandard,
|
|
6004
|
+
sourceTokenDecimals: 6,
|
|
6005
|
+
destinationTokenDecimals: 6,
|
|
6006
|
+
sourceProviderChainName: facts.sourceName,
|
|
6007
|
+
destinationProviderChainName: facts.destinationName,
|
|
6008
|
+
sourceProviderChainId: facts.sourceProviderChainId,
|
|
6009
|
+
destinationProviderChainId: facts.destinationProviderChainId,
|
|
6010
|
+
sourceWormholeChainId: facts.sourceWormholeChainId,
|
|
6011
|
+
destinationWormholeChainId: facts.destinationWormholeChainId,
|
|
6012
|
+
sourceUsdcDeploymentId: facts.sourceUsdcDeploymentId,
|
|
6013
|
+
sourceUsdcAddress: facts.sourceUsdcAddress,
|
|
6014
|
+
sourceUsdcStandard: facts.sourceUsdcStandard,
|
|
6015
|
+
sourceUsdcDecimals: 6,
|
|
6016
|
+
swiftContract: facts.swiftContract,
|
|
6017
|
+
forwarderAddress: facts.sourceChainId === ETHEREUM_CHAIN_ID ? ETHEREUM_FORWARDER : null,
|
|
6018
|
+
forwarderFunctionSelector: facts.sourceChainId === ETHEREUM_CHAIN_ID ? "0x30dedc57" : null,
|
|
6019
|
+
jupiterProgramAddress: facts.sourceChainId === SOLANA_CHAIN_ID ? SOLANA_JUPITER_V6 : null,
|
|
6020
|
+
builderEndpoint: DEFAULT_BUILDER_ENDPOINT,
|
|
6021
|
+
explorerEndpoint: DEFAULT_EXPLORER_ENDPOINT,
|
|
6022
|
+
dependencies: facts.sourceChainId === SOLANA_CHAIN_ID ? [...DEPENDENCIES, "jupiter-v6-source-swap"] : [...DEPENDENCIES],
|
|
6023
|
+
status: "active"
|
|
6024
|
+
};
|
|
6025
|
+
const actualKeys = Object.keys(capability).sort();
|
|
6026
|
+
const expectedKeys = Object.keys(expected).sort();
|
|
6027
|
+
if (actualKeys.length !== expectedKeys.length || actualKeys.some((key, index) => key !== expectedKeys[index])) {
|
|
6028
|
+
fail("BRIDGE_UNSUPPORTED_ROUTE");
|
|
6029
|
+
}
|
|
6030
|
+
for (const key of expectedKeys) {
|
|
6031
|
+
const actual = capability[key];
|
|
6032
|
+
const wanted = expected[key];
|
|
6033
|
+
if (Array.isArray(actual) || Array.isArray(wanted)) {
|
|
6034
|
+
if (JSON.stringify(actual) !== JSON.stringify(wanted)) fail("BRIDGE_UNSUPPORTED_ROUTE");
|
|
6035
|
+
} else if (actual !== wanted) {
|
|
6036
|
+
fail("BRIDGE_UNSUPPORTED_ROUTE");
|
|
6037
|
+
}
|
|
6038
|
+
}
|
|
6039
|
+
};
|
|
6040
|
+
var validateBridgeRoute = (request) => {
|
|
6041
|
+
const value = requireRecord(request, "BRIDGE_INVALID_ARGUMENT");
|
|
6042
|
+
const keys = /* @__PURE__ */ new Set([
|
|
6043
|
+
"sourceChainId",
|
|
6044
|
+
"destinationChainId",
|
|
6045
|
+
"sourceTokenDeploymentId",
|
|
6046
|
+
"destinationTokenDeploymentId",
|
|
6047
|
+
"amountIn",
|
|
6048
|
+
"slippageBps"
|
|
6049
|
+
]);
|
|
6050
|
+
if (Object.keys(value).some((key) => !keys.has(key))) fail("BRIDGE_INVALID_ARGUMENT");
|
|
6051
|
+
const sourceChainId = requireString(value.sourceChainId, "BRIDGE_INVALID_ARGUMENT");
|
|
6052
|
+
const destinationChainId = requireString(value.destinationChainId, "BRIDGE_INVALID_ARGUMENT");
|
|
6053
|
+
const sourceTokenDeploymentId = requireString(
|
|
6054
|
+
value.sourceTokenDeploymentId,
|
|
6055
|
+
"BRIDGE_INVALID_ARGUMENT"
|
|
6056
|
+
);
|
|
6057
|
+
const destinationTokenDeploymentId = requireString(
|
|
6058
|
+
value.destinationTokenDeploymentId,
|
|
6059
|
+
"BRIDGE_INVALID_ARGUMENT"
|
|
6060
|
+
);
|
|
6061
|
+
const amountIn = normalizePositiveUint64(value.amountIn, "BRIDGE_INVALID_ARGUMENT");
|
|
6062
|
+
const slippageBps = normalizeSlippage(value.slippageBps, "BRIDGE_INVALID_ARGUMENT");
|
|
6063
|
+
const facts = directionFacts(sourceChainId, destinationChainId);
|
|
6064
|
+
if (sourceTokenDeploymentId !== facts.sourceTokenDeploymentId || destinationTokenDeploymentId !== facts.destinationTokenDeploymentId) {
|
|
6065
|
+
fail("BRIDGE_UNSUPPORTED_ROUTE");
|
|
6066
|
+
}
|
|
6067
|
+
validateCatalogDirection(facts);
|
|
6068
|
+
const capability = findCapability(facts);
|
|
6069
|
+
validateCapability(capability, facts);
|
|
6070
|
+
return {
|
|
6071
|
+
request: {
|
|
6072
|
+
sourceChainId,
|
|
6073
|
+
destinationChainId,
|
|
6074
|
+
sourceTokenDeploymentId,
|
|
6075
|
+
destinationTokenDeploymentId,
|
|
6076
|
+
amountIn: amountIn.source,
|
|
6077
|
+
slippageBps
|
|
6078
|
+
},
|
|
6079
|
+
facts,
|
|
6080
|
+
capability
|
|
6081
|
+
};
|
|
6082
|
+
};
|
|
6083
|
+
var isJsonWhitespace = (character) => character === " " || character === "\n" || character === "\r" || character === " ";
|
|
6084
|
+
var StrictJsonParser = class {
|
|
6085
|
+
#source;
|
|
6086
|
+
#index = 0;
|
|
6087
|
+
constructor(source) {
|
|
6088
|
+
this.#source = source;
|
|
6089
|
+
}
|
|
6090
|
+
parse() {
|
|
6091
|
+
this.#skipWhitespace();
|
|
6092
|
+
const value = this.#value(0);
|
|
6093
|
+
this.#skipWhitespace();
|
|
6094
|
+
if (this.#index !== this.#source.length) this.#invalid();
|
|
6095
|
+
return value;
|
|
6096
|
+
}
|
|
6097
|
+
#value(depth) {
|
|
6098
|
+
if (depth > MAX_JSON_DEPTH) this.#invalid();
|
|
6099
|
+
const start = this.#index;
|
|
6100
|
+
const character = this.#source[this.#index];
|
|
6101
|
+
if (character === "{") return this.#object(start, depth);
|
|
6102
|
+
if (character === "[") return this.#array(start, depth);
|
|
6103
|
+
if (character === '"') {
|
|
6104
|
+
const value = this.#string();
|
|
6105
|
+
return { value, start, end: this.#index };
|
|
6106
|
+
}
|
|
6107
|
+
if (character === "t" && this.#source.startsWith("true", this.#index)) {
|
|
6108
|
+
this.#index += 4;
|
|
6109
|
+
return { value: true, start, end: this.#index };
|
|
6110
|
+
}
|
|
6111
|
+
if (character === "f" && this.#source.startsWith("false", this.#index)) {
|
|
6112
|
+
this.#index += 5;
|
|
6113
|
+
return { value: false, start, end: this.#index };
|
|
6114
|
+
}
|
|
6115
|
+
if (character === "n" && this.#source.startsWith("null", this.#index)) {
|
|
6116
|
+
this.#index += 4;
|
|
6117
|
+
return { value: null, start, end: this.#index };
|
|
6118
|
+
}
|
|
6119
|
+
if (character === "-" || character !== void 0 && /[0-9]/u.test(character)) {
|
|
6120
|
+
const rawNumber = this.#number();
|
|
6121
|
+
const value = Number(rawNumber);
|
|
6122
|
+
if (!Number.isFinite(value)) this.#invalid();
|
|
6123
|
+
return { value, start, end: this.#index, rawNumber };
|
|
6124
|
+
}
|
|
6125
|
+
return this.#invalid();
|
|
6126
|
+
}
|
|
6127
|
+
#object(start, depth) {
|
|
6128
|
+
this.#index += 1;
|
|
6129
|
+
this.#skipWhitespace();
|
|
6130
|
+
const object = /* @__PURE__ */ Object.create(null);
|
|
6131
|
+
const entries = /* @__PURE__ */ new Map();
|
|
6132
|
+
if (this.#source[this.#index] === "}") {
|
|
6133
|
+
this.#index += 1;
|
|
6134
|
+
return { value: object, start, end: this.#index, objectEntries: entries };
|
|
6135
|
+
}
|
|
6136
|
+
while (true) {
|
|
6137
|
+
if (this.#source[this.#index] !== '"') this.#invalid();
|
|
6138
|
+
const key = this.#string();
|
|
6139
|
+
if (entries.has(key)) this.#invalid();
|
|
6140
|
+
this.#skipWhitespace();
|
|
6141
|
+
if (this.#source[this.#index] !== ":") this.#invalid();
|
|
6142
|
+
this.#index += 1;
|
|
6143
|
+
this.#skipWhitespace();
|
|
6144
|
+
const child = this.#value(depth + 1);
|
|
6145
|
+
Object.defineProperty(object, key, {
|
|
6146
|
+
configurable: true,
|
|
6147
|
+
enumerable: true,
|
|
6148
|
+
value: child.value,
|
|
6149
|
+
writable: true
|
|
6150
|
+
});
|
|
6151
|
+
entries.set(key, child);
|
|
6152
|
+
this.#skipWhitespace();
|
|
6153
|
+
const delimiter = this.#source[this.#index];
|
|
6154
|
+
if (delimiter === "}") {
|
|
6155
|
+
this.#index += 1;
|
|
6156
|
+
return {
|
|
6157
|
+
value: object,
|
|
6158
|
+
start,
|
|
6159
|
+
end: this.#index,
|
|
6160
|
+
objectEntries: entries
|
|
6161
|
+
};
|
|
6162
|
+
}
|
|
6163
|
+
if (delimiter !== ",") this.#invalid();
|
|
6164
|
+
this.#index += 1;
|
|
6165
|
+
this.#skipWhitespace();
|
|
6166
|
+
}
|
|
6167
|
+
}
|
|
6168
|
+
#array(start, depth) {
|
|
6169
|
+
this.#index += 1;
|
|
6170
|
+
this.#skipWhitespace();
|
|
6171
|
+
const items = [];
|
|
6172
|
+
if (this.#source[this.#index] === "]") {
|
|
6173
|
+
this.#index += 1;
|
|
6174
|
+
return { value: items, start, end: this.#index, arrayItems: items };
|
|
6175
|
+
}
|
|
6176
|
+
while (true) {
|
|
6177
|
+
items.push(this.#value(depth + 1));
|
|
6178
|
+
this.#skipWhitespace();
|
|
6179
|
+
const delimiter = this.#source[this.#index];
|
|
6180
|
+
if (delimiter === "]") {
|
|
6181
|
+
this.#index += 1;
|
|
6182
|
+
return { value: items, start, end: this.#index, arrayItems: items };
|
|
6183
|
+
}
|
|
6184
|
+
if (delimiter !== ",") this.#invalid();
|
|
6185
|
+
this.#index += 1;
|
|
6186
|
+
this.#skipWhitespace();
|
|
6187
|
+
}
|
|
6188
|
+
}
|
|
6189
|
+
#string() {
|
|
6190
|
+
const start = this.#index;
|
|
6191
|
+
this.#index += 1;
|
|
6192
|
+
while (this.#index < this.#source.length) {
|
|
6193
|
+
const character = this.#source[this.#index];
|
|
6194
|
+
if (character === '"') {
|
|
6195
|
+
this.#index += 1;
|
|
6196
|
+
const raw = this.#source.slice(start, this.#index);
|
|
6197
|
+
try {
|
|
6198
|
+
return JSON.parse(raw);
|
|
6199
|
+
} catch {
|
|
6200
|
+
this.#invalid();
|
|
6201
|
+
}
|
|
6202
|
+
}
|
|
6203
|
+
if (character === "\\") {
|
|
6204
|
+
this.#index += 1;
|
|
6205
|
+
const escape = this.#source[this.#index];
|
|
6206
|
+
if (escape === "u") {
|
|
6207
|
+
const digits = this.#source.slice(this.#index + 1, this.#index + 5);
|
|
6208
|
+
if (!/^[0-9a-fA-F]{4}$/u.test(digits)) this.#invalid();
|
|
6209
|
+
this.#index += 5;
|
|
6210
|
+
continue;
|
|
6211
|
+
}
|
|
6212
|
+
if (escape === void 0 || !'"\\/bfnrt'.includes(escape)) this.#invalid();
|
|
6213
|
+
this.#index += 1;
|
|
6214
|
+
continue;
|
|
6215
|
+
}
|
|
6216
|
+
if (character === void 0 || character.charCodeAt(0) < 32) this.#invalid();
|
|
6217
|
+
this.#index += 1;
|
|
6218
|
+
}
|
|
6219
|
+
return this.#invalid();
|
|
6220
|
+
}
|
|
6221
|
+
#number() {
|
|
6222
|
+
const match = /-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?/uy.exec(
|
|
6223
|
+
this.#source.slice(this.#index)
|
|
6224
|
+
);
|
|
6225
|
+
if (!match) this.#invalid();
|
|
6226
|
+
const raw = match?.[0];
|
|
6227
|
+
this.#index += raw.length;
|
|
6228
|
+
return raw;
|
|
6229
|
+
}
|
|
6230
|
+
#skipWhitespace() {
|
|
6231
|
+
while (this.#index < this.#source.length && isJsonWhitespace(this.#source[this.#index] ?? "")) {
|
|
6232
|
+
this.#index += 1;
|
|
6233
|
+
}
|
|
6234
|
+
}
|
|
6235
|
+
#invalid() {
|
|
6236
|
+
return fail("BRIDGE_PROVIDER_INVALID_RESPONSE");
|
|
6237
|
+
}
|
|
6238
|
+
};
|
|
6239
|
+
var parseProviderResponse = (text) => {
|
|
6240
|
+
let root;
|
|
6241
|
+
try {
|
|
6242
|
+
root = new StrictJsonParser(text).parse();
|
|
6243
|
+
} catch (error) {
|
|
6244
|
+
if (error instanceof BridgeError) throw error;
|
|
6245
|
+
return fail("BRIDGE_PROVIDER_INVALID_RESPONSE");
|
|
6246
|
+
}
|
|
6247
|
+
return { text, root };
|
|
6248
|
+
};
|
|
6249
|
+
var objectEntry = (node, key) => node.objectEntries?.get(key);
|
|
6250
|
+
var objectValue5 = (node, key) => objectEntry(node, key)?.value;
|
|
6251
|
+
var byteLength = (value) => new TextEncoder().encode(value).byteLength;
|
|
6252
|
+
var requestAbortError = () => new DOMException("The bridge provider request was aborted", "AbortError");
|
|
6253
|
+
var throwIfRequestAborted = (controlled, external) => {
|
|
6254
|
+
if (controlled.timedOut()) fail("BRIDGE_TIMEOUT");
|
|
6255
|
+
if (external?.aborted) fail("BRIDGE_ABORTED");
|
|
6256
|
+
};
|
|
6257
|
+
var readWithSignal = async (reader, signal) => {
|
|
6258
|
+
if (signal.aborted) throw signal.reason ?? requestAbortError();
|
|
6259
|
+
return new Promise((resolve, reject) => {
|
|
6260
|
+
let settled = false;
|
|
6261
|
+
const cleanup = () => {
|
|
6262
|
+
signal.removeEventListener("abort", onAbort);
|
|
6263
|
+
};
|
|
6264
|
+
const onAbort = () => {
|
|
6265
|
+
if (settled) return;
|
|
6266
|
+
settled = true;
|
|
6267
|
+
cleanup();
|
|
6268
|
+
reject(signal.reason ?? requestAbortError());
|
|
6269
|
+
};
|
|
6270
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
6271
|
+
let read;
|
|
6272
|
+
try {
|
|
6273
|
+
read = reader.read();
|
|
6274
|
+
} catch (error) {
|
|
6275
|
+
if (settled) return;
|
|
6276
|
+
settled = true;
|
|
6277
|
+
cleanup();
|
|
6278
|
+
reject(error);
|
|
6279
|
+
return;
|
|
6280
|
+
}
|
|
6281
|
+
void read.then(
|
|
6282
|
+
(result) => {
|
|
6283
|
+
if (settled) return;
|
|
6284
|
+
settled = true;
|
|
6285
|
+
cleanup();
|
|
6286
|
+
resolve(result);
|
|
6287
|
+
},
|
|
6288
|
+
(error) => {
|
|
6289
|
+
if (settled) return;
|
|
6290
|
+
settled = true;
|
|
6291
|
+
cleanup();
|
|
6292
|
+
reject(error);
|
|
6293
|
+
}
|
|
6294
|
+
);
|
|
6295
|
+
});
|
|
6296
|
+
};
|
|
6297
|
+
var cancelResponseBody = (response) => {
|
|
6298
|
+
const body = response.body;
|
|
6299
|
+
if (body === null || response.bodyUsed) return;
|
|
6300
|
+
try {
|
|
6301
|
+
void body.cancel().catch(() => void 0);
|
|
6302
|
+
} catch {
|
|
6303
|
+
}
|
|
6304
|
+
};
|
|
6305
|
+
var decodeResponseBody = async (response, controlled, external) => {
|
|
6306
|
+
throwIfRequestAborted(controlled, external);
|
|
6307
|
+
const contentLength = response.headers.get("content-length");
|
|
6308
|
+
if (contentLength !== null && /^\d+$/u.test(contentLength)) {
|
|
6309
|
+
try {
|
|
6310
|
+
if (BigInt(contentLength) > BigInt(MAX_RESPONSE_BYTES)) {
|
|
6311
|
+
fail("BRIDGE_PROVIDER_INVALID_RESPONSE");
|
|
6312
|
+
}
|
|
6313
|
+
} catch {
|
|
6314
|
+
}
|
|
6315
|
+
}
|
|
6316
|
+
const body = response.body;
|
|
6317
|
+
if (body === null) return "";
|
|
6318
|
+
let reader;
|
|
6319
|
+
try {
|
|
6320
|
+
reader = body.getReader();
|
|
6321
|
+
} catch {
|
|
6322
|
+
return fail("BRIDGE_PROVIDER_TRANSPORT");
|
|
6323
|
+
}
|
|
6324
|
+
const chunks = [];
|
|
6325
|
+
let totalBytes = 0;
|
|
6326
|
+
let bodyComplete = false;
|
|
6327
|
+
const releaseReader = () => {
|
|
6328
|
+
try {
|
|
6329
|
+
reader.releaseLock();
|
|
6330
|
+
} catch {
|
|
6331
|
+
}
|
|
6332
|
+
};
|
|
6333
|
+
try {
|
|
6334
|
+
while (true) {
|
|
6335
|
+
let result;
|
|
6336
|
+
try {
|
|
6337
|
+
result = await readWithSignal(reader, controlled.signal);
|
|
6338
|
+
} catch (error) {
|
|
6339
|
+
throwIfRequestAborted(controlled, external);
|
|
6340
|
+
if (error instanceof BridgeError) throw error;
|
|
6341
|
+
return fail("BRIDGE_PROVIDER_TRANSPORT");
|
|
6342
|
+
}
|
|
6343
|
+
throwIfRequestAborted(controlled, external);
|
|
6344
|
+
if (result.done) {
|
|
6345
|
+
bodyComplete = true;
|
|
6346
|
+
break;
|
|
6347
|
+
}
|
|
6348
|
+
const chunk = result.value;
|
|
6349
|
+
if (!(chunk instanceof Uint8Array)) return fail("BRIDGE_PROVIDER_TRANSPORT");
|
|
6350
|
+
totalBytes += chunk.byteLength;
|
|
6351
|
+
if (totalBytes > MAX_RESPONSE_BYTES) {
|
|
6352
|
+
fail("BRIDGE_PROVIDER_INVALID_RESPONSE");
|
|
6353
|
+
}
|
|
6354
|
+
chunks.push(chunk);
|
|
6355
|
+
}
|
|
6356
|
+
throwIfRequestAborted(controlled, external);
|
|
6357
|
+
const bytes = new Uint8Array(totalBytes);
|
|
6358
|
+
let offset = 0;
|
|
6359
|
+
for (const chunk of chunks) {
|
|
6360
|
+
bytes.set(chunk, offset);
|
|
6361
|
+
offset += chunk.byteLength;
|
|
6362
|
+
}
|
|
6363
|
+
try {
|
|
6364
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
6365
|
+
} catch {
|
|
6366
|
+
return fail("BRIDGE_PROVIDER_INVALID_RESPONSE");
|
|
6367
|
+
}
|
|
6368
|
+
} finally {
|
|
6369
|
+
if (!bodyComplete) {
|
|
6370
|
+
try {
|
|
6371
|
+
const cancellation = reader.cancel();
|
|
6372
|
+
void cancellation.then(releaseReader, releaseReader);
|
|
6373
|
+
} catch {
|
|
6374
|
+
}
|
|
6375
|
+
}
|
|
6376
|
+
releaseReader();
|
|
6377
|
+
}
|
|
6378
|
+
};
|
|
6379
|
+
var CONFIG_KEYS = /* @__PURE__ */ new Set([
|
|
6380
|
+
"builderEndpoint",
|
|
6381
|
+
"explorerEndpoint",
|
|
6382
|
+
"builderApiKey",
|
|
6383
|
+
"allowUnauthenticatedBuild",
|
|
6384
|
+
"minimumQuoteValiditySeconds",
|
|
6385
|
+
"timeoutMs",
|
|
6386
|
+
"fetch"
|
|
6387
|
+
]);
|
|
6388
|
+
var endpointWithPath2 = (base, path) => new URL(path.replace(/^\/+/, ""), `${base.toString().replace(/\/+$/u, "")}/`);
|
|
6389
|
+
var normalizeProviderEndpoint = (value, fallback, label) => {
|
|
6390
|
+
const sourceValue = value === void 0 ? fallback : value;
|
|
6391
|
+
if (typeof sourceValue !== "string") return fail("BRIDGE_INVALID_ARGUMENT");
|
|
6392
|
+
const source = sourceValue;
|
|
6393
|
+
if (source.length === 0 || source.trim() !== source) {
|
|
6394
|
+
fail("BRIDGE_INVALID_ARGUMENT");
|
|
6395
|
+
}
|
|
6396
|
+
if (!/^[a-z][a-z\d+.-]*:\/\//iu.test(source) || source.includes("#")) {
|
|
6397
|
+
fail("BRIDGE_INVALID_ARGUMENT");
|
|
6398
|
+
}
|
|
6399
|
+
const authorityStart = source.indexOf("://") + 3;
|
|
6400
|
+
const authority = source.slice(authorityStart).split(/[/?#]/u, 1)[0] ?? "";
|
|
6401
|
+
if (authority.length === 0 || authority.includes("@")) fail("BRIDGE_INVALID_ARGUMENT");
|
|
6402
|
+
let endpoint;
|
|
6403
|
+
try {
|
|
6404
|
+
endpoint = new URL(source);
|
|
6405
|
+
} catch {
|
|
6406
|
+
return fail("BRIDGE_INVALID_ARGUMENT");
|
|
6407
|
+
}
|
|
6408
|
+
if (endpoint.protocol !== "https:" && !(endpoint.protocol === "http:" && ["localhost", "127.0.0.1", "[::1]", "::1"].includes(endpoint.hostname))) {
|
|
6409
|
+
fail("BRIDGE_INVALID_ARGUMENT");
|
|
6410
|
+
}
|
|
6411
|
+
if (!endpoint.hostname || endpoint.username || endpoint.password || source.includes("?") || endpoint.search || endpoint.hash) {
|
|
6412
|
+
fail("BRIDGE_INVALID_ARGUMENT");
|
|
6413
|
+
}
|
|
6414
|
+
if (endpoint.port !== "" && (!/^\d+$/u.test(endpoint.port) || Number(endpoint.port) > 65535)) {
|
|
6415
|
+
fail("BRIDGE_INVALID_ARGUMENT");
|
|
6416
|
+
}
|
|
6417
|
+
endpoint.pathname = endpoint.pathname.replace(/\/+$/u, "") || "/";
|
|
6418
|
+
if (!endpoint.pathname.startsWith("/")) fail("BRIDGE_INVALID_ARGUMENT");
|
|
6419
|
+
return endpoint;
|
|
6420
|
+
};
|
|
6421
|
+
var normalizeBridgeConfig = (config) => {
|
|
6422
|
+
const value = config === void 0 ? {} : requireRecord(config, "BRIDGE_INVALID_ARGUMENT");
|
|
6423
|
+
if (Object.keys(value).some((key) => !CONFIG_KEYS.has(key))) {
|
|
6424
|
+
fail("BRIDGE_INVALID_ARGUMENT");
|
|
6425
|
+
}
|
|
6426
|
+
const builderEndpoint = normalizeProviderEndpoint(
|
|
6427
|
+
value.builderEndpoint,
|
|
6428
|
+
DEFAULT_BUILDER_ENDPOINT);
|
|
6429
|
+
const explorerEndpoint = normalizeProviderEndpoint(
|
|
6430
|
+
value.explorerEndpoint,
|
|
6431
|
+
DEFAULT_EXPLORER_ENDPOINT);
|
|
6432
|
+
let builderApiKey;
|
|
6433
|
+
if (value.builderApiKey !== void 0) {
|
|
6434
|
+
const builderApiKeyValue = value.builderApiKey;
|
|
6435
|
+
if (typeof builderApiKeyValue !== "string" || /[\u0000-\u001f\u007f]/u.test(builderApiKeyValue)) {
|
|
6436
|
+
fail("BRIDGE_INVALID_ARGUMENT");
|
|
6437
|
+
}
|
|
6438
|
+
const validatedBuilderApiKey = builderApiKeyValue;
|
|
6439
|
+
builderApiKey = validatedBuilderApiKey.length === 0 ? void 0 : validatedBuilderApiKey;
|
|
6440
|
+
}
|
|
6441
|
+
const allowUnauthenticatedBuildValue = value.allowUnauthenticatedBuild ?? false;
|
|
6442
|
+
if (typeof allowUnauthenticatedBuildValue !== "boolean") {
|
|
6443
|
+
fail("BRIDGE_INVALID_ARGUMENT");
|
|
6444
|
+
}
|
|
6445
|
+
const allowUnauthenticatedBuild = allowUnauthenticatedBuildValue;
|
|
6446
|
+
const minimumQuoteValiditySecondsValue = value.minimumQuoteValiditySeconds ?? 60;
|
|
6447
|
+
if (typeof minimumQuoteValiditySecondsValue !== "number" || !Number.isSafeInteger(minimumQuoteValiditySecondsValue) || minimumQuoteValiditySecondsValue < 0 || minimumQuoteValiditySecondsValue > 300) {
|
|
6448
|
+
fail("BRIDGE_INVALID_ARGUMENT");
|
|
6449
|
+
}
|
|
6450
|
+
const minimumQuoteValiditySeconds = minimumQuoteValiditySecondsValue;
|
|
6451
|
+
const timeoutMsValue = value.timeoutMs ?? DEFAULT_TIMEOUT_MS2;
|
|
6452
|
+
if (typeof timeoutMsValue !== "number" || !Number.isSafeInteger(timeoutMsValue) || timeoutMsValue <= 0) {
|
|
6453
|
+
fail("BRIDGE_INVALID_ARGUMENT");
|
|
6454
|
+
}
|
|
6455
|
+
const timeoutMs = timeoutMsValue;
|
|
6456
|
+
const implementation = value.fetch ?? globalThis.fetch;
|
|
6457
|
+
if (typeof implementation !== "function") fail("BRIDGE_INVALID_ARGUMENT");
|
|
6458
|
+
return Object.freeze({
|
|
6459
|
+
builderEndpoint,
|
|
6460
|
+
explorerEndpoint,
|
|
6461
|
+
...builderApiKey === void 0 ? {} : { builderApiKey },
|
|
6462
|
+
allowUnauthenticatedBuild,
|
|
6463
|
+
minimumQuoteValiditySeconds,
|
|
6464
|
+
timeoutMs,
|
|
6465
|
+
fetch: implementation
|
|
6466
|
+
});
|
|
6467
|
+
};
|
|
6468
|
+
var requestSignal = (timeoutMs, external) => {
|
|
6469
|
+
const controller = new AbortController();
|
|
6470
|
+
let timedOut = false;
|
|
6471
|
+
const timeout = setTimeout(() => {
|
|
6472
|
+
timedOut = true;
|
|
6473
|
+
controller.abort();
|
|
6474
|
+
}, timeoutMs);
|
|
6475
|
+
const onAbort = () => controller.abort();
|
|
6476
|
+
if (external?.aborted) controller.abort();
|
|
6477
|
+
else external?.addEventListener("abort", onAbort, { once: true });
|
|
6478
|
+
return {
|
|
6479
|
+
signal: controller.signal,
|
|
6480
|
+
timedOut: () => timedOut,
|
|
6481
|
+
cleanup: () => {
|
|
6482
|
+
clearTimeout(timeout);
|
|
6483
|
+
external?.removeEventListener("abort", onAbort);
|
|
6484
|
+
}
|
|
6485
|
+
};
|
|
6486
|
+
};
|
|
6487
|
+
var providerRequest = async (config, endpoint, method, body, includeBuilderKey, options) => {
|
|
6488
|
+
if (options?.signal?.aborted) fail("BRIDGE_ABORTED");
|
|
6489
|
+
const controlled = requestSignal(config.timeoutMs, options?.signal);
|
|
6490
|
+
try {
|
|
6491
|
+
const headers = {};
|
|
6492
|
+
headers.accept = "application/json";
|
|
6493
|
+
if (body !== void 0) headers["content-type"] = "application/json";
|
|
6494
|
+
if (includeBuilderKey && config.builderApiKey !== void 0) {
|
|
6495
|
+
headers["x-api-key"] = config.builderApiKey;
|
|
6496
|
+
}
|
|
6497
|
+
let response;
|
|
6498
|
+
try {
|
|
6499
|
+
response = await config.fetch(endpoint, {
|
|
6500
|
+
method,
|
|
6501
|
+
headers,
|
|
6502
|
+
...body === void 0 ? {} : { body },
|
|
6503
|
+
signal: controlled.signal,
|
|
6504
|
+
credentials: "omit",
|
|
6505
|
+
redirect: "manual"
|
|
6506
|
+
});
|
|
6507
|
+
if (controlled.timedOut()) fail("BRIDGE_TIMEOUT");
|
|
6508
|
+
if (options?.signal?.aborted) fail("BRIDGE_ABORTED");
|
|
6509
|
+
} catch {
|
|
6510
|
+
if (controlled.timedOut()) fail("BRIDGE_TIMEOUT");
|
|
6511
|
+
if (options?.signal?.aborted) fail("BRIDGE_ABORTED");
|
|
6512
|
+
return fail("BRIDGE_PROVIDER_TRANSPORT");
|
|
6513
|
+
}
|
|
6514
|
+
return {
|
|
6515
|
+
response,
|
|
6516
|
+
controlled,
|
|
6517
|
+
external: options?.signal
|
|
6518
|
+
};
|
|
6519
|
+
} catch (error) {
|
|
6520
|
+
controlled.cleanup();
|
|
6521
|
+
if (error instanceof BridgeError) throw error;
|
|
6522
|
+
if (controlled.timedOut()) fail("BRIDGE_TIMEOUT");
|
|
6523
|
+
if (options?.signal?.aborted) fail("BRIDGE_ABORTED");
|
|
6524
|
+
return fail("BRIDGE_PROVIDER_TRANSPORT");
|
|
6525
|
+
}
|
|
6526
|
+
};
|
|
6527
|
+
var providerHttpStatus = (response, kind) => {
|
|
6528
|
+
if (kind === "status" && response.status === 404) fail("BRIDGE_STATUS_NOT_FOUND");
|
|
6529
|
+
if (response.status >= 300 && response.status < 400) {
|
|
6530
|
+
fail("BRIDGE_PROVIDER_TRANSPORT");
|
|
6531
|
+
}
|
|
6532
|
+
if (kind === "build" && (response.status === 401 || response.status === 403)) {
|
|
6533
|
+
fail("BRIDGE_PROVIDER_AUTH_REQUIRED");
|
|
6534
|
+
}
|
|
6535
|
+
if (response.status !== 200 && response.status !== 201) {
|
|
6536
|
+
fail("BRIDGE_PROVIDER_HTTP", response.status);
|
|
6537
|
+
}
|
|
6538
|
+
};
|
|
6539
|
+
var providerJson = async (request, kind) => {
|
|
6540
|
+
try {
|
|
6541
|
+
providerHttpStatus(request.response, kind);
|
|
6542
|
+
const text = await decodeResponseBody(
|
|
6543
|
+
request.response,
|
|
6544
|
+
request.controlled,
|
|
6545
|
+
request.external
|
|
6546
|
+
);
|
|
6547
|
+
throwIfRequestAborted(request.controlled, request.external);
|
|
6548
|
+
const parsed = parseProviderResponse(text);
|
|
6549
|
+
throwIfRequestAborted(request.controlled, request.external);
|
|
6550
|
+
return parsed;
|
|
6551
|
+
} finally {
|
|
6552
|
+
cancelResponseBody(request.response);
|
|
6553
|
+
request.controlled.cleanup();
|
|
6554
|
+
}
|
|
6555
|
+
};
|
|
6556
|
+
var encodeBase58 = (bytes) => {
|
|
6557
|
+
let zeroes = 0;
|
|
6558
|
+
while (zeroes < bytes.length && bytes[zeroes] === 0) zeroes += 1;
|
|
6559
|
+
if (zeroes === bytes.length) return "1".repeat(zeroes);
|
|
6560
|
+
const digits = [0];
|
|
6561
|
+
for (const byte of bytes) {
|
|
6562
|
+
let carry = byte;
|
|
6563
|
+
for (let index = 0; index < digits.length; index += 1) {
|
|
6564
|
+
const value = (digits[index] ?? 0) * 256 + carry;
|
|
6565
|
+
digits[index] = value % 58;
|
|
6566
|
+
carry = Math.floor(value / 58);
|
|
6567
|
+
}
|
|
6568
|
+
while (carry > 0) {
|
|
6569
|
+
digits.push(carry % 58);
|
|
6570
|
+
carry = Math.floor(carry / 58);
|
|
6571
|
+
}
|
|
6572
|
+
}
|
|
6573
|
+
let result = "1".repeat(zeroes);
|
|
6574
|
+
for (let index = digits.length - 1; index >= 0; index -= 1) {
|
|
6575
|
+
result += BASE58_ALPHABET[digits[index] ?? 0] ?? "";
|
|
6576
|
+
}
|
|
6577
|
+
return result === "" ? "1" : result;
|
|
6578
|
+
};
|
|
6579
|
+
var decodeBase58 = (value, expectedBytes, code) => {
|
|
6580
|
+
const text = requireString(value, code);
|
|
6581
|
+
if (text.length > expectedBytes * 2 || [...text].some((char) => !BASE58_ALPHABET.includes(char))) {
|
|
6582
|
+
fail(code);
|
|
6583
|
+
}
|
|
6584
|
+
const bytes = [0];
|
|
6585
|
+
for (const char of text) {
|
|
6586
|
+
const digit = BASE58_ALPHABET.indexOf(char);
|
|
6587
|
+
let carry = digit;
|
|
6588
|
+
for (let index = 0; index < bytes.length; index += 1) {
|
|
6589
|
+
const current = (bytes[index] ?? 0) * 58 + carry;
|
|
6590
|
+
bytes[index] = current % 256;
|
|
6591
|
+
carry = Math.floor(current / 256);
|
|
6592
|
+
}
|
|
6593
|
+
while (carry > 0) {
|
|
6594
|
+
bytes.push(carry % 256);
|
|
6595
|
+
carry = Math.floor(carry / 256);
|
|
6596
|
+
}
|
|
6597
|
+
}
|
|
6598
|
+
let zeroes = 0;
|
|
6599
|
+
while (zeroes < text.length && text[zeroes] === "1") zeroes += 1;
|
|
6600
|
+
const result = new Uint8Array(zeroes + (bytes.length === 1 && bytes[0] === 0 ? 0 : bytes.length));
|
|
6601
|
+
for (let index = 0; index < bytes.length && !(bytes.length === 1 && bytes[0] === 0); index += 1) {
|
|
6602
|
+
result[result.length - 1 - index] = bytes[index] ?? 0;
|
|
6603
|
+
}
|
|
6604
|
+
if (result.length !== expectedBytes || encodeBase58(result) !== text) fail(code);
|
|
6605
|
+
return result;
|
|
6606
|
+
};
|
|
6607
|
+
var decodeBase64 = (value, code) => {
|
|
6608
|
+
const text = requireString(value, code);
|
|
6609
|
+
if (!BASE64.test(text)) fail(code);
|
|
6610
|
+
let binary;
|
|
6611
|
+
try {
|
|
6612
|
+
binary = globalThis.atob(text);
|
|
6613
|
+
} catch {
|
|
6614
|
+
return fail(code);
|
|
6615
|
+
}
|
|
6616
|
+
const result = Uint8Array.from(binary, (character) => character.charCodeAt(0));
|
|
6617
|
+
let canonical;
|
|
6618
|
+
try {
|
|
6619
|
+
canonical = globalThis.btoa(binary);
|
|
6620
|
+
} catch {
|
|
6621
|
+
return fail(code);
|
|
6622
|
+
}
|
|
6623
|
+
if (canonical !== text) fail(code);
|
|
6624
|
+
return result;
|
|
6625
|
+
};
|
|
6626
|
+
var readBytes = (cursor, count, code) => {
|
|
6627
|
+
if (!Number.isSafeInteger(count) || count < 0 || cursor.index + count > cursor.bytes.length) fail(code);
|
|
6628
|
+
const value = cursor.bytes.slice(cursor.index, cursor.index + count);
|
|
6629
|
+
cursor.index += count;
|
|
6630
|
+
return value;
|
|
6631
|
+
};
|
|
6632
|
+
var readShortVec = (cursor, maximum, code) => {
|
|
6633
|
+
let value = 0;
|
|
6634
|
+
let shift = 0;
|
|
6635
|
+
for (let count = 0; count < 5; count += 1) {
|
|
6636
|
+
const byte = readBytes(cursor, 1, code)[0] ?? 0;
|
|
6637
|
+
const payload = byte & 127;
|
|
6638
|
+
if (shift >= 28 || payload > Math.floor(Number.MAX_SAFE_INTEGER / 2 ** shift)) fail(code);
|
|
6639
|
+
value += payload * 2 ** shift;
|
|
6640
|
+
if ((byte & 128) === 0) {
|
|
6641
|
+
if (count > 0 && payload === 0) fail(code);
|
|
6642
|
+
if (!Number.isSafeInteger(value) || value > maximum) fail(code);
|
|
6643
|
+
return value;
|
|
6644
|
+
}
|
|
6645
|
+
shift += 7;
|
|
6646
|
+
}
|
|
6647
|
+
return fail(code);
|
|
6648
|
+
};
|
|
6649
|
+
var allZero = (bytes) => bytes.every((byte) => byte === 0);
|
|
6650
|
+
var validateSolanaTransaction = (encoded, feePayer) => {
|
|
6651
|
+
const bytes = decodeBase64(encoded, "BRIDGE_BUILD_INVALID");
|
|
6652
|
+
if (bytes.byteLength === 0 || bytes.byteLength > 1232) fail("BRIDGE_BUILD_INVALID");
|
|
6653
|
+
const cursor = { bytes, index: 0 };
|
|
6654
|
+
const signatureCount = readShortVec(cursor, 1, "BRIDGE_BUILD_INVALID");
|
|
6655
|
+
if (signatureCount !== 1 || !allZero(readBytes(cursor, 64, "BRIDGE_BUILD_INVALID"))) {
|
|
6656
|
+
fail("BRIDGE_BUILD_INVALID");
|
|
6657
|
+
}
|
|
6658
|
+
const version = readBytes(cursor, 1, "BRIDGE_BUILD_INVALID")[0];
|
|
6659
|
+
if (version !== 128) fail("BRIDGE_BUILD_INVALID");
|
|
6660
|
+
const requiredSignatures = readBytes(cursor, 1, "BRIDGE_BUILD_INVALID")[0] ?? 0;
|
|
6661
|
+
const readonlySigned = readBytes(cursor, 1, "BRIDGE_BUILD_INVALID")[0] ?? 0;
|
|
6662
|
+
const readonlyUnsigned = readBytes(cursor, 1, "BRIDGE_BUILD_INVALID")[0] ?? 0;
|
|
6663
|
+
if (requiredSignatures !== 1 || readonlySigned !== 0) fail("BRIDGE_BUILD_INVALID");
|
|
6664
|
+
const staticKeyCount = readShortVec(cursor, 64, "BRIDGE_BUILD_INVALID");
|
|
6665
|
+
if (staticKeyCount === 0 || readonlyUnsigned >= staticKeyCount) fail("BRIDGE_BUILD_INVALID");
|
|
6666
|
+
const staticKeys = readBytes(cursor, staticKeyCount * 32, "BRIDGE_BUILD_INVALID");
|
|
6667
|
+
const payerBytes = decodeBase58(feePayer, 32, "BRIDGE_BUILD_INVALID");
|
|
6668
|
+
if (!staticKeys.slice(0, 32).every((byte, index) => byte === payerBytes[index])) fail("BRIDGE_BUILD_INVALID");
|
|
6669
|
+
readBytes(cursor, 32, "BRIDGE_BUILD_INVALID");
|
|
6670
|
+
const instructionCount = readShortVec(cursor, 64, "BRIDGE_BUILD_INVALID");
|
|
6671
|
+
let largestAccountIndex = -1;
|
|
6672
|
+
for (let instruction = 0; instruction < instructionCount; instruction += 1) {
|
|
6673
|
+
largestAccountIndex = Math.max(
|
|
6674
|
+
largestAccountIndex,
|
|
6675
|
+
readBytes(cursor, 1, "BRIDGE_BUILD_INVALID")[0] ?? 0
|
|
6676
|
+
);
|
|
6677
|
+
const accountCount = readShortVec(cursor, 64, "BRIDGE_BUILD_INVALID");
|
|
6678
|
+
const accountIndices = readBytes(cursor, accountCount, "BRIDGE_BUILD_INVALID");
|
|
6679
|
+
for (const index of accountIndices) largestAccountIndex = Math.max(largestAccountIndex, index);
|
|
6680
|
+
const dataLength = readShortVec(cursor, 1024, "BRIDGE_BUILD_INVALID");
|
|
6681
|
+
readBytes(cursor, dataLength, "BRIDGE_BUILD_INVALID");
|
|
6682
|
+
}
|
|
6683
|
+
const lookupCount = readShortVec(cursor, 32, "BRIDGE_BUILD_INVALID");
|
|
6684
|
+
let loadedAddressCount = 0;
|
|
6685
|
+
for (let lookup = 0; lookup < lookupCount; lookup += 1) {
|
|
6686
|
+
readBytes(cursor, 32, "BRIDGE_BUILD_INVALID");
|
|
6687
|
+
const writableCount = readShortVec(cursor, 64, "BRIDGE_BUILD_INVALID");
|
|
6688
|
+
readBytes(cursor, writableCount, "BRIDGE_BUILD_INVALID");
|
|
6689
|
+
const readonlyCount = readShortVec(cursor, 64, "BRIDGE_BUILD_INVALID");
|
|
6690
|
+
readBytes(cursor, readonlyCount, "BRIDGE_BUILD_INVALID");
|
|
6691
|
+
loadedAddressCount += writableCount + readonlyCount;
|
|
6692
|
+
if (loadedAddressCount > 256) fail("BRIDGE_BUILD_INVALID");
|
|
6693
|
+
}
|
|
6694
|
+
if (largestAccountIndex >= staticKeyCount + loadedAddressCount || cursor.index !== bytes.length) {
|
|
6695
|
+
fail("BRIDGE_BUILD_INVALID");
|
|
6696
|
+
}
|
|
6697
|
+
return requireString(encoded, "BRIDGE_BUILD_INVALID");
|
|
6698
|
+
};
|
|
6699
|
+
var providerInvalid = () => fail("BRIDGE_PROVIDER_INVALID_RESPONSE");
|
|
6700
|
+
var requiredNode = (node, key) => {
|
|
6701
|
+
const child = objectEntry(node, key);
|
|
6702
|
+
if (!child) providerInvalid();
|
|
6703
|
+
return child;
|
|
6704
|
+
};
|
|
6705
|
+
var providerString = (node, key, allowEmpty = false) => {
|
|
6706
|
+
const value = requiredNode(node, key).value;
|
|
6707
|
+
if (typeof value !== "string" || !allowEmpty && value.length === 0) {
|
|
6708
|
+
providerInvalid();
|
|
6709
|
+
}
|
|
6710
|
+
return value;
|
|
6711
|
+
};
|
|
6712
|
+
var providerBoolean = (node, key) => {
|
|
6713
|
+
const value = requiredNode(node, key).value;
|
|
6714
|
+
if (typeof value !== "boolean") providerInvalid();
|
|
6715
|
+
return value;
|
|
6716
|
+
};
|
|
6717
|
+
var providerNumber = (node, key) => {
|
|
6718
|
+
const value = requiredNode(node, key).value;
|
|
6719
|
+
if (typeof value !== "number" || !Number.isFinite(value)) providerInvalid();
|
|
6720
|
+
return value;
|
|
6721
|
+
};
|
|
6722
|
+
var providerInteger = (node, key) => {
|
|
6723
|
+
const value = providerNumber(node, key);
|
|
6724
|
+
if (!Number.isSafeInteger(value)) providerInvalid();
|
|
6725
|
+
return value;
|
|
6726
|
+
};
|
|
6727
|
+
var providerAddress = (node, key, expected) => {
|
|
6728
|
+
const value = providerString(node, key);
|
|
6729
|
+
if (!providerAddressEquals(value, expected)) providerInvalid();
|
|
6730
|
+
return EVM_ADDRESS2.test(value) ? value.toLowerCase() : value;
|
|
6731
|
+
};
|
|
6732
|
+
var providerUint64 = (node, key, positive) => {
|
|
6733
|
+
const value = providerString(node, key);
|
|
6734
|
+
try {
|
|
6735
|
+
return positive ? normalizePositiveUint64(value, "BRIDGE_PROVIDER_INVALID_RESPONSE") : normalizeCanonicalUint64(value, "BRIDGE_PROVIDER_INVALID_RESPONSE");
|
|
6736
|
+
} catch (error) {
|
|
6737
|
+
if (error instanceof BridgeError) throw error;
|
|
6738
|
+
return providerInvalid();
|
|
6739
|
+
}
|
|
6740
|
+
};
|
|
6741
|
+
var providerToken = (node, expected) => {
|
|
6742
|
+
if (!isRecord4(node.value)) providerInvalid();
|
|
6743
|
+
const contract = providerString(node, "contract");
|
|
6744
|
+
if (!providerAddressEquals(contract, expected.address)) providerInvalid();
|
|
6745
|
+
const mint = providerString(node, "mint", true);
|
|
6746
|
+
if (mint !== expected.mint) providerInvalid();
|
|
6747
|
+
const realOrigin = providerString(node, "realOriginContractAddress");
|
|
6748
|
+
if (!providerAddressEquals(realOrigin, expected.address)) providerInvalid();
|
|
6749
|
+
if (providerString(node, "name") !== "EuroC") providerInvalid();
|
|
6750
|
+
if (providerString(node, "standard") !== expected.standard) providerInvalid();
|
|
6751
|
+
if (providerInteger(node, "chainId") !== expected.chainId) providerInvalid();
|
|
6752
|
+
if (providerInteger(node, "wChainId") !== expected.wormholeChainId) providerInvalid();
|
|
6753
|
+
if (providerInteger(node, "realOriginChainId") !== expected.wormholeChainId) providerInvalid();
|
|
6754
|
+
if (providerInteger(node, "decimals") !== 6) providerInvalid();
|
|
6755
|
+
};
|
|
6756
|
+
var quoteProviderStandard = (facts) => facts.sourceChainId === ETHEREUM_CHAIN_ID ? "erc20" : "spl";
|
|
6757
|
+
var ensureQuoteDeadline = (deadlineValue, margin) => {
|
|
6758
|
+
const now = BigInt(Math.floor(Date.now() / 1e3));
|
|
6759
|
+
if (!Number.isSafeInteger(Number(now)) || deadlineValue < now + BigInt(margin)) {
|
|
6760
|
+
fail("BRIDGE_QUOTE_EXPIRED");
|
|
6761
|
+
}
|
|
6762
|
+
};
|
|
6763
|
+
var validateProviderQuote = (node, text, request, facts, config) => {
|
|
6764
|
+
if (!isRecord4(node.value)) providerInvalid();
|
|
6765
|
+
const raw = text.slice(node.start, node.end);
|
|
6766
|
+
if (byteLength(raw) > MAX_RAW_QUOTE_BYTES) providerInvalid();
|
|
6767
|
+
if (providerString(node, "type") !== "SWIFT") providerInvalid();
|
|
6768
|
+
if (providerString(node, "swiftVersion") !== "V2") providerInvalid();
|
|
6769
|
+
if (providerBoolean(node, "gasless")) providerInvalid();
|
|
6770
|
+
if (providerString(node, "fromChain") !== facts.sourceName) providerInvalid();
|
|
6771
|
+
if (providerString(node, "toChain") !== facts.destinationName) providerInvalid();
|
|
6772
|
+
if (providerInteger(node, "slippageBps") !== request.slippageBps) providerInvalid();
|
|
6773
|
+
if (providerBoolean(node, "onlyBridging")) providerInvalid();
|
|
6774
|
+
const effectiveAmount = providerUint64(node, "effectiveAmountIn64", true);
|
|
6775
|
+
if (effectiveAmount.source !== request.amountIn) providerInvalid();
|
|
6776
|
+
const expectedAmountOut = providerUint64(node, "expectedAmountOutBaseUnits", true);
|
|
6777
|
+
const minimumAmountOut = providerUint64(node, "minAmountOutBaseUnits", true);
|
|
6778
|
+
const minimumReceived = providerUint64(node, "minReceivedBaseUnits", true);
|
|
6779
|
+
if (minimumAmountOut.value > expectedAmountOut.value || minimumReceived.value > minimumAmountOut.value) {
|
|
6780
|
+
providerInvalid();
|
|
6781
|
+
}
|
|
6782
|
+
const deadline = providerUint64(node, "deadline64", true);
|
|
6783
|
+
ensureQuoteDeadline(deadline.value, config.minimumQuoteValiditySeconds);
|
|
6784
|
+
const sourceToken = requiredNode(node, "fromToken");
|
|
6785
|
+
const destinationToken = requiredNode(node, "toToken");
|
|
6786
|
+
providerToken(sourceToken, {
|
|
6787
|
+
address: facts.sourceTokenAddress,
|
|
6788
|
+
standard: facts.sourceChainId === ETHEREUM_CHAIN_ID ? "erc20" : "spl",
|
|
6789
|
+
chainId: facts.sourceProviderChainId,
|
|
6790
|
+
wormholeChainId: facts.sourceWormholeChainId,
|
|
6791
|
+
mint: facts.sourceEurcMint
|
|
6792
|
+
});
|
|
6793
|
+
providerToken(destinationToken, {
|
|
6794
|
+
address: facts.destinationTokenAddress,
|
|
6795
|
+
standard: facts.destinationChainId === ETHEREUM_CHAIN_ID ? "erc20" : "spl",
|
|
6796
|
+
chainId: facts.destinationProviderChainId,
|
|
6797
|
+
wormholeChainId: facts.destinationWormholeChainId,
|
|
6798
|
+
mint: facts.destinationEurcMint
|
|
6799
|
+
});
|
|
6800
|
+
const sourceUsdcStandard = quoteProviderStandard(facts);
|
|
6801
|
+
if (providerAddress(node, "swiftInputContract", facts.sourceUsdcAddress) !== facts.sourceUsdcAddress) {
|
|
6802
|
+
providerInvalid();
|
|
6803
|
+
}
|
|
6804
|
+
if (providerString(node, "swiftInputContractStandard") !== sourceUsdcStandard) providerInvalid();
|
|
6805
|
+
if (providerInteger(node, "swiftInputDecimals") !== 6) providerInvalid();
|
|
6806
|
+
if (providerAddress(node, "swiftMayanContract", facts.swiftContract) !== facts.swiftContract) {
|
|
6807
|
+
providerInvalid();
|
|
6808
|
+
}
|
|
6809
|
+
const middleNode = requiredNode(node, "minMiddleAmount");
|
|
6810
|
+
if (middleNode.rawNumber === void 0 || typeof middleNode.value !== "number" || !Number.isFinite(middleNode.value) || middleNode.value <= 0) {
|
|
6811
|
+
providerInvalid();
|
|
6812
|
+
}
|
|
6813
|
+
let routerAddress;
|
|
6814
|
+
let routerKind;
|
|
6815
|
+
if (facts.sourceChainId === ETHEREUM_CHAIN_ID) {
|
|
6816
|
+
routerAddress = normalizeEvmAddress3(
|
|
6817
|
+
providerString(node, "evmSwapRouterAddress"),
|
|
6818
|
+
"BRIDGE_PROVIDER_INVALID_RESPONSE"
|
|
6819
|
+
);
|
|
6820
|
+
routerKind = "provider-selected-evm";
|
|
6821
|
+
} else {
|
|
6822
|
+
routerAddress = SOLANA_JUPITER_V6;
|
|
6823
|
+
routerKind = "jupiter-v6";
|
|
6824
|
+
const evmRouter = objectValue5(node, "evmSwapRouterAddress");
|
|
6825
|
+
if (evmRouter !== void 0 && evmRouter !== null) providerInvalid();
|
|
6826
|
+
}
|
|
6827
|
+
const quoteId = providerString(node, "quoteId");
|
|
6828
|
+
if (!/^0x[0-9a-fA-F]{32}$/u.test(quoteId)) providerInvalid();
|
|
6829
|
+
const signature = providerString(node, "signature");
|
|
6830
|
+
if (!EVM_SIGNATURE.test(signature)) providerInvalid();
|
|
6831
|
+
const dependencies = facts.sourceChainId === SOLANA_CHAIN_ID ? [...DEPENDENCIES, "jupiter-v6-source-swap"] : [...DEPENDENCIES];
|
|
6832
|
+
const quote = {
|
|
6833
|
+
quoteKind: "mayan-swift-v2",
|
|
6834
|
+
providerId: "mayan-swift-v2",
|
|
6835
|
+
sourceChainId: request.sourceChainId,
|
|
6836
|
+
destinationChainId: request.destinationChainId,
|
|
6837
|
+
sourceTokenDeploymentId: request.sourceTokenDeploymentId,
|
|
6838
|
+
destinationTokenDeploymentId: request.destinationTokenDeploymentId,
|
|
6839
|
+
amountIn: effectiveAmount.source,
|
|
6840
|
+
expectedAmountOut: expectedAmountOut.source,
|
|
6841
|
+
minimumAmountOut: minimumAmountOut.source,
|
|
6842
|
+
minimumReceived: minimumReceived.source,
|
|
6843
|
+
deadline: deadline.source,
|
|
6844
|
+
slippageBps: request.slippageBps,
|
|
6845
|
+
quoteId: quoteId.toLowerCase(),
|
|
6846
|
+
providerSignature: signature.toLowerCase(),
|
|
6847
|
+
sourceSwap: {
|
|
6848
|
+
required: true,
|
|
6849
|
+
inputTokenDeploymentId: facts.sourceTokenDeploymentId,
|
|
6850
|
+
intermediateTokenDeploymentId: facts.sourceUsdcDeploymentId,
|
|
6851
|
+
intermediateTokenAddress: facts.sourceUsdcAddress,
|
|
6852
|
+
intermediateTokenStandard: sourceUsdcStandard,
|
|
6853
|
+
intermediateTokenDecimals: 6,
|
|
6854
|
+
providerMinimumAmount: middleNode.rawNumber,
|
|
6855
|
+
routerKind,
|
|
6856
|
+
routerAddress
|
|
6857
|
+
},
|
|
6858
|
+
dependencies,
|
|
6859
|
+
quoteVerification: "provider-signed-not-locally-verified",
|
|
6860
|
+
rawSignedQuoteJson: raw
|
|
6861
|
+
};
|
|
6862
|
+
return {
|
|
6863
|
+
quote: deepFreeze6(quote),
|
|
6864
|
+
rawNode: node,
|
|
6865
|
+
deadlineValue: deadline.value
|
|
6866
|
+
};
|
|
6867
|
+
};
|
|
6868
|
+
var QUOTE_KEYS = [
|
|
6869
|
+
"quoteKind",
|
|
6870
|
+
"providerId",
|
|
6871
|
+
"sourceChainId",
|
|
6872
|
+
"destinationChainId",
|
|
6873
|
+
"sourceTokenDeploymentId",
|
|
6874
|
+
"destinationTokenDeploymentId",
|
|
6875
|
+
"amountIn",
|
|
6876
|
+
"expectedAmountOut",
|
|
6877
|
+
"minimumAmountOut",
|
|
6878
|
+
"minimumReceived",
|
|
6879
|
+
"deadline",
|
|
6880
|
+
"slippageBps",
|
|
6881
|
+
"quoteId",
|
|
6882
|
+
"providerSignature",
|
|
6883
|
+
"sourceSwap",
|
|
6884
|
+
"dependencies",
|
|
6885
|
+
"quoteVerification",
|
|
6886
|
+
"rawSignedQuoteJson"
|
|
6887
|
+
];
|
|
6888
|
+
var SOURCE_SWAP_KEYS = [
|
|
6889
|
+
"required",
|
|
6890
|
+
"inputTokenDeploymentId",
|
|
6891
|
+
"intermediateTokenDeploymentId",
|
|
6892
|
+
"intermediateTokenAddress",
|
|
6893
|
+
"intermediateTokenStandard",
|
|
6894
|
+
"intermediateTokenDecimals",
|
|
6895
|
+
"providerMinimumAmount",
|
|
6896
|
+
"routerKind",
|
|
6897
|
+
"routerAddress"
|
|
6898
|
+
];
|
|
6899
|
+
var exactObjectKeys = (value, expected, code) => {
|
|
6900
|
+
const actual = Object.keys(value).sort();
|
|
6901
|
+
const wanted = [...expected].sort();
|
|
6902
|
+
if (actual.length !== wanted.length || actual.some((key, index) => key !== wanted[index])) {
|
|
6903
|
+
fail(code);
|
|
6904
|
+
}
|
|
6905
|
+
};
|
|
6906
|
+
var stableJson = (value) => {
|
|
6907
|
+
if (Array.isArray(value)) return `[${value.map(stableJson).join(",")}]`;
|
|
6908
|
+
if (isRecord4(value)) {
|
|
6909
|
+
return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${stableJson(value[key])}`).join(",")}}`;
|
|
6910
|
+
}
|
|
6911
|
+
return JSON.stringify(value);
|
|
6912
|
+
};
|
|
6913
|
+
var cloneQuote = (quote) => {
|
|
6914
|
+
try {
|
|
6915
|
+
return JSON.parse(JSON.stringify(quote));
|
|
6916
|
+
} catch {
|
|
6917
|
+
return fail("BRIDGE_INVALID_ARGUMENT");
|
|
6918
|
+
}
|
|
6919
|
+
};
|
|
6920
|
+
var validateNormalizedQuoteShape = (quote, code) => {
|
|
6921
|
+
const value = requireRecord(quote, code);
|
|
6922
|
+
exactObjectKeys(value, QUOTE_KEYS, code);
|
|
6923
|
+
if (value.quoteKind !== "mayan-swift-v2" || value.providerId !== "mayan-swift-v2") fail(code);
|
|
6924
|
+
const sourceChainId = requireString(value.sourceChainId, code);
|
|
6925
|
+
const destinationChainId = requireString(value.destinationChainId, code);
|
|
6926
|
+
const facts = directionFacts(sourceChainId, destinationChainId);
|
|
6927
|
+
if (value.sourceTokenDeploymentId !== facts.sourceTokenDeploymentId || value.destinationTokenDeploymentId !== facts.destinationTokenDeploymentId) fail(code);
|
|
6928
|
+
const amountIn = normalizePositiveUint64(value.amountIn, code);
|
|
6929
|
+
const expectedAmountOut = normalizePositiveUint64(value.expectedAmountOut, code);
|
|
6930
|
+
const minimumAmountOut = normalizePositiveUint64(value.minimumAmountOut, code);
|
|
6931
|
+
const minimumReceived = normalizePositiveUint64(value.minimumReceived, code);
|
|
6932
|
+
if (minimumAmountOut.value > expectedAmountOut.value || minimumReceived.value > minimumAmountOut.value) fail(code);
|
|
6933
|
+
const deadline = normalizePositiveUint64(value.deadline, code);
|
|
6934
|
+
const slippageBps = normalizeSlippage(value.slippageBps, code);
|
|
6935
|
+
if (value.quoteVerification !== "provider-signed-not-locally-verified") fail(code);
|
|
6936
|
+
const quoteId = requireString(value.quoteId, code);
|
|
6937
|
+
if (!/^0x[0-9a-fA-F]{32}$/u.test(quoteId)) fail(code);
|
|
6938
|
+
const signature = requireString(value.providerSignature, code);
|
|
6939
|
+
if (!EVM_SIGNATURE.test(signature)) fail(code);
|
|
6940
|
+
const sourceSwap = requireRecord(value.sourceSwap, code);
|
|
6941
|
+
exactObjectKeys(sourceSwap, SOURCE_SWAP_KEYS, code);
|
|
6942
|
+
if (sourceSwap.required !== true) fail(code);
|
|
6943
|
+
if (sourceSwap.inputTokenDeploymentId !== facts.sourceTokenDeploymentId || sourceSwap.intermediateTokenDeploymentId !== facts.sourceUsdcDeploymentId) fail(code);
|
|
6944
|
+
if (sourceSwap.intermediateTokenAddress !== facts.sourceUsdcAddress) fail(code);
|
|
6945
|
+
const providerStandard = quoteProviderStandard(facts);
|
|
6946
|
+
if (sourceSwap.intermediateTokenStandard !== providerStandard || sourceSwap.intermediateTokenDecimals !== 6) fail(code);
|
|
6947
|
+
const providerMinimumAmount = sourceSwap.providerMinimumAmount;
|
|
6948
|
+
if (typeof providerMinimumAmount !== "string" || providerMinimumAmount.length === 0) fail(code);
|
|
6949
|
+
let normalizedRouterAddress;
|
|
6950
|
+
let normalizedRouterKind;
|
|
6951
|
+
if (facts.sourceChainId === ETHEREUM_CHAIN_ID) {
|
|
6952
|
+
const routerAddress = sourceSwap.routerAddress;
|
|
6953
|
+
if (sourceSwap.routerKind !== "provider-selected-evm" || typeof routerAddress !== "string" || !EVM_ADDRESS2.test(routerAddress) || /^0x0{40}$/iu.test(routerAddress)) return fail(code);
|
|
6954
|
+
normalizedRouterAddress = routerAddress.toLowerCase();
|
|
6955
|
+
normalizedRouterKind = "provider-selected-evm";
|
|
6956
|
+
} else if (sourceSwap.routerKind !== "jupiter-v6" || sourceSwap.routerAddress !== SOLANA_JUPITER_V6) {
|
|
6957
|
+
return fail(code);
|
|
6958
|
+
} else {
|
|
6959
|
+
normalizedRouterAddress = SOLANA_JUPITER_V6;
|
|
6960
|
+
normalizedRouterKind = "jupiter-v6";
|
|
6961
|
+
}
|
|
6962
|
+
if (!Array.isArray(value.dependencies)) fail(code);
|
|
6963
|
+
const wantedDependencies = facts.sourceChainId === SOLANA_CHAIN_ID ? [...DEPENDENCIES, "jupiter-v6-source-swap"] : [...DEPENDENCIES];
|
|
6964
|
+
if (stableJson(value.dependencies) !== stableJson(wantedDependencies)) fail(code);
|
|
6965
|
+
const rawSignedQuoteJson = requireString(value.rawSignedQuoteJson, code);
|
|
6966
|
+
if (byteLength(rawSignedQuoteJson) > MAX_RAW_QUOTE_BYTES) fail(code);
|
|
6967
|
+
return {
|
|
6968
|
+
quoteKind: "mayan-swift-v2",
|
|
6969
|
+
providerId: "mayan-swift-v2",
|
|
6970
|
+
sourceChainId,
|
|
6971
|
+
destinationChainId,
|
|
6972
|
+
sourceTokenDeploymentId: facts.sourceTokenDeploymentId,
|
|
6973
|
+
destinationTokenDeploymentId: facts.destinationTokenDeploymentId,
|
|
6974
|
+
amountIn: amountIn.source,
|
|
6975
|
+
expectedAmountOut: expectedAmountOut.source,
|
|
6976
|
+
minimumAmountOut: minimumAmountOut.source,
|
|
6977
|
+
minimumReceived: minimumReceived.source,
|
|
6978
|
+
deadline: deadline.source,
|
|
6979
|
+
slippageBps,
|
|
6980
|
+
quoteId: quoteId.toLowerCase(),
|
|
6981
|
+
providerSignature: signature.toLowerCase(),
|
|
6982
|
+
sourceSwap: {
|
|
6983
|
+
required: true,
|
|
6984
|
+
inputTokenDeploymentId: facts.sourceTokenDeploymentId,
|
|
6985
|
+
intermediateTokenDeploymentId: facts.sourceUsdcDeploymentId,
|
|
6986
|
+
intermediateTokenAddress: facts.sourceUsdcAddress,
|
|
6987
|
+
intermediateTokenStandard: providerStandard,
|
|
6988
|
+
intermediateTokenDecimals: 6,
|
|
6989
|
+
providerMinimumAmount,
|
|
6990
|
+
routerKind: normalizedRouterKind,
|
|
6991
|
+
routerAddress: normalizedRouterAddress
|
|
6992
|
+
},
|
|
6993
|
+
dependencies: wantedDependencies,
|
|
6994
|
+
quoteVerification: "provider-signed-not-locally-verified",
|
|
6995
|
+
rawSignedQuoteJson
|
|
6996
|
+
};
|
|
6997
|
+
};
|
|
6998
|
+
var quoteRequestBody = (route) => JSON.stringify({
|
|
6999
|
+
fromToken: route.facts.sourceTokenAddress,
|
|
7000
|
+
fromChain: route.facts.sourceName,
|
|
7001
|
+
toToken: route.facts.destinationTokenAddress,
|
|
7002
|
+
toChain: route.facts.destinationName,
|
|
7003
|
+
amountIn64: route.request.amountIn,
|
|
7004
|
+
slippageBps: route.request.slippageBps,
|
|
7005
|
+
swift: true,
|
|
7006
|
+
mctp: false,
|
|
7007
|
+
fastMctp: false,
|
|
7008
|
+
wormhole: false,
|
|
7009
|
+
monoChain: false,
|
|
7010
|
+
gasless: false,
|
|
7011
|
+
fullList: true,
|
|
7012
|
+
guaranteedOutput: true,
|
|
7013
|
+
gasDrop: 0
|
|
7014
|
+
});
|
|
7015
|
+
var quoteFromResponse = (response, request, facts, config) => {
|
|
7016
|
+
if (!isRecord4(response.root.value) || response.root.objectEntries === void 0) {
|
|
7017
|
+
providerInvalid();
|
|
7018
|
+
}
|
|
7019
|
+
if (objectValue5(response.root, "success") !== true) providerInvalid();
|
|
7020
|
+
const quotesNode = requiredNode(response.root, "quotes");
|
|
7021
|
+
const quoteNodes = quotesNode.arrayItems;
|
|
7022
|
+
if (!quoteNodes || quoteNodes.length > MAX_QUOTES) return providerInvalid();
|
|
7023
|
+
const selected = [];
|
|
7024
|
+
for (const quoteNode of quoteNodes) {
|
|
7025
|
+
if (!isRecord4(quoteNode.value)) continue;
|
|
7026
|
+
if (quoteNode.value.type !== "SWIFT" || quoteNode.value.swiftVersion !== "V2" || quoteNode.value.gasless !== false) continue;
|
|
7027
|
+
const validated = validateProviderQuote(
|
|
7028
|
+
quoteNode,
|
|
7029
|
+
response.text,
|
|
7030
|
+
request,
|
|
7031
|
+
facts,
|
|
7032
|
+
config
|
|
7033
|
+
);
|
|
7034
|
+
selected.push(validated.quote);
|
|
7035
|
+
}
|
|
7036
|
+
if (selected.length === 0) fail("BRIDGE_QUOTE_UNAVAILABLE");
|
|
7037
|
+
for (const quote of selected) {
|
|
7038
|
+
ensureQuoteDeadline(
|
|
7039
|
+
normalizePositiveUint64(quote.deadline, "BRIDGE_PROVIDER_INVALID_RESPONSE").value,
|
|
7040
|
+
config.minimumQuoteValiditySeconds
|
|
7041
|
+
);
|
|
7042
|
+
}
|
|
7043
|
+
return deepFreeze6(selected);
|
|
7044
|
+
};
|
|
7045
|
+
var buildRequestSnapshot = (value) => {
|
|
7046
|
+
const request = requireRecord(value, "BRIDGE_INVALID_ARGUMENT");
|
|
7047
|
+
const allowed = /* @__PURE__ */ new Set(["quote", "swapperAddress", "destinationAddress", "refundAddress"]);
|
|
7048
|
+
if (Object.keys(request).some((key) => !allowed.has(key))) fail("BRIDGE_INVALID_ARGUMENT");
|
|
7049
|
+
if (!Object.hasOwn(request, "quote") || !Object.hasOwn(request, "swapperAddress") || !Object.hasOwn(request, "destinationAddress")) {
|
|
7050
|
+
fail("BRIDGE_INVALID_ARGUMENT");
|
|
7051
|
+
}
|
|
7052
|
+
const rawQuote = request.quote;
|
|
7053
|
+
if (!isRecord4(rawQuote)) fail("BRIDGE_INVALID_ARGUMENT");
|
|
7054
|
+
let quote;
|
|
7055
|
+
try {
|
|
7056
|
+
quote = cloneQuote(rawQuote);
|
|
7057
|
+
} catch {
|
|
7058
|
+
return fail("BRIDGE_INVALID_ARGUMENT");
|
|
7059
|
+
}
|
|
7060
|
+
return {
|
|
7061
|
+
quote,
|
|
7062
|
+
swapperAddress: request.swapperAddress,
|
|
7063
|
+
destinationAddress: request.destinationAddress,
|
|
7064
|
+
refundAddress: request.refundAddress
|
|
7065
|
+
};
|
|
7066
|
+
};
|
|
7067
|
+
var normalizeChainAddress = (value, chainId, code) => {
|
|
7068
|
+
if (chainId === ETHEREUM_CHAIN_ID) return normalizeEvmAddress3(value, code);
|
|
7069
|
+
const address = requireString(value, code);
|
|
7070
|
+
decodeBase58(address, 32, code);
|
|
7071
|
+
return address;
|
|
7072
|
+
};
|
|
7073
|
+
var isCanonicalSolanaAddress = (value) => {
|
|
7074
|
+
if (typeof value !== "string") return false;
|
|
7075
|
+
try {
|
|
7076
|
+
decodeBase58(value, 32, "BRIDGE_INVALID_ARGUMENT");
|
|
7077
|
+
return true;
|
|
7078
|
+
} catch {
|
|
7079
|
+
return false;
|
|
7080
|
+
}
|
|
7081
|
+
};
|
|
7082
|
+
var rejectAddressFromOtherChain = (value, expectedChainId) => {
|
|
7083
|
+
const isEvm = typeof value === "string" && EVM_ADDRESS2.test(value) && !/^0x0{40}$/iu.test(value);
|
|
7084
|
+
const isSolana = isCanonicalSolanaAddress(value);
|
|
7085
|
+
if (expectedChainId === ETHEREUM_CHAIN_ID && isSolana || expectedChainId === SOLANA_CHAIN_ID && isEvm) {
|
|
7086
|
+
fail("BRIDGE_QUOTE_MISMATCH");
|
|
7087
|
+
}
|
|
7088
|
+
};
|
|
7089
|
+
var normalizeDestinationAddress = (value, destinationChainId) => {
|
|
7090
|
+
return normalizeChainAddress(value, destinationChainId, "BRIDGE_INVALID_ARGUMENT");
|
|
7091
|
+
};
|
|
7092
|
+
var buildRouteFromQuote = (quote) => {
|
|
7093
|
+
let normalizedQuote;
|
|
7094
|
+
try {
|
|
7095
|
+
normalizedQuote = validateNormalizedQuoteShape(quote, "BRIDGE_QUOTE_MISMATCH");
|
|
7096
|
+
} catch (error) {
|
|
7097
|
+
if (error instanceof BridgeError) throw error;
|
|
7098
|
+
return fail("BRIDGE_QUOTE_MISMATCH");
|
|
7099
|
+
}
|
|
7100
|
+
if (stableJson(quote) !== stableJson(normalizedQuote)) fail("BRIDGE_QUOTE_MISMATCH");
|
|
7101
|
+
const route = validateBridgeRoute({
|
|
7102
|
+
sourceChainId: normalizedQuote.sourceChainId,
|
|
7103
|
+
destinationChainId: normalizedQuote.destinationChainId,
|
|
7104
|
+
sourceTokenDeploymentId: normalizedQuote.sourceTokenDeploymentId,
|
|
7105
|
+
destinationTokenDeploymentId: normalizedQuote.destinationTokenDeploymentId,
|
|
7106
|
+
amountIn: normalizedQuote.amountIn,
|
|
7107
|
+
slippageBps: normalizedQuote.slippageBps
|
|
7108
|
+
});
|
|
7109
|
+
return { route, normalizedQuote };
|
|
7110
|
+
};
|
|
7111
|
+
var validateRawQuoteForBuild = (quote, route, config) => {
|
|
7112
|
+
let response;
|
|
7113
|
+
try {
|
|
7114
|
+
response = parseProviderResponse(quote.rawSignedQuoteJson);
|
|
7115
|
+
} catch (error) {
|
|
7116
|
+
if (error instanceof BridgeError && error.code === "BRIDGE_QUOTE_EXPIRED") throw error;
|
|
7117
|
+
return fail("BRIDGE_QUOTE_MISMATCH");
|
|
7118
|
+
}
|
|
7119
|
+
if (!isRecord4(response.root.value) || response.root.start !== 0 || response.root.end !== response.text.length) {
|
|
7120
|
+
fail("BRIDGE_QUOTE_MISMATCH");
|
|
7121
|
+
}
|
|
7122
|
+
let rebuilt;
|
|
7123
|
+
try {
|
|
7124
|
+
rebuilt = validateProviderQuote(
|
|
7125
|
+
response.root,
|
|
7126
|
+
response.text,
|
|
7127
|
+
route.request,
|
|
7128
|
+
route.facts,
|
|
7129
|
+
config
|
|
7130
|
+
);
|
|
7131
|
+
} catch (error) {
|
|
7132
|
+
if (error instanceof BridgeError && error.code === "BRIDGE_QUOTE_EXPIRED") throw error;
|
|
7133
|
+
return fail("BRIDGE_QUOTE_MISMATCH");
|
|
7134
|
+
}
|
|
7135
|
+
if (stableJson(rebuilt.quote) !== stableJson(quote)) fail("BRIDGE_QUOTE_MISMATCH");
|
|
7136
|
+
return deepFreeze6(quote);
|
|
7137
|
+
};
|
|
7138
|
+
var numericZero = (value) => {
|
|
7139
|
+
if (typeof value === "number") return Number.isFinite(value) && value === 0;
|
|
7140
|
+
if (typeof value !== "string") return false;
|
|
7141
|
+
if (value === "0") return true;
|
|
7142
|
+
return /^0x0+$/iu.test(value);
|
|
7143
|
+
};
|
|
7144
|
+
var validateEvmBuildResult = (wrapper, swapperAddress) => {
|
|
7145
|
+
if (providerString(wrapper, "chainCategory") !== "evm") providerInvalid();
|
|
7146
|
+
if (providerString(wrapper, "quoteType") !== "SWIFT") providerInvalid();
|
|
7147
|
+
if (providerBoolean(wrapper, "gasless")) providerInvalid();
|
|
7148
|
+
const transaction = requiredNode(wrapper, "transaction");
|
|
7149
|
+
if (!isRecord4(transaction.value)) providerInvalid();
|
|
7150
|
+
const to = providerString(transaction, "to");
|
|
7151
|
+
if (!providerAddressEquals(to, ETHEREUM_FORWARDER)) providerInvalid();
|
|
7152
|
+
const chainId = providerInteger(transaction, "chainId");
|
|
7153
|
+
if (chainId !== ETHEREUM_PROVIDER_CHAIN_ID) providerInvalid();
|
|
7154
|
+
if (!numericZero(objectValue5(transaction, "value"))) providerInvalid();
|
|
7155
|
+
const data = providerString(transaction, "data").toLowerCase();
|
|
7156
|
+
if (!HEX_BYTES2.test(data) || data.length % 2 !== 0 || !data.startsWith(ETHEREUM_FORWARDER_SELECTOR) || data.length < 2 + 8 + 13 * 64) {
|
|
7157
|
+
providerInvalid();
|
|
7158
|
+
}
|
|
7159
|
+
return {
|
|
7160
|
+
kind: "evm-unsigned-transaction",
|
|
7161
|
+
chainId: "eip155:1",
|
|
7162
|
+
from: swapperAddress,
|
|
7163
|
+
to: ETHEREUM_FORWARDER,
|
|
7164
|
+
data,
|
|
7165
|
+
value: "0"
|
|
7166
|
+
};
|
|
7167
|
+
};
|
|
7168
|
+
var validateSolanaBuildResult = (wrapper, swapperAddress) => {
|
|
7169
|
+
if (providerString(wrapper, "chainCategory") !== "svm") providerInvalid();
|
|
7170
|
+
if (providerString(wrapper, "quoteType") !== "SWIFT") providerInvalid();
|
|
7171
|
+
const gaslessNode = objectEntry(wrapper, "gasless");
|
|
7172
|
+
if (gaslessNode !== void 0 && gaslessNode.value !== false) providerInvalid();
|
|
7173
|
+
const transaction = requiredNode(wrapper, "transaction");
|
|
7174
|
+
const encoded = validateSolanaTransaction(transaction.value, swapperAddress);
|
|
7175
|
+
return {
|
|
7176
|
+
kind: "solana-v0-unsigned-transaction",
|
|
7177
|
+
chainId: SOLANA_CHAIN_ID,
|
|
7178
|
+
feePayer: swapperAddress,
|
|
7179
|
+
transactionBase64: encoded
|
|
7180
|
+
};
|
|
7181
|
+
};
|
|
7182
|
+
var validateBuildResponse = (response, quote, facts, swapperAddress) => {
|
|
7183
|
+
if (!isRecord4(response.root.value) || objectValue5(response.root, "success") !== true) {
|
|
7184
|
+
fail("BRIDGE_BUILD_INVALID");
|
|
7185
|
+
}
|
|
7186
|
+
const wrapper = requiredNode(response.root, "transaction");
|
|
7187
|
+
if (!isRecord4(wrapper.value)) fail("BRIDGE_BUILD_INVALID");
|
|
7188
|
+
const signers = objectValue5(wrapper, "signers");
|
|
7189
|
+
if (signers !== void 0 && signers !== null && (!Array.isArray(signers) || signers.length !== 0)) {
|
|
7190
|
+
fail("BRIDGE_BUILD_INVALID");
|
|
7191
|
+
}
|
|
7192
|
+
const swapMessageV0Params = objectValue5(wrapper, "swapMessageV0Params");
|
|
7193
|
+
if (swapMessageV0Params !== void 0 && swapMessageV0Params !== null) {
|
|
7194
|
+
fail("BRIDGE_BUILD_INVALID");
|
|
7195
|
+
}
|
|
7196
|
+
let transaction;
|
|
7197
|
+
try {
|
|
7198
|
+
transaction = facts.sourceChainId === ETHEREUM_CHAIN_ID ? validateEvmBuildResult(wrapper, swapperAddress) : validateSolanaBuildResult(wrapper, swapperAddress);
|
|
7199
|
+
} catch (error) {
|
|
7200
|
+
if (error instanceof BridgeError) {
|
|
7201
|
+
if (error.code === "BRIDGE_BUILD_INVALID") throw error;
|
|
7202
|
+
return fail("BRIDGE_BUILD_INVALID");
|
|
7203
|
+
}
|
|
7204
|
+
return fail("BRIDGE_BUILD_INVALID");
|
|
7205
|
+
}
|
|
7206
|
+
return deepFreeze6({
|
|
7207
|
+
buildKind: "mayan-swift-v2-unsigned",
|
|
7208
|
+
providerId: "mayan-swift-v2",
|
|
7209
|
+
quote,
|
|
7210
|
+
sourceChainId: quote.sourceChainId,
|
|
7211
|
+
destinationChainId: quote.destinationChainId,
|
|
7212
|
+
transaction,
|
|
7213
|
+
allowance: facts.sourceChainId === ETHEREUM_CHAIN_ID ? {
|
|
7214
|
+
tokenDeploymentId: "deployment-0011",
|
|
7215
|
+
tokenAddress: ETHEREUM_EURC_ADDRESS,
|
|
7216
|
+
owner: swapperAddress,
|
|
7217
|
+
spender: ETHEREUM_FORWARDER,
|
|
7218
|
+
requiredAmount: quote.amountIn
|
|
7219
|
+
} : null,
|
|
7220
|
+
validation: {
|
|
7221
|
+
level: "structural",
|
|
7222
|
+
quoteSignatureLocallyVerified: false,
|
|
7223
|
+
transactionSemanticsLocallyVerified: false,
|
|
7224
|
+
settlementLocallyVerified: false
|
|
7225
|
+
},
|
|
7226
|
+
rawProviderBuildJson: response.text
|
|
7227
|
+
});
|
|
7228
|
+
};
|
|
7229
|
+
var boundedProviderString = (value, code, maximum = 1024) => {
|
|
7230
|
+
if (typeof value !== "string" || value.length === 0 || value.length > maximum) fail(code);
|
|
7231
|
+
return value;
|
|
7232
|
+
};
|
|
7233
|
+
var statusRequestSnapshot = (value) => {
|
|
7234
|
+
const request = requireRecord(value, "BRIDGE_INVALID_ARGUMENT");
|
|
7235
|
+
exactObjectKeys(request, ["sourceChainId", "sourceTransactionHash"], "BRIDGE_INVALID_ARGUMENT");
|
|
7236
|
+
return {
|
|
7237
|
+
sourceChainId: requireString(request.sourceChainId, "BRIDGE_INVALID_ARGUMENT"),
|
|
7238
|
+
sourceTransactionHash: requireString(request.sourceTransactionHash, "BRIDGE_INVALID_ARGUMENT")
|
|
7239
|
+
};
|
|
7240
|
+
};
|
|
7241
|
+
var normalizeStatusRequest = (value) => {
|
|
7242
|
+
const request = statusRequestSnapshot(value);
|
|
7243
|
+
if (request.sourceChainId === ETHEREUM_CHAIN_ID) {
|
|
7244
|
+
if (!EVM_HASH.test(request.sourceTransactionHash)) fail("BRIDGE_INVALID_ARGUMENT");
|
|
7245
|
+
return {
|
|
7246
|
+
sourceChainId: request.sourceChainId,
|
|
7247
|
+
sourceTransactionHash: request.sourceTransactionHash.toLowerCase()
|
|
7248
|
+
};
|
|
7249
|
+
}
|
|
7250
|
+
if (request.sourceChainId === SOLANA_CHAIN_ID) {
|
|
7251
|
+
decodeBase58(request.sourceTransactionHash, 64, "BRIDGE_INVALID_ARGUMENT");
|
|
7252
|
+
return request;
|
|
7253
|
+
}
|
|
7254
|
+
return fail("BRIDGE_UNSUPPORTED_ROUTE");
|
|
7255
|
+
};
|
|
7256
|
+
var statusFromResponse = (response, request) => {
|
|
7257
|
+
if (!isRecord4(response.root.value)) fail("BRIDGE_PROVIDER_INVALID_RESPONSE");
|
|
7258
|
+
const providerClientStatus = boundedProviderString(
|
|
7259
|
+
objectValue5(response.root, "clientStatus"),
|
|
7260
|
+
"BRIDGE_PROVIDER_INVALID_RESPONSE",
|
|
7261
|
+
128
|
|
7262
|
+
);
|
|
7263
|
+
const providerStatusValue = objectValue5(response.root, "status");
|
|
7264
|
+
if (providerStatusValue !== null && providerStatusValue !== void 0 && (typeof providerStatusValue !== "string" || providerStatusValue.length > 1024)) {
|
|
7265
|
+
fail("BRIDGE_PROVIDER_INVALID_RESPONSE");
|
|
7266
|
+
}
|
|
7267
|
+
const providerStatus = providerStatusValue === void 0 ? null : providerStatusValue;
|
|
7268
|
+
const state = providerClientStatus === "INPROGRESS" ? "in-progress" : providerClientStatus === "COMPLETED" ? "completed" : providerClientStatus === "REFUNDED" ? "refunded" : "unknown";
|
|
7269
|
+
return deepFreeze6({
|
|
7270
|
+
statusKind: "mayan-explorer-index",
|
|
7271
|
+
providerId: "mayan-swift-v2",
|
|
7272
|
+
sourceChainId: request.sourceChainId,
|
|
7273
|
+
sourceTransactionHash: request.sourceTransactionHash,
|
|
7274
|
+
state,
|
|
7275
|
+
providerClientStatus,
|
|
7276
|
+
providerStatus,
|
|
7277
|
+
statusVerification: "provider-indexed-not-locally-verified",
|
|
7278
|
+
rawProviderStatusJson: response.text
|
|
7279
|
+
});
|
|
7280
|
+
};
|
|
7281
|
+
var buildBody = (quote, sourceChainId, swapperAddress, destinationAddress, refundAddress) => {
|
|
7282
|
+
const params = {
|
|
7283
|
+
swapperAddress,
|
|
7284
|
+
destinationAddress
|
|
7285
|
+
};
|
|
7286
|
+
if (sourceChainId === ETHEREUM_CHAIN_ID) params.signerChainId = 1;
|
|
7287
|
+
if (refundAddress !== void 0) params.swiftRefundAddress = refundAddress;
|
|
7288
|
+
return `{"quote":${quote.rawSignedQuoteJson},"params":${JSON.stringify(params)}}`;
|
|
7289
|
+
};
|
|
7290
|
+
var quoteRequestBodyForRoute = (route) => quoteRequestBody(route);
|
|
7291
|
+
var MayanSwiftV2BridgeClient = class {
|
|
7292
|
+
#config;
|
|
7293
|
+
constructor(config) {
|
|
7294
|
+
this.#config = normalizeBridgeConfig(config);
|
|
7295
|
+
}
|
|
7296
|
+
async quoteExactInput(request, options) {
|
|
7297
|
+
const route = validateBridgeRoute(request);
|
|
7298
|
+
const response = await providerRequest(
|
|
7299
|
+
this.#config,
|
|
7300
|
+
endpointWithPath2(this.#config.builderEndpoint, "/quote"),
|
|
7301
|
+
"POST",
|
|
7302
|
+
quoteRequestBodyForRoute(route),
|
|
7303
|
+
false,
|
|
7304
|
+
options
|
|
7305
|
+
);
|
|
7306
|
+
const parsed = await providerJson(response, "quote");
|
|
7307
|
+
const quotes = quoteFromResponse(parsed, route.request, route.facts, this.#config);
|
|
7308
|
+
return deepFreeze6([...quotes]);
|
|
7309
|
+
}
|
|
7310
|
+
async buildUnsigned(request, options) {
|
|
7311
|
+
const snapshot = buildRequestSnapshot(request);
|
|
7312
|
+
const { route, normalizedQuote } = buildRouteFromQuote(snapshot.quote);
|
|
7313
|
+
rejectAddressFromOtherChain(snapshot.swapperAddress, route.facts.sourceChainId);
|
|
7314
|
+
const sourceAddress = normalizeChainAddress(
|
|
7315
|
+
snapshot.swapperAddress,
|
|
7316
|
+
route.facts.sourceChainId,
|
|
7317
|
+
"BRIDGE_INVALID_ARGUMENT"
|
|
7318
|
+
);
|
|
7319
|
+
const destinationAddress = normalizeDestinationAddress(
|
|
7320
|
+
snapshot.destinationAddress,
|
|
7321
|
+
route.facts.destinationChainId
|
|
7322
|
+
);
|
|
7323
|
+
let refundAddress;
|
|
7324
|
+
if (snapshot.refundAddress !== void 0) {
|
|
7325
|
+
refundAddress = normalizeChainAddress(
|
|
7326
|
+
snapshot.refundAddress,
|
|
7327
|
+
route.facts.sourceChainId,
|
|
7328
|
+
"BRIDGE_INVALID_ARGUMENT"
|
|
7329
|
+
);
|
|
7330
|
+
}
|
|
7331
|
+
if (this.#config.builderApiKey === void 0 && !this.#config.allowUnauthenticatedBuild) {
|
|
7332
|
+
fail("BRIDGE_PROVIDER_AUTH_REQUIRED");
|
|
7333
|
+
}
|
|
7334
|
+
const quote = validateRawQuoteForBuild(normalizedQuote, route, this.#config);
|
|
7335
|
+
const response = await providerRequest(
|
|
7336
|
+
this.#config,
|
|
7337
|
+
endpointWithPath2(this.#config.builderEndpoint, "/build"),
|
|
7338
|
+
"POST",
|
|
7339
|
+
buildBody(quote, route.facts.sourceChainId, sourceAddress, destinationAddress, refundAddress),
|
|
7340
|
+
true,
|
|
7341
|
+
options
|
|
7342
|
+
);
|
|
7343
|
+
const parsed = await providerJson(response, "build");
|
|
7344
|
+
ensureQuoteDeadline(
|
|
7345
|
+
normalizePositiveUint64(quote.deadline, "BRIDGE_QUOTE_EXPIRED").value,
|
|
7346
|
+
this.#config.minimumQuoteValiditySeconds
|
|
7347
|
+
);
|
|
7348
|
+
return validateBuildResponse(parsed, quote, route.facts, sourceAddress);
|
|
7349
|
+
}
|
|
7350
|
+
async getStatus(request, options) {
|
|
7351
|
+
const normalized = normalizeStatusRequest(request);
|
|
7352
|
+
const response = await providerRequest(
|
|
7353
|
+
this.#config,
|
|
7354
|
+
endpointWithPath2(
|
|
7355
|
+
this.#config.explorerEndpoint,
|
|
7356
|
+
`/swap/trx/${encodeURIComponent(normalized.sourceTransactionHash)}`
|
|
7357
|
+
),
|
|
7358
|
+
"GET",
|
|
7359
|
+
void 0,
|
|
7360
|
+
false,
|
|
7361
|
+
options
|
|
7362
|
+
);
|
|
7363
|
+
const parsed = await providerJson(response, "status");
|
|
7364
|
+
return statusFromResponse(parsed, normalized);
|
|
7365
|
+
}
|
|
7366
|
+
close() {
|
|
7367
|
+
}
|
|
7368
|
+
};
|
|
7369
|
+
var createMayanSwiftV2BridgeClient = (config) => new MayanSwiftV2BridgeClient(config);
|
|
7370
|
+
|
|
5003
7371
|
exports.AVALANCHE_AVAX_METHODS = AVALANCHE_AVAX_METHODS;
|
|
5004
7372
|
exports.AVALANCHE_INDEX_METHODS = AVALANCHE_INDEX_METHODS;
|
|
5005
7373
|
exports.AVALANCHE_INFO_METHODS = AVALANCHE_INFO_METHODS;
|
|
@@ -5007,6 +7375,11 @@ exports.AVALANCHE_PROPOSER_VM_METHODS = AVALANCHE_PROPOSER_VM_METHODS;
|
|
|
5007
7375
|
exports.AVALANCHE_P_CHAIN_METHODS = AVALANCHE_P_CHAIN_METHODS;
|
|
5008
7376
|
exports.AVALANCHE_X_CHAIN_METHODS = AVALANCHE_X_CHAIN_METHODS;
|
|
5009
7377
|
exports.AccountClient = AccountClient;
|
|
7378
|
+
exports.BRIDGE_CAPABILITIES_AS_OF_DATE = BRIDGE_CAPABILITIES_AS_OF_DATE;
|
|
7379
|
+
exports.BRIDGE_CAPABILITIES_CONTENT_DIGEST = BRIDGE_CAPABILITIES_CONTENT_DIGEST;
|
|
7380
|
+
exports.BRIDGE_CAPABILITIES_JSON = BRIDGE_CAPABILITIES_JSON;
|
|
7381
|
+
exports.BRIDGE_ERROR_MESSAGES = BRIDGE_ERROR_MESSAGES;
|
|
7382
|
+
exports.BridgeError = BridgeError;
|
|
5010
7383
|
exports.CloudCatalogClient = CloudCatalogClient;
|
|
5011
7384
|
exports.CloudCreditClient = CloudCreditClient;
|
|
5012
7385
|
exports.CloudResourcesClient = CloudResourcesClient;
|
|
@@ -5030,9 +7403,11 @@ exports.ErpcError = ErpcError;
|
|
|
5030
7403
|
exports.ErpcHttpError = ErpcHttpError;
|
|
5031
7404
|
exports.ErpcInvalidResponseError = ErpcInvalidResponseError;
|
|
5032
7405
|
exports.ErpcJsonRpcError = ErpcJsonRpcError;
|
|
7406
|
+
exports.ErpcNotConfiguredError = ErpcNotConfiguredError;
|
|
5033
7407
|
exports.ErpcTimeoutError = ErpcTimeoutError;
|
|
5034
7408
|
exports.ErpcTransportError = ErpcTransportError;
|
|
5035
7409
|
exports.EthereumSubscriptions = EthereumSubscriptions;
|
|
7410
|
+
exports.MayanSwiftV2BridgeClient = MayanSwiftV2BridgeClient;
|
|
5036
7411
|
exports.NATIVE_WRAP_DEFINITIONS = NATIVE_WRAP_DEFINITIONS;
|
|
5037
7412
|
exports.POOL_DEFINITIONS = POOL_DEFINITIONS;
|
|
5038
7413
|
exports.PendingRpcBatchRequest = PendingRpcBatchRequest;
|
|
@@ -5045,8 +7420,15 @@ exports.SOLANA_ENHANCED_SUBSCRIPTION_METHODS = SOLANA_ENHANCED_SUBSCRIPTION_METH
|
|
|
5045
7420
|
exports.SOLANA_HISTORY_METHODS = SOLANA_HISTORY_METHODS;
|
|
5046
7421
|
exports.SOLANA_LEADER_METHODS = SOLANA_LEADER_METHODS;
|
|
5047
7422
|
exports.SOLANA_RPC_METHODS = SOLANA_RPC_METHODS;
|
|
7423
|
+
exports.SWAP_EXECUTION_CAPABILITIES_AS_OF_DATE = SWAP_EXECUTION_CAPABILITIES_AS_OF_DATE;
|
|
7424
|
+
exports.SWAP_EXECUTION_CAPABILITIES_CONTENT_DIGEST = SWAP_EXECUTION_CAPABILITIES_CONTENT_DIGEST;
|
|
7425
|
+
exports.SWAP_EXECUTION_CAPABILITIES_JSON = SWAP_EXECUTION_CAPABILITIES_JSON;
|
|
7426
|
+
exports.SWAP_EXECUTION_ERROR_MESSAGES = SWAP_EXECUTION_ERROR_MESSAGES;
|
|
7427
|
+
exports.SWAP_EXECUTION_FUNCTION_SELECTOR = SWAP_EXECUTION_FUNCTION_SELECTOR;
|
|
7428
|
+
exports.SWAP_EXECUTION_FUNCTION_SIGNATURE = SWAP_EXECUTION_FUNCTION_SIGNATURE;
|
|
5048
7429
|
exports.SWAP_QUOTE_ERROR_MESSAGES = SWAP_QUOTE_ERROR_MESSAGES;
|
|
5049
7430
|
exports.SolanaSubscriptions = SolanaSubscriptions;
|
|
7431
|
+
exports.SwapExecutionError = SwapExecutionError;
|
|
5050
7432
|
exports.SwapQuoteError = SwapQuoteError;
|
|
5051
7433
|
exports.TOKEN_ALIASES = TOKEN_ALIASES;
|
|
5052
7434
|
exports.TOKEN_ASSETS = TOKEN_ASSETS;
|
|
@@ -5062,6 +7444,7 @@ exports.TOKEN_RANKINGS_METADATA = TOKEN_RANKINGS_METADATA;
|
|
|
5062
7444
|
exports.UsageClient = UsageClient;
|
|
5063
7445
|
exports.createErpcClient = createErpcClient;
|
|
5064
7446
|
exports.createErpcCloudClient = createErpcCloudClient;
|
|
7447
|
+
exports.createMayanSwiftV2BridgeClient = createMayanSwiftV2BridgeClient;
|
|
5065
7448
|
exports.dexes = dexes;
|
|
5066
7449
|
exports.findPoolDefinitionByAddress = findPoolDefinitionByAddress;
|
|
5067
7450
|
exports.findPoolDefinitionsByPair = findPoolDefinitionsByPair;
|