@proof-labs/trading-sdk 7.2.0 → 7.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bridge.d.ts +5 -8
- package/dist/bridge.d.ts.map +1 -1
- package/dist/bridge.js +5 -8
- package/dist/bridge.js.map +1 -1
- package/dist/client.d.ts +17 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +114 -63
- package/dist/client.js.map +1 -1
- package/dist/errors.d.ts +2 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +6 -0
- package/dist/errors.js.map +1 -1
- package/dist/financial-state.d.ts +2 -1
- package/dist/financial-state.d.ts.map +1 -1
- package/dist/financial-state.js +3 -5
- package/dist/financial-state.js.map +1 -1
- package/dist/http-transport.d.ts +56 -0
- package/dist/http-transport.d.ts.map +1 -0
- package/dist/http-transport.js +78 -0
- package/dist/http-transport.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/market-snapshot.d.ts +2 -1
- package/dist/market-snapshot.d.ts.map +1 -1
- package/dist/market-snapshot.js +4 -2
- package/dist/market-snapshot.js.map +1 -1
- package/dist/wasm/proof_trading_sdk_wasm.d.ts +1 -1
- package/dist/wasm/proof_trading_sdk_wasm.js +1 -1
- package/dist/wasm/proof_trading_sdk_wasm_bg.wasm +0 -0
- package/dist/wasm-loader.d.ts +3 -3
- package/dist/wasm-loader.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/client.js
CHANGED
|
@@ -14,6 +14,7 @@ import { getPublicKey, pubkeyToOwner, ownerToHex, bytesToHex, sign, verify, sign
|
|
|
14
14
|
import { decodeAdminSignerRegistryInfo, decodeAuthoritiesSnapshot, decodeNodeVersion, decodeUpgradesInfo, decodeEventInfo, decodeProposalPage, } from "./governance-query.js";
|
|
15
15
|
import { Decoder } from "@msgpack/msgpack";
|
|
16
16
|
import { readMarketsSnapshot } from "./market-snapshot.js";
|
|
17
|
+
import { defaultExchangeHttpTransport, exchangeHttpPriorityForAction, gatewayReadContext, readContext, surfaceFor, } from "./http-transport.js";
|
|
17
18
|
import { yesOrder } from "./binary.js";
|
|
18
19
|
import { Side } from "./types.js";
|
|
19
20
|
import { sha256 } from "@noble/hashes/sha2.js";
|
|
@@ -30,8 +31,12 @@ import { decodeOraclePermissions, validateOraclePermissionMarket, } from "./orac
|
|
|
30
31
|
* (raw `signAndEncode` callers, gateway-side helpers) that build wire
|
|
31
32
|
* bytes without going through a client instance.
|
|
32
33
|
*/
|
|
33
|
-
export async function fetchChainId(rpcUrl) {
|
|
34
|
-
const res = await
|
|
34
|
+
export async function fetchChainId(rpcUrl, httpTransport = defaultExchangeHttpTransport) {
|
|
35
|
+
const res = await httpTransport(`${rpcUrl}/status`, undefined, {
|
|
36
|
+
purpose: "chain_id",
|
|
37
|
+
priority: "critical",
|
|
38
|
+
surface: "status_chain_id",
|
|
39
|
+
});
|
|
35
40
|
if (!res.ok)
|
|
36
41
|
throw new Error(`/status returned HTTP ${res.status}`);
|
|
37
42
|
const json = (await res.json());
|
|
@@ -47,6 +52,7 @@ export class ExchangeClient {
|
|
|
47
52
|
gatewayUrl;
|
|
48
53
|
useGateway;
|
|
49
54
|
apiKey;
|
|
55
|
+
httpTransport;
|
|
50
56
|
wsUrl;
|
|
51
57
|
/**
|
|
52
58
|
* 32-byte chain_id binding for v3 signatures (audit B4). `null` until
|
|
@@ -104,6 +110,7 @@ export class ExchangeClient {
|
|
|
104
110
|
this.rpcUrl = stripTrailingSlash(opts.rpcUrl ?? deriveNodeUrl(this.gatewayUrl, "26657"));
|
|
105
111
|
this.apiUrl = stripTrailingSlash(opts.apiUrl ?? deriveNodeUrl(this.gatewayUrl, "8080"));
|
|
106
112
|
this.apiKey = opts.apiKey ?? null;
|
|
113
|
+
this.httpTransport = opts.httpTransport ?? defaultExchangeHttpTransport;
|
|
107
114
|
// WebSocket base URL (no path). Per-stream methods append the channel
|
|
108
115
|
// path (`/account-events`, `/orderbook-deltas`). Mirrors the Python SDK,
|
|
109
116
|
// which derives the WS base by swapping the gateway URL's scheme. Local
|
|
@@ -174,7 +181,7 @@ export class ExchangeClient {
|
|
|
174
181
|
try {
|
|
175
182
|
// `fetchChainId` appends `/status`, so the gateway path resolves from
|
|
176
183
|
// `${gatewayUrl}/v1/status` and the direct path from `${rpcUrl}/status`.
|
|
177
|
-
const bound = await fetchChainId(this.chainBase);
|
|
184
|
+
const bound = await fetchChainId(this.chainBase, this.httpTransport);
|
|
178
185
|
this.chainId = bound;
|
|
179
186
|
return bound;
|
|
180
187
|
}
|
|
@@ -338,7 +345,8 @@ export class ExchangeClient {
|
|
|
338
345
|
const hex = addressHex ?? this.addressHex;
|
|
339
346
|
if (!hex)
|
|
340
347
|
throw new Error("No address available");
|
|
341
|
-
const
|
|
348
|
+
const url = `${this.readBaseUrl}/v1/account/${hex}/recent-nonces`;
|
|
349
|
+
const res = await this.httpTransport(url, undefined, readContext(url, "account"));
|
|
342
350
|
const json = await res.json();
|
|
343
351
|
if (json.error)
|
|
344
352
|
throw new Error(json.error);
|
|
@@ -402,7 +410,7 @@ export class ExchangeClient {
|
|
|
402
410
|
// the tx but could not report the outcome in time, so the SDK reconciles by
|
|
403
411
|
// hash on the caller's behalf, exactly as before.
|
|
404
412
|
if (this.autoVerifyDelivery && r.hash && !this.isFinalResult(r)) {
|
|
405
|
-
this.spawnDeliveryVerifier(r.hash);
|
|
413
|
+
this.spawnDeliveryVerifier(r.hash, action);
|
|
406
414
|
}
|
|
407
415
|
return r;
|
|
408
416
|
}
|
|
@@ -440,10 +448,10 @@ export class ExchangeClient {
|
|
|
440
448
|
* the internal `useGateway: false` opt-out. Both submit identical bytes.
|
|
441
449
|
* Shared by `submitSignedTx` and `submitSignedTxCommit`.
|
|
442
450
|
*/
|
|
443
|
-
async broadcastSignedBytes(txBytes) {
|
|
451
|
+
async broadcastSignedBytes(txBytes, action) {
|
|
444
452
|
return this.useGateway
|
|
445
|
-
? this.submitViaGateway(txBytes)
|
|
446
|
-
: this.submitViaCometBFT(txBytes);
|
|
453
|
+
? this.submitViaGateway(txBytes, action)
|
|
454
|
+
: this.submitViaCometBFT(txBytes, action);
|
|
447
455
|
}
|
|
448
456
|
/**
|
|
449
457
|
* Internal: sign + submit with a fresh timestamp nonce. Does NOT spawn a
|
|
@@ -458,7 +466,7 @@ export class ExchangeClient {
|
|
|
458
466
|
* Both paths submit identical signed wire bytes.
|
|
459
467
|
*/
|
|
460
468
|
async broadcastSigned(action) {
|
|
461
|
-
return this.broadcastSignedBytes(await this.signTx(action));
|
|
469
|
+
return this.broadcastSignedBytes(await this.signTx(action), action);
|
|
462
470
|
}
|
|
463
471
|
/**
|
|
464
472
|
* Submit signed wire bytes via the public API gateway
|
|
@@ -480,7 +488,7 @@ export class ExchangeClient {
|
|
|
480
488
|
* For transport-level failures (rate limit, auth, etc.) we synthesize a code
|
|
481
489
|
* from the HTTP status.
|
|
482
490
|
*/
|
|
483
|
-
async submitViaGateway(txBytes) {
|
|
491
|
+
async submitViaGateway(txBytes, action) {
|
|
484
492
|
const txHash = computeCometTxHash(txBytes);
|
|
485
493
|
const headers = {
|
|
486
494
|
"Content-Type": "application/json",
|
|
@@ -491,10 +499,11 @@ export class ExchangeClient {
|
|
|
491
499
|
let res;
|
|
492
500
|
let gatewayBody;
|
|
493
501
|
try {
|
|
494
|
-
res = await
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
502
|
+
res = await this.httpTransport(`${this.gatewayUrl}/exchange`, { method: "POST", headers, body }, {
|
|
503
|
+
purpose: "submit",
|
|
504
|
+
priority: action ? exchangeHttpPriorityForAction(action) : "critical",
|
|
505
|
+
surface: "gateway_exchange",
|
|
506
|
+
action,
|
|
498
507
|
});
|
|
499
508
|
gatewayBody = await readGatewayBody(res);
|
|
500
509
|
}
|
|
@@ -633,9 +642,9 @@ export class ExchangeClient {
|
|
|
633
642
|
* when `useGateway` is false (internal-tools opt-out). Bypasses
|
|
634
643
|
* gateway auth/rate-limit and goes directly to CometBFT.
|
|
635
644
|
*/
|
|
636
|
-
async submitViaCometBFT(txBytes) {
|
|
645
|
+
async submitViaCometBFT(txBytes, action) {
|
|
637
646
|
const b64 = toBase64(txBytes);
|
|
638
|
-
const res = await
|
|
647
|
+
const res = await this.httpTransport(this.rpcUrl, {
|
|
639
648
|
method: "POST",
|
|
640
649
|
headers: { "Content-Type": "application/json" },
|
|
641
650
|
body: JSON.stringify({
|
|
@@ -644,6 +653,11 @@ export class ExchangeClient {
|
|
|
644
653
|
method: "broadcast_tx_sync",
|
|
645
654
|
params: { tx: b64 },
|
|
646
655
|
}),
|
|
656
|
+
}, {
|
|
657
|
+
purpose: "submit",
|
|
658
|
+
priority: action ? exchangeHttpPriorityForAction(action) : "critical",
|
|
659
|
+
surface: "rpc_broadcast_tx_sync",
|
|
660
|
+
action,
|
|
647
661
|
});
|
|
648
662
|
const json = await res.json();
|
|
649
663
|
if (json.error) {
|
|
@@ -663,11 +677,11 @@ export class ExchangeClient {
|
|
|
663
677
|
* block rather than via a chained .finally() so there's no microtask race
|
|
664
678
|
* between p settling and the awaitPendingVerifies() loop seeing size==0.
|
|
665
679
|
*/
|
|
666
|
-
spawnDeliveryVerifier(txHash) {
|
|
680
|
+
spawnDeliveryVerifier(txHash, action) {
|
|
667
681
|
let self;
|
|
668
682
|
const verify = async () => {
|
|
669
683
|
try {
|
|
670
|
-
const result = await this.
|
|
684
|
+
const result = await this.pollDelivery(txTimeout(txHash, "awaiting delivery"), { timeoutMs: 5_000 }, action);
|
|
671
685
|
if (result.outcome !== "timeout")
|
|
672
686
|
this.deliveryResults.push(result);
|
|
673
687
|
}
|
|
@@ -722,7 +736,7 @@ export class ExchangeClient {
|
|
|
722
736
|
// path so we don't double-spawn a background verifier — we're going to
|
|
723
737
|
// do our own synchronous verification below.
|
|
724
738
|
const sync = await this.broadcastSigned(action);
|
|
725
|
-
return this.awaitCommit(sync);
|
|
739
|
+
return this.awaitCommit(sync, action);
|
|
726
740
|
}
|
|
727
741
|
/**
|
|
728
742
|
* Submit **externally signed** wire bytes and wait for block inclusion —
|
|
@@ -746,13 +760,19 @@ export class ExchangeClient {
|
|
|
746
760
|
* final chain verdict, polling `/tx?hash=` only when the outcome is still
|
|
747
761
|
* unknown.
|
|
748
762
|
*/
|
|
749
|
-
async awaitCommit(sync) {
|
|
750
|
-
return this.
|
|
763
|
+
async awaitCommit(sync, action) {
|
|
764
|
+
return this.pollDelivery(sync, {}, action);
|
|
751
765
|
}
|
|
752
766
|
/** Per-submission finality. Timeout/cancellation retains the hash as unknown.
|
|
753
767
|
* Never resubmits writes. Use setUnsafeFastSubmit(true) to avoid a second poller.
|
|
754
768
|
*/
|
|
755
769
|
async waitForDelivery(sync, opts = {}) {
|
|
770
|
+
return this.pollDelivery(sync, opts);
|
|
771
|
+
}
|
|
772
|
+
/** `waitForDelivery`, with the originating action (when the client signed
|
|
773
|
+
* it) passed to the transport so its confirmation keeps the action's
|
|
774
|
+
* priority. */
|
|
775
|
+
async pollDelivery(sync, opts, action) {
|
|
756
776
|
if (this.isFinalResult(sync))
|
|
757
777
|
return sync;
|
|
758
778
|
if (!sync.hash)
|
|
@@ -770,8 +790,14 @@ export class ExchangeClient {
|
|
|
770
790
|
break;
|
|
771
791
|
const request = readDeadline(Math.ceil(remaining), opts.signal);
|
|
772
792
|
try {
|
|
773
|
-
const
|
|
774
|
-
|
|
793
|
+
const url = this.txStatusUrl(sync.hash);
|
|
794
|
+
const res = await this.httpTransport(url, { signal: request.signal }, {
|
|
795
|
+
purpose: "confirmation",
|
|
796
|
+
priority: action
|
|
797
|
+
? exchangeHttpPriorityForAction(action)
|
|
798
|
+
: "critical",
|
|
799
|
+
surface: surfaceFor(url),
|
|
800
|
+
action,
|
|
775
801
|
});
|
|
776
802
|
if (!res.ok)
|
|
777
803
|
continue;
|
|
@@ -954,8 +980,16 @@ export class ExchangeClient {
|
|
|
954
980
|
*/
|
|
955
981
|
async queryOwnerScoped(info, nodePath) {
|
|
956
982
|
return this.useGateway
|
|
957
|
-
? postInfoJson(this.gatewayUrl, info
|
|
958
|
-
|
|
983
|
+
? postInfoJson(this.httpTransport, this.gatewayUrl, info, {
|
|
984
|
+
purpose: "account",
|
|
985
|
+
priority: "urgent",
|
|
986
|
+
surface: `api_info_${String(info.type)}`,
|
|
987
|
+
})
|
|
988
|
+
: this.apiJson(`${this.apiUrl}${nodePath}`, "account");
|
|
989
|
+
}
|
|
990
|
+
/** `fetchApiJson` through this client's transport. */
|
|
991
|
+
apiJson(url, purpose = "query") {
|
|
992
|
+
return fetchApiJson(this.httpTransport, url, readContext(url, purpose));
|
|
959
993
|
}
|
|
960
994
|
/** Decode gateway MessagePack bytes after the authoritative strict preflight
|
|
961
995
|
* (`rejectFloats`). Reads now route through the WASM core, so this
|
|
@@ -971,7 +1005,7 @@ export class ExchangeClient {
|
|
|
971
1005
|
return value;
|
|
972
1006
|
}
|
|
973
1007
|
async queryOrderbook(market) {
|
|
974
|
-
const json = await
|
|
1008
|
+
const json = await this.apiJson(`${this.readBaseUrl}/v1/orderbook/${market}`);
|
|
975
1009
|
const bytes = fromBase64(json.data);
|
|
976
1010
|
const raw = (await this.strictDecode(bytes));
|
|
977
1011
|
const parseLevel = (arr) => ({
|
|
@@ -990,11 +1024,11 @@ export class ExchangeClient {
|
|
|
990
1024
|
if (!this.inventoryChainId) {
|
|
991
1025
|
throw new Error("market snapshot requires an explicit pinned chainId");
|
|
992
1026
|
}
|
|
993
|
-
return readMarketsSnapshot(this.gatewayUrl, this.inventoryChainId);
|
|
1027
|
+
return readMarketsSnapshot(this.gatewayUrl, this.inventoryChainId, this.httpTransport);
|
|
994
1028
|
}
|
|
995
1029
|
/** List all registered market configs. */
|
|
996
1030
|
async queryMarkets() {
|
|
997
|
-
const json = await
|
|
1031
|
+
const json = await this.apiJson(`${this.readBaseUrl}/v1/markets`);
|
|
998
1032
|
const bytes = fromBase64(json.data);
|
|
999
1033
|
const raw = (await this.strictDecode(bytes));
|
|
1000
1034
|
return raw.map((m) => decodeMarketConfig(m));
|
|
@@ -1005,7 +1039,7 @@ export class ExchangeClient {
|
|
|
1005
1039
|
* governance reads: a missing envelope or malformed row is a refusal.
|
|
1006
1040
|
* Decoder pinned to the engine golden vector in governance-query.test.ts. */
|
|
1007
1041
|
async queryEvents() {
|
|
1008
|
-
const json = await
|
|
1042
|
+
const json = await this.apiJson(`${this.readBaseUrl}/v1/events`);
|
|
1009
1043
|
if (typeof json.data !== "string") {
|
|
1010
1044
|
throw new Error("governance decode: events response has no encoded-data envelope");
|
|
1011
1045
|
}
|
|
@@ -1020,7 +1054,8 @@ export class ExchangeClient {
|
|
|
1020
1054
|
* here to map 404 -> null while still throwing on real transport / decode
|
|
1021
1055
|
* failures. */
|
|
1022
1056
|
async queryEvent(eventId) {
|
|
1023
|
-
const
|
|
1057
|
+
const url = `${this.readBaseUrl}/v1/event/${eventId}`;
|
|
1058
|
+
const res = await this.httpTransport(url, undefined, readContext(url, "query"));
|
|
1024
1059
|
if (res.status === 404)
|
|
1025
1060
|
return null;
|
|
1026
1061
|
const json = (await res.json());
|
|
@@ -1046,7 +1081,7 @@ export class ExchangeClient {
|
|
|
1046
1081
|
throw new Error("trigger owner must be a 40-character hex address");
|
|
1047
1082
|
}
|
|
1048
1083
|
const path = `/v1/triggers/${hex.toLowerCase()}`;
|
|
1049
|
-
const json = await
|
|
1084
|
+
const json = await this.apiJson(`${this.readBaseUrl}${path}`, "account");
|
|
1050
1085
|
const bytes = fromBase64(requireEncodedData(json, path));
|
|
1051
1086
|
return decodePositionTriggerInfos(await this.strictDecode(bytes));
|
|
1052
1087
|
}
|
|
@@ -1054,7 +1089,7 @@ export class ExchangeClient {
|
|
|
1054
1089
|
* markets are disabled; callers must never synthesize a default policy. */
|
|
1055
1090
|
async queryTriggerMarketConfigs() {
|
|
1056
1091
|
const path = "/v1/triggers/markets";
|
|
1057
|
-
const json = await
|
|
1092
|
+
const json = await this.apiJson(`${this.readBaseUrl}${path}`);
|
|
1058
1093
|
const bytes = fromBase64(requireEncodedData(json, path));
|
|
1059
1094
|
return decodeTriggerMarketConfigInfos(await this.strictDecode(bytes));
|
|
1060
1095
|
}
|
|
@@ -1069,7 +1104,8 @@ export class ExchangeClient {
|
|
|
1069
1104
|
* for the 404 response. It will be removed in the next major version. */
|
|
1070
1105
|
async queryTriggerStatus() {
|
|
1071
1106
|
const path = "/v1/triggers/status";
|
|
1072
|
-
const
|
|
1107
|
+
const url = `${this.readBaseUrl}${path}`;
|
|
1108
|
+
const res = await this.httpTransport(url, undefined, readContext(url, "query"));
|
|
1073
1109
|
const text = await res.text();
|
|
1074
1110
|
if (!res.ok) {
|
|
1075
1111
|
let message = `HTTP ${res.status}`;
|
|
@@ -1089,7 +1125,7 @@ export class ExchangeClient {
|
|
|
1089
1125
|
async queryOraclePermissions(market) {
|
|
1090
1126
|
validateOraclePermissionMarket(market);
|
|
1091
1127
|
const path = `/v1/oracle/permissions/${market}`;
|
|
1092
|
-
const json = await
|
|
1128
|
+
const json = await this.apiJson(`${this.readBaseUrl}${path}`);
|
|
1093
1129
|
const bytes = fromBase64(requireEncodedData(json, path));
|
|
1094
1130
|
return decodeOraclePermissions(await this.strictDecode(bytes), market);
|
|
1095
1131
|
}
|
|
@@ -1106,7 +1142,7 @@ export class ExchangeClient {
|
|
|
1106
1142
|
const canonicalOwner = hex.toLowerCase();
|
|
1107
1143
|
const params = positionTriggerHistorySearchParams(filters);
|
|
1108
1144
|
const qs = params.toString();
|
|
1109
|
-
const json = await
|
|
1145
|
+
const json = await this.apiJson(`${this.gatewayUrl}/v1/history/triggers/${canonicalOwner}${qs ? `?${qs}` : ""}`, "account");
|
|
1110
1146
|
return decodePositionTriggerHistoryPage(json, canonicalOwner, filters.market);
|
|
1111
1147
|
}
|
|
1112
1148
|
/** Shared market-level deferred/resumed trigger transitions. These are not
|
|
@@ -1115,7 +1151,7 @@ export class ExchangeClient {
|
|
|
1115
1151
|
validateTriggerHistoryMarket(market);
|
|
1116
1152
|
const params = triggerMarketHistorySearchParams(filters);
|
|
1117
1153
|
const qs = params.toString();
|
|
1118
|
-
const json = await
|
|
1154
|
+
const json = await this.apiJson(`${this.gatewayUrl}/v1/history/trigger-markets/${market}${qs ? `?${qs}` : ""}`);
|
|
1119
1155
|
return decodeTriggerMarketHistoryPage(json, market);
|
|
1120
1156
|
}
|
|
1121
1157
|
/**
|
|
@@ -1128,7 +1164,7 @@ export class ExchangeClient {
|
|
|
1128
1164
|
* differently.
|
|
1129
1165
|
*/
|
|
1130
1166
|
async queryAdminSignerRegistry() {
|
|
1131
|
-
const json = await
|
|
1167
|
+
const json = await this.apiJson(`${this.readBaseUrl}/v1/admin/signer-registry`);
|
|
1132
1168
|
const bytes = fromBase64(requireEncodedData(json, "/v1/admin/signer-registry"));
|
|
1133
1169
|
return decodeAdminSignerRegistryInfo(await this.strictDecode(bytes));
|
|
1134
1170
|
}
|
|
@@ -1140,7 +1176,7 @@ export class ExchangeClient {
|
|
|
1140
1176
|
* empty set.
|
|
1141
1177
|
*/
|
|
1142
1178
|
async queryAuthorities() {
|
|
1143
|
-
const json = await
|
|
1179
|
+
const json = await this.apiJson(`${this.readBaseUrl}/v1/admin/authorities`);
|
|
1144
1180
|
const bytes = fromBase64(requireEncodedData(json, "/v1/admin/authorities"));
|
|
1145
1181
|
return decodeAuthoritiesSnapshot(await this.strictDecode(bytes));
|
|
1146
1182
|
}
|
|
@@ -1149,7 +1185,7 @@ export class ExchangeClient {
|
|
|
1149
1185
|
* ledger via the gateway proxy (`GET /v1/upgrades`).
|
|
1150
1186
|
*/
|
|
1151
1187
|
async queryUpgrades() {
|
|
1152
|
-
const json = await
|
|
1188
|
+
const json = await this.apiJson(`${this.readBaseUrl}/v1/upgrades`);
|
|
1153
1189
|
const bytes = fromBase64(requireEncodedData(json, "/v1/upgrades"));
|
|
1154
1190
|
return decodeUpgradesInfo(await this.strictDecode(bytes));
|
|
1155
1191
|
}
|
|
@@ -1159,7 +1195,7 @@ export class ExchangeClient {
|
|
|
1159
1195
|
* whichever node served the request.
|
|
1160
1196
|
*/
|
|
1161
1197
|
async queryNodeVersion() {
|
|
1162
|
-
const json = await
|
|
1198
|
+
const json = await this.apiJson(`${this.readBaseUrl}/v1/version`);
|
|
1163
1199
|
return decodeNodeVersion(json);
|
|
1164
1200
|
}
|
|
1165
1201
|
/**
|
|
@@ -1183,7 +1219,7 @@ export class ExchangeClient {
|
|
|
1183
1219
|
if (opts?.limit != null)
|
|
1184
1220
|
params.set("limit", String(opts.limit));
|
|
1185
1221
|
const qs = params.toString();
|
|
1186
|
-
const json = await
|
|
1222
|
+
const json = await this.apiJson(`${this.readBaseUrl}/v1/proposals${qs ? `?${qs}` : ""}`);
|
|
1187
1223
|
const bytes = fromBase64(requireEncodedData(json, "/v1/proposals"));
|
|
1188
1224
|
return decodeProposalPage(await this.strictDecode(bytes));
|
|
1189
1225
|
}
|
|
@@ -1238,7 +1274,7 @@ export class ExchangeClient {
|
|
|
1238
1274
|
}
|
|
1239
1275
|
/** Raw finalized ledger facts, independent of oracle valuation. Not authorization. */
|
|
1240
1276
|
async queryFinancialState(selection) {
|
|
1241
|
-
return fetchFinancialState(this.gatewayUrl, selection);
|
|
1277
|
+
return fetchFinancialState(this.gatewayUrl, selection, this.httpTransport);
|
|
1242
1278
|
}
|
|
1243
1279
|
/** Raw finalized ledger facts, independent of oracle valuation. Not authorization. */
|
|
1244
1280
|
async queryAccountState(addressHex) {
|
|
@@ -1248,7 +1284,7 @@ export class ExchangeClient {
|
|
|
1248
1284
|
}
|
|
1249
1285
|
const owner = hex.toLowerCase();
|
|
1250
1286
|
const path = `/v1/account/${owner}/state`;
|
|
1251
|
-
const json = await
|
|
1287
|
+
const json = await this.apiJson(`${this.readBaseUrl}${path}`, "account");
|
|
1252
1288
|
return decodeAccountState(await this.strictDecode(fromBase64(requireEncodedData(json, path))), owner);
|
|
1253
1289
|
}
|
|
1254
1290
|
async queryAccount(addressHex) {
|
|
@@ -1342,14 +1378,16 @@ export class ExchangeClient {
|
|
|
1342
1378
|
return acct ? acct.equity : null;
|
|
1343
1379
|
}
|
|
1344
1380
|
async queryHealth() {
|
|
1345
|
-
const
|
|
1381
|
+
const url = `${this.readBaseUrl}/v1/health`;
|
|
1382
|
+
const res = await this.httpTransport(url, undefined, readContext(url, "query"));
|
|
1346
1383
|
return res.json();
|
|
1347
1384
|
}
|
|
1348
1385
|
/** Fetch the auto-deleveraging queue for a market — profitable positions
|
|
1349
1386
|
* ranked by `adlScore` desc (highest first). Empty array if the market has
|
|
1350
1387
|
* no profitable positions. Routes through the gateway's `/v1/adl/queue`. */
|
|
1351
1388
|
async queryAdlQueue(market) {
|
|
1352
|
-
const
|
|
1389
|
+
const url = `${this.readBaseUrl}/v1/adl/queue/${market}`;
|
|
1390
|
+
const res = await this.httpTransport(url, undefined, readContext(url, "query"));
|
|
1353
1391
|
const json = await res.json();
|
|
1354
1392
|
if (json.error)
|
|
1355
1393
|
return [];
|
|
@@ -1370,7 +1408,8 @@ export class ExchangeClient {
|
|
|
1370
1408
|
* and top-of-book blobs). Returns `null` if the market is unknown. Routes
|
|
1371
1409
|
* through the gateway's `/v1/ticker`. */
|
|
1372
1410
|
async queryTicker(market) {
|
|
1373
|
-
const
|
|
1411
|
+
const url = `${this.readBaseUrl}/v1/ticker/${market}`;
|
|
1412
|
+
const res = await this.httpTransport(url, undefined, readContext(url, "query"));
|
|
1374
1413
|
if (!res.ok)
|
|
1375
1414
|
return null;
|
|
1376
1415
|
const row = (await res.json());
|
|
@@ -1420,7 +1459,7 @@ export class ExchangeClient {
|
|
|
1420
1459
|
const pages = await Promise.all(kinds.map(async (kind) => {
|
|
1421
1460
|
const query = new URLSearchParams(params);
|
|
1422
1461
|
query.set("event_type", kind);
|
|
1423
|
-
const json = await
|
|
1462
|
+
const json = await this.apiJson(`${this.gatewayUrl}/v1/history/account-events?${query}`, "account");
|
|
1424
1463
|
return decodeCashFlowEvents(json, owner, kind);
|
|
1425
1464
|
}));
|
|
1426
1465
|
return pages
|
|
@@ -1454,7 +1493,7 @@ export class ExchangeClient {
|
|
|
1454
1493
|
params.set("limit", String(opts.limit));
|
|
1455
1494
|
const qs = params.toString();
|
|
1456
1495
|
const url = `${this.readBaseUrl}/v1/history/resolutions/${hex}${qs ? `?${qs}` : ""}`;
|
|
1457
|
-
const json = await fetchApiArray(url);
|
|
1496
|
+
const json = await fetchApiArray(this.httpTransport, url, readContext(url, "account"));
|
|
1458
1497
|
return json.map((row) => ({
|
|
1459
1498
|
kind: row.kind,
|
|
1460
1499
|
eventId: String(row.event_id ?? ""),
|
|
@@ -1476,7 +1515,8 @@ export class ExchangeClient {
|
|
|
1476
1515
|
* Uses this client's gateway; never targets a node, even in internal node mode.
|
|
1477
1516
|
*/
|
|
1478
1517
|
reads(opts = {}) {
|
|
1479
|
-
return new GatewayReads(this.gatewayUrl, opts.fetch ??
|
|
1518
|
+
return new GatewayReads(this.gatewayUrl, opts.fetch ??
|
|
1519
|
+
((url, init) => this.httpTransport(url, init, gatewayReadContext(url, init))));
|
|
1480
1520
|
}
|
|
1481
1521
|
/** Gateway/indexer portfolio page, including in internal node mode.
|
|
1482
1522
|
* Keep bounds fixed across opaque cursors. */
|
|
@@ -1486,7 +1526,8 @@ export class ExchangeClient {
|
|
|
1486
1526
|
throw new Error("Invalid portfolio owner");
|
|
1487
1527
|
const params = portfolioHistorySearchParams(opts);
|
|
1488
1528
|
opts.signal?.throwIfAborted();
|
|
1489
|
-
const
|
|
1529
|
+
const url = `${this.gatewayUrl}/v1/history/portfolio/${canonicalOwner}?${params}`;
|
|
1530
|
+
const res = await this.httpTransport(url, { signal: opts.signal }, readContext(url, "account"));
|
|
1490
1531
|
if (!res.ok)
|
|
1491
1532
|
throw new GatewayHttpError(res.status, res);
|
|
1492
1533
|
const page = await res.json();
|
|
@@ -1496,7 +1537,14 @@ export class ExchangeClient {
|
|
|
1496
1537
|
/** One newest-first gateway page of a market's indexed `price_updated`
|
|
1497
1538
|
* history. Keep market and bounds fixed across opaque cursors. */
|
|
1498
1539
|
async queryOraclePriceHistory(market, options) {
|
|
1499
|
-
return queryOraclePriceHistoryPage(this.gatewayUrl, market,
|
|
1540
|
+
return queryOraclePriceHistoryPage(this.gatewayUrl, market, {
|
|
1541
|
+
...options,
|
|
1542
|
+
fetch: options.fetch ??
|
|
1543
|
+
((input, init) => {
|
|
1544
|
+
const url = String(input instanceof Request ? input.url : input);
|
|
1545
|
+
return this.httpTransport(url, init, readContext(url, "query"));
|
|
1546
|
+
}),
|
|
1547
|
+
});
|
|
1500
1548
|
}
|
|
1501
1549
|
/** Compatibility array of position snapshots. Absent close fields are empty
|
|
1502
1550
|
* strings and timestamps are milliseconds. Use queryHistoryPositionsPage for
|
|
@@ -1529,7 +1577,7 @@ export class ExchangeClient {
|
|
|
1529
1577
|
params.set("market", String(opts.market));
|
|
1530
1578
|
}
|
|
1531
1579
|
const query = params.toString();
|
|
1532
|
-
const json = await
|
|
1580
|
+
const json = await this.apiJson(`${this.gatewayUrl}/v1/history/positions/${owner}${query ? `?${query}` : ""}`, "account");
|
|
1533
1581
|
return decodeHistoryPositionsPage(json, owner, opts.market);
|
|
1534
1582
|
}
|
|
1535
1583
|
/**
|
|
@@ -1558,7 +1606,7 @@ export class ExchangeClient {
|
|
|
1558
1606
|
}
|
|
1559
1607
|
const qs = params.toString();
|
|
1560
1608
|
const url = `${this.readBaseUrl}/v1/history/fills/${hex}${qs ? `?${qs}` : ""}`;
|
|
1561
|
-
const res = await
|
|
1609
|
+
const res = await this.httpTransport(url, undefined, readContext(url, "account"));
|
|
1562
1610
|
const json = (await res.json());
|
|
1563
1611
|
const body = (json ?? {});
|
|
1564
1612
|
if (!res.ok || "error" in body) {
|
|
@@ -1589,7 +1637,8 @@ export class ExchangeClient {
|
|
|
1589
1637
|
// Chain status & block queries (via the gateway's /v1/* by default)
|
|
1590
1638
|
// -----------------------------------------------------------------------
|
|
1591
1639
|
async status() {
|
|
1592
|
-
const
|
|
1640
|
+
const url = `${this.chainBase}/status`;
|
|
1641
|
+
const res = await this.httpTransport(url, undefined, readContext(url, "query"));
|
|
1593
1642
|
const json = await res.json();
|
|
1594
1643
|
const info = json.result.sync_info;
|
|
1595
1644
|
return {
|
|
@@ -1599,14 +1648,16 @@ export class ExchangeClient {
|
|
|
1599
1648
|
}
|
|
1600
1649
|
async getBlock(height) {
|
|
1601
1650
|
const params = height != null ? `?height=${height}` : "";
|
|
1602
|
-
const
|
|
1651
|
+
const url = `${this.chainBase}/block${params}`;
|
|
1652
|
+
const res = await this.httpTransport(url, undefined, readContext(url, "query"));
|
|
1603
1653
|
const json = await res.json();
|
|
1604
1654
|
if (json.error)
|
|
1605
1655
|
throw new Error(json.error.message ?? JSON.stringify(json.error));
|
|
1606
1656
|
return json.result;
|
|
1607
1657
|
}
|
|
1608
1658
|
async getBlockResults(height) {
|
|
1609
|
-
const
|
|
1659
|
+
const url = `${this.chainBase}/block_results?height=${height}`;
|
|
1660
|
+
const res = await this.httpTransport(url, undefined, readContext(url, "query"));
|
|
1610
1661
|
const json = await res.json();
|
|
1611
1662
|
if (json.error)
|
|
1612
1663
|
throw new Error(json.error.message ?? JSON.stringify(json.error));
|
|
@@ -1830,8 +1881,8 @@ function accountWsAuthMessage(chainId, ownerHex, afterId, timestampMs) {
|
|
|
1830
1881
|
* Fetch JSON from the API server and throw on non-2xx or `json.error`.
|
|
1831
1882
|
* Returns the parsed JSON body on success.
|
|
1832
1883
|
*/
|
|
1833
|
-
async function fetchApiJson(url) {
|
|
1834
|
-
const res = await
|
|
1884
|
+
async function fetchApiJson(transport, url, context) {
|
|
1885
|
+
const res = await transport(url, undefined, context);
|
|
1835
1886
|
const json = (await res.json());
|
|
1836
1887
|
if (!res.ok || json.error) {
|
|
1837
1888
|
const msg = json.error ?? `HTTP ${res.status}`;
|
|
@@ -1857,12 +1908,12 @@ function requireEncodedData(json, endpoint) {
|
|
|
1857
1908
|
* `{ data: <base64-msgpack> }`). Used for owner-scoped reads the gateway
|
|
1858
1909
|
* does not expose as GETs (account, open orders, withdrawal).
|
|
1859
1910
|
*/
|
|
1860
|
-
async function postInfoJson(gatewayUrl, info) {
|
|
1861
|
-
const res = await
|
|
1911
|
+
async function postInfoJson(transport, gatewayUrl, info, context) {
|
|
1912
|
+
const res = await transport(`${gatewayUrl}/info`, {
|
|
1862
1913
|
method: "POST",
|
|
1863
1914
|
headers: { "Content-Type": "application/json" },
|
|
1864
1915
|
body: JSON.stringify(info),
|
|
1865
|
-
});
|
|
1916
|
+
}, context);
|
|
1866
1917
|
// The error keeps an unread copy, so callers can still read the body.
|
|
1867
1918
|
const unread = res.clone();
|
|
1868
1919
|
let parsed;
|
|
@@ -1889,8 +1940,8 @@ async function postInfoJson(gatewayUrl, info) {
|
|
|
1889
1940
|
* Fetch a JSON array from the API server and throw on non-2xx or
|
|
1890
1941
|
* `json.error`. Returns the parsed array on success.
|
|
1891
1942
|
*/
|
|
1892
|
-
async function fetchApiArray(url) {
|
|
1893
|
-
const res = await
|
|
1943
|
+
async function fetchApiArray(transport, url, context) {
|
|
1944
|
+
const res = await transport(url, undefined, context);
|
|
1894
1945
|
const json = (await res.json());
|
|
1895
1946
|
if (!res.ok ||
|
|
1896
1947
|
(json !== null &&
|