ault-sdk-ts 0.0.2-alpha.19 → 0.0.2-alpha.21
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 +54 -43
- package/dist/core/manifest-resolver.d.ts +4 -3
- package/dist/core/network.d.ts +14 -5
- package/dist/index.cjs +382 -144
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +381 -144
- package/dist/index.js.map +1 -1
- package/dist/utils/address.d.ts +1 -0
- package/dist/ws/client.d.ts +42 -5
- package/dist/ws/types.d.ts +334 -72
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -337,7 +337,7 @@ var NETWORKS = {
|
|
|
337
337
|
rpcUrl: "https://mainnet-rpc-proxy.aultblockchain.xyz/cosmos-rpc",
|
|
338
338
|
restUrl: "https://mainnet-rpc-proxy.aultblockchain.xyz/rest",
|
|
339
339
|
evmRpcUrl: "https://mainnet-rpc-proxy.aultblockchain.xyz/evm",
|
|
340
|
-
explorerUrl: "https://ault.explorer.xangle.io
|
|
340
|
+
explorerUrl: "https://ault.explorer.xangle.io",
|
|
341
341
|
isProduction: true
|
|
342
342
|
},
|
|
343
343
|
"ault_10904-1": {
|
|
@@ -417,6 +417,15 @@ var GAS_CONSTANTS = {
|
|
|
417
417
|
DENOM: "aault",
|
|
418
418
|
/** Chain multiplier applied to min gas to derive max gas (120 = 1.2×). */
|
|
419
419
|
FEE_FREE_GAS_MULTIPLIER: 120,
|
|
420
|
+
/**
|
|
421
|
+
* EIP-712 ante overhead, in gas. The chain's per-message MinGasLimit values
|
|
422
|
+
* (mirrored below) measure the *handler* cost only; a fee-free EIP-712 tx
|
|
423
|
+
* additionally burns ante gas (sig verification + typed-data decode + tx
|
|
424
|
+
* size) that counts against gasWanted. For small-base ops (cancel) the 1.2×
|
|
425
|
+
* buffer doesn't cover it, so the high-level (EIP-712) path adds this on top.
|
|
426
|
+
* The agent path is capped at MinGasLimit×1.2 and must not add it.
|
|
427
|
+
*/
|
|
428
|
+
EIP712_ANTE_GAS: 3e4,
|
|
420
429
|
// License module
|
|
421
430
|
PER_LICENSE: 2e5,
|
|
422
431
|
PER_KYC_MEMBER: 2e5,
|
|
@@ -432,13 +441,15 @@ var GAS_CONSTANTS = {
|
|
|
432
441
|
REVOKE_BUILDER: 57e3,
|
|
433
442
|
SET_REFERRAL_CODE: 76e3,
|
|
434
443
|
SET_REFERRAL: 9e4,
|
|
435
|
-
// CLOB module
|
|
436
|
-
|
|
437
|
-
|
|
444
|
+
// CLOB module — mirror the chain's x/clob MinGasLimit* (agent_msg.go) exactly:
|
|
445
|
+
// the agent ante enforces MinGasLimit ≤ gas ≤ MinGasLimit×1.2, so any drift
|
|
446
|
+
// makes the SDK over- or under-shoot that window.
|
|
447
|
+
LIMIT_ORDER: 125e3,
|
|
448
|
+
MARKET_ORDER: 119e3,
|
|
438
449
|
SCALE_ORDER_BASE: 115e3,
|
|
439
|
-
SCALE_ORDER_PER_SUB:
|
|
440
|
-
CANCEL_ORDER_BASE:
|
|
441
|
-
CANCEL_ORDER_PER_ID:
|
|
450
|
+
SCALE_ORDER_PER_SUB: 13e3,
|
|
451
|
+
CANCEL_ORDER_BASE: 5e4,
|
|
452
|
+
CANCEL_ORDER_PER_ID: 19e3,
|
|
442
453
|
// Accountx module
|
|
443
454
|
OPEN_ACCOUNT: 96e3,
|
|
444
455
|
// Staking module (cosmos-sdk standard, NOT fee-free — user pays gas).
|
|
@@ -11120,30 +11131,165 @@ function createDexApi(context) {
|
|
|
11120
11131
|
};
|
|
11121
11132
|
}
|
|
11122
11133
|
|
|
11134
|
+
// src/utils/address.ts
|
|
11135
|
+
import { fromBech32, toBech32, fromHex, toHex } from "@cosmjs/encoding";
|
|
11136
|
+
import { getAddress, isAddress } from "viem";
|
|
11137
|
+
function evmToAult(evmAddress) {
|
|
11138
|
+
const hex = evmAddress.toLowerCase().replace("0x", "");
|
|
11139
|
+
const bytes = fromHex(hex);
|
|
11140
|
+
return toBech32("ault", bytes);
|
|
11141
|
+
}
|
|
11142
|
+
function aultToEvm(aultAddress) {
|
|
11143
|
+
const { data } = fromBech32(aultAddress);
|
|
11144
|
+
return getAddress(`0x${toHex(data)}`);
|
|
11145
|
+
}
|
|
11146
|
+
function isValidAultAddress(address) {
|
|
11147
|
+
try {
|
|
11148
|
+
const { prefix, data } = fromBech32(address);
|
|
11149
|
+
return prefix === "ault" && data.length === 20;
|
|
11150
|
+
} catch {
|
|
11151
|
+
return false;
|
|
11152
|
+
}
|
|
11153
|
+
}
|
|
11154
|
+
function isValidEvmAddress(address) {
|
|
11155
|
+
if (!/^0[xX][a-fA-F0-9]{40}$/.test(address)) return false;
|
|
11156
|
+
const normalized = `0x${address.slice(2)}`;
|
|
11157
|
+
const body = normalized.slice(2);
|
|
11158
|
+
const allLower = body === body.toLowerCase();
|
|
11159
|
+
const allUpper = body === body.toUpperCase();
|
|
11160
|
+
if (allLower || allUpper) return true;
|
|
11161
|
+
return isAddress(normalized, { strict: true });
|
|
11162
|
+
}
|
|
11163
|
+
function addressEquals(a, b) {
|
|
11164
|
+
const ab = addressBytes(a);
|
|
11165
|
+
const bb = addressBytes(b);
|
|
11166
|
+
if (!ab || !bb || ab.length !== bb.length) return false;
|
|
11167
|
+
for (let i = 0; i < ab.length; i++) {
|
|
11168
|
+
if (ab[i] !== bb[i]) return false;
|
|
11169
|
+
}
|
|
11170
|
+
return true;
|
|
11171
|
+
}
|
|
11172
|
+
function addressBytes(s) {
|
|
11173
|
+
if (!s) return null;
|
|
11174
|
+
if (s.startsWith("0x") || s.startsWith("0X")) {
|
|
11175
|
+
const body = s.slice(2);
|
|
11176
|
+
if (!/^[a-fA-F0-9]+$/.test(body) || body.length !== 40) return null;
|
|
11177
|
+
return fromHex(body.toLowerCase());
|
|
11178
|
+
}
|
|
11179
|
+
try {
|
|
11180
|
+
const { data } = fromBech32(s);
|
|
11181
|
+
return data;
|
|
11182
|
+
} catch {
|
|
11183
|
+
return null;
|
|
11184
|
+
}
|
|
11185
|
+
}
|
|
11186
|
+
function isValidValidatorAddress(address) {
|
|
11187
|
+
try {
|
|
11188
|
+
const { prefix, data } = fromBech32(address);
|
|
11189
|
+
return prefix === "aultvaloper" && data.length === 20;
|
|
11190
|
+
} catch {
|
|
11191
|
+
return false;
|
|
11192
|
+
}
|
|
11193
|
+
}
|
|
11194
|
+
function assertValidatorAddress(address) {
|
|
11195
|
+
if (isValidValidatorAddress(address)) {
|
|
11196
|
+
return address;
|
|
11197
|
+
}
|
|
11198
|
+
throw new Error(`Invalid validator address format: ${address}`);
|
|
11199
|
+
}
|
|
11200
|
+
|
|
11123
11201
|
// src/ws/client.ts
|
|
11202
|
+
import { toHex as toHex2 } from "@cosmjs/encoding";
|
|
11124
11203
|
var NORMAL_CLOSURE = 1e3;
|
|
11125
|
-
var
|
|
11204
|
+
var WS_READY_STATE_OPEN = 1;
|
|
11126
11205
|
var COMMAND_TIMEOUT_MS = 1e4;
|
|
11206
|
+
var USER_AMBIGUOUS_CHANNELS = /* @__PURE__ */ new Set(["orderUpdates", "userEvents"]);
|
|
11207
|
+
var MARKET_CHANNELS = /* @__PURE__ */ new Set([
|
|
11208
|
+
"l2Book",
|
|
11209
|
+
"bbo",
|
|
11210
|
+
"trades",
|
|
11211
|
+
"candle",
|
|
11212
|
+
"activeAssetCtx"
|
|
11213
|
+
]);
|
|
11214
|
+
var USER_CHANNELS = /* @__PURE__ */ new Set([
|
|
11215
|
+
"orderUpdates",
|
|
11216
|
+
"openOrders",
|
|
11217
|
+
"userFills",
|
|
11218
|
+
"userEvents",
|
|
11219
|
+
"spotState"
|
|
11220
|
+
]);
|
|
11221
|
+
var CHANNEL_SUBSCRIPTION_RESPONSE = "subscriptionResponse";
|
|
11222
|
+
var CHANNEL_ERROR = "error";
|
|
11223
|
+
var CHANNEL_SUBSCRIPTION_EXPIRED = "subscriptionExpired";
|
|
11224
|
+
function topicKey(sub) {
|
|
11225
|
+
const parts = [sub.type];
|
|
11226
|
+
if (sub.coin) parts.push(sub.coin);
|
|
11227
|
+
if (sub.user) parts.push(canonicalUser(sub.user));
|
|
11228
|
+
if (sub.interval) parts.push(sub.interval);
|
|
11229
|
+
return parts.join("|");
|
|
11230
|
+
}
|
|
11231
|
+
function canonicalUser(user) {
|
|
11232
|
+
const bytes = addressBytes(user);
|
|
11233
|
+
return bytes ? toHex2(bytes).toLowerCase() : user.toLowerCase();
|
|
11234
|
+
}
|
|
11235
|
+
function buildSubscriptionPayload(sub, ttl) {
|
|
11236
|
+
const out = { type: sub.type };
|
|
11237
|
+
if (sub.coin) out.coin = sub.coin;
|
|
11238
|
+
if (sub.user) out.user = sub.user;
|
|
11239
|
+
if (sub.interval) out.interval = sub.interval;
|
|
11240
|
+
if (ttl !== void 0) out.ttl = ttl;
|
|
11241
|
+
return out;
|
|
11242
|
+
}
|
|
11243
|
+
function isRecord(value) {
|
|
11244
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
11245
|
+
}
|
|
11246
|
+
function frameMatches(sub, data) {
|
|
11247
|
+
switch (sub.type) {
|
|
11248
|
+
case "l2Book":
|
|
11249
|
+
case "bbo":
|
|
11250
|
+
case "activeAssetCtx":
|
|
11251
|
+
return isRecord(data) && data.coin === sub.coin;
|
|
11252
|
+
case "trades": {
|
|
11253
|
+
if (!Array.isArray(data) || data.length === 0) return false;
|
|
11254
|
+
const first = data[0];
|
|
11255
|
+
return first?.coin === sub.coin;
|
|
11256
|
+
}
|
|
11257
|
+
case "candle": {
|
|
11258
|
+
if (!Array.isArray(data) || data.length === 0) return false;
|
|
11259
|
+
const first = data[0];
|
|
11260
|
+
return first?.s === sub.coin && first?.i === sub.interval;
|
|
11261
|
+
}
|
|
11262
|
+
case "openOrders":
|
|
11263
|
+
case "userFills":
|
|
11264
|
+
case "spotState":
|
|
11265
|
+
return isRecord(data) && typeof data.user === "string" && !!sub.user && addressEquals(data.user, sub.user);
|
|
11266
|
+
case "orderUpdates":
|
|
11267
|
+
case "userEvents":
|
|
11268
|
+
return true;
|
|
11269
|
+
case "allMids":
|
|
11270
|
+
case "chainBlock":
|
|
11271
|
+
return true;
|
|
11272
|
+
default:
|
|
11273
|
+
return false;
|
|
11274
|
+
}
|
|
11275
|
+
}
|
|
11127
11276
|
var isowsModulePromise = null;
|
|
11277
|
+
function wrapCtor(Ctor) {
|
|
11278
|
+
return (url, protocols) => protocols === void 0 ? new Ctor(url) : new Ctor(url, protocols);
|
|
11279
|
+
}
|
|
11280
|
+
function currentGlobalCtor() {
|
|
11281
|
+
return typeof globalThis !== "undefined" && typeof globalThis.WebSocket === "function" ? globalThis.WebSocket : null;
|
|
11282
|
+
}
|
|
11128
11283
|
function getDefaultWebSocketFactory() {
|
|
11129
|
-
|
|
11130
|
-
|
|
11131
|
-
const Ctor = globalThis.WebSocket;
|
|
11132
|
-
return protocols === void 0 ? new Ctor(url) : new Ctor(url, protocols);
|
|
11133
|
-
};
|
|
11134
|
-
}
|
|
11284
|
+
const G = currentGlobalCtor();
|
|
11285
|
+
if (G) return wrapCtor(G);
|
|
11135
11286
|
if (!isowsModulePromise) {
|
|
11136
11287
|
isowsModulePromise = import("isows");
|
|
11137
11288
|
}
|
|
11138
11289
|
return isowsModulePromise.then((mod) => {
|
|
11139
|
-
|
|
11140
|
-
|
|
11141
|
-
|
|
11142
|
-
return protocols === void 0 ? new Ctor2(url) : new Ctor2(url, protocols);
|
|
11143
|
-
};
|
|
11144
|
-
}
|
|
11145
|
-
const Ctor = mod.WebSocket;
|
|
11146
|
-
return (url, protocols) => protocols === void 0 ? new Ctor(url) : new Ctor(url, protocols);
|
|
11290
|
+
const G2 = currentGlobalCtor();
|
|
11291
|
+
if (G2) return wrapCtor(G2);
|
|
11292
|
+
return wrapCtor(mod.WebSocket);
|
|
11147
11293
|
});
|
|
11148
11294
|
}
|
|
11149
11295
|
var DexWsClient = class {
|
|
@@ -11151,7 +11297,12 @@ var DexWsClient = class {
|
|
|
11151
11297
|
this.ws = null;
|
|
11152
11298
|
this.connectionState = "disconnected";
|
|
11153
11299
|
this.subscriptions = /* @__PURE__ */ new Map();
|
|
11154
|
-
|
|
11300
|
+
// Per-event handler sets keyed by event name. Typed against the event map so
|
|
11301
|
+
// each set holds exactly that event's handler signature — no `Function`/`any`
|
|
11302
|
+
// erasure. `on`/`emit` index it generically; the lone cast at insertion (in
|
|
11303
|
+
// `on`) is sound because the public `on<E>` overload pins `handler` to
|
|
11304
|
+
// `DexWsEventMap[E]` at the boundary.
|
|
11305
|
+
this.listeners = {};
|
|
11155
11306
|
this.pendingCommands = /* @__PURE__ */ new Map();
|
|
11156
11307
|
this.reconnectAttempt = 0;
|
|
11157
11308
|
this.reconnectTimer = null;
|
|
@@ -11202,39 +11353,121 @@ var DexWsClient = class {
|
|
|
11202
11353
|
this.rejectConnect(err);
|
|
11203
11354
|
this.rejectPendingCommands(err);
|
|
11204
11355
|
}
|
|
11205
|
-
|
|
11356
|
+
/**
|
|
11357
|
+
* Subscribe to a channel. `handlers.onData` receives every frame (snapshot
|
|
11358
|
+
* and deltas alike); the SDK makes no assumption that the first frame is a
|
|
11359
|
+
* delta. Returns a handle whose `unsubscribe()` tears the subscription down.
|
|
11360
|
+
*/
|
|
11361
|
+
subscribe(subscription, handlers, options) {
|
|
11362
|
+
this.assertRequiredKey(subscription);
|
|
11363
|
+
this.assertNoUserAmbiguity(subscription);
|
|
11206
11364
|
const ttl = options?.ttl;
|
|
11207
|
-
|
|
11208
|
-
|
|
11365
|
+
const key = topicKey(subscription);
|
|
11366
|
+
const active = { subscription, handlers, ttl };
|
|
11367
|
+
this.subscriptions.set(key, active);
|
|
11368
|
+
this.dispatchSubscribe(active);
|
|
11369
|
+
return {
|
|
11370
|
+
subscription,
|
|
11371
|
+
unsubscribe: () => this.unsubscribe(subscription)
|
|
11372
|
+
};
|
|
11373
|
+
}
|
|
11374
|
+
/**
|
|
11375
|
+
* Send a subscribe for an already-registered active subscription and wire its
|
|
11376
|
+
* ack/error/onSubscribed handling. Shared by the initial subscribe and the
|
|
11377
|
+
* reconnect replay so both paths surface failures via onError instead of
|
|
11378
|
+
* leaving a silent dead subscription.
|
|
11379
|
+
*/
|
|
11380
|
+
dispatchSubscribe(active) {
|
|
11381
|
+
const { subscription, handlers, ttl } = active;
|
|
11382
|
+
const key = topicKey(subscription);
|
|
11383
|
+
const deleteIfOwned = () => {
|
|
11384
|
+
if (this.subscriptions.get(key) === active) this.subscriptions.delete(key);
|
|
11385
|
+
};
|
|
11386
|
+
this.sendSubscribe(subscription, ttl).then(
|
|
11209
11387
|
(resp) => {
|
|
11210
11388
|
if (resp.success) {
|
|
11211
|
-
handlers.onSubscribed?.();
|
|
11389
|
+
handlers.onSubscribed?.(resp.ack);
|
|
11212
11390
|
} else {
|
|
11213
|
-
|
|
11391
|
+
deleteIfOwned();
|
|
11214
11392
|
handlers.onError?.(new WebSocketError(resp.error ?? "Subscribe failed"));
|
|
11215
11393
|
}
|
|
11216
11394
|
},
|
|
11217
11395
|
(err) => {
|
|
11218
|
-
|
|
11396
|
+
deleteIfOwned();
|
|
11219
11397
|
handlers.onError?.(err);
|
|
11220
11398
|
}
|
|
11221
11399
|
);
|
|
11222
|
-
|
|
11223
|
-
|
|
11224
|
-
|
|
11400
|
+
}
|
|
11401
|
+
/**
|
|
11402
|
+
* Enforce the per-channel required key at runtime. The `WsSubscription` union
|
|
11403
|
+
* already encodes this at compile time, but a caller can cast through it (e.g.
|
|
11404
|
+
* `type` via `as never` for a not-yet-typed channel). Registering a partial
|
|
11405
|
+
* sub is unsafe: a user channel with no `user` would match every frame on that
|
|
11406
|
+
* channel (frameMatches returns true for orderUpdates/userEvents) until the
|
|
11407
|
+
* async server reject lands. Throw before `subscriptions.set` instead.
|
|
11408
|
+
*/
|
|
11409
|
+
assertRequiredKey(sub) {
|
|
11410
|
+
const isMarket = MARKET_CHANNELS.has(sub.type);
|
|
11411
|
+
const isUser = USER_CHANNELS.has(sub.type);
|
|
11412
|
+
if (isMarket && !sub.coin) {
|
|
11413
|
+
throw new WebSocketError(`"${sub.type}" requires a "coin"`);
|
|
11414
|
+
}
|
|
11415
|
+
if (sub.type === "candle" && !sub.interval) {
|
|
11416
|
+
throw new WebSocketError(`"candle" requires an "interval"`);
|
|
11417
|
+
}
|
|
11418
|
+
if (isUser && !sub.user) {
|
|
11419
|
+
throw new WebSocketError(`"${sub.type}" requires a "user"`);
|
|
11420
|
+
}
|
|
11421
|
+
const allowed = {
|
|
11422
|
+
coin: isMarket,
|
|
11423
|
+
user: isUser,
|
|
11424
|
+
interval: sub.type === "candle"
|
|
11225
11425
|
};
|
|
11426
|
+
for (const field of ["coin", "user", "interval"]) {
|
|
11427
|
+
if (sub[field] !== void 0 && !allowed[field]) {
|
|
11428
|
+
throw new WebSocketError(`"${sub.type}" does not accept a "${field}" field`);
|
|
11429
|
+
}
|
|
11430
|
+
}
|
|
11431
|
+
}
|
|
11432
|
+
/**
|
|
11433
|
+
* Guard against multiplexing two different users across a user-ambiguous
|
|
11434
|
+
* channel (orderUpdates/userEvents) on one connection. Their frames carry no
|
|
11435
|
+
* `user`, so a second user's subscription would fan out into the first's
|
|
11436
|
+
* handler and vice-versa, leaking private order flow. Re-subscribing the same
|
|
11437
|
+
* user is allowed (it just refreshes handlers/ttl).
|
|
11438
|
+
*/
|
|
11439
|
+
assertNoUserAmbiguity(sub) {
|
|
11440
|
+
if (!USER_AMBIGUOUS_CHANNELS.has(sub.type)) return;
|
|
11441
|
+
for (const active of this.subscriptions.values()) {
|
|
11442
|
+
if (active.subscription.type !== sub.type) continue;
|
|
11443
|
+
const existingUser = active.subscription.user;
|
|
11444
|
+
if (!existingUser || !sub.user || !addressEquals(existingUser, sub.user)) {
|
|
11445
|
+
throw new WebSocketError(
|
|
11446
|
+
`Cannot subscribe a second user to "${sub.type}" on a shared connection: this channel carries no user field, so frames cannot be disambiguated. Use a separate DexWsClient per user.`
|
|
11447
|
+
);
|
|
11448
|
+
}
|
|
11449
|
+
}
|
|
11226
11450
|
}
|
|
11227
|
-
unsubscribe(
|
|
11228
|
-
|
|
11451
|
+
unsubscribe(subscription) {
|
|
11452
|
+
const key = topicKey(subscription);
|
|
11453
|
+
this.subscriptions.delete(key);
|
|
11454
|
+
const pending = this.pendingCommands.get(key);
|
|
11455
|
+
if (pending) {
|
|
11456
|
+
clearTimeout(pending.timer);
|
|
11457
|
+
this.pendingCommands.delete(key);
|
|
11458
|
+
}
|
|
11229
11459
|
if (this.connectionState === "connected") {
|
|
11230
|
-
this.send({
|
|
11460
|
+
this.send({
|
|
11461
|
+
method: "unsubscribe",
|
|
11462
|
+
subscription: buildSubscriptionPayload(subscription)
|
|
11463
|
+
});
|
|
11231
11464
|
}
|
|
11232
11465
|
}
|
|
11233
11466
|
on(event, handler) {
|
|
11234
|
-
let set = this.listeners
|
|
11467
|
+
let set = this.listeners[event];
|
|
11235
11468
|
if (!set) {
|
|
11236
11469
|
set = /* @__PURE__ */ new Set();
|
|
11237
|
-
this.listeners
|
|
11470
|
+
this.listeners[event] = set;
|
|
11238
11471
|
}
|
|
11239
11472
|
set.add(handler);
|
|
11240
11473
|
return () => set.delete(handler);
|
|
@@ -11313,58 +11546,126 @@ var DexWsClient = class {
|
|
|
11313
11546
|
return;
|
|
11314
11547
|
}
|
|
11315
11548
|
this.emit("message", msg2);
|
|
11316
|
-
|
|
11317
|
-
|
|
11318
|
-
|
|
11549
|
+
switch (msg2.channel) {
|
|
11550
|
+
case CHANNEL_SUBSCRIPTION_RESPONSE:
|
|
11551
|
+
this.handleAck(msg2.data);
|
|
11552
|
+
return;
|
|
11553
|
+
case CHANNEL_ERROR:
|
|
11554
|
+
this.handleChannelError(msg2.data);
|
|
11555
|
+
return;
|
|
11556
|
+
case CHANNEL_SUBSCRIPTION_EXPIRED:
|
|
11557
|
+
this.handleExpired(msg2.data);
|
|
11558
|
+
return;
|
|
11559
|
+
default:
|
|
11560
|
+
this.dispatchData(msg2);
|
|
11561
|
+
}
|
|
11562
|
+
}
|
|
11563
|
+
handleChannelError(data) {
|
|
11564
|
+
const err = new WebSocketError(data?.message ?? "WebSocket error");
|
|
11565
|
+
const sub = data?.subscription;
|
|
11566
|
+
if (sub) {
|
|
11567
|
+
const key = topicKey(sub);
|
|
11319
11568
|
const pending = this.pendingCommands.get(key);
|
|
11320
11569
|
if (pending) {
|
|
11321
11570
|
clearTimeout(pending.timer);
|
|
11322
11571
|
this.pendingCommands.delete(key);
|
|
11323
|
-
pending.
|
|
11572
|
+
pending.reject(err);
|
|
11573
|
+
return;
|
|
11324
11574
|
}
|
|
11325
|
-
|
|
11326
|
-
|
|
11327
|
-
|
|
11328
|
-
|
|
11329
|
-
|
|
11330
|
-
this.subscriptions.delete(msg2.topic);
|
|
11331
|
-
sub2.handlers.onExpired?.();
|
|
11575
|
+
const active = this.subscriptions.get(key);
|
|
11576
|
+
if (active) {
|
|
11577
|
+
this.subscriptions.delete(key);
|
|
11578
|
+
active.handlers.onError?.(err);
|
|
11579
|
+
return;
|
|
11332
11580
|
}
|
|
11581
|
+
this.emit("error", err);
|
|
11333
11582
|
return;
|
|
11334
11583
|
}
|
|
11335
|
-
|
|
11584
|
+
this.emit("error", err);
|
|
11585
|
+
}
|
|
11586
|
+
// The ack echoes the subscription (type/coin/user/interval) plus the
|
|
11587
|
+
// effective ttl and/or an error. It carries no `method`, so subscribe and
|
|
11588
|
+
// unsubscribe acks are indistinguishable on the wire — only subscribe
|
|
11589
|
+
// registers a pending command, and unsubscribe drops its pending eagerly, so
|
|
11590
|
+
// there is never a collision on the topic key.
|
|
11591
|
+
handleAck(ack) {
|
|
11592
|
+
const key = topicKey({
|
|
11593
|
+
type: ack.type,
|
|
11594
|
+
coin: ack.coin,
|
|
11595
|
+
user: ack.user,
|
|
11596
|
+
interval: ack.interval
|
|
11597
|
+
});
|
|
11598
|
+
const pending = this.pendingCommands.get(key);
|
|
11599
|
+
if (!pending) return;
|
|
11600
|
+
clearTimeout(pending.timer);
|
|
11601
|
+
this.pendingCommands.delete(key);
|
|
11602
|
+
pending.resolve({
|
|
11603
|
+
success: !ack.error,
|
|
11604
|
+
error: ack.error,
|
|
11605
|
+
ttl: ack.ttl,
|
|
11606
|
+
ack
|
|
11607
|
+
});
|
|
11608
|
+
}
|
|
11609
|
+
handleExpired(data) {
|
|
11610
|
+
const key = topicKey(data.subscription);
|
|
11611
|
+
const sub = this.subscriptions.get(key);
|
|
11336
11612
|
if (sub) {
|
|
11337
|
-
|
|
11338
|
-
|
|
11339
|
-
|
|
11340
|
-
|
|
11341
|
-
|
|
11613
|
+
this.subscriptions.delete(key);
|
|
11614
|
+
sub.handlers.onExpired?.();
|
|
11615
|
+
}
|
|
11616
|
+
}
|
|
11617
|
+
dispatchData(msg2) {
|
|
11618
|
+
const mappedType = msg2.channel === "user" ? "userEvents" : msg2.channel;
|
|
11619
|
+
const channelSubs = [...this.subscriptions.values()].filter(
|
|
11620
|
+
(s) => s.subscription.type === mappedType
|
|
11621
|
+
);
|
|
11622
|
+
if (channelSubs.length === 0) return;
|
|
11623
|
+
const isUnattributableEmpty = (mappedType === "trades" || mappedType === "candle") && Array.isArray(msg2.data) && msg2.data.length === 0;
|
|
11624
|
+
if (isUnattributableEmpty) {
|
|
11625
|
+
for (const sub of channelSubs) this.deliver(sub, msg2);
|
|
11626
|
+
return;
|
|
11627
|
+
}
|
|
11628
|
+
for (const sub of channelSubs) {
|
|
11629
|
+
if (!frameMatches(sub.subscription, msg2.data)) continue;
|
|
11630
|
+
this.deliver(sub, msg2);
|
|
11631
|
+
}
|
|
11632
|
+
}
|
|
11633
|
+
deliver(sub, msg2) {
|
|
11634
|
+
try {
|
|
11635
|
+
sub.handlers.onData(msg2.data, msg2);
|
|
11636
|
+
} catch (err) {
|
|
11637
|
+
sub.handlers.onError?.(err instanceof Error ? err : new Error(String(err)));
|
|
11342
11638
|
}
|
|
11343
11639
|
}
|
|
11344
11640
|
send(data) {
|
|
11345
|
-
if (this.ws?.readyState ===
|
|
11641
|
+
if (this.ws?.readyState === WS_READY_STATE_OPEN) {
|
|
11346
11642
|
this.ws.send(JSON.stringify(data));
|
|
11347
11643
|
}
|
|
11348
11644
|
}
|
|
11349
|
-
|
|
11645
|
+
sendSubscribe(sub, ttl) {
|
|
11350
11646
|
if (this.connectionState !== "connected") {
|
|
11351
11647
|
return Promise.reject(new WebSocketError("Not connected"));
|
|
11352
11648
|
}
|
|
11353
11649
|
return new Promise((resolve, reject) => {
|
|
11354
|
-
const
|
|
11355
|
-
const
|
|
11356
|
-
|
|
11650
|
+
const key = topicKey(sub);
|
|
11651
|
+
const existing = this.pendingCommands.get(key);
|
|
11652
|
+
if (existing) {
|
|
11653
|
+
clearTimeout(existing.timer);
|
|
11654
|
+
existing.reject(new WebSocketError("Subscribe superseded by a newer subscribe"));
|
|
11655
|
+
}
|
|
11357
11656
|
const timer = setTimeout(() => {
|
|
11657
|
+
if (this.pendingCommands.get(key) !== entry) return;
|
|
11358
11658
|
this.pendingCommands.delete(key);
|
|
11359
|
-
reject(new WebSocketError(
|
|
11659
|
+
reject(new WebSocketError("Command timed out: subscribe"));
|
|
11360
11660
|
}, COMMAND_TIMEOUT_MS);
|
|
11361
|
-
|
|
11362
|
-
this.
|
|
11661
|
+
const entry = { resolve, reject, timer };
|
|
11662
|
+
this.pendingCommands.set(key, entry);
|
|
11663
|
+
this.send({
|
|
11664
|
+
method: "subscribe",
|
|
11665
|
+
subscription: buildSubscriptionPayload(sub, ttl)
|
|
11666
|
+
});
|
|
11363
11667
|
});
|
|
11364
11668
|
}
|
|
11365
|
-
commandKey(action, topic) {
|
|
11366
|
-
return `${action}:${topic}`;
|
|
11367
|
-
}
|
|
11368
11669
|
scheduleReconnect() {
|
|
11369
11670
|
if (this.reconnectAttempt >= this.options.maxReconnectAttempts) {
|
|
11370
11671
|
this.connectionState = "disconnected";
|
|
@@ -11396,8 +11697,8 @@ var DexWsClient = class {
|
|
|
11396
11697
|
}
|
|
11397
11698
|
resubscribeAll() {
|
|
11398
11699
|
this.emit("reconnected");
|
|
11399
|
-
for (const
|
|
11400
|
-
this.
|
|
11700
|
+
for (const sub of [...this.subscriptions.values()]) {
|
|
11701
|
+
this.dispatchSubscribe(sub);
|
|
11401
11702
|
}
|
|
11402
11703
|
}
|
|
11403
11704
|
resolveConnect() {
|
|
@@ -11424,9 +11725,9 @@ var DexWsClient = class {
|
|
|
11424
11725
|
this.pendingCommands.clear();
|
|
11425
11726
|
}
|
|
11426
11727
|
emit(event, ...args) {
|
|
11427
|
-
const set = this.listeners
|
|
11728
|
+
const set = this.listeners[event];
|
|
11428
11729
|
if (!set) return;
|
|
11429
|
-
for (const fn of set) {
|
|
11730
|
+
for (const fn of [...set]) {
|
|
11430
11731
|
try {
|
|
11431
11732
|
fn(...args);
|
|
11432
11733
|
} catch {
|
|
@@ -13900,75 +14201,6 @@ async function resolveEvmFeesPerGas(network, options) {
|
|
|
13900
14201
|
|
|
13901
14202
|
// src/eip712/signers.ts
|
|
13902
14203
|
import { privateKeyToAccount } from "viem/accounts";
|
|
13903
|
-
|
|
13904
|
-
// src/utils/address.ts
|
|
13905
|
-
import { fromBech32, toBech32, fromHex, toHex } from "@cosmjs/encoding";
|
|
13906
|
-
import { getAddress, isAddress } from "viem";
|
|
13907
|
-
function evmToAult(evmAddress) {
|
|
13908
|
-
const hex = evmAddress.toLowerCase().replace("0x", "");
|
|
13909
|
-
const bytes = fromHex(hex);
|
|
13910
|
-
return toBech32("ault", bytes);
|
|
13911
|
-
}
|
|
13912
|
-
function aultToEvm(aultAddress) {
|
|
13913
|
-
const { data } = fromBech32(aultAddress);
|
|
13914
|
-
return getAddress(`0x${toHex(data)}`);
|
|
13915
|
-
}
|
|
13916
|
-
function isValidAultAddress(address) {
|
|
13917
|
-
try {
|
|
13918
|
-
const { prefix, data } = fromBech32(address);
|
|
13919
|
-
return prefix === "ault" && data.length === 20;
|
|
13920
|
-
} catch {
|
|
13921
|
-
return false;
|
|
13922
|
-
}
|
|
13923
|
-
}
|
|
13924
|
-
function isValidEvmAddress(address) {
|
|
13925
|
-
if (!/^0[xX][a-fA-F0-9]{40}$/.test(address)) return false;
|
|
13926
|
-
const normalized = `0x${address.slice(2)}`;
|
|
13927
|
-
const body = normalized.slice(2);
|
|
13928
|
-
const allLower = body === body.toLowerCase();
|
|
13929
|
-
const allUpper = body === body.toUpperCase();
|
|
13930
|
-
if (allLower || allUpper) return true;
|
|
13931
|
-
return isAddress(normalized, { strict: true });
|
|
13932
|
-
}
|
|
13933
|
-
function addressEquals(a, b) {
|
|
13934
|
-
const ab = addressBytes(a);
|
|
13935
|
-
const bb = addressBytes(b);
|
|
13936
|
-
if (!ab || !bb || ab.length !== bb.length) return false;
|
|
13937
|
-
for (let i = 0; i < ab.length; i++) {
|
|
13938
|
-
if (ab[i] !== bb[i]) return false;
|
|
13939
|
-
}
|
|
13940
|
-
return true;
|
|
13941
|
-
}
|
|
13942
|
-
function addressBytes(s) {
|
|
13943
|
-
if (!s) return null;
|
|
13944
|
-
if (s.startsWith("0x") || s.startsWith("0X")) {
|
|
13945
|
-
const body = s.slice(2);
|
|
13946
|
-
if (!/^[a-fA-F0-9]+$/.test(body) || body.length !== 40) return null;
|
|
13947
|
-
return fromHex(body.toLowerCase());
|
|
13948
|
-
}
|
|
13949
|
-
try {
|
|
13950
|
-
const { data } = fromBech32(s);
|
|
13951
|
-
return data;
|
|
13952
|
-
} catch {
|
|
13953
|
-
return null;
|
|
13954
|
-
}
|
|
13955
|
-
}
|
|
13956
|
-
function isValidValidatorAddress(address) {
|
|
13957
|
-
try {
|
|
13958
|
-
const { prefix, data } = fromBech32(address);
|
|
13959
|
-
return prefix === "aultvaloper" && data.length === 20;
|
|
13960
|
-
} catch {
|
|
13961
|
-
return false;
|
|
13962
|
-
}
|
|
13963
|
-
}
|
|
13964
|
-
function assertValidatorAddress(address) {
|
|
13965
|
-
if (isValidValidatorAddress(address)) {
|
|
13966
|
-
return address;
|
|
13967
|
-
}
|
|
13968
|
-
throw new Error(`Invalid validator address format: ${address}`);
|
|
13969
|
-
}
|
|
13970
|
-
|
|
13971
|
-
// src/eip712/signers.ts
|
|
13972
14204
|
var AULT_SIGNER_MARKER = /* @__PURE__ */ Symbol.for("ault-sdk:signer");
|
|
13973
14205
|
function isAultSigner(obj) {
|
|
13974
14206
|
return typeof obj === "object" && obj !== null && AULT_SIGNER_MARKER in obj && obj[AULT_SIGNER_MARKER] === true;
|
|
@@ -21787,13 +22019,16 @@ function createClobTxApi({ signerAddress, exec, getGas }) {
|
|
|
21787
22019
|
},
|
|
21788
22020
|
async cancelOrders({ orderIds, gasLimit, feeAmount, memo, confirmationTimeoutMs }) {
|
|
21789
22021
|
const gas = getGas();
|
|
21790
|
-
|
|
21791
|
-
|
|
22022
|
+
const cancelGas = gasLimit ?? (BigInt(
|
|
22023
|
+
computeFeeFreeGasCompound(
|
|
21792
22024
|
gas.CANCEL_ORDER_BASE,
|
|
21793
22025
|
gas.CANCEL_ORDER_PER_ID,
|
|
21794
22026
|
orderIds.length,
|
|
21795
22027
|
gas.FEE_FREE_GAS_MULTIPLIER
|
|
21796
|
-
)
|
|
22028
|
+
)
|
|
22029
|
+
) + BigInt(gas.EIP712_ANTE_GAS)).toString();
|
|
22030
|
+
return exec([buildCancelOrdersMsg(signerAddress, orderIds)], {
|
|
22031
|
+
gasLimit: cancelGas,
|
|
21797
22032
|
feeAmount,
|
|
21798
22033
|
memo,
|
|
21799
22034
|
confirmationTimeoutMs
|
|
@@ -22884,6 +23119,7 @@ function applyGasOverrides(gas) {
|
|
|
22884
23119
|
EIP712_FEE_AMOUNT: _ignoredFee,
|
|
22885
23120
|
EIP712_GAS_LIMIT: _ignoredGas,
|
|
22886
23121
|
DENOM: _ignoredDenom,
|
|
23122
|
+
EIP712_ANTE_GAS: _ignoredAnte,
|
|
22887
23123
|
...rest
|
|
22888
23124
|
} = gas;
|
|
22889
23125
|
const safeRest = {};
|
|
@@ -22994,7 +23230,7 @@ function createStaticResolver(optsOrNetwork, legacyManifest) {
|
|
|
22994
23230
|
// src/agent/attach.ts
|
|
22995
23231
|
import { secp256k1 as secp256k12 } from "@noble/curves/secp256k1";
|
|
22996
23232
|
import { keccak256 as keccak2562, hexToBytes as hexToBytes3 } from "viem";
|
|
22997
|
-
import { toHex as
|
|
23233
|
+
import { toHex as toHex3 } from "@cosmjs/encoding";
|
|
22998
23234
|
|
|
22999
23235
|
// src/agent/sign-and-broadcast.ts
|
|
23000
23236
|
import { secp256k1 } from "@noble/curves/secp256k1";
|
|
@@ -23251,7 +23487,7 @@ function deriveAgentAddress(privateKeyHex) {
|
|
|
23251
23487
|
const uncompressedPubkey = secp256k12.getPublicKey(privKeyBytes, false);
|
|
23252
23488
|
const pubkeyHash = hexToBytes3(keccak2562(uncompressedPubkey.slice(1)));
|
|
23253
23489
|
const evmAddressBytes = pubkeyHash.slice(12);
|
|
23254
|
-
const evmHex = `0x${
|
|
23490
|
+
const evmHex = `0x${toHex3(evmAddressBytes)}`;
|
|
23255
23491
|
return evmToAult(evmHex);
|
|
23256
23492
|
}
|
|
23257
23493
|
function buildAgentSurface(options, ctx) {
|
|
@@ -24129,7 +24365,7 @@ function createDexWs(url, options) {
|
|
|
24129
24365
|
}
|
|
24130
24366
|
|
|
24131
24367
|
// src/index.ts
|
|
24132
|
-
var SDK_VERSION = "0.0.2-alpha.
|
|
24368
|
+
var SDK_VERSION = "0.0.2-alpha.21";
|
|
24133
24369
|
export {
|
|
24134
24370
|
AGENT_DEFAULTS,
|
|
24135
24371
|
AULT_SIGNER_MARKER,
|
|
@@ -24219,6 +24455,7 @@ export {
|
|
|
24219
24455
|
WebSocketError,
|
|
24220
24456
|
WithdrawRewardsPartialError,
|
|
24221
24457
|
_resetIndexingCache,
|
|
24458
|
+
addressBytes,
|
|
24222
24459
|
addressEquals,
|
|
24223
24460
|
applyGasOverrides,
|
|
24224
24461
|
applyNetworkOverrides,
|