@continuumdao/ctm-mpc-defi 0.2.22 → 0.2.25

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.
Files changed (43) hide show
  1. package/dist/agent/catalog.cjs +806 -18
  2. package/dist/agent/catalog.cjs.map +1 -1
  3. package/dist/agent/catalog.d.ts +2400 -43
  4. package/dist/agent/catalog.js +772 -19
  5. package/dist/agent/catalog.js.map +1 -1
  6. package/dist/agent/skills/arcus/SKILL.md +72 -0
  7. package/dist/agent/skills/gmx/SKILL.md +32 -0
  8. package/dist/agent/skills/hyperliquid/SKILL.md +44 -3
  9. package/dist/agent/skills/uniswap-v4/SKILL.md +31 -1
  10. package/dist/core/index.cjs +55 -0
  11. package/dist/core/index.cjs.map +1 -1
  12. package/dist/core/index.d.ts +15 -21
  13. package/dist/core/index.js +54 -1
  14. package/dist/core/index.js.map +1 -1
  15. package/dist/eip712Multisign-xhXpUYp3.d.ts +36 -0
  16. package/dist/index.cjs +399 -12
  17. package/dist/index.cjs.map +1 -1
  18. package/dist/index.d.ts +2 -1
  19. package/dist/index.js +398 -13
  20. package/dist/index.js.map +1 -1
  21. package/dist/protocols/evm/arcus/index.cjs +2061 -0
  22. package/dist/protocols/evm/arcus/index.cjs.map +1 -0
  23. package/dist/protocols/evm/arcus/index.d.ts +726 -0
  24. package/dist/protocols/evm/arcus/index.js +1964 -0
  25. package/dist/protocols/evm/arcus/index.js.map +1 -0
  26. package/dist/protocols/evm/gmx/index.cjs +39 -0
  27. package/dist/protocols/evm/gmx/index.cjs.map +1 -1
  28. package/dist/protocols/evm/gmx/index.d.ts +21 -1
  29. package/dist/protocols/evm/gmx/index.js +38 -1
  30. package/dist/protocols/evm/gmx/index.js.map +1 -1
  31. package/dist/protocols/evm/hyperliquid/index.cjs +478 -208
  32. package/dist/protocols/evm/hyperliquid/index.cjs.map +1 -1
  33. package/dist/protocols/evm/hyperliquid/index.d.ts +166 -51
  34. package/dist/protocols/evm/hyperliquid/index.js +467 -210
  35. package/dist/protocols/evm/hyperliquid/index.js.map +1 -1
  36. package/dist/protocols/evm/permit2/index.cjs.map +1 -1
  37. package/dist/protocols/evm/permit2/index.js.map +1 -1
  38. package/dist/protocols/evm/uniswap-v4/index.cjs +734 -15
  39. package/dist/protocols/evm/uniswap-v4/index.cjs.map +1 -1
  40. package/dist/protocols/evm/uniswap-v4/index.d.ts +189 -17
  41. package/dist/protocols/evm/uniswap-v4/index.js +698 -16
  42. package/dist/protocols/evm/uniswap-v4/index.js.map +1 -1
  43. package/package.json +6 -1
@@ -348,6 +348,12 @@ async function hyperliquidFetchOpenOrders(args) {
348
348
  const orders = await hyperliquidInfoPost(args.chainId, req);
349
349
  return { orders: orders ?? [] };
350
350
  }
351
+ async function hyperliquidFetchFrontendOpenOrders(args) {
352
+ const req = { type: "frontendOpenOrders", user: args.user };
353
+ if (args.dex?.trim()) req.dex = args.dex.trim();
354
+ const orders = await hyperliquidInfoPost(args.chainId, req);
355
+ return { orders: orders ?? [] };
356
+ }
351
357
  async function hyperliquidFetchUserVaultEquities(args) {
352
358
  const rows = await hyperliquidInfoPost(args.chainId, {
353
359
  type: "userVaultEquities",
@@ -1159,8 +1165,404 @@ async function buildEvmMultisignBatch(args) {
1159
1165
  }
1160
1166
  return result;
1161
1167
  }
1168
+ var EIP712_SIGN_REQUEST_KIND = "eip712";
1169
+ function msgHashNo0x(digest) {
1170
+ return digest.startsWith("0x") ? digest.slice(2) : digest;
1171
+ }
1172
+ function eip712SignatureText(domain, primaryType) {
1173
+ return JSON.stringify({
1174
+ kind: "EIP-712",
1175
+ ...typeof domain.name === "string" ? { name: domain.name } : {},
1176
+ primaryType
1177
+ });
1178
+ }
1179
+ function jsonStringifyForAudit(value) {
1180
+ return JSON.stringify(value, (_, v) => typeof v === "bigint" ? v.toString() : v);
1181
+ }
1182
+ function eip712MsgRawHex(typedData, delivery) {
1183
+ const envelope = {
1184
+ version: 1,
1185
+ ...typedData,
1186
+ delivery
1187
+ };
1188
+ return viem.stringToHex(jsonStringifyForAudit(envelope));
1189
+ }
1190
+ function buildEip712MultisignBody(args) {
1191
+ const { typedData, delivery } = args;
1192
+ const digest = viem.hashTypedData({
1193
+ domain: typedData.domain,
1194
+ types: typedData.types,
1195
+ primaryType: typedData.primaryType,
1196
+ message: typedData.message
1197
+ });
1198
+ const extraJSON = {
1199
+ signRequestKind: EIP712_SIGN_REQUEST_KIND,
1200
+ eip712: {
1201
+ version: 1,
1202
+ digest,
1203
+ domain: typedData.domain,
1204
+ types: typedData.types,
1205
+ primaryType: typedData.primaryType,
1206
+ message: typedData.message
1207
+ },
1208
+ delivery
1209
+ };
1210
+ return finalizeMultisign({
1211
+ keyGen: args.keyGen,
1212
+ purposeText: args.purposeText,
1213
+ purposeSuffix: args.purposeSuffix,
1214
+ destinationChainID: args.destinationChainID,
1215
+ destinationAddress: args.destinationAddress,
1216
+ extraJSON,
1217
+ expiryDate: args.expiryDate,
1218
+ legs: [
1219
+ {
1220
+ msgHash: msgHashNo0x(digest),
1221
+ msgRaw: eip712MsgRawHex(typedData, delivery),
1222
+ destinationAddress: args.destinationAddress,
1223
+ signatureText: eip712SignatureText(typedData.domain, typedData.primaryType),
1224
+ audit: {
1225
+ kind: "EIP712",
1226
+ primaryType: typedData.primaryType,
1227
+ deliveryKind: delivery.kind,
1228
+ ...args.audit ?? {}
1229
+ },
1230
+ feeSnapshot: {}
1231
+ }
1232
+ ]
1233
+ });
1234
+ }
1235
+ var EXCHANGE_DOMAIN = {
1236
+ name: "Exchange",
1237
+ version: "1",
1238
+ chainId: 1337,
1239
+ verifyingContract: "0x0000000000000000000000000000000000000000"
1240
+ };
1241
+ var AGENT_TYPES = {
1242
+ Agent: [
1243
+ { name: "source", type: "string" },
1244
+ { name: "connectionId", type: "bytes32" }
1245
+ ]
1246
+ };
1247
+ function adjustMsgpackValue(value) {
1248
+ if (Array.isArray(value)) return value.map(adjustMsgpackValue);
1249
+ if (typeof value === "object" && value !== null) {
1250
+ const result = {};
1251
+ for (const key of Object.keys(value)) {
1252
+ const entry = value[key];
1253
+ if (entry !== void 0) result[key] = adjustMsgpackValue(entry);
1254
+ }
1255
+ return result;
1256
+ }
1257
+ if (typeof value === "number" && Number.isInteger(value) && (value >= 4294967296 || value < -2147483648)) {
1258
+ return BigInt(value);
1259
+ }
1260
+ return value;
1261
+ }
1262
+ function toUint64Bytes(n) {
1263
+ const bytes = new Uint8Array(8);
1264
+ new DataView(bytes.buffer).setBigUint64(0, BigInt(n));
1265
+ return bytes;
1266
+ }
1267
+ function concatBytes(...parts) {
1268
+ const total = parts.reduce((sum, p) => sum + p.length, 0);
1269
+ const out = new Uint8Array(total);
1270
+ let offset = 0;
1271
+ for (const p of parts) {
1272
+ out.set(p, offset);
1273
+ offset += p.length;
1274
+ }
1275
+ return out;
1276
+ }
1277
+ function hexToBytes(hex) {
1278
+ const h = hex.startsWith("0x") ? hex.slice(2) : hex;
1279
+ const bytes = new Uint8Array(h.length / 2);
1280
+ for (let i = 0; i < bytes.length; i++) {
1281
+ bytes[i] = parseInt(h.slice(i * 2, i * 2 + 2), 16);
1282
+ }
1283
+ return bytes;
1284
+ }
1285
+ function createHyperliquidL1ActionHash(args) {
1286
+ const { action, nonce, vaultAddress, expiresAfter } = args;
1287
+ const actionBytes = msgpack.encode(adjustMsgpackValue(action));
1288
+ const nonceBytes = toUint64Bytes(nonce);
1289
+ const vaultMarker = vaultAddress ? new Uint8Array([1]) : new Uint8Array([0]);
1290
+ const vaultBytes = vaultAddress ? hexToBytes(vaultAddress) : new Uint8Array();
1291
+ const expiresMarker = expiresAfter !== void 0 ? new Uint8Array([0]) : new Uint8Array();
1292
+ const expiresBytes = expiresAfter !== void 0 ? toUint64Bytes(expiresAfter) : new Uint8Array();
1293
+ const bytes = concatBytes(actionBytes, nonceBytes, vaultMarker, vaultBytes, expiresMarker, expiresBytes);
1294
+ return viem.keccak256(bytes);
1295
+ }
1296
+ function buildHyperliquidAgentTypedData(args) {
1297
+ const connectionId = createHyperliquidL1ActionHash(args);
1298
+ const source = args.isTestnet ? "b" : "a";
1299
+ return {
1300
+ domain: EXCHANGE_DOMAIN,
1301
+ types: AGENT_TYPES,
1302
+ primaryType: "Agent",
1303
+ message: {
1304
+ source,
1305
+ connectionId
1306
+ },
1307
+ connectionId,
1308
+ nonce: args.nonce,
1309
+ action: args.action
1310
+ };
1311
+ }
1312
+ function buildHyperliquidUpdateLeverageAction(args) {
1313
+ const leverage = Math.trunc(args.leverage);
1314
+ if (!Number.isFinite(leverage) || leverage < 1) {
1315
+ throw new Error("leverage must be a positive integer");
1316
+ }
1317
+ return {
1318
+ type: "updateLeverage",
1319
+ asset: args.asset,
1320
+ isCross: args.isCross !== false,
1321
+ leverage
1322
+ };
1323
+ }
1324
+ function hyperliquidFloatToWire(value) {
1325
+ const n = typeof value === "number" ? value : Number.parseFloat(String(value).trim());
1326
+ if (!Number.isFinite(n)) throw new Error("Invalid Hyperliquid wire number.");
1327
+ const rounded = n.toFixed(8);
1328
+ if (Math.abs(Number.parseFloat(rounded) - n) >= 1e-12) {
1329
+ throw new Error(`Hyperliquid wire value ${value} has too many decimal places.`);
1330
+ }
1331
+ const normalized = Number(rounded === "-0" ? "0" : rounded);
1332
+ return String(normalized);
1333
+ }
1334
+ function hyperliquidL1TifFromKey(tif) {
1335
+ const key = String(tif ?? "gtc").trim().toLowerCase();
1336
+ if (key === "alo") return "Alo";
1337
+ if (key === "ioc") return "Ioc";
1338
+ if (key === "gtc") return "Gtc";
1339
+ throw new Error(`Unknown time-in-force: ${tif}. Use alo, gtc, or ioc.`);
1340
+ }
1341
+ function buildHyperliquidLimitOrderWire(args) {
1342
+ return {
1343
+ a: args.asset,
1344
+ b: args.isBuy,
1345
+ p: hyperliquidFloatToWire(args.limitPxHuman),
1346
+ s: hyperliquidFloatToWire(args.szHuman),
1347
+ r: Boolean(args.reduceOnly),
1348
+ t: { limit: { tif: hyperliquidL1TifFromKey(args.tif) } }
1349
+ };
1350
+ }
1351
+ function buildHyperliquidTriggerOrderWire(args) {
1352
+ const mode = args.tpslExecMode ?? "limit_at_trigger";
1353
+ const triggerPx = hyperliquidFloatToWire(args.triggerPxHuman);
1354
+ const limitPx = mode === "market" ? triggerPx : hyperliquidFloatToWire(args.limitPxHuman ?? args.triggerPxHuman);
1355
+ return {
1356
+ a: args.asset,
1357
+ b: args.isBuy,
1358
+ p: limitPx,
1359
+ s: hyperliquidFloatToWire(args.szHuman),
1360
+ r: true,
1361
+ t: {
1362
+ trigger: {
1363
+ isMarket: mode === "market",
1364
+ triggerPx,
1365
+ tpsl: args.tpsl
1366
+ }
1367
+ }
1368
+ };
1369
+ }
1370
+ function validateHyperliquidTpslGeometry(args) {
1371
+ const entry = Number.parseFloat(String(args.entryPxHuman).trim());
1372
+ const tp = args.takeProfitTriggerPxHuman?.trim() != null && args.takeProfitTriggerPxHuman.trim() !== "" ? Number.parseFloat(args.takeProfitTriggerPxHuman.trim()) : null;
1373
+ const sl = args.stopLossTriggerPxHuman?.trim() != null && args.stopLossTriggerPxHuman.trim() !== "" ? Number.parseFloat(args.stopLossTriggerPxHuman.trim()) : null;
1374
+ if (!Number.isFinite(entry)) throw new Error("Invalid entry limit price for TP/SL validation.");
1375
+ if (tp != null && !Number.isFinite(tp)) throw new Error("Invalid take-profit trigger price.");
1376
+ if (sl != null && !Number.isFinite(sl)) throw new Error("Invalid stop-loss trigger price.");
1377
+ if (args.isBuy) {
1378
+ if (sl != null && sl >= entry) {
1379
+ throw new Error("Long stop-loss must be below entry price.");
1380
+ }
1381
+ if (tp != null && tp <= entry) {
1382
+ throw new Error("Long take-profit must be above entry price.");
1383
+ }
1384
+ } else {
1385
+ if (sl != null && sl <= entry) {
1386
+ throw new Error("Short stop-loss must be above entry price.");
1387
+ }
1388
+ if (tp != null && tp >= entry) {
1389
+ throw new Error("Short take-profit must be below entry price.");
1390
+ }
1391
+ }
1392
+ }
1393
+ function buildHyperliquidNormalTpslOrderWires(args) {
1394
+ validateHyperliquidTpslGeometry({
1395
+ isBuy: args.isBuy,
1396
+ entryPxHuman: args.limitPxHuman,
1397
+ takeProfitTriggerPxHuman: args.takeProfitTriggerPxHuman,
1398
+ stopLossTriggerPxHuman: args.stopLossTriggerPxHuman
1399
+ });
1400
+ const orders = [
1401
+ buildHyperliquidLimitOrderWire({
1402
+ asset: args.asset,
1403
+ isBuy: args.isBuy,
1404
+ limitPxHuman: args.limitPxHuman,
1405
+ szHuman: args.szHuman,
1406
+ tif: args.tif
1407
+ })
1408
+ ];
1409
+ const closeIsBuy = !args.isBuy;
1410
+ const tp = args.takeProfitTriggerPxHuman?.trim();
1411
+ const sl = args.stopLossTriggerPxHuman?.trim();
1412
+ if (tp) {
1413
+ orders.push(
1414
+ buildHyperliquidTriggerOrderWire({
1415
+ asset: args.asset,
1416
+ isBuy: closeIsBuy,
1417
+ szHuman: args.szHuman,
1418
+ triggerPxHuman: tp,
1419
+ tpsl: "tp",
1420
+ tpslExecMode: args.tpslExecMode
1421
+ })
1422
+ );
1423
+ }
1424
+ if (sl) {
1425
+ orders.push(
1426
+ buildHyperliquidTriggerOrderWire({
1427
+ asset: args.asset,
1428
+ isBuy: closeIsBuy,
1429
+ szHuman: args.szHuman,
1430
+ triggerPxHuman: sl,
1431
+ tpsl: "sl",
1432
+ tpslExecMode: args.tpslExecMode
1433
+ })
1434
+ );
1435
+ }
1436
+ return orders;
1437
+ }
1438
+ function buildHyperliquidOrderAction(args) {
1439
+ return {
1440
+ type: "order",
1441
+ orders: args.orders,
1442
+ grouping: args.grouping ?? "na"
1443
+ };
1444
+ }
1445
+
1446
+ // src/protocols/evm/hyperliquid/exchangeMultisign.ts
1447
+ var ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
1448
+ async function buildHyperliquidLimitOrderWithTpslMultisign(args) {
1449
+ const marketKind = args.marketKind ?? "perp";
1450
+ const { asset } = await hyperliquidResolveAsset({
1451
+ chainId: args.chainId,
1452
+ coin: args.coin,
1453
+ marketKind,
1454
+ dex: args.dex
1455
+ });
1456
+ const tp = args.takeProfitTriggerPxHuman?.trim();
1457
+ const sl = args.stopLossTriggerPxHuman?.trim();
1458
+ if (!tp && !sl) {
1459
+ throw new Error("At least one of takeProfitTriggerPxHuman or stopLossTriggerPxHuman is required for bracket orders.");
1460
+ }
1461
+ const orderWires = buildHyperliquidNormalTpslOrderWires({
1462
+ asset,
1463
+ isBuy: args.isBuy,
1464
+ limitPxHuman: args.limitPxHuman,
1465
+ szHuman: args.szHuman,
1466
+ tif: args.tif,
1467
+ takeProfitTriggerPxHuman: tp || void 0,
1468
+ stopLossTriggerPxHuman: sl || void 0,
1469
+ tpslExecMode: args.tpslExecMode
1470
+ });
1471
+ const action = buildHyperliquidOrderAction({
1472
+ orders: orderWires,
1473
+ grouping: "normalTpsl"
1474
+ });
1475
+ const nonce = args.nonce ?? Date.now();
1476
+ const isTestnet = args.chainId === 998;
1477
+ const typed = buildHyperliquidAgentTypedData({ action, nonce, isTestnet });
1478
+ return buildEip712MultisignBody({
1479
+ keyGen: args.keyGen,
1480
+ purposeText: args.purposeText,
1481
+ destinationChainID: String(args.chainId),
1482
+ destinationAddress: ZERO_ADDRESS,
1483
+ typedData: {
1484
+ domain: typed.domain,
1485
+ types: typed.types,
1486
+ primaryType: typed.primaryType,
1487
+ message: typed.message
1488
+ },
1489
+ delivery: {
1490
+ kind: "hyperliquid_exchange",
1491
+ chainId: args.chainId,
1492
+ isTestnet,
1493
+ action,
1494
+ nonce
1495
+ },
1496
+ audit: {
1497
+ protocol: "hyperliquid",
1498
+ action: "limitOrderWithTpsl",
1499
+ coin: args.coin,
1500
+ marketKind,
1501
+ asset,
1502
+ isBuy: args.isBuy,
1503
+ limitPxHuman: args.limitPxHuman,
1504
+ szHuman: args.szHuman,
1505
+ takeProfitTriggerPxHuman: tp ?? null,
1506
+ stopLossTriggerPxHuman: sl ?? null,
1507
+ tpslExecMode: args.tpslExecMode ?? "limit_at_trigger",
1508
+ grouping: "normalTpsl",
1509
+ nonce,
1510
+ connectionId: typed.connectionId
1511
+ }
1512
+ });
1513
+ }
1514
+ async function buildHyperliquidUpdateLeverageMultisign(args) {
1515
+ const marketKind = args.marketKind ?? "perp";
1516
+ const { asset } = await hyperliquidResolveAsset({
1517
+ chainId: args.chainId,
1518
+ coin: args.coin,
1519
+ marketKind,
1520
+ dex: args.dex
1521
+ });
1522
+ const action = buildHyperliquidUpdateLeverageAction({
1523
+ asset,
1524
+ leverage: args.leverage,
1525
+ isCross: args.isCross
1526
+ });
1527
+ const nonce = args.nonce ?? Date.now();
1528
+ const isTestnet = args.chainId === 998;
1529
+ const typed = buildHyperliquidAgentTypedData({ action, nonce, isTestnet });
1530
+ return buildEip712MultisignBody({
1531
+ keyGen: args.keyGen,
1532
+ purposeText: args.purposeText,
1533
+ destinationChainID: String(args.chainId),
1534
+ destinationAddress: ZERO_ADDRESS,
1535
+ typedData: {
1536
+ domain: typed.domain,
1537
+ types: typed.types,
1538
+ primaryType: typed.primaryType,
1539
+ message: typed.message
1540
+ },
1541
+ delivery: {
1542
+ kind: "hyperliquid_exchange",
1543
+ chainId: args.chainId,
1544
+ isTestnet,
1545
+ action,
1546
+ nonce
1547
+ },
1548
+ audit: {
1549
+ protocol: "hyperliquid",
1550
+ action: "updateLeverage",
1551
+ coin: args.coin,
1552
+ marketKind,
1553
+ asset,
1554
+ leverage: action.leverage,
1555
+ isCross: action.isCross,
1556
+ nonce,
1557
+ connectionId: typed.connectionId
1558
+ }
1559
+ });
1560
+ }
1162
1561
 
1163
1562
  // src/protocols/evm/hyperliquid/multisign.ts
1563
+ function hasHyperliquidBracketTpsl(args) {
1564
+ return Boolean(args.takeProfitTriggerPxHuman?.trim() || args.stopLossTriggerPxHuman?.trim());
1565
+ }
1164
1566
  async function buildCoreWriterMultisign(common, data, signatureMeta) {
1165
1567
  const step = coreWriterStep(data);
1166
1568
  return buildEvmMultisignBatch({
@@ -1185,6 +1587,26 @@ async function buildCoreWriterMultisign(common, data, signatureMeta) {
1185
1587
  });
1186
1588
  }
1187
1589
  async function buildEvmMultisignBodyHyperliquidLimitOrderBatch(args) {
1590
+ if (hasHyperliquidBracketTpsl(args)) {
1591
+ if (args.reduceOnly) {
1592
+ throw new Error("reduceOnly cannot be combined with takeProfitTriggerPxHuman or stopLossTriggerPxHuman.");
1593
+ }
1594
+ return buildHyperliquidLimitOrderWithTpslMultisign({
1595
+ keyGen: args.keyGen,
1596
+ chainId: args.chainId,
1597
+ purposeText: args.purposeText,
1598
+ coin: args.coin,
1599
+ marketKind: args.marketKind,
1600
+ dex: args.dex,
1601
+ isBuy: args.isBuy,
1602
+ limitPxHuman: args.limitPxHuman,
1603
+ szHuman: args.szHuman,
1604
+ tif: args.tif,
1605
+ takeProfitTriggerPxHuman: args.takeProfitTriggerPxHuman,
1606
+ stopLossTriggerPxHuman: args.stopLossTriggerPxHuman,
1607
+ tpslExecMode: args.tpslExecMode
1608
+ });
1609
+ }
1188
1610
  const marketKind = args.marketKind ?? "perp";
1189
1611
  const { asset } = await hyperliquidResolveAsset({
1190
1612
  chainId: args.chainId,
@@ -1334,212 +1756,6 @@ async function buildEvmMultisignBodyHyperliquidDelegateOnlyBatch(args) {
1334
1756
  async function buildEvmMultisignBodyHyperliquidUndelegateBatch(args) {
1335
1757
  return buildEvmMultisignBodyHyperliquidDelegateBatch({ ...args, isUndelegate: true });
1336
1758
  }
1337
- var EXCHANGE_DOMAIN = {
1338
- name: "Exchange",
1339
- version: "1",
1340
- chainId: 1337,
1341
- verifyingContract: "0x0000000000000000000000000000000000000000"
1342
- };
1343
- var AGENT_TYPES = {
1344
- Agent: [
1345
- { name: "source", type: "string" },
1346
- { name: "connectionId", type: "bytes32" }
1347
- ]
1348
- };
1349
- function adjustMsgpackValue(value) {
1350
- if (Array.isArray(value)) return value.map(adjustMsgpackValue);
1351
- if (typeof value === "object" && value !== null) {
1352
- const result = {};
1353
- for (const key of Object.keys(value)) {
1354
- const entry = value[key];
1355
- if (entry !== void 0) result[key] = adjustMsgpackValue(entry);
1356
- }
1357
- return result;
1358
- }
1359
- if (typeof value === "number" && Number.isInteger(value) && (value >= 4294967296 || value < -2147483648)) {
1360
- return BigInt(value);
1361
- }
1362
- return value;
1363
- }
1364
- function toUint64Bytes(n) {
1365
- const bytes = new Uint8Array(8);
1366
- new DataView(bytes.buffer).setBigUint64(0, BigInt(n));
1367
- return bytes;
1368
- }
1369
- function concatBytes(...parts) {
1370
- const total = parts.reduce((sum, p) => sum + p.length, 0);
1371
- const out = new Uint8Array(total);
1372
- let offset = 0;
1373
- for (const p of parts) {
1374
- out.set(p, offset);
1375
- offset += p.length;
1376
- }
1377
- return out;
1378
- }
1379
- function hexToBytes(hex) {
1380
- const h = hex.startsWith("0x") ? hex.slice(2) : hex;
1381
- const bytes = new Uint8Array(h.length / 2);
1382
- for (let i = 0; i < bytes.length; i++) {
1383
- bytes[i] = parseInt(h.slice(i * 2, i * 2 + 2), 16);
1384
- }
1385
- return bytes;
1386
- }
1387
- function createHyperliquidL1ActionHash(args) {
1388
- const { action, nonce, vaultAddress, expiresAfter } = args;
1389
- const actionBytes = msgpack.encode(adjustMsgpackValue(action));
1390
- const nonceBytes = toUint64Bytes(nonce);
1391
- const vaultMarker = vaultAddress ? new Uint8Array([1]) : new Uint8Array([0]);
1392
- const vaultBytes = vaultAddress ? hexToBytes(vaultAddress) : new Uint8Array();
1393
- const expiresMarker = expiresAfter !== void 0 ? new Uint8Array([0]) : new Uint8Array();
1394
- const expiresBytes = expiresAfter !== void 0 ? toUint64Bytes(expiresAfter) : new Uint8Array();
1395
- const bytes = concatBytes(actionBytes, nonceBytes, vaultMarker, vaultBytes, expiresMarker, expiresBytes);
1396
- return viem.keccak256(bytes);
1397
- }
1398
- function buildHyperliquidAgentTypedData(args) {
1399
- const connectionId = createHyperliquidL1ActionHash(args);
1400
- const source = args.isTestnet ? "b" : "a";
1401
- return {
1402
- domain: EXCHANGE_DOMAIN,
1403
- types: AGENT_TYPES,
1404
- primaryType: "Agent",
1405
- message: {
1406
- source,
1407
- connectionId
1408
- },
1409
- connectionId,
1410
- nonce: args.nonce,
1411
- action: args.action
1412
- };
1413
- }
1414
- function buildHyperliquidUpdateLeverageAction(args) {
1415
- const leverage = Math.trunc(args.leverage);
1416
- if (!Number.isFinite(leverage) || leverage < 1) {
1417
- throw new Error("leverage must be a positive integer");
1418
- }
1419
- return {
1420
- type: "updateLeverage",
1421
- asset: args.asset,
1422
- isCross: args.isCross !== false,
1423
- leverage
1424
- };
1425
- }
1426
- var EIP712_SIGN_REQUEST_KIND = "eip712";
1427
- function msgHashNo0x(digest) {
1428
- return digest.startsWith("0x") ? digest.slice(2) : digest;
1429
- }
1430
- function eip712SignatureText(domain, primaryType) {
1431
- return JSON.stringify({
1432
- kind: "EIP-712",
1433
- ...typeof domain.name === "string" ? { name: domain.name } : {},
1434
- primaryType
1435
- });
1436
- }
1437
- function jsonStringifyForAudit(value) {
1438
- return JSON.stringify(value, (_, v) => typeof v === "bigint" ? v.toString() : v);
1439
- }
1440
- function eip712MsgRawHex(typedData, delivery) {
1441
- const envelope = {
1442
- version: 1,
1443
- ...typedData,
1444
- delivery
1445
- };
1446
- return viem.stringToHex(jsonStringifyForAudit(envelope));
1447
- }
1448
- function buildEip712MultisignBody(args) {
1449
- const { typedData, delivery } = args;
1450
- const digest = viem.hashTypedData({
1451
- domain: typedData.domain,
1452
- types: typedData.types,
1453
- primaryType: typedData.primaryType,
1454
- message: typedData.message
1455
- });
1456
- const extraJSON = {
1457
- signRequestKind: EIP712_SIGN_REQUEST_KIND,
1458
- eip712: {
1459
- version: 1,
1460
- digest,
1461
- domain: typedData.domain,
1462
- types: typedData.types,
1463
- primaryType: typedData.primaryType,
1464
- message: typedData.message
1465
- },
1466
- delivery
1467
- };
1468
- return finalizeMultisign({
1469
- keyGen: args.keyGen,
1470
- purposeText: args.purposeText,
1471
- purposeSuffix: args.purposeSuffix,
1472
- destinationChainID: args.destinationChainID,
1473
- destinationAddress: args.destinationAddress,
1474
- extraJSON,
1475
- expiryDate: args.expiryDate,
1476
- legs: [
1477
- {
1478
- msgHash: msgHashNo0x(digest),
1479
- msgRaw: eip712MsgRawHex(typedData, delivery),
1480
- destinationAddress: args.destinationAddress,
1481
- signatureText: eip712SignatureText(typedData.domain, typedData.primaryType),
1482
- audit: {
1483
- kind: "EIP712",
1484
- primaryType: typedData.primaryType,
1485
- deliveryKind: delivery.kind,
1486
- ...args.audit ?? {}
1487
- },
1488
- feeSnapshot: {}
1489
- }
1490
- ]
1491
- });
1492
- }
1493
-
1494
- // src/protocols/evm/hyperliquid/exchangeMultisign.ts
1495
- var ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
1496
- async function buildHyperliquidUpdateLeverageMultisign(args) {
1497
- const marketKind = args.marketKind ?? "perp";
1498
- const { asset } = await hyperliquidResolveAsset({
1499
- chainId: args.chainId,
1500
- coin: args.coin,
1501
- marketKind,
1502
- dex: args.dex
1503
- });
1504
- const action = buildHyperliquidUpdateLeverageAction({
1505
- asset,
1506
- leverage: args.leverage,
1507
- isCross: args.isCross
1508
- });
1509
- const nonce = args.nonce ?? Date.now();
1510
- const isTestnet = args.chainId === 998;
1511
- const typed = buildHyperliquidAgentTypedData({ action, nonce, isTestnet });
1512
- return buildEip712MultisignBody({
1513
- keyGen: args.keyGen,
1514
- purposeText: args.purposeText,
1515
- destinationChainID: String(args.chainId),
1516
- destinationAddress: ZERO_ADDRESS,
1517
- typedData: {
1518
- domain: typed.domain,
1519
- types: typed.types,
1520
- primaryType: typed.primaryType,
1521
- message: typed.message
1522
- },
1523
- delivery: {
1524
- kind: "hyperliquid_exchange",
1525
- chainId: args.chainId,
1526
- isTestnet,
1527
- action,
1528
- nonce
1529
- },
1530
- audit: {
1531
- protocol: "hyperliquid",
1532
- action: "updateLeverage",
1533
- coin: args.coin,
1534
- marketKind,
1535
- asset,
1536
- leverage: action.leverage,
1537
- isCross: action.isCross,
1538
- nonce,
1539
- connectionId: typed.connectionId
1540
- }
1541
- });
1542
- }
1543
1759
  var HYPERLIQUID_BRIDGE_MIN_DEPOSIT_USDC = 5;
1544
1760
  var HYPERLIQUID_BRIDGE_ARBITRUM_MAINNET_CHAIN_ID = 42161;
1545
1761
  var HYPERLIQUID_BRIDGE_ARBITRUM_TESTNET_CHAIN_ID = 421614;
@@ -1736,6 +1952,44 @@ async function buildHyperliquidBridgeWithdrawMultisign(args) {
1736
1952
  });
1737
1953
  }
1738
1954
 
1955
+ // src/protocols/evm/hyperliquid/pendingClose.ts
1956
+ function parsePositiveSize(value) {
1957
+ if (value == null || !String(value).trim()) return null;
1958
+ const n = Number.parseFloat(String(value).trim());
1959
+ if (!Number.isFinite(n) || n <= 0) return null;
1960
+ return n;
1961
+ }
1962
+ function hyperliquidOrderClosesPosition(position, orderSide) {
1963
+ const side = String(orderSide).trim().toUpperCase();
1964
+ return position.isLong ? side === "A" : side === "B";
1965
+ }
1966
+ function hyperliquidOrderCountsAsPendingClose(order) {
1967
+ return Boolean(order.reduceOnly) || Boolean(order.isTrigger);
1968
+ }
1969
+ function hyperliquidPendingCloseForPosition(args) {
1970
+ const positionSize = parsePositiveSize(args.position.size);
1971
+ if (positionSize == null) {
1972
+ return { pendingCloseSz: 0, pendingClosePct: 0 };
1973
+ }
1974
+ let pendingCloseSz = 0;
1975
+ for (const order of args.orders) {
1976
+ if (order.coin !== args.position.coin) continue;
1977
+ if (!hyperliquidOrderClosesPosition(args.position, order.side)) continue;
1978
+ if (!hyperliquidOrderCountsAsPendingClose(order)) continue;
1979
+ const sz = parsePositiveSize(order.sz);
1980
+ if (sz != null) pendingCloseSz += sz;
1981
+ }
1982
+ const pendingClosePct = Math.min(100, pendingCloseSz / positionSize * 100);
1983
+ return { pendingCloseSz, pendingClosePct };
1984
+ }
1985
+ function hyperliquidPendingCloseByCoin(args) {
1986
+ const out = /* @__PURE__ */ new Map();
1987
+ for (const position of args.positions) {
1988
+ out.set(position.key, hyperliquidPendingCloseForPosition({ position, orders: args.orders }));
1989
+ }
1990
+ return out;
1991
+ }
1992
+
1739
1993
  // src/protocols/evm/hyperliquid/index.ts
1740
1994
  var HYPERLIQUID_PROTOCOL_ID = "hyperliquid";
1741
1995
  var hyperliquidProtocolModule = {
@@ -1755,7 +2009,7 @@ var hyperliquidProtocolModule = {
1755
2009
  id: "hyperliquid.limitOrder",
1756
2010
  protocolId: HYPERLIQUID_PROTOCOL_ID,
1757
2011
  chainCategory: "evm",
1758
- description: "Place a Hyperliquid limit/IoC order via HyperEVM CoreWriter",
2012
+ description: "Place a Hyperliquid limit/IoC order via CoreWriter, or L1 normalTpsl bracket when TP/SL triggers are set",
1759
2013
  commonParams: ["keyGen", "purposeText", "useCustomGas"],
1760
2014
  params: {
1761
2015
  coin: { type: "string", required: true, description: "Market coin e.g. BTC" },
@@ -1764,7 +2018,10 @@ var hyperliquidProtocolModule = {
1764
2018
  szHuman: { type: "string", required: true, description: "Order size in coin units" },
1765
2019
  marketKind: { type: "string", required: false, description: "perp (default) or spot" },
1766
2020
  tif: { type: "string", required: false, description: "gtc, ioc, or alo" },
1767
- dex: { type: "string", required: false, description: "HIP-3 dex name when applicable" }
2021
+ dex: { type: "string", required: false, description: "HIP-3 dex name when applicable" },
2022
+ takeProfitTriggerPxHuman: { type: "string", required: false, description: "TP trigger \u2014 enables L1 bracket with SL" },
2023
+ stopLossTriggerPxHuman: { type: "string", required: false, description: "SL trigger \u2014 enables L1 bracket with TP" },
2024
+ tpslExecMode: { type: "string", required: false, description: "limit_at_trigger (default) or market" }
1768
2025
  }
1769
2026
  },
1770
2027
  {
@@ -1949,6 +2206,11 @@ exports.buildEvmMultisignBodyHyperliquidVaultDepositBatch = buildEvmMultisignBod
1949
2206
  exports.buildEvmMultisignBodyHyperliquidVaultWithdrawBatch = buildEvmMultisignBodyHyperliquidVaultWithdrawBatch;
1950
2207
  exports.buildHyperliquidAgentTypedData = buildHyperliquidAgentTypedData;
1951
2208
  exports.buildHyperliquidBridgeWithdrawMultisign = buildHyperliquidBridgeWithdrawMultisign;
2209
+ exports.buildHyperliquidLimitOrderWire = buildHyperliquidLimitOrderWire;
2210
+ exports.buildHyperliquidLimitOrderWithTpslMultisign = buildHyperliquidLimitOrderWithTpslMultisign;
2211
+ exports.buildHyperliquidNormalTpslOrderWires = buildHyperliquidNormalTpslOrderWires;
2212
+ exports.buildHyperliquidOrderAction = buildHyperliquidOrderAction;
2213
+ exports.buildHyperliquidTriggerOrderWire = buildHyperliquidTriggerOrderWire;
1952
2214
  exports.buildHyperliquidUpdateLeverageAction = buildHyperliquidUpdateLeverageAction;
1953
2215
  exports.buildHyperliquidUpdateLeverageMultisign = buildHyperliquidUpdateLeverageMultisign;
1954
2216
  exports.buildHyperliquidWithdraw3Action = buildHyperliquidWithdraw3Action;
@@ -1969,6 +2231,7 @@ exports.hyperliquidFetchClearinghouseState = hyperliquidFetchClearinghouseState;
1969
2231
  exports.hyperliquidFetchDelegations = hyperliquidFetchDelegations;
1970
2232
  exports.hyperliquidFetchDelegationsForExecutor = hyperliquidFetchDelegationsForExecutor;
1971
2233
  exports.hyperliquidFetchDexList = hyperliquidFetchDexList;
2234
+ exports.hyperliquidFetchFrontendOpenOrders = hyperliquidFetchFrontendOpenOrders;
1972
2235
  exports.hyperliquidFetchMarketSnapshot = hyperliquidFetchMarketSnapshot;
1973
2236
  exports.hyperliquidFetchMarketSnapshotSummary = hyperliquidFetchMarketSnapshotSummary;
1974
2237
  exports.hyperliquidFetchMarketsSummary = hyperliquidFetchMarketsSummary;
@@ -1994,9 +2257,15 @@ exports.hyperliquidFetchUserVaultEquities = hyperliquidFetchUserVaultEquities;
1994
2257
  exports.hyperliquidFetchUserVaultEquitiesSummary = hyperliquidFetchUserVaultEquitiesSummary;
1995
2258
  exports.hyperliquidFetchVaultCatalogSummary = hyperliquidFetchVaultCatalogSummary;
1996
2259
  exports.hyperliquidFetchVaultSummaries = hyperliquidFetchVaultSummaries;
2260
+ exports.hyperliquidFloatToWire = hyperliquidFloatToWire;
1997
2261
  exports.hyperliquidInferDexFromCoin = hyperliquidInferDexFromCoin;
1998
2262
  exports.hyperliquidIsSpotAssetId = hyperliquidIsSpotAssetId;
2263
+ exports.hyperliquidL1TifFromKey = hyperliquidL1TifFromKey;
1999
2264
  exports.hyperliquidMarketSymbol = hyperliquidMarketSymbol;
2265
+ exports.hyperliquidOrderClosesPosition = hyperliquidOrderClosesPosition;
2266
+ exports.hyperliquidOrderCountsAsPendingClose = hyperliquidOrderCountsAsPendingClose;
2267
+ exports.hyperliquidPendingCloseByCoin = hyperliquidPendingCloseByCoin;
2268
+ exports.hyperliquidPendingCloseForPosition = hyperliquidPendingCloseForPosition;
2000
2269
  exports.hyperliquidProtocolModule = hyperliquidProtocolModule;
2001
2270
  exports.hyperliquidResolveAsset = hyperliquidResolveAsset;
2002
2271
  exports.hyperliquidResolveOhlcvWindow = hyperliquidResolveOhlcvWindow;
@@ -2011,5 +2280,6 @@ exports.isHyperliquidBridgeArbitrumChainSupported = isHyperliquidBridgeArbitrumC
2011
2280
  exports.isHyperliquidBridgeArbitrumUsdcRow = isHyperliquidBridgeArbitrumUsdcRow;
2012
2281
  exports.isHyperliquidChainSupported = isHyperliquidChainSupported;
2013
2282
  exports.isHyperliquidHyperEvmUsdcRow = isHyperliquidHyperEvmUsdcRow;
2283
+ exports.validateHyperliquidTpslGeometry = validateHyperliquidTpslGeometry;
2014
2284
  //# sourceMappingURL=index.cjs.map
2015
2285
  //# sourceMappingURL=index.cjs.map