@continuumdao/ctm-mpc-defi 0.2.5 → 0.2.6

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.
@@ -1,9 +1,10 @@
1
1
  import { getAddress, isAddress } from 'viem';
2
2
  import { zodToJsonSchema } from 'zod-to-json-schema';
3
3
  import { z } from 'zod';
4
+ import { createRequire } from 'module';
5
+ import { fileURLToPath } from 'url';
4
6
  import { readFileSync } from 'fs';
5
7
  import { join, dirname } from 'path';
6
- import { fileURLToPath } from 'url';
7
8
 
8
9
  // src/core/registry.ts
9
10
  var modules = [];
@@ -876,6 +877,154 @@ var mcpEulerV2CollateralWithdrawInputSchema = mcpMultisignInput({
876
877
  collateralAsset: evmAddressSchema,
877
878
  amountHuman: z.string().min(1)
878
879
  });
880
+ function mcpGmxMultisignInput(fields) {
881
+ return evmMultisignCommonInputSchema.extend(fields);
882
+ }
883
+ var gmxDirectionSchema = z.enum(["long", "short"]);
884
+ var gmxOrderTypeSchema = z.enum(["market", "limit"]);
885
+ var mcpGmxFetchMarketsInputSchema = z.object({
886
+ chainId: z.number().int().positive()
887
+ });
888
+ var mcpGmxFetchMarketsOutputSchema = z.object({
889
+ markets: z.array(
890
+ z.object({
891
+ symbol: z.string(),
892
+ marketTokenAddress: z.string(),
893
+ maxLeverageLabel: z.string().nullable()
894
+ })
895
+ )
896
+ });
897
+ var mcpGmxFetchPositionsInputSchema = z.object({
898
+ chainId: z.number().int().positive(),
899
+ executorAddress: evmAddressSchema
900
+ });
901
+ var mcpGmxFetchPositionsOutputSchema = z.object({
902
+ positions: z.array(z.record(z.unknown()))
903
+ });
904
+ var mcpGmxIncreaseInputSchema = mcpGmxMultisignInput({
905
+ symbol: z.string().min(1),
906
+ direction: gmxDirectionSchema,
907
+ orderType: gmxOrderTypeSchema,
908
+ sizeUsdHuman: z.string().min(1),
909
+ collateralToken: z.string().min(1),
910
+ collateralAmountHuman: z.string().min(1),
911
+ triggerPriceUsdHuman: z.string().optional(),
912
+ slippageBps: z.number().int().nonnegative().optional(),
913
+ executionFeeBufferBps: z.number().int().nonnegative().optional()
914
+ });
915
+ var mcpGmxDecreaseInputSchema = mcpGmxMultisignInput({
916
+ symbol: z.string().min(1),
917
+ direction: gmxDirectionSchema,
918
+ orderType: gmxOrderTypeSchema,
919
+ sizeUsdHuman: z.string().min(1),
920
+ collateralToken: z.string().min(1),
921
+ receiveToken: z.string().optional(),
922
+ triggerPriceUsdHuman: z.string().optional(),
923
+ keepLeverage: z.boolean().optional(),
924
+ slippageBps: z.number().int().nonnegative().optional(),
925
+ executionFeeBufferBps: z.number().int().nonnegative().optional()
926
+ });
927
+ var mcpGmxCancelInputSchema = mcpGmxMultisignInput({
928
+ orderId: z.string().min(1)
929
+ });
930
+ var mcpGmxGmDepositInputSchema = mcpGmxMultisignInput({
931
+ marketSymbol: z.string().min(1),
932
+ collateralToken: z.string().min(1),
933
+ collateralAmountHuman: z.string().min(1),
934
+ executionFeeBufferBps: z.number().int().nonnegative().optional(),
935
+ isNativeIn: z.boolean().optional(),
936
+ nativeWrapped: evmAddressSchema.optional()
937
+ });
938
+ var mcpGmxGmWithdrawInputSchema = mcpGmxMultisignInput({
939
+ marketSymbol: z.string().min(1),
940
+ gmAmountHuman: z.string().min(1),
941
+ gmDecimals: z.number().int().nonnegative().optional(),
942
+ executionFeeBufferBps: z.number().int().nonnegative().optional(),
943
+ isNativeOut: z.boolean().optional()
944
+ });
945
+ var mcpGmxStakeGmxInputSchema = mcpGmxMultisignInput({
946
+ amountHuman: z.string().min(1),
947
+ gmxDecimals: z.number().int().nonnegative().optional()
948
+ });
949
+ var mcpGmxUnstakeGmxInputSchema = mcpGmxMultisignInput({
950
+ amountHuman: z.string().min(1),
951
+ gmxDecimals: z.number().int().nonnegative().optional()
952
+ });
953
+ var mcpGmxFetchGmMarketsInputSchema = z.object({
954
+ chainId: z.number().int().positive()
955
+ });
956
+ var mcpGmxGmMarketApyRowSchema = z.object({
957
+ symbol: z.string(),
958
+ marketTokenAddress: z.string(),
959
+ longTokenSymbol: z.string().nullable(),
960
+ shortTokenSymbol: z.string().nullable(),
961
+ apy: z.number().nullable(),
962
+ baseApy: z.number().nullable(),
963
+ bonusApr: z.number().nullable(),
964
+ apyPercentLabel: z.string().nullable()
965
+ });
966
+ var mcpGmxFetchGmMarketsOutputSchema = z.object({
967
+ markets: z.array(mcpGmxGmMarketApyRowSchema)
968
+ });
969
+ var mcpGmxFetchGmApyInputSchema = z.object({
970
+ chainId: z.number().int().positive()
971
+ });
972
+ var mcpGmxFetchGmApyOutputSchema = z.object({
973
+ markets: z.array(
974
+ z.object({
975
+ symbol: z.string(),
976
+ marketTokenAddress: z.string(),
977
+ longTokenSymbol: z.string().nullable(),
978
+ shortTokenSymbol: z.string().nullable(),
979
+ apy: z.number().nullable(),
980
+ baseApy: z.number().nullable(),
981
+ bonusApr: z.number().nullable(),
982
+ apyPercentLabel: z.string().nullable()
983
+ })
984
+ )
985
+ });
986
+ var mcpGmxFetchStakingPowerInputSchema = z.object({
987
+ chainId: z.number().int().positive(),
988
+ executorAddress: evmAddressSchema
989
+ });
990
+ var mcpGmxFetchStakingPowerOutputSchema = z.object({
991
+ stakingPower: z.record(z.unknown())
992
+ });
993
+ var gmxOhlcvTimeframeSchema = z.enum(["1m", "5m", "15m", "1h", "4h", "1d", "1w", "1M"]);
994
+ var gmxOhlcvCandleSchema = z.object({
995
+ timestampMs: z.number(),
996
+ timeLabel: z.string(),
997
+ open: z.string(),
998
+ high: z.string(),
999
+ low: z.string(),
1000
+ close: z.string()
1001
+ });
1002
+ var mcpGmxFetchMarketPricesInputSchema = z.object({
1003
+ chainId: z.number().int().positive(),
1004
+ symbol: z.string().min(1),
1005
+ collateralSymbol: z.string().min(1)
1006
+ });
1007
+ var mcpGmxFetchMarketPricesOutputSchema = z.object({
1008
+ symbol: z.string(),
1009
+ indexLabel: z.string(),
1010
+ collateralSymbol: z.string(),
1011
+ indexMarkUsd: z.string().nullable(),
1012
+ collateralUsd: z.string().nullable(),
1013
+ indexPerCollateral: z.string().nullable(),
1014
+ fetchedAtMs: z.number()
1015
+ });
1016
+ var mcpGmxFetchOhlcvInputSchema = z.object({
1017
+ chainId: z.number().int().positive(),
1018
+ symbol: z.string().min(1),
1019
+ timeframe: gmxOhlcvTimeframeSchema.optional(),
1020
+ limit: z.number().int().positive().max(500).optional(),
1021
+ sort: z.enum(["asc", "desc"]).optional()
1022
+ });
1023
+ var mcpGmxFetchOhlcvOutputSchema = z.object({
1024
+ symbol: z.string(),
1025
+ timeframe: gmxOhlcvTimeframeSchema,
1026
+ candles: z.array(gmxOhlcvCandleSchema)
1027
+ });
879
1028
 
880
1029
  // src/agent/mcpProtocolTools.ts
881
1030
  function defineProtocolMcpTool(def) {
@@ -1202,6 +1351,83 @@ var MCP_PROTOCOL_TOOL_DEFINITIONS = [
1202
1351
  followUp: ["Sign messageToSign", "POST /multiSignRequest"],
1203
1352
  handler: { importPath: "protocols/evm/euler-v2", exportName: "buildEvmMultisignBodyEulerV2BorrowCollateralWithdrawBatch" },
1204
1353
  inputZod: mcpEulerV2CollateralWithdrawInputSchema
1354
+ }),
1355
+ defineProtocolMcpTool({
1356
+ name: "ctm_gmx_build_increase_multisign",
1357
+ actionId: "gmx.increase",
1358
+ protocolId: "gmx",
1359
+ chainCategory: "evm",
1360
+ description: "Build mpc-auth multiSignRequest for GMX classic increase order (open/add perp). Keeper executes asynchronously after on-chain broadcast.",
1361
+ prerequisites: ["keyGen", "executorAddress", "GMX market symbol", "classic mode only \u2014 no subaccounts"],
1362
+ followUp: ["Sign messageToSign", "POST /multiSignRequest"],
1363
+ handler: { importPath: "protocols/evm/gmx", exportName: "buildEvmMultisignBodyGmxIncreaseBatch" },
1364
+ inputZod: mcpGmxIncreaseInputSchema
1365
+ }),
1366
+ defineProtocolMcpTool({
1367
+ name: "ctm_gmx_build_decrease_multisign",
1368
+ actionId: "gmx.decrease",
1369
+ protocolId: "gmx",
1370
+ chainCategory: "evm",
1371
+ description: "Build mpc-auth multiSignRequest for GMX classic decrease order (close/reduce perp).",
1372
+ prerequisites: ["keyGen", "executorAddress", "position market symbol"],
1373
+ followUp: ["Sign messageToSign", "POST /multiSignRequest"],
1374
+ handler: { importPath: "protocols/evm/gmx", exportName: "buildEvmMultisignBodyGmxDecreaseBatch" },
1375
+ inputZod: mcpGmxDecreaseInputSchema
1376
+ }),
1377
+ defineProtocolMcpTool({
1378
+ name: "ctm_gmx_build_cancel_multisign",
1379
+ actionId: "gmx.cancel",
1380
+ protocolId: "gmx",
1381
+ chainCategory: "evm",
1382
+ description: "Build mpc-auth multiSignRequest to cancel a pending GMX order (classic on-chain).",
1383
+ prerequisites: ["keyGen", "executorAddress", "orderId from fetchOrders"],
1384
+ followUp: ["Sign messageToSign", "POST /multiSignRequest"],
1385
+ handler: { importPath: "protocols/evm/gmx", exportName: "buildEvmMultisignBodyGmxCancelBatch" },
1386
+ inputZod: mcpGmxCancelInputSchema
1387
+ }),
1388
+ defineProtocolMcpTool({
1389
+ name: "ctm_gmx_build_gm_deposit_multisign",
1390
+ actionId: "gmx.gmDeposit",
1391
+ protocolId: "gmx",
1392
+ chainCategory: "evm",
1393
+ description: "Build mpc-auth multiSignRequest for GMX GM pool deposit (ExchangeRouter multicall). Keeper executes asynchronously.",
1394
+ prerequisites: ["keyGen", "executorAddress", "marketSymbol", "collateral token"],
1395
+ followUp: ["Sign messageToSign", "POST /multiSignRequest"],
1396
+ handler: { importPath: "protocols/evm/gmx", exportName: "buildEvmMultisignBodyGmxGmDepositBatch" },
1397
+ inputZod: mcpGmxGmDepositInputSchema
1398
+ }),
1399
+ defineProtocolMcpTool({
1400
+ name: "ctm_gmx_build_gm_withdraw_multisign",
1401
+ actionId: "gmx.gmWithdraw",
1402
+ protocolId: "gmx",
1403
+ chainCategory: "evm",
1404
+ description: "Build mpc-auth multiSignRequest for GMX GM pool withdrawal (ExchangeRouter multicall). Keeper executes asynchronously.",
1405
+ prerequisites: ["keyGen", "executorAddress", "GM market token balance"],
1406
+ followUp: ["Sign messageToSign", "POST /multiSignRequest"],
1407
+ handler: { importPath: "protocols/evm/gmx", exportName: "buildEvmMultisignBodyGmxGmWithdrawBatch" },
1408
+ inputZod: mcpGmxGmWithdrawInputSchema
1409
+ }),
1410
+ defineProtocolMcpTool({
1411
+ name: "ctm_gmx_build_stake_gmx_multisign",
1412
+ actionId: "gmx.stakeGmx",
1413
+ protocolId: "gmx",
1414
+ chainCategory: "evm",
1415
+ description: "Build mpc-auth multiSignRequest to stake GMX via RewardRouter.",
1416
+ prerequisites: ["keyGen", "executorAddress", "GMX token balance"],
1417
+ followUp: ["Sign messageToSign", "POST /multiSignRequest"],
1418
+ handler: { importPath: "protocols/evm/gmx", exportName: "buildEvmMultisignBodyGmxStakeGmxBatch" },
1419
+ inputZod: mcpGmxStakeGmxInputSchema
1420
+ }),
1421
+ defineProtocolMcpTool({
1422
+ name: "ctm_gmx_build_unstake_gmx_multisign",
1423
+ actionId: "gmx.unstakeGmx",
1424
+ protocolId: "gmx",
1425
+ chainCategory: "evm",
1426
+ description: "Build mpc-auth multiSignRequest to unstake GMX via RewardRouter.",
1427
+ prerequisites: ["keyGen", "executorAddress", "staked GMX balance"],
1428
+ followUp: ["Sign messageToSign", "POST /multiSignRequest"],
1429
+ handler: { importPath: "protocols/evm/gmx", exportName: "buildEvmMultisignBodyGmxUnstakeGmxBatch" },
1430
+ inputZod: mcpGmxUnstakeGmxInputSchema
1205
1431
  })
1206
1432
  ];
1207
1433
 
@@ -1466,6 +1692,90 @@ var CORE_MCP_TOOL_DEFINITIONS = [
1466
1692
  handler: { importPath: "protocols/evm/curve-dao", exportName: "buildEvmMultisignBodyCurveDaoBatch" },
1467
1693
  inputZod: mcpCurveDaoBuildSwapMultisignInputSchema,
1468
1694
  outputZod: multisignOutputSchema
1695
+ }),
1696
+ defineMcpTool({
1697
+ name: "ctm_gmx_fetch_markets",
1698
+ actionId: "gmx.fetch-markets",
1699
+ protocolId: "gmx",
1700
+ chainCategory: "evm",
1701
+ description: "List GMX V2 perp markets for a chain (symbol, market token, maxLeverageLabel). Read-only; uses GMX HTTP API.",
1702
+ prerequisites: ["chainId"],
1703
+ followUp: ["ctm_gmx_build_increase_multisign", "ctm_gmx_build_decrease_multisign"],
1704
+ handler: { importPath: "protocols/evm/gmx", exportName: "gmxFetchMarketsSummary" },
1705
+ inputZod: mcpGmxFetchMarketsInputSchema,
1706
+ outputZod: mcpGmxFetchMarketsOutputSchema
1707
+ }),
1708
+ defineMcpTool({
1709
+ name: "ctm_gmx_fetch_positions",
1710
+ actionId: "gmx.fetch-positions",
1711
+ protocolId: "gmx",
1712
+ chainCategory: "evm",
1713
+ description: "Fetch open GMX perp positions for an executor address. Read-only.",
1714
+ prerequisites: ["chainId", "executorAddress (MPC ethereumaddress)"],
1715
+ followUp: ["ctm_gmx_build_decrease_multisign"],
1716
+ handler: { importPath: "protocols/evm/gmx", exportName: "gmxFetchPositionsForExecutor" },
1717
+ inputZod: mcpGmxFetchPositionsInputSchema,
1718
+ outputZod: mcpGmxFetchPositionsOutputSchema
1719
+ }),
1720
+ defineMcpTool({
1721
+ name: "ctm_gmx_fetch_market_prices",
1722
+ actionId: "gmx.fetch-market-prices",
1723
+ protocolId: "gmx",
1724
+ chainCategory: "evm",
1725
+ description: "Latest GMX index mark and collateral USD prices for a perp market. Read-only via GMX HTTP API.",
1726
+ prerequisites: ["chainId", "symbol from ctm_gmx_fetch_markets", "collateralSymbol e.g. USDC"],
1727
+ followUp: ["ctm_gmx_fetch_ohlcv", "ctm_gmx_build_increase_multisign"],
1728
+ handler: { importPath: "protocols/evm/gmx", exportName: "gmxFetchMarketPrices" },
1729
+ inputZod: mcpGmxFetchMarketPricesInputSchema,
1730
+ outputZod: mcpGmxFetchMarketPricesOutputSchema
1731
+ }),
1732
+ defineMcpTool({
1733
+ name: "ctm_gmx_fetch_ohlcv",
1734
+ actionId: "gmx.fetch-ohlcv",
1735
+ protocolId: "gmx",
1736
+ chainCategory: "evm",
1737
+ description: "OHLCV candle table for a GMX perp index (USD). Default timeframe 15m. Read-only via GMX HTTP API.",
1738
+ prerequisites: ["chainId", "symbol from ctm_gmx_fetch_markets"],
1739
+ followUp: ["ctm_gmx_fetch_market_prices"],
1740
+ handler: { importPath: "protocols/evm/gmx", exportName: "gmxFetchOhlcv" },
1741
+ inputZod: mcpGmxFetchOhlcvInputSchema,
1742
+ outputZod: mcpGmxFetchOhlcvOutputSchema
1743
+ }),
1744
+ defineMcpTool({
1745
+ name: "ctm_gmx_fetch_gm_markets",
1746
+ actionId: "gmx.fetch-gm-markets",
1747
+ protocolId: "gmx",
1748
+ chainCategory: "evm",
1749
+ description: "List GMX GM liquidity pool markets with long/short collateral symbols and estimated APY. Read-only.",
1750
+ prerequisites: ["chainId"],
1751
+ followUp: ["ctm_gmx_build_gm_deposit_multisign", "ctm_gmx_build_gm_withdraw_multisign"],
1752
+ handler: { importPath: "protocols/evm/gmx", exportName: "gmxFetchGmMarkets" },
1753
+ inputZod: mcpGmxFetchGmMarketsInputSchema,
1754
+ outputZod: mcpGmxFetchGmMarketsOutputSchema
1755
+ }),
1756
+ defineMcpTool({
1757
+ name: "ctm_gmx_fetch_gm_apy",
1758
+ actionId: "gmx.fetch-gm-apy",
1759
+ protocolId: "gmx",
1760
+ chainCategory: "evm",
1761
+ description: "Fetch estimated GM liquidity pool APY for each GMX market (sorted highest first). Read-only.",
1762
+ prerequisites: ["chainId"],
1763
+ followUp: ["ctm_gmx_build_gm_deposit_multisign", "ctm_gmx_fetch_gm_markets"],
1764
+ handler: { importPath: "protocols/evm/gmx", exportName: "gmxFetchGmApy" },
1765
+ inputZod: mcpGmxFetchGmApyInputSchema,
1766
+ outputZod: mcpGmxFetchGmApyOutputSchema
1767
+ }),
1768
+ defineMcpTool({
1769
+ name: "ctm_gmx_fetch_staking_power",
1770
+ actionId: "gmx.fetch-staking-power",
1771
+ protocolId: "gmx",
1772
+ chainCategory: "evm",
1773
+ description: "Fetch GMX token staking power for an executor address. Read-only.",
1774
+ prerequisites: ["chainId", "executorAddress"],
1775
+ followUp: ["ctm_gmx_build_stake_gmx_multisign", "ctm_gmx_build_unstake_gmx_multisign"],
1776
+ handler: { importPath: "protocols/evm/gmx", exportName: "gmxFetchStakingPower" },
1777
+ inputZod: mcpGmxFetchStakingPowerInputSchema,
1778
+ outputZod: mcpGmxFetchStakingPowerOutputSchema
1469
1779
  })
1470
1780
  ];
1471
1781
  var MCP_TOOL_DEFINITIONS = [
@@ -1857,6 +2167,231 @@ var eulerV2ProtocolModule = {
1857
2167
  ]
1858
2168
  };
1859
2169
  registerProtocolModule(eulerV2ProtocolModule);
2170
+
2171
+ // src/protocols/evm/gmx/support.ts
2172
+ var GMX_SUPPORTED_CHAIN_IDS = [42161, 43114];
2173
+ function isGmxChainSupported(chainId) {
2174
+ return GMX_SUPPORTED_CHAIN_IDS.includes(chainId);
2175
+ }
2176
+ var require2 = createRequire(fileURLToPath(import.meta.url));
2177
+ var { GmxApiSdk } = require2("@gmx-io/sdk/v2");
2178
+ var GMX_HTTP_TIMEOUT_MS = 3e4;
2179
+ var GMX_API_BASE_URLS = {
2180
+ 42161: ["https://arbitrum.gmxapi.io/v1", "https://arbitrum.gmxapi.ai/v1"],
2181
+ 43114: ["https://avalanche.gmxapi.io/v1", "https://avalanche.gmxapi.ai/v1"]
2182
+ };
2183
+ function buildGmxUrl(baseUrl, path, query) {
2184
+ const base = baseUrl.replace(/\/$/, "");
2185
+ if (!query) return `${base}${path}`;
2186
+ const params = new URLSearchParams();
2187
+ for (const [key, value] of Object.entries(query)) {
2188
+ if (value !== void 0 && value !== null) params.set(key, String(value));
2189
+ }
2190
+ const qs = params.toString();
2191
+ return qs ? `${base}${path}?${qs}` : `${base}${path}`;
2192
+ }
2193
+ function bigintReplacer(_key, value) {
2194
+ return typeof value === "bigint" ? value.toString() : value;
2195
+ }
2196
+ async function gmxHttpFetchJson(baseUrl, path, opts) {
2197
+ const url = buildGmxUrl(baseUrl, path, opts?.query);
2198
+ const controller = new AbortController();
2199
+ const timeoutId = setTimeout(() => controller.abort(), GMX_HTTP_TIMEOUT_MS);
2200
+ try {
2201
+ const response = await fetch(url, {
2202
+ headers: { Accept: "application/json" },
2203
+ signal: controller.signal
2204
+ });
2205
+ if (!response.ok) {
2206
+ let message = `HTTP ${response.status}: ${response.statusText}`;
2207
+ try {
2208
+ const body = await response.json();
2209
+ if (body?.message) message = `HTTP ${response.status}: ${body.message}`;
2210
+ } catch {
2211
+ }
2212
+ throw new Error(`${message} (${url})`);
2213
+ }
2214
+ const json = await response.json();
2215
+ return opts?.transform ? opts.transform(json) : json;
2216
+ } finally {
2217
+ clearTimeout(timeoutId);
2218
+ }
2219
+ }
2220
+ async function gmxHttpPostJson(baseUrl, path, body, opts) {
2221
+ const url = buildGmxUrl(baseUrl, path);
2222
+ const controller = new AbortController();
2223
+ const timeoutId = setTimeout(() => controller.abort(), GMX_HTTP_TIMEOUT_MS);
2224
+ try {
2225
+ const response = await fetch(url, {
2226
+ method: "POST",
2227
+ headers: { "Content-Type": "application/json", Accept: "application/json" },
2228
+ body: JSON.stringify(body, bigintReplacer),
2229
+ signal: controller.signal
2230
+ });
2231
+ if (!response.ok) {
2232
+ let message = `HTTP ${response.status}: ${response.statusText}`;
2233
+ try {
2234
+ const errBody = await response.json();
2235
+ if (errBody?.message) message = `HTTP ${response.status}: ${errBody.message}`;
2236
+ } catch {
2237
+ }
2238
+ throw new Error(`${message} (${url})`);
2239
+ }
2240
+ const json = await response.json();
2241
+ return opts?.transform ? opts.transform(json) : json;
2242
+ } finally {
2243
+ clearTimeout(timeoutId);
2244
+ }
2245
+ }
2246
+ function createIoFirstGmxHttpApi(chainId) {
2247
+ const urls = GMX_API_BASE_URLS[chainId];
2248
+ if (!urls) {
2249
+ throw new Error(`GMX API URLs are not configured for chain ${chainId}.`);
2250
+ }
2251
+ async function withIoFirstFallback(fn) {
2252
+ let lastError;
2253
+ for (const baseUrl of urls) {
2254
+ try {
2255
+ return await fn(baseUrl);
2256
+ } catch (e) {
2257
+ lastError = e;
2258
+ }
2259
+ }
2260
+ const detail = lastError instanceof Error ? lastError.message : String(lastError);
2261
+ throw new Error(
2262
+ `GMX API request failed on all endpoints (${urls.join(", ")}). Last error: ${detail}`
2263
+ );
2264
+ }
2265
+ return {
2266
+ fetchJson: (path, opts) => withIoFirstFallback((baseUrl) => gmxHttpFetchJson(baseUrl, path, opts)),
2267
+ postJson: (path, body, opts) => withIoFirstFallback((baseUrl) => gmxHttpPostJson(baseUrl, path, body, opts))
2268
+ };
2269
+ }
2270
+ var sdkByChain = /* @__PURE__ */ new Map();
2271
+ function getGmxApiSdk(chainId) {
2272
+ if (!isGmxChainSupported(chainId)) {
2273
+ throw new Error(`GMX is not supported on chain ${chainId}. Supported: 42161 (Arbitrum), 43114 (Avalanche).`);
2274
+ }
2275
+ let sdk = sdkByChain.get(chainId);
2276
+ if (!sdk) {
2277
+ sdk = new GmxApiSdk({ chainId, api: createIoFirstGmxHttpApi(chainId) });
2278
+ sdkByChain.set(chainId, sdk);
2279
+ }
2280
+ return sdk;
2281
+ }
2282
+ async function gmxFetchTokens(chainId) {
2283
+ const sdk = getGmxApiSdk(chainId);
2284
+ return sdk.fetchTokens();
2285
+ }
2286
+ async function gmxFetchCollateralTokens(args) {
2287
+ const tokens = await gmxFetchTokens(args.chainId);
2288
+ return tokens.map((t) => ({
2289
+ symbol: t.symbol,
2290
+ address: t.address,
2291
+ decimals: t.decimals
2292
+ }));
2293
+ }
2294
+
2295
+ // src/protocols/evm/gmx/index.ts
2296
+ var GMX_PROTOCOL_ID = "gmx";
2297
+ var gmxProtocolModule = {
2298
+ id: GMX_PROTOCOL_ID,
2299
+ chainCategory: "evm",
2300
+ isChainSupported(ctx) {
2301
+ if (ctx.chainCategory !== "evm") return false;
2302
+ const chainId = typeof ctx.chainId === "number" ? ctx.chainId : Number(ctx.chainId);
2303
+ return Number.isFinite(chainId) && isGmxChainSupported(chainId);
2304
+ },
2305
+ isTokenSupported(token) {
2306
+ if (token.category !== "evm") return false;
2307
+ return token.kind === "erc20";
2308
+ },
2309
+ actions: [
2310
+ {
2311
+ id: "gmx.increase",
2312
+ protocolId: GMX_PROTOCOL_ID,
2313
+ chainCategory: "evm",
2314
+ description: "Open or add to a GMX perp position (classic on-chain order)",
2315
+ commonParams: ["keyGen", "purposeText", "useCustomGas"],
2316
+ params: {
2317
+ symbol: { type: "string", required: true, description: "GMX market symbol e.g. ETH/USD [WETH-USDC]" },
2318
+ direction: { type: "string", required: true, description: "long or short" },
2319
+ orderType: { type: "string", required: true, description: "market or limit" },
2320
+ sizeUsdHuman: { type: "string", required: true, description: "Position size in USD" },
2321
+ collateralToken: { type: "string", required: true, description: "Collateral token symbol e.g. USDC" },
2322
+ collateralAmountHuman: { type: "string", required: true, description: "Collateral amount in token units" }
2323
+ }
2324
+ },
2325
+ {
2326
+ id: "gmx.decrease",
2327
+ protocolId: GMX_PROTOCOL_ID,
2328
+ chainCategory: "evm",
2329
+ description: "Close or reduce a GMX perp position (classic on-chain order)",
2330
+ commonParams: ["keyGen", "purposeText", "useCustomGas"],
2331
+ params: {
2332
+ symbol: { type: "string", required: true, description: "GMX market symbol" },
2333
+ direction: { type: "string", required: true, description: "long or short" },
2334
+ orderType: { type: "string", required: true, description: "market or limit" },
2335
+ sizeUsdHuman: { type: "string", required: true, description: "Size to close in USD" },
2336
+ collateralToken: { type: "string", required: true, description: "Position collateral token symbol" }
2337
+ }
2338
+ },
2339
+ {
2340
+ id: "gmx.cancel",
2341
+ protocolId: GMX_PROTOCOL_ID,
2342
+ chainCategory: "evm",
2343
+ description: "Cancel a pending GMX order (classic on-chain)",
2344
+ commonParams: ["keyGen", "purposeText", "useCustomGas"],
2345
+ params: {
2346
+ orderId: { type: "string", required: true, description: "Order key from fetchOrders" }
2347
+ }
2348
+ },
2349
+ {
2350
+ id: "gmx.gmDeposit",
2351
+ protocolId: GMX_PROTOCOL_ID,
2352
+ chainCategory: "evm",
2353
+ description: "Deposit collateral into a GMX GM liquidity pool (ExchangeRouter multicall)",
2354
+ commonParams: ["keyGen", "purposeText", "useCustomGas"],
2355
+ params: {
2356
+ marketSymbol: { type: "string", required: true, description: "GM market symbol e.g. ETH/USD [WETH-USDC]" },
2357
+ collateralToken: { type: "string", required: true, description: "Pool collateral token symbol" },
2358
+ collateralAmountHuman: { type: "string", required: true, description: "Amount in token units" }
2359
+ }
2360
+ },
2361
+ {
2362
+ id: "gmx.gmWithdraw",
2363
+ protocolId: GMX_PROTOCOL_ID,
2364
+ chainCategory: "evm",
2365
+ description: "Withdraw GM market tokens from a GMX liquidity pool",
2366
+ commonParams: ["keyGen", "purposeText", "useCustomGas"],
2367
+ params: {
2368
+ marketSymbol: { type: "string", required: true, description: "GM market symbol" },
2369
+ gmAmountHuman: { type: "string", required: true, description: "GM token amount to withdraw" }
2370
+ }
2371
+ },
2372
+ {
2373
+ id: "gmx.stakeGmx",
2374
+ protocolId: GMX_PROTOCOL_ID,
2375
+ chainCategory: "evm",
2376
+ description: "Stake GMX tokens via RewardRouter",
2377
+ commonParams: ["keyGen", "purposeText", "useCustomGas"],
2378
+ params: {
2379
+ amountHuman: { type: "string", required: true, description: "GMX amount to stake" }
2380
+ }
2381
+ },
2382
+ {
2383
+ id: "gmx.unstakeGmx",
2384
+ protocolId: GMX_PROTOCOL_ID,
2385
+ chainCategory: "evm",
2386
+ description: "Unstake GMX tokens via RewardRouter",
2387
+ commonParams: ["keyGen", "purposeText", "useCustomGas"],
2388
+ params: {
2389
+ amountHuman: { type: "string", required: true, description: "GMX amount to unstake" }
2390
+ }
2391
+ }
2392
+ ]
2393
+ };
2394
+ registerProtocolModule(gmxProtocolModule);
1860
2395
  var skillsDir = join(dirname(fileURLToPath(import.meta.url)), "skills");
1861
2396
  var SKILL_PROTOCOL_IDS = [
1862
2397
  "aave-v4",
@@ -1866,7 +2401,8 @@ var SKILL_PROTOCOL_IDS = [
1866
2401
  "ethena",
1867
2402
  "euler-v2",
1868
2403
  "maple-syrup",
1869
- "sky"
2404
+ "sky",
2405
+ "gmx"
1870
2406
  ];
1871
2407
  function getToolsForProtocol(protocolId) {
1872
2408
  return MCP_TOOL_DEFINITIONS.filter((t) => t.protocolId === protocolId);
@@ -2098,6 +2634,35 @@ var PROTOCOL_SUPPORT_ADVISORS = {
2098
2634
  notes: "Sky Lockstake and sUSDS use fixed mainnet contract addresses in protocol builders."
2099
2635
  };
2100
2636
  }
2637
+ }),
2638
+ [GMX_PROTOCOL_ID]: advisor(GMX_PROTOCOL_ID, "api_underlyings", {
2639
+ async supportedChainIds() {
2640
+ return [42161, 43114];
2641
+ },
2642
+ async supportedTokens(chainId) {
2643
+ if (!isGmxChainSupported(chainId)) {
2644
+ return { tokens: [], notes: "GMX v1 supports Arbitrum (42161) and Avalanche (43114)." };
2645
+ }
2646
+ const tokens = await gmxFetchCollateralTokens({ chainId });
2647
+ return {
2648
+ tokens: tokens.map((t) => ({
2649
+ address: t.address,
2650
+ symbol: t.symbol,
2651
+ roles: ["collateral"]
2652
+ })),
2653
+ notes: "Collateral ERC-20 tokens from GMX API. Perp markets listed via fetch_markets."
2654
+ };
2655
+ },
2656
+ async isTokenSupported(chainId, address) {
2657
+ if (!isGmxChainSupported(chainId)) return false;
2658
+ const tokens = await gmxFetchCollateralTokens({ chainId });
2659
+ try {
2660
+ const normalized = getAddress(address).toLowerCase();
2661
+ return tokens.some((t) => t.address.toLowerCase() === normalized);
2662
+ } catch {
2663
+ return false;
2664
+ }
2665
+ }
2101
2666
  })
2102
2667
  };
2103
2668
  function getProtocolSupportAdvisor(protocolId) {
@@ -2132,6 +2697,7 @@ registerProtocolModule(mapleProtocolModule);
2132
2697
  registerProtocolModule(skyProtocolModule);
2133
2698
  registerProtocolModule(aaveV4ProtocolModule);
2134
2699
  registerProtocolModule(eulerV2ProtocolModule);
2700
+ registerProtocolModule(gmxProtocolModule);
2135
2701
  function getAgentCatalog() {
2136
2702
  return {
2137
2703
  protocols: getProtocolModules(),
@@ -2148,11 +2714,12 @@ function getAgentCatalog() {
2148
2714
  sky: skyProtocolModule,
2149
2715
  aaveV4: aaveV4ProtocolModule,
2150
2716
  eulerV2: eulerV2ProtocolModule,
2717
+ gmx: gmxProtocolModule,
2151
2718
  /** Prefer getAgentCatalogForMcp() or getMcpToolDefinitions() for MCP servers. */
2152
2719
  mcp: getAgentCatalogForMcp()
2153
2720
  };
2154
2721
  }
2155
2722
 
2156
- export { EVM_COMMON_PARAM_DOCS, MANAGEMENT_SIG_DOC, MCP_NON_SUBMIT_TOOL_NAMES, MCP_TOOL_DEFINITIONS, MCP_TOOL_INPUT_SCHEMAS, MCP_TOOL_OUTPUT_SCHEMAS, MULTISIGN_OUTPUT_DOC, PROTOCOL_SUPPORT_ADVISORS, chainDetailSchema, evmAddressSchema, evmMultisignCommonInputSchema, getActionsByChainCategory, getAgentCatalog, getAgentCatalogForMcp, getMcpToolByName, getMcpToolDefinitions, getMcpToolInputSchema, getMcpToolOutputSchema, getProtocolDiscoverySummary, getProtocolModules, getProtocolSkill, getProtocolSupportAdvisor, getToolsForProtocol, jsonObjectSchema, keyGenSchema, listProtocolSupportAdvisorIds, listProtocolsWithSkills, mcpAaveV4BorrowInputSchema, mcpAaveV4DepositInputSchema, mcpAaveV4RepayInputSchema, mcpAaveV4WithdrawInputSchema, mcpCurveDaoBuildSwapMultisignInputSchema, mcpCurveDaoQuoteInputSchema, mcpCurveDaoQuoteOutputSchema, mcpEthenaClaimInputSchema, mcpEthenaCooldownInputSchema, mcpEthenaRedeemInputSchema, mcpEthenaStakeInputSchema, mcpEulerV2BorrowRepayInputSchema, mcpEulerV2CollateralDepositInputSchema, mcpEulerV2CollateralWithdrawInputSchema, mcpEulerV2IsolatedBorrowInputSchema, mcpEulerV2IsolatedLendInputSchema, mcpEulerV2VaultWithdrawInputSchema, mcpLidoClaimWithdrawalInputSchema, mcpLidoRequestWithdrawalsInputSchema, mcpLidoSubmitInputSchema, mcpLidoUnwrapWstEthInputSchema, mcpLidoWrapStEthInputSchema, mcpMapleDepositInputSchema, mcpMapleRequestRedeemInputSchema, mcpMultisignInput, multisignOutputSchema as mcpMultisignOutputSchema, mcpServerCommonInputSchema, mcpServerMultisignInput, mcpServerSubmitOutputSchema, mcpSkyLockstakeCloseInputSchema, mcpSkyLockstakeDrawInputSchema, mcpSkyLockstakeGetRewardInputSchema, mcpSkyLockstakeStakeInputSchema, mcpSkyLockstakeWipeInputSchema, mcpSkySusdsDepositInputSchema, mcpSkySusdsRedeemInputSchema, mcpUniswapV4BuildCollectFeesMultisignInputSchema, mcpUniswapV4BuildDecreaseLiquidityMultisignInputSchema, mcpUniswapV4BuildIncreaseLiquidityMultisignInputSchema, mcpUniswapV4BuildMintLiquidityMultisignInputSchema, mcpUniswapV4BuildSwapMultisignInputSchema, mcpUniswapV4CreateSwapInputSchema, mcpUniswapV4CreateSwapOutputSchema, mcpUniswapV4LpClaimInputSchema, mcpUniswapV4LpClaimOutputSchema, mcpUniswapV4LpCreatePositionInputSchema, mcpUniswapV4LpCreatePositionOutputSchema, mcpUniswapV4LpDecreaseInputSchema, mcpUniswapV4LpDecreaseOutputSchema, mcpUniswapV4LpIncreaseInputSchema, mcpUniswapV4LpIncreaseOutputSchema, mcpUniswapV4LpListPoolsInputSchema, mcpUniswapV4LpListPoolsOutputSchema, mcpUniswapV4LpListPositionsInputSchema, mcpUniswapV4LpListPositionsOutputSchema, mcpUniswapV4QuoteInputSchema, mcpUniswapV4QuoteOutputSchema, mcpUniswapV4RegisterPositionFromMintTxInputSchema, mcpUniswapV4RegisterPositionFromMintTxOutputSchema, mcpUniswapV4RegisterPositionNftInputSchema, mcpUniswapV4RegisterPositionNftOutputSchema, multisignOutputSchema, parseMcpToolInput, parseMcpToolOutput, uniswapQuoteTradeTypeSchema, zodSchemaToMcpJsonSchema };
2723
+ export { EVM_COMMON_PARAM_DOCS, MANAGEMENT_SIG_DOC, MCP_NON_SUBMIT_TOOL_NAMES, MCP_TOOL_DEFINITIONS, MCP_TOOL_INPUT_SCHEMAS, MCP_TOOL_OUTPUT_SCHEMAS, MULTISIGN_OUTPUT_DOC, PROTOCOL_SUPPORT_ADVISORS, chainDetailSchema, evmAddressSchema, evmMultisignCommonInputSchema, getActionsByChainCategory, getAgentCatalog, getAgentCatalogForMcp, getMcpToolByName, getMcpToolDefinitions, getMcpToolInputSchema, getMcpToolOutputSchema, getProtocolDiscoverySummary, getProtocolModules, getProtocolSkill, getProtocolSupportAdvisor, getToolsForProtocol, jsonObjectSchema, keyGenSchema, listProtocolSupportAdvisorIds, listProtocolsWithSkills, mcpAaveV4BorrowInputSchema, mcpAaveV4DepositInputSchema, mcpAaveV4RepayInputSchema, mcpAaveV4WithdrawInputSchema, mcpCurveDaoBuildSwapMultisignInputSchema, mcpCurveDaoQuoteInputSchema, mcpCurveDaoQuoteOutputSchema, mcpEthenaClaimInputSchema, mcpEthenaCooldownInputSchema, mcpEthenaRedeemInputSchema, mcpEthenaStakeInputSchema, mcpEulerV2BorrowRepayInputSchema, mcpEulerV2CollateralDepositInputSchema, mcpEulerV2CollateralWithdrawInputSchema, mcpEulerV2IsolatedBorrowInputSchema, mcpEulerV2IsolatedLendInputSchema, mcpEulerV2VaultWithdrawInputSchema, mcpGmxCancelInputSchema, mcpGmxDecreaseInputSchema, mcpGmxFetchGmApyInputSchema, mcpGmxFetchGmApyOutputSchema, mcpGmxFetchGmMarketsInputSchema, mcpGmxFetchGmMarketsOutputSchema, mcpGmxFetchMarketPricesInputSchema, mcpGmxFetchMarketPricesOutputSchema, mcpGmxFetchMarketsInputSchema, mcpGmxFetchMarketsOutputSchema, mcpGmxFetchOhlcvInputSchema, mcpGmxFetchOhlcvOutputSchema, mcpGmxFetchPositionsInputSchema, mcpGmxFetchPositionsOutputSchema, mcpGmxFetchStakingPowerInputSchema, mcpGmxFetchStakingPowerOutputSchema, mcpGmxGmDepositInputSchema, mcpGmxGmWithdrawInputSchema, mcpGmxIncreaseInputSchema, multisignOutputSchema as mcpGmxMultisignOutputSchema, mcpGmxStakeGmxInputSchema, mcpGmxUnstakeGmxInputSchema, mcpLidoClaimWithdrawalInputSchema, mcpLidoRequestWithdrawalsInputSchema, mcpLidoSubmitInputSchema, mcpLidoUnwrapWstEthInputSchema, mcpLidoWrapStEthInputSchema, mcpMapleDepositInputSchema, mcpMapleRequestRedeemInputSchema, mcpMultisignInput, multisignOutputSchema as mcpMultisignOutputSchema, mcpServerCommonInputSchema, mcpServerMultisignInput, mcpServerSubmitOutputSchema, mcpSkyLockstakeCloseInputSchema, mcpSkyLockstakeDrawInputSchema, mcpSkyLockstakeGetRewardInputSchema, mcpSkyLockstakeStakeInputSchema, mcpSkyLockstakeWipeInputSchema, mcpSkySusdsDepositInputSchema, mcpSkySusdsRedeemInputSchema, mcpUniswapV4BuildCollectFeesMultisignInputSchema, mcpUniswapV4BuildDecreaseLiquidityMultisignInputSchema, mcpUniswapV4BuildIncreaseLiquidityMultisignInputSchema, mcpUniswapV4BuildMintLiquidityMultisignInputSchema, mcpUniswapV4BuildSwapMultisignInputSchema, mcpUniswapV4CreateSwapInputSchema, mcpUniswapV4CreateSwapOutputSchema, mcpUniswapV4LpClaimInputSchema, mcpUniswapV4LpClaimOutputSchema, mcpUniswapV4LpCreatePositionInputSchema, mcpUniswapV4LpCreatePositionOutputSchema, mcpUniswapV4LpDecreaseInputSchema, mcpUniswapV4LpDecreaseOutputSchema, mcpUniswapV4LpIncreaseInputSchema, mcpUniswapV4LpIncreaseOutputSchema, mcpUniswapV4LpListPoolsInputSchema, mcpUniswapV4LpListPoolsOutputSchema, mcpUniswapV4LpListPositionsInputSchema, mcpUniswapV4LpListPositionsOutputSchema, mcpUniswapV4QuoteInputSchema, mcpUniswapV4QuoteOutputSchema, mcpUniswapV4RegisterPositionFromMintTxInputSchema, mcpUniswapV4RegisterPositionFromMintTxOutputSchema, mcpUniswapV4RegisterPositionNftInputSchema, mcpUniswapV4RegisterPositionNftOutputSchema, multisignOutputSchema, parseMcpToolInput, parseMcpToolOutput, uniswapQuoteTradeTypeSchema, zodSchemaToMcpJsonSchema };
2157
2724
  //# sourceMappingURL=catalog.js.map
2158
2725
  //# sourceMappingURL=catalog.js.map