@exagent/agent 0.1.39 → 0.1.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -45,6 +45,8 @@ __export(index_exports, {
45
45
  HyperliquidWebSocket: () => HyperliquidWebSocket,
46
46
  LLMConfigSchema: () => LLMConfigSchema,
47
47
  LLMProviderSchema: () => LLMProviderSchema,
48
+ MARKET_CATEGORIES: () => MARKET_CATEGORIES,
49
+ MarketBrowser: () => MarketBrowser,
48
50
  MarketDataService: () => MarketDataService,
49
51
  MistralAdapter: () => MistralAdapter,
50
52
  OllamaAdapter: () => OllamaAdapter,
@@ -53,8 +55,14 @@ __export(index_exports, {
53
55
  PerpConfigSchema: () => PerpConfigSchema,
54
56
  PerpOnboarding: () => PerpOnboarding,
55
57
  PerpTradeRecorder: () => PerpTradeRecorder,
58
+ PolymarketClient: () => PolymarketClient,
56
59
  PositionManager: () => PositionManager,
57
60
  PositionTracker: () => PositionTracker,
61
+ PredictionConfigSchema: () => PredictionConfigSchema,
62
+ PredictionFunding: () => PredictionFunding,
63
+ PredictionOrderManager: () => PredictionOrderManager,
64
+ PredictionPositionManager: () => PredictionPositionManager,
65
+ PredictionTradeRecorder: () => PredictionTradeRecorder,
58
66
  RelayClient: () => RelayClient,
59
67
  RelayConfigSchema: () => RelayConfigSchema,
60
68
  RiskManager: () => RiskManager,
@@ -66,9 +74,12 @@ __export(index_exports, {
66
74
  VaultConfigSchema: () => VaultConfigSchema,
67
75
  VaultManager: () => VaultManager,
68
76
  VaultPolicySchema: () => VaultPolicySchema,
77
+ calculatePredictionFee: () => calculatePredictionFee,
69
78
  createLLMAdapter: () => createLLMAdapter,
70
79
  createSampleConfig: () => createSampleConfig,
80
+ decodePredictionInstrument: () => decodePredictionInstrument,
71
81
  decryptEnvFile: () => decryptEnvFile,
82
+ encodePredictionInstrument: () => encodePredictionInstrument,
72
83
  encryptEnvFile: () => encryptEnvFile,
73
84
  fillHashToBytes32: () => fillHashToBytes32,
74
85
  fillOidToBytes32: () => fillOidToBytes32,
@@ -78,6 +89,8 @@ __export(index_exports, {
78
89
  loadConfig: () => loadConfig,
79
90
  loadSecureEnv: () => loadSecureEnv,
80
91
  loadStrategy: () => loadStrategy,
92
+ orderIdToBytes32: () => orderIdToBytes32,
93
+ tradeIdToBytes32: () => tradeIdToBytes32,
81
94
  validateConfig: () => validateConfig,
82
95
  validateStrategy: () => validateStrategy
83
96
  });
@@ -85,9 +98,9 @@ module.exports = __toCommonJS(index_exports);
85
98
 
86
99
  // src/runtime.ts
87
100
  var import_sdk = require("@exagent/sdk");
88
- var import_viem7 = require("viem");
89
- var import_chains5 = require("viem/chains");
90
- var import_accounts5 = require("viem/accounts");
101
+ var import_viem11 = require("viem");
102
+ var import_chains8 = require("viem/chains");
103
+ var import_accounts8 = require("viem/accounts");
91
104
 
92
105
  // src/trading/market.ts
93
106
  var import_viem2 = require("viem");
@@ -2001,6 +2014,49 @@ var PerpConfigSchema = import_zod.z.object({
2001
2014
  /** Allowed perp instruments (e.g. ["ETH", "BTC", "SOL"]). If empty, all instruments allowed. */
2002
2015
  allowedInstruments: import_zod.z.array(import_zod.z.string()).optional()
2003
2016
  }).optional();
2017
+ var PredictionConfigSchema = import_zod.z.object({
2018
+ /** Enable prediction market trading */
2019
+ enabled: import_zod.z.boolean().default(false),
2020
+ /** Polymarket CLOB API URL */
2021
+ clobApiUrl: import_zod.z.string().url().default("https://clob.polymarket.com"),
2022
+ /** Gamma API URL for market discovery */
2023
+ gammaApiUrl: import_zod.z.string().url().default("https://gamma-api.polymarket.com"),
2024
+ /** Polygon RPC URL */
2025
+ polygonRpcUrl: import_zod.z.string().url().optional(),
2026
+ /** CLOB API key (L2 auth) — derived from wallet signature if not provided */
2027
+ clobApiKey: import_zod.z.string().optional(),
2028
+ /** CLOB API secret (L2 auth) */
2029
+ clobApiSecret: import_zod.z.string().optional(),
2030
+ /** CLOB API passphrase (L2 auth) */
2031
+ clobApiPassphrase: import_zod.z.string().optional(),
2032
+ /** Private key for the prediction relayer (calls recordPerpTrade on Base). Falls back to agent wallet. */
2033
+ predictionRelayerKey: import_zod.z.string().optional(),
2034
+ /** Maximum notional per trade in USD (default: 500) */
2035
+ maxNotionalUSD: import_zod.z.number().min(1).default(500),
2036
+ /** Maximum total exposure across all markets in USD (default: 5000) */
2037
+ maxTotalExposureUSD: import_zod.z.number().min(1).default(5e3),
2038
+ /** Target USDC allocation on Polygon in USD (default: 100) */
2039
+ polygonAllocationUSD: import_zod.z.number().min(1).default(100),
2040
+ /** Allowed market categories (empty = all categories) */
2041
+ allowedCategories: import_zod.z.array(import_zod.z.string()).optional(),
2042
+ /** Fee bridge threshold in USD — accumulated fees are bridged to Base when exceeded (default: 10) */
2043
+ feeBridgeThresholdUSD: import_zod.z.number().min(1).default(10),
2044
+ /** Prediction vault configuration (ERC-4626 + ERC-1271 on Polygon) */
2045
+ vault: import_zod.z.object({
2046
+ /** Enable vault mode for prediction trading */
2047
+ enabled: import_zod.z.boolean().default(false),
2048
+ /** PredictionVaultFactory address on Polygon */
2049
+ factoryAddress: import_zod.z.string(),
2050
+ /** PredictionFeeCollector address on Polygon */
2051
+ feeCollectorAddress: import_zod.z.string().optional(),
2052
+ /** Existing vault address (if already created) */
2053
+ vaultAddress: import_zod.z.string().optional(),
2054
+ /** Agent's fee recipient address */
2055
+ feeRecipient: import_zod.z.string().optional(),
2056
+ /** Whether agent is verified (higher deposit cap) */
2057
+ isVerified: import_zod.z.boolean().default(false)
2058
+ }).optional()
2059
+ }).optional();
2004
2060
  var AgentConfigSchema = import_zod.z.object({
2005
2061
  // Identity (from on-chain registration)
2006
2062
  agentId: import_zod.z.union([import_zod.z.number().positive(), import_zod.z.string()]),
@@ -2020,6 +2076,8 @@ var AgentConfigSchema = import_zod.z.object({
2020
2076
  relay: RelayConfigSchema,
2021
2077
  // Perp trading configuration (Hyperliquid)
2022
2078
  perp: PerpConfigSchema,
2079
+ // Prediction market configuration (Polymarket)
2080
+ prediction: PredictionConfigSchema,
2023
2081
  // Allowed tokens (addresses)
2024
2082
  allowedTokens: import_zod.z.array(import_zod.z.string()).optional()
2025
2083
  });
@@ -4987,205 +5045,2061 @@ var CORE_DEPOSIT_WALLET_ABI = (0, import_viem6.parseAbi)([
4987
5045
  "function deposit(uint256 amount, uint32 destinationDex) external"
4988
5046
  ]);
4989
5047
 
4990
- // src/runtime.ts
4991
- var FUNDS_LOW_THRESHOLD = 5e-3;
4992
- var FUNDS_CRITICAL_THRESHOLD = 1e-3;
4993
- var AgentRuntime = class {
5048
+ // src/prediction/types.ts
5049
+ var DEFAULT_PREDICTION_CONFIG = {
5050
+ clobApiUrl: "https://clob.polymarket.com",
5051
+ gammaApiUrl: "https://gamma-api.polymarket.com",
5052
+ polygonRpcUrl: "https://polygon-rpc.com",
5053
+ maxNotionalUSD: 1e3,
5054
+ maxTotalExposureUSD: 5e3,
5055
+ polygonAllocationUSD: 100,
5056
+ feeBridgeThresholdUSD: 10
5057
+ };
5058
+ var POLYGON_CHAIN_ID = 137;
5059
+ var POLYMARKET_SIGNATURE_TYPE_EOA = 0;
5060
+ var POLYMARKET_SIGNATURE_TYPE_GNOSIS_SAFE = 1;
5061
+ var POLYGON_USDC_ADDRESS = "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359";
5062
+ var POLYGON_TOKEN_MESSENGER_V2 = "0x28b5a0e9C621a5BadaA536219b3a228C8168cf5d";
5063
+ var POLYGON_MESSAGE_TRANSMITTER_V2 = "0x81D40F21F12A8F0E3252Bccb954D722d4c464B64";
5064
+ var CCTP_DOMAIN_BASE = 6;
5065
+ var CCTP_DOMAIN_POLYGON = 7;
5066
+ var PREDICTION_INSTRUMENT_PREFIX = "POLY:";
5067
+
5068
+ // src/prediction/client.ts
5069
+ var import_clob_client = require("@polymarket/clob-client");
5070
+ var import_ethers = require("ethers");
5071
+ var PolymarketClient = class {
4994
5072
  config;
4995
- client;
4996
- llm;
4997
- strategy;
4998
- executor;
4999
- riskManager;
5000
- marketData;
5001
- vaultManager;
5002
- relay = null;
5003
- isRunning = false;
5004
- mode = "idle";
5005
- configHash;
5006
- pendingConfigHash = null;
5007
- lastConfigCheckAt = 0;
5008
- // Timestamp of last pending config RPC check
5009
- cycleCount = 0;
5010
- lastCycleAt = 0;
5011
- lastPortfolioValue = 0;
5012
- lastEthBalance = "0";
5013
- lastPrices = {};
5014
- processAlive = true;
5015
- riskUniverse = 0;
5016
- allowedTokens = /* @__PURE__ */ new Set();
5017
- strategyContext;
5018
- positionTracker;
5019
- // Paper trading components (null when not in paper mode)
5020
- paperExecutor = null;
5021
- paperPortfolio = null;
5022
- /** Whether agent was started directly in paper mode via CLI */
5023
- startInPaperMode = false;
5024
- // Perp trading components (null if perp not enabled)
5025
- perpClient = null;
5026
- perpSigner = null;
5027
- perpOrders = null;
5028
- perpPositions = null;
5029
- perpWebSocket = null;
5030
- perpRecorder = null;
5031
- perpOnboarding = null;
5032
- perpStrategy = null;
5033
- // Two-layer perp control:
5034
- // perpConnected = Hyperliquid infrastructure is initialized (WS, signer, recorder ready)
5035
- // perpTradingActive = Dedicated perp trading cycle is mandated to run
5036
- // When perpConnected && !perpTradingActive: agent's strategy can optionally return perp signals
5037
- // When perpConnected && perpTradingActive: dedicated runPerpCycle() runs every interval
5038
- perpConnected = false;
5039
- perpTradingActive = false;
5040
- // Cached perp account data for synchronous heartbeat inclusion (refreshed async)
5041
- cachedPerpEquity = 0;
5042
- cachedPerpUnrealizedPnl = 0;
5043
- cachedPerpMarginUsed = 0;
5044
- cachedPerpLeverage = 0;
5045
- cachedPerpOpenPositions = 0;
5046
- constructor(config, options) {
5047
- this.config = config;
5048
- this.startInPaperMode = options?.paperMode ?? false;
5049
- this._paperInitialBalances = options?.paperBalances;
5073
+ clobClient = null;
5074
+ apiCreds = null;
5075
+ signer;
5076
+ walletAddress;
5077
+ /** Vault mode: when true, orders use maker=vaultAddress with POLY_GNOSIS_SAFE signature type */
5078
+ vaultMode;
5079
+ /** Vault contract address (only set in vault mode) */
5080
+ vaultAddress;
5081
+ /** Cache for Gamma API market data (conditionId -> market) */
5082
+ marketCache = /* @__PURE__ */ new Map();
5083
+ CACHE_TTL_MS = 6e4;
5084
+ // 60 seconds
5085
+ constructor(privateKey, config, vaultOpts) {
5086
+ this.config = { ...DEFAULT_PREDICTION_CONFIG, ...config };
5087
+ this.signer = new import_ethers.Wallet(privateKey);
5088
+ this.walletAddress = this.signer.address;
5089
+ this.vaultMode = vaultOpts?.vaultMode ?? false;
5090
+ this.vaultAddress = vaultOpts?.vaultAddress;
5050
5091
  }
5051
- /** Initial balances override for paper mode (from CLI --initial-eth/--initial-usdc) */
5052
- _paperInitialBalances;
5092
+ // ============================================================
5093
+ // INITIALIZATION
5094
+ // ============================================================
5053
5095
  /**
5054
- * Initialize the agent runtime
5096
+ * Initialize the CLOB client with L2 API credentials.
5097
+ * Must be called once before placing orders.
5055
5098
  */
5056
5099
  async initialize() {
5057
- console.log(`Initializing agent: ${this.config.name} (ID: ${this.config.agentId})`);
5058
- this.client = new import_sdk.ExagentClient({
5059
- privateKey: this.config.privateKey,
5060
- network: this.config.network
5061
- });
5062
- console.log(`Wallet: ${this.client.address}`);
5063
- const agent = await this.client.registry.getAgent(BigInt(this.config.agentId));
5064
- if (!agent) {
5065
- throw new Error(`Agent ID ${this.config.agentId} not found on-chain. Please register first.`);
5100
+ const initClient = new import_clob_client.ClobClient(
5101
+ this.config.clobApiUrl,
5102
+ POLYGON_CHAIN_ID,
5103
+ this.signer
5104
+ );
5105
+ this.apiCreds = await initClient.createOrDeriveApiKey();
5106
+ const signatureType = this.vaultMode ? POLYMARKET_SIGNATURE_TYPE_GNOSIS_SAFE : POLYMARKET_SIGNATURE_TYPE_EOA;
5107
+ this.clobClient = new import_clob_client.ClobClient(
5108
+ this.config.clobApiUrl,
5109
+ POLYGON_CHAIN_ID,
5110
+ this.signer,
5111
+ this.apiCreds,
5112
+ signatureType
5113
+ );
5114
+ if (this.vaultMode) {
5115
+ console.log(`Polymarket CLOB initialized in VAULT MODE \u2014 maker: ${this.vaultAddress}, signer: ${this.walletAddress}`);
5116
+ } else {
5117
+ console.log(`Polymarket CLOB initialized for ${this.walletAddress}`);
5066
5118
  }
5067
- console.log(`Agent verified: ${agent.name}`);
5068
- await this.ensureWalletLinked();
5069
- await this.loadTradingRestrictions();
5070
- console.log(`Initializing LLM: ${this.config.llm.provider}`);
5071
- this.llm = await createLLMAdapter(this.config.llm);
5072
- const llmMeta = this.llm.getMetadata();
5073
- console.log(`LLM ready: ${llmMeta.provider} (${llmMeta.model})`);
5074
- await this.syncConfigHash();
5075
- this.strategy = await loadStrategy();
5076
- const store = new FileStore();
5077
- this.strategyContext = {
5078
- store,
5079
- agentId: Number(this.config.agentId),
5080
- walletAddress: this.client.address
5119
+ }
5120
+ /**
5121
+ * Check if the client is initialized with CLOB credentials.
5122
+ */
5123
+ get isInitialized() {
5124
+ return this.clobClient !== null && this.apiCreds !== null;
5125
+ }
5126
+ // ============================================================
5127
+ // CLOB API — ORDER BOOK & PRICES
5128
+ // ============================================================
5129
+ /**
5130
+ * Get the order book for a specific outcome token.
5131
+ */
5132
+ async getOrderBook(tokenId) {
5133
+ this.ensureInitialized();
5134
+ const book = await this.clobClient.getOrderBook(tokenId);
5135
+ return {
5136
+ bids: (book.bids || []).map((b) => ({ price: parseFloat(b.price), size: parseFloat(b.size) })),
5137
+ asks: (book.asks || []).map((a) => ({ price: parseFloat(a.price), size: parseFloat(a.size) }))
5081
5138
  };
5082
- this.positionTracker = new PositionTracker(store, { maxTradeHistory: 50 });
5083
- this.executor = new TradeExecutor(this.client, this.config, () => this.getConfigHash());
5084
- this.riskManager = new RiskManager(this.config.trading, this.riskUniverse);
5085
- this.marketData = new MarketDataService(this.getRpcUrl(), store);
5086
- setGlobalResolver(this.marketData.getResolver());
5087
- const savedRisk = this.positionTracker.getRiskState();
5088
- if (savedRisk.lastResetDate) {
5089
- this.riskManager.restoreState(savedRisk);
5090
- }
5091
- await this.initializeVaultManager();
5092
- await this.initializePerp();
5093
- await this.initializeRelay();
5094
- console.log("Agent initialized successfully");
5095
5139
  }
5096
5140
  /**
5097
- * Initialize the relay client for command center connectivity
5141
+ * Get the midpoint price for an outcome token.
5098
5142
  */
5099
- async initializeRelay() {
5100
- const relayConfig = this.config.relay;
5101
- const relayEnabled = process.env.EXAGENT_RELAY_ENABLED !== "false";
5102
- if (!relayConfig?.enabled || !relayEnabled) {
5103
- console.log("Relay: Disabled");
5104
- return;
5105
- }
5106
- const apiUrl = process.env.EXAGENT_API_URL || relayConfig.apiUrl;
5107
- if (!apiUrl) {
5108
- console.log("Relay: No API URL configured, skipping");
5109
- return;
5110
- }
5111
- this.relay = new RelayClient({
5112
- agentId: String(this.config.agentId),
5113
- privateKey: this.config.privateKey,
5114
- relay: {
5115
- ...relayConfig,
5116
- apiUrl
5117
- },
5118
- onCommand: (cmd) => this.handleCommand(cmd)
5143
+ async getMidpointPrice(tokenId) {
5144
+ const book = await this.getOrderBook(tokenId);
5145
+ if (book.bids.length === 0 || book.asks.length === 0) return 0;
5146
+ return (book.bids[0].price + book.asks[0].price) / 2;
5147
+ }
5148
+ /**
5149
+ * Get the last trade price for an outcome token.
5150
+ */
5151
+ async getLastTradePrice(tokenId) {
5152
+ this.ensureInitialized();
5153
+ const resp = await this.clobClient.getLastTradePrice(tokenId);
5154
+ return parseFloat(resp?.price || "0");
5155
+ }
5156
+ // ============================================================
5157
+ // CLOB API — ORDERS
5158
+ // ============================================================
5159
+ /**
5160
+ * Place a limit order on the CLOB.
5161
+ */
5162
+ async placeLimitOrder(params) {
5163
+ this.ensureInitialized();
5164
+ const order = await this.clobClient.createAndPostOrder({
5165
+ tokenID: params.tokenId,
5166
+ price: params.price,
5167
+ side: params.side,
5168
+ size: params.size
5169
+ });
5170
+ return {
5171
+ orderId: order?.orderID || "",
5172
+ success: !!order?.orderID
5173
+ };
5174
+ }
5175
+ /**
5176
+ * Place a market order (aggressive limit at best available price).
5177
+ */
5178
+ async placeMarketOrder(params) {
5179
+ this.ensureInitialized();
5180
+ const order = await this.clobClient.createMarketOrder({
5181
+ tokenID: params.tokenId,
5182
+ amount: params.amount,
5183
+ side: params.side
5119
5184
  });
5185
+ const result = await this.clobClient.postOrder(order);
5186
+ return {
5187
+ orderId: result?.orderID || "",
5188
+ success: !!result?.orderID
5189
+ };
5190
+ }
5191
+ /**
5192
+ * Cancel an open order by ID.
5193
+ */
5194
+ async cancelOrder(orderId) {
5195
+ this.ensureInitialized();
5120
5196
  try {
5121
- await this.relay.connect();
5122
- console.log("Relay: Connected to command center");
5123
- this.sendRelayStatus();
5124
- } catch (error) {
5125
- console.warn(
5126
- "Relay: Failed to connect (agent will work locally):",
5127
- error instanceof Error ? error.message : error
5128
- );
5197
+ await this.clobClient.cancelOrder({ orderID: orderId });
5198
+ return true;
5199
+ } catch {
5200
+ return false;
5129
5201
  }
5130
5202
  }
5131
5203
  /**
5132
- * Initialize the vault manager based on config
5204
+ * Cancel all open orders.
5133
5205
  */
5134
- async initializeVaultManager() {
5135
- const vaultConfig = this.config.vault || { policy: "disabled", preferVaultTrading: false };
5136
- this.vaultManager = new VaultManager({
5137
- agentId: BigInt(this.config.agentId),
5138
- agentName: this.config.name,
5139
- network: this.config.network,
5140
- walletKey: this.config.privateKey,
5141
- vaultConfig
5142
- });
5143
- console.log(`Vault policy: ${vaultConfig.policy}`);
5144
- const status = await this.vaultManager.getVaultStatus();
5145
- if (status.hasVault) {
5146
- console.log(`Vault exists: ${status.vaultAddress}`);
5147
- console.log(`Vault TVL: ${Number(status.totalAssets) / 1e6} USDC`);
5148
- } else {
5149
- console.log("No vault exists for this agent");
5150
- if (vaultConfig.policy === "manual") {
5151
- console.log("Vault creation is manual \u2014 use the command center to create one");
5152
- }
5206
+ async cancelAllOrders() {
5207
+ this.ensureInitialized();
5208
+ try {
5209
+ await this.clobClient.cancelAll();
5210
+ return true;
5211
+ } catch {
5212
+ return false;
5153
5213
  }
5154
5214
  }
5155
5215
  /**
5156
- * Initialize Hyperliquid perp trading components.
5157
- * Only initializes if perp is enabled in config AND risk universe >= 2.
5216
+ * Get open orders for the agent's wallet.
5158
5217
  */
5159
- async initializePerp() {
5160
- const perpConfig = this.config.perp;
5161
- if (!perpConfig?.enabled) {
5162
- console.log("Perp trading: Disabled");
5163
- return;
5218
+ async getOpenOrders() {
5219
+ this.ensureInitialized();
5220
+ return this.clobClient.getOpenOrders();
5221
+ }
5222
+ /**
5223
+ * Get trade history (fills) for the agent's wallet.
5224
+ */
5225
+ async getTradeHistory() {
5226
+ this.ensureInitialized();
5227
+ const trades = await this.clobClient.getTrades();
5228
+ return (trades || []).map((t) => this.parseRawFill(t));
5229
+ }
5230
+ // ============================================================
5231
+ // GAMMA API — MARKET DISCOVERY (public, no auth)
5232
+ // ============================================================
5233
+ /**
5234
+ * Get active prediction markets from Gamma API.
5235
+ */
5236
+ async getMarkets(params) {
5237
+ const query = new URLSearchParams();
5238
+ if (params?.limit) query.set("limit", String(params.limit));
5239
+ if (params?.offset) query.set("offset", String(params.offset));
5240
+ if (params?.active !== void 0) query.set("active", String(params.active));
5241
+ if (params?.category) query.set("tag", params.category);
5242
+ const url = `${this.config.gammaApiUrl}/markets?${query.toString()}`;
5243
+ const resp = await fetch(url);
5244
+ if (!resp.ok) throw new Error(`Gamma API error: ${resp.status} ${await resp.text()}`);
5245
+ const raw = await resp.json();
5246
+ return (raw || []).map((m) => this.parseGammaMarket(m));
5247
+ }
5248
+ /**
5249
+ * Get a single market by condition ID.
5250
+ */
5251
+ async getMarketByConditionId(conditionId) {
5252
+ const cached = this.marketCache.get(conditionId);
5253
+ if (cached && Date.now() - cached.cachedAt < this.CACHE_TTL_MS) {
5254
+ return cached.market;
5164
5255
  }
5165
- if (this.riskUniverse < 2) {
5166
- console.warn(`Perp trading: Blocked by risk universe ${this.riskUniverse} (need >= 2)`);
5167
- return;
5256
+ const url = `${this.config.gammaApiUrl}/markets?condition_id=${conditionId}`;
5257
+ const resp = await fetch(url);
5258
+ if (!resp.ok) return null;
5259
+ const raw = await resp.json();
5260
+ if (!raw || raw.length === 0) return null;
5261
+ const market = this.parseGammaMarket(raw[0]);
5262
+ this.marketCache.set(conditionId, { market, cachedAt: Date.now() });
5263
+ return market;
5264
+ }
5265
+ /**
5266
+ * Search markets by query string.
5267
+ */
5268
+ async searchMarkets(query, limit = 20) {
5269
+ const url = `${this.config.gammaApiUrl}/markets?_q=${encodeURIComponent(query)}&limit=${limit}&active=true`;
5270
+ const resp = await fetch(url);
5271
+ if (!resp.ok) return [];
5272
+ const raw = await resp.json();
5273
+ return (raw || []).map((m) => this.parseGammaMarket(m));
5274
+ }
5275
+ /**
5276
+ * Get trending markets sorted by volume.
5277
+ */
5278
+ async getTrendingMarkets(limit = 10) {
5279
+ const url = `${this.config.gammaApiUrl}/markets?active=true&limit=${limit}&order=volume24hr&ascending=false`;
5280
+ const resp = await fetch(url);
5281
+ if (!resp.ok) return [];
5282
+ const raw = await resp.json();
5283
+ return (raw || []).map((m) => this.parseGammaMarket(m));
5284
+ }
5285
+ // ============================================================
5286
+ // WALLET ADDRESS
5287
+ // ============================================================
5288
+ getWalletAddress() {
5289
+ return this.walletAddress;
5290
+ }
5291
+ /**
5292
+ * Get the effective maker address for CLOB orders.
5293
+ * In vault mode, this is the vault contract address.
5294
+ * In normal mode, this is the agent's EOA address.
5295
+ */
5296
+ getMakerAddress() {
5297
+ return this.vaultMode && this.vaultAddress ? this.vaultAddress : this.walletAddress;
5298
+ }
5299
+ /**
5300
+ * Whether the client is operating in vault mode.
5301
+ */
5302
+ get isVaultMode() {
5303
+ return this.vaultMode;
5304
+ }
5305
+ /**
5306
+ * Get the vault address (null if not in vault mode).
5307
+ */
5308
+ getVaultAddress() {
5309
+ return this.vaultAddress;
5310
+ }
5311
+ // ============================================================
5312
+ // PRIVATE HELPERS
5313
+ // ============================================================
5314
+ ensureInitialized() {
5315
+ if (!this.clobClient) {
5316
+ throw new Error("PolymarketClient not initialized. Call initialize() first.");
5168
5317
  }
5169
- try {
5170
- const config = {
5171
- enabled: true,
5172
- apiUrl: perpConfig.apiUrl || "https://api.hyperliquid.xyz",
5173
- wsUrl: perpConfig.wsUrl || "wss://api.hyperliquid.xyz/ws",
5174
- builderAddress: perpConfig.builderAddress,
5175
- builderFeeTenthsBps: perpConfig.builderFeeTenthsBps ?? 100,
5176
- maxLeverage: perpConfig.maxLeverage ?? 10,
5177
- maxNotionalUSD: perpConfig.maxNotionalUSD ?? 5e4,
5178
- allowedInstruments: perpConfig.allowedInstruments
5179
- };
5180
- this.perpClient = new HyperliquidClient(config);
5181
- const perpKey = perpConfig.perpRelayerKey || this.config.privateKey;
5182
- const account = (0, import_accounts5.privateKeyToAccount)(perpKey);
5183
- const walletClient = (0, import_viem7.createWalletClient)({
5184
- chain: { id: 42161, name: "Arbitrum", nativeCurrency: { name: "ETH", symbol: "ETH", decimals: 18 }, rpcUrls: { default: { http: ["https://arb1.arbitrum.io/rpc"] } } },
5185
- transport: (0, import_viem7.http)("https://arb1.arbitrum.io/rpc", { timeout: 6e4 }),
5186
- account
5187
- });
5188
- this.perpSigner = new HyperliquidSigner(walletClient);
5318
+ }
5319
+ parseGammaMarket(raw) {
5320
+ const outcomes = raw.outcomes ? JSON.parse(raw.outcomes) : ["Yes", "No"];
5321
+ const outcomePrices = raw.outcomePrices ? JSON.parse(raw.outcomePrices) : [0, 0];
5322
+ const outcomeTokenIds = raw.clobTokenIds ? JSON.parse(raw.clobTokenIds) : [];
5323
+ return {
5324
+ conditionId: raw.conditionId || raw.condition_id || "",
5325
+ question: raw.question || "",
5326
+ description: raw.description || "",
5327
+ category: raw.groupItemTitle || raw.category || "Other",
5328
+ outcomes,
5329
+ outcomeTokenIds,
5330
+ outcomePrices: outcomePrices.map((p) => parseFloat(p) || 0),
5331
+ volume24h: parseFloat(raw.volume24hr || raw.volume24h || "0"),
5332
+ liquidity: parseFloat(raw.liquidity || "0"),
5333
+ endDate: raw.endDate ? new Date(raw.endDate).getTime() / 1e3 : 0,
5334
+ active: raw.active !== false && raw.closed !== true,
5335
+ resolved: raw.resolved === true,
5336
+ winningOutcome: raw.winningOutcome,
5337
+ resolutionSource: raw.resolutionSource || void 0,
5338
+ uniqueTraders: raw.uniqueTraders || void 0
5339
+ };
5340
+ }
5341
+ parseRawFill(raw) {
5342
+ const tokenId = raw.asset_id || void 0;
5343
+ const marketConditionId = raw.market || raw.asset_id || "";
5344
+ return {
5345
+ orderId: raw.orderId || raw.order_id || "",
5346
+ tradeId: raw.id || raw.tradeId || "",
5347
+ marketConditionId,
5348
+ outcomeIndex: raw.outcome_index ?? (raw.side === "BUY" ? 0 : 1),
5349
+ side: raw.side === "BUY" || raw.side === "buy" ? "BUY" : "SELL",
5350
+ price: String(raw.price || "0"),
5351
+ size: String(raw.size || "0"),
5352
+ fee: String(raw.fee || "0"),
5353
+ timestamp: raw.timestamp || raw.created_at ? new Date(raw.created_at).getTime() : Date.now(),
5354
+ isMaker: raw.maker_order || raw.is_maker || false,
5355
+ tokenId
5356
+ };
5357
+ }
5358
+ };
5359
+
5360
+ // src/prediction/signer.ts
5361
+ var import_viem7 = require("viem");
5362
+ function tradeIdToBytes32(tradeId) {
5363
+ if (tradeId.startsWith("0x") && tradeId.length === 66) {
5364
+ return tradeId;
5365
+ }
5366
+ return (0, import_viem7.keccak256)((0, import_viem7.encodePacked)(["string"], [tradeId]));
5367
+ }
5368
+ function orderIdToBytes32(orderId) {
5369
+ return (0, import_viem7.keccak256)((0, import_viem7.encodePacked)(["string"], [`poly-order:${orderId}`]));
5370
+ }
5371
+ function encodePredictionInstrument(conditionId, outcomeIndex) {
5372
+ return `${PREDICTION_INSTRUMENT_PREFIX}${conditionId}:${outcomeIndex}`;
5373
+ }
5374
+ function decodePredictionInstrument(instrument) {
5375
+ if (!instrument.startsWith(PREDICTION_INSTRUMENT_PREFIX)) return null;
5376
+ const payload = instrument.slice(PREDICTION_INSTRUMENT_PREFIX.length);
5377
+ const lastColon = payload.lastIndexOf(":");
5378
+ if (lastColon === -1) return null;
5379
+ const conditionId = payload.slice(0, lastColon);
5380
+ const outcomeIndex = parseInt(payload.slice(lastColon + 1), 10);
5381
+ if (isNaN(outcomeIndex)) return null;
5382
+ return { conditionId, outcomeIndex };
5383
+ }
5384
+ function calculatePredictionFee(notionalUSD) {
5385
+ return notionalUSD * 20n / 10000n;
5386
+ }
5387
+
5388
+ // src/prediction/order-manager.ts
5389
+ var PredictionOrderManager = class {
5390
+ client;
5391
+ config;
5392
+ /** Recent fills tracked for recording */
5393
+ recentFills = [];
5394
+ MAX_RECENT_FILLS = 100;
5395
+ constructor(client, config) {
5396
+ this.client = client;
5397
+ this.config = config;
5398
+ }
5399
+ // ============================================================
5400
+ // ORDER PLACEMENT
5401
+ // ============================================================
5402
+ /**
5403
+ * Execute a prediction trade signal.
5404
+ * Routes to limit or market order based on signal.orderType.
5405
+ */
5406
+ async executeSignal(signal) {
5407
+ try {
5408
+ const violation = this.checkRiskLimits(signal);
5409
+ if (violation) {
5410
+ return { success: false, status: "error", error: violation };
5411
+ }
5412
+ const market = await this.client.getMarketByConditionId(signal.marketConditionId);
5413
+ if (!market) {
5414
+ return { success: false, status: "error", error: `Market not found: ${signal.marketConditionId}` };
5415
+ }
5416
+ if (!market.active) {
5417
+ return { success: false, status: "error", error: `Market is closed: ${signal.marketQuestion}` };
5418
+ }
5419
+ const tokenId = market.outcomeTokenIds[signal.outcomeIndex];
5420
+ if (!tokenId) {
5421
+ return { success: false, status: "error", error: `Invalid outcome index ${signal.outcomeIndex} for market` };
5422
+ }
5423
+ const isBuy = signal.action === "buy_yes" || signal.action === "buy_no";
5424
+ const side = isBuy ? "BUY" : "SELL";
5425
+ let result;
5426
+ if (signal.orderType === "market") {
5427
+ result = await this.client.placeMarketOrder({
5428
+ tokenId,
5429
+ amount: signal.amount,
5430
+ side
5431
+ });
5432
+ } else {
5433
+ result = await this.client.placeLimitOrder({
5434
+ tokenId,
5435
+ price: signal.limitPrice,
5436
+ size: signal.amount,
5437
+ side
5438
+ });
5439
+ }
5440
+ if (result.success) {
5441
+ console.log(
5442
+ `Prediction order placed: ${signal.action} ${signal.amount} @ $${signal.limitPrice} \u2014 "${signal.marketQuestion}" \u2014 order: ${result.orderId}`
5443
+ );
5444
+ return {
5445
+ success: true,
5446
+ orderId: result.orderId,
5447
+ status: signal.orderType === "market" ? "filled" : "resting"
5448
+ };
5449
+ }
5450
+ return { success: false, status: "error", error: "Order placement failed" };
5451
+ } catch (error) {
5452
+ const message = error instanceof Error ? error.message : String(error);
5453
+ console.error(`Prediction order failed for "${signal.marketQuestion}":`, message);
5454
+ return { success: false, status: "error", error: message };
5455
+ }
5456
+ }
5457
+ /**
5458
+ * Cancel an open order by ID.
5459
+ */
5460
+ async cancelOrder(orderId) {
5461
+ return this.client.cancelOrder(orderId);
5462
+ }
5463
+ /**
5464
+ * Cancel all open orders.
5465
+ */
5466
+ async cancelAllOrders() {
5467
+ return this.client.cancelAllOrders();
5468
+ }
5469
+ // ============================================================
5470
+ // FILL TRACKING
5471
+ // ============================================================
5472
+ /**
5473
+ * Poll for new fills since last check.
5474
+ * Returns fills that haven't been seen before.
5475
+ */
5476
+ async pollNewFills() {
5477
+ try {
5478
+ const allFills = await this.client.getTradeHistory();
5479
+ const seenIds = new Set(this.recentFills.map((f) => f.tradeId));
5480
+ const newFills = allFills.filter((f) => !seenIds.has(f.tradeId));
5481
+ for (const fill of newFills) {
5482
+ this.recentFills.push(fill);
5483
+ if (this.recentFills.length > this.MAX_RECENT_FILLS) {
5484
+ this.recentFills.shift();
5485
+ }
5486
+ }
5487
+ return newFills;
5488
+ } catch (error) {
5489
+ const message = error instanceof Error ? error.message : String(error);
5490
+ console.error("Failed to poll prediction fills:", message);
5491
+ return [];
5492
+ }
5493
+ }
5494
+ /**
5495
+ * Get recent fills (from local cache).
5496
+ */
5497
+ getRecentFills() {
5498
+ return [...this.recentFills];
5499
+ }
5500
+ // ============================================================
5501
+ // RISK CHECKS
5502
+ // ============================================================
5503
+ /**
5504
+ * Check if a signal violates risk limits.
5505
+ * Returns error string if violated, null if OK.
5506
+ */
5507
+ checkRiskLimits(signal) {
5508
+ if (signal.amount > this.config.maxNotionalUSD) {
5509
+ return `Trade amount $${signal.amount} exceeds max notional $${this.config.maxNotionalUSD}`;
5510
+ }
5511
+ if (signal.limitPrice <= 0 || signal.limitPrice >= 1) {
5512
+ return `Limit price ${signal.limitPrice} out of bounds (must be 0.01-0.99)`;
5513
+ }
5514
+ if (signal.confidence < 0.3) {
5515
+ return `Confidence ${signal.confidence} below minimum threshold (0.3)`;
5516
+ }
5517
+ if (this.config.allowedCategories && this.config.allowedCategories.length > 0) {
5518
+ }
5519
+ return null;
5520
+ }
5521
+ };
5522
+
5523
+ // src/prediction/position-manager.ts
5524
+ var PredictionPositionManager = class {
5525
+ client;
5526
+ config;
5527
+ /** Local position tracking (conditionId:outcomeIndex -> position data) */
5528
+ positions = /* @__PURE__ */ new Map();
5529
+ /** Cache TTL */
5530
+ lastPriceRefresh = 0;
5531
+ PRICE_REFRESH_MS = 1e4;
5532
+ // 10 seconds
5533
+ constructor(client, config) {
5534
+ this.client = client;
5535
+ this.config = config;
5536
+ }
5537
+ // ============================================================
5538
+ // POSITION QUERIES
5539
+ // ============================================================
5540
+ /**
5541
+ * Get all open prediction positions with current prices.
5542
+ */
5543
+ async getPositions(forceRefresh = false) {
5544
+ if (forceRefresh || Date.now() - this.lastPriceRefresh > this.PRICE_REFRESH_MS) {
5545
+ await this.refreshPrices();
5546
+ }
5547
+ return Array.from(this.positions.values()).filter((p) => p.balance > 0).map((p) => this.toExternalPosition(p));
5548
+ }
5549
+ /**
5550
+ * Get a specific position.
5551
+ */
5552
+ async getPosition(conditionId, outcomeIndex) {
5553
+ const key = `${conditionId}:${outcomeIndex}`;
5554
+ const pos = this.positions.get(key);
5555
+ if (!pos || pos.balance <= 0) return null;
5556
+ return this.toExternalPosition(pos);
5557
+ }
5558
+ /**
5559
+ * Get account summary (balances, exposure, PnL).
5560
+ */
5561
+ async getAccountSummary() {
5562
+ const positions = await this.getPositions();
5563
+ const totalExposure = positions.reduce((sum, p) => sum + p.costBasis, 0);
5564
+ const totalUnrealizedPnl = positions.reduce((sum, p) => sum + p.unrealizedPnl, 0);
5565
+ const openMarkets = new Set(positions.map((p) => p.marketConditionId));
5566
+ return {
5567
+ polygonUSDC: 0,
5568
+ // Filled in by runtime from on-chain balance
5569
+ baseUSDC: 0,
5570
+ // Filled in by runtime from on-chain balance
5571
+ totalExposure,
5572
+ totalUnrealizedPnl,
5573
+ openMarketCount: openMarkets.size,
5574
+ openPositionCount: positions.length,
5575
+ pendingFees: 0
5576
+ // Filled in by recorder
5577
+ };
5578
+ }
5579
+ /**
5580
+ * Get total exposure across all positions.
5581
+ */
5582
+ getTotalExposure() {
5583
+ let total = 0;
5584
+ for (const pos of this.positions.values()) {
5585
+ if (pos.balance > 0) total += pos.totalCostBasis;
5586
+ }
5587
+ return total;
5588
+ }
5589
+ /**
5590
+ * Get total unrealized PnL.
5591
+ */
5592
+ getTotalUnrealizedPnl() {
5593
+ let total = 0;
5594
+ for (const pos of this.positions.values()) {
5595
+ if (pos.balance > 0) {
5596
+ total += (pos.currentPrice - pos.averageEntryPrice) * pos.balance;
5597
+ }
5598
+ }
5599
+ return total;
5600
+ }
5601
+ // ============================================================
5602
+ // FILL PROCESSING
5603
+ // ============================================================
5604
+ /**
5605
+ * Process a fill and update position tracking.
5606
+ * Called when a new fill is detected by the order manager.
5607
+ */
5608
+ processFill(fill) {
5609
+ const key = `${fill.marketConditionId}:${fill.outcomeIndex}`;
5610
+ let pos = this.positions.get(key);
5611
+ const price = parseFloat(fill.price);
5612
+ const size = parseFloat(fill.size);
5613
+ if (!pos) {
5614
+ pos = {
5615
+ marketConditionId: fill.marketConditionId,
5616
+ outcomeIndex: fill.outcomeIndex,
5617
+ marketQuestion: fill.marketQuestion || "",
5618
+ tokenId: fill.tokenId || "",
5619
+ balance: 0,
5620
+ totalBought: 0,
5621
+ totalSold: 0,
5622
+ totalCostBasis: 0,
5623
+ totalProceeds: 0,
5624
+ averageEntryPrice: 0,
5625
+ currentPrice: price,
5626
+ category: void 0,
5627
+ endDate: void 0
5628
+ };
5629
+ this.positions.set(key, pos);
5630
+ } else if (!pos.tokenId && fill.tokenId) {
5631
+ pos.tokenId = fill.tokenId;
5632
+ }
5633
+ if (fill.side === "BUY") {
5634
+ const oldCost = pos.averageEntryPrice * pos.balance;
5635
+ const newCost = price * size;
5636
+ pos.balance += size;
5637
+ pos.totalBought += size;
5638
+ pos.totalCostBasis += newCost;
5639
+ pos.averageEntryPrice = pos.balance > 0 ? (oldCost + newCost) / pos.balance : 0;
5640
+ } else {
5641
+ pos.balance -= size;
5642
+ pos.totalSold += size;
5643
+ pos.totalProceeds += price * size;
5644
+ if (pos.balance < 0) pos.balance = 0;
5645
+ }
5646
+ }
5647
+ /**
5648
+ * Process multiple fills (batch update).
5649
+ */
5650
+ processFills(fills) {
5651
+ for (const fill of fills) {
5652
+ this.processFill(fill);
5653
+ }
5654
+ }
5655
+ // ============================================================
5656
+ // MARKET RESOLUTION
5657
+ // ============================================================
5658
+ /**
5659
+ * Mark a market as resolved. Positions settle at $0 or $1.
5660
+ */
5661
+ resolveMarket(conditionId, winningOutcome) {
5662
+ for (const [key, pos] of this.positions.entries()) {
5663
+ if (pos.marketConditionId === conditionId) {
5664
+ const isWinner = pos.outcomeIndex === winningOutcome;
5665
+ pos.currentPrice = isWinner ? 1 : 0;
5666
+ if (pos.balance > 0) {
5667
+ pos.totalProceeds += pos.currentPrice * pos.balance;
5668
+ pos.totalSold += pos.balance;
5669
+ pos.balance = 0;
5670
+ }
5671
+ }
5672
+ }
5673
+ }
5674
+ // ============================================================
5675
+ // PERSISTENCE
5676
+ // ============================================================
5677
+ /**
5678
+ * Export position state for persistence across restarts.
5679
+ */
5680
+ exportState() {
5681
+ return Array.from(this.positions.values());
5682
+ }
5683
+ /**
5684
+ * Import position state from previous session.
5685
+ */
5686
+ importState(state) {
5687
+ this.positions.clear();
5688
+ for (const pos of state) {
5689
+ const key = `${pos.marketConditionId}:${pos.outcomeIndex}`;
5690
+ this.positions.set(key, pos);
5691
+ }
5692
+ }
5693
+ // ============================================================
5694
+ // PRIVATE
5695
+ // ============================================================
5696
+ /**
5697
+ * Refresh current prices for all open positions from CLOB.
5698
+ */
5699
+ async refreshPrices() {
5700
+ const openPositions = Array.from(this.positions.values()).filter((p) => p.balance > 0);
5701
+ await Promise.all(
5702
+ openPositions.map(async (pos) => {
5703
+ try {
5704
+ if (!pos.tokenId) {
5705
+ const market = await this.client.getMarketByConditionId(pos.marketConditionId);
5706
+ if (market && market.outcomeTokenIds[pos.outcomeIndex]) {
5707
+ pos.tokenId = market.outcomeTokenIds[pos.outcomeIndex];
5708
+ }
5709
+ }
5710
+ if (pos.tokenId) {
5711
+ const price = await this.client.getMidpointPrice(pos.tokenId);
5712
+ if (price > 0) pos.currentPrice = price;
5713
+ }
5714
+ } catch {
5715
+ }
5716
+ })
5717
+ );
5718
+ this.lastPriceRefresh = Date.now();
5719
+ }
5720
+ toExternalPosition(pos) {
5721
+ const unrealizedPnl = pos.balance > 0 ? (pos.currentPrice - pos.averageEntryPrice) * pos.balance : 0;
5722
+ return {
5723
+ marketConditionId: pos.marketConditionId,
5724
+ marketQuestion: pos.marketQuestion,
5725
+ outcomeIndex: pos.outcomeIndex,
5726
+ outcomeLabel: pos.outcomeIndex === 0 ? "Yes" : "No",
5727
+ tokenId: pos.tokenId,
5728
+ balance: pos.balance,
5729
+ averageEntryPrice: pos.averageEntryPrice,
5730
+ currentPrice: pos.currentPrice,
5731
+ unrealizedPnl,
5732
+ costBasis: pos.totalCostBasis - pos.totalProceeds,
5733
+ endDate: pos.endDate,
5734
+ category: pos.category
5735
+ };
5736
+ }
5737
+ };
5738
+
5739
+ // src/prediction/recorder.ts
5740
+ var import_viem8 = require("viem");
5741
+ var import_chains5 = require("viem/chains");
5742
+ var import_accounts5 = require("viem/accounts");
5743
+ var ROUTER_ADDRESS2 = "0x1BCFa13f677fDCf697D8b7d5120f544817F1de1A";
5744
+ var ROUTER_ABI2 = [
5745
+ {
5746
+ type: "function",
5747
+ name: "recordPerpTrade",
5748
+ stateMutability: "nonpayable",
5749
+ inputs: [
5750
+ { name: "agentId", type: "uint256" },
5751
+ { name: "configHash", type: "bytes32" },
5752
+ { name: "instrument", type: "string" },
5753
+ { name: "isLong", type: "bool" },
5754
+ { name: "notionalUSD", type: "uint256" },
5755
+ { name: "feeUSD", type: "uint256" },
5756
+ { name: "fillId", type: "bytes32" }
5757
+ ],
5758
+ outputs: [{ name: "", type: "bool" }]
5759
+ }
5760
+ ];
5761
+ var MAX_RETRIES2 = 3;
5762
+ var RETRY_DELAY_MS2 = 5e3;
5763
+ var PredictionTradeRecorder = class {
5764
+ publicClient;
5765
+ walletClient;
5766
+ account;
5767
+ agentId;
5768
+ configHash;
5769
+ /** Retry queue for failed recordings */
5770
+ retryQueue = [];
5771
+ /** Set of fill IDs already recorded (or in-progress) to prevent local dups */
5772
+ recordedFills = /* @__PURE__ */ new Set();
5773
+ /** Timer for processing retry queue */
5774
+ retryTimer = null;
5775
+ /** Accumulated fees pending bridge to Base treasury (in USD, 6-decimal) */
5776
+ accumulatedFees = 0n;
5777
+ constructor(opts) {
5778
+ this.agentId = opts.agentId;
5779
+ this.configHash = opts.configHash;
5780
+ this.account = (0, import_accounts5.privateKeyToAccount)(opts.privateKey);
5781
+ const rpcUrl = opts.rpcUrl || "https://mainnet.base.org";
5782
+ const transport = (0, import_viem8.http)(rpcUrl, { timeout: 6e4 });
5783
+ this.publicClient = (0, import_viem8.createPublicClient)({
5784
+ chain: import_chains5.base,
5785
+ transport
5786
+ });
5787
+ this.walletClient = (0, import_viem8.createWalletClient)({
5788
+ chain: import_chains5.base,
5789
+ transport,
5790
+ account: this.account
5791
+ });
5792
+ this.retryTimer = setInterval(() => this.processRetryQueue(), RETRY_DELAY_MS2);
5793
+ }
5794
+ // ============================================================
5795
+ // PUBLIC API
5796
+ // ============================================================
5797
+ /**
5798
+ * Record a prediction fill on-chain.
5799
+ * Converts the Polymarket fill into recordPerpTrade params with POLY: prefix.
5800
+ */
5801
+ async recordFill(fill) {
5802
+ const fillId = tradeIdToBytes32(fill.tradeId);
5803
+ const fillIdStr = fillId.toLowerCase();
5804
+ if (this.recordedFills.has(fillIdStr)) {
5805
+ return { success: true };
5806
+ }
5807
+ this.recordedFills.add(fillIdStr);
5808
+ const notionalUSD = this.calculateNotionalUSD(fill);
5809
+ const feeUSD = calculatePredictionFee(notionalUSD);
5810
+ const params = {
5811
+ agentId: this.agentId,
5812
+ configHash: this.configHash,
5813
+ instrument: encodePredictionInstrument(fill.marketConditionId, fill.outcomeIndex),
5814
+ isLong: fill.side === "BUY",
5815
+ notionalUSD,
5816
+ feeUSD,
5817
+ fillId
5818
+ };
5819
+ this.accumulatedFees += feeUSD;
5820
+ return this.submitRecord(params);
5821
+ }
5822
+ /**
5823
+ * Update the config hash (when epoch changes).
5824
+ */
5825
+ updateConfigHash(configHash) {
5826
+ this.configHash = configHash;
5827
+ }
5828
+ /**
5829
+ * Get the number of fills pending retry.
5830
+ */
5831
+ get pendingRetries() {
5832
+ return this.retryQueue.length;
5833
+ }
5834
+ /**
5835
+ * Get the number of fills recorded (local dedup set size).
5836
+ */
5837
+ get recordedCount() {
5838
+ return this.recordedFills.size;
5839
+ }
5840
+ /**
5841
+ * Get accumulated fees pending bridge (6-decimal USD).
5842
+ */
5843
+ get pendingFees() {
5844
+ return this.accumulatedFees;
5845
+ }
5846
+ /**
5847
+ * Get accumulated fees as human-readable USD.
5848
+ */
5849
+ get pendingFeesUSD() {
5850
+ return Number(this.accumulatedFees) / 1e6;
5851
+ }
5852
+ /**
5853
+ * Reset accumulated fees (after bridge to treasury).
5854
+ */
5855
+ resetAccumulatedFees() {
5856
+ this.accumulatedFees = 0n;
5857
+ }
5858
+ /**
5859
+ * Stop the recorder (clear retry timer).
5860
+ */
5861
+ stop() {
5862
+ if (this.retryTimer) {
5863
+ clearInterval(this.retryTimer);
5864
+ this.retryTimer = null;
5865
+ }
5866
+ }
5867
+ // ============================================================
5868
+ // PRIVATE
5869
+ // ============================================================
5870
+ /**
5871
+ * Submit a recordPerpTrade transaction on Base.
5872
+ */
5873
+ async submitRecord(params) {
5874
+ try {
5875
+ const { request } = await this.publicClient.simulateContract({
5876
+ address: ROUTER_ADDRESS2,
5877
+ abi: ROUTER_ABI2,
5878
+ functionName: "recordPerpTrade",
5879
+ args: [
5880
+ params.agentId,
5881
+ params.configHash,
5882
+ params.instrument,
5883
+ params.isLong,
5884
+ params.notionalUSD,
5885
+ params.feeUSD,
5886
+ params.fillId
5887
+ ],
5888
+ account: this.account
5889
+ });
5890
+ const txHash = await this.walletClient.writeContract(request);
5891
+ const action = params.isLong ? "BUY" : "SELL";
5892
+ console.log(
5893
+ `Prediction trade recorded: ${action} ${params.instrument} $${Number(params.notionalUSD) / 1e6} \u2014 tx: ${txHash}`
5894
+ );
5895
+ return { success: true, txHash };
5896
+ } catch (error) {
5897
+ const message = error instanceof Error ? error.message : String(error);
5898
+ if (message.includes("FillAlreadyProcessed") || message.includes("already recorded")) {
5899
+ console.log(`Prediction fill already recorded on-chain: ${params.fillId}`);
5900
+ return { success: true };
5901
+ }
5902
+ console.error(`Failed to record prediction trade: ${message}`);
5903
+ this.retryQueue.push({
5904
+ params,
5905
+ retries: 0,
5906
+ lastAttempt: Date.now()
5907
+ });
5908
+ return { success: false, error: message };
5909
+ }
5910
+ }
5911
+ /**
5912
+ * Process the retry queue — attempt to re-submit failed recordings.
5913
+ */
5914
+ async processRetryQueue() {
5915
+ if (this.retryQueue.length === 0) return;
5916
+ const now = Date.now();
5917
+ const toRetry = this.retryQueue.filter(
5918
+ (item) => now - item.lastAttempt >= RETRY_DELAY_MS2
5919
+ );
5920
+ for (const item of toRetry) {
5921
+ item.retries++;
5922
+ item.lastAttempt = now;
5923
+ if (item.retries > MAX_RETRIES2) {
5924
+ console.error(
5925
+ `Prediction trade recording permanently failed after ${MAX_RETRIES2} retries: ${item.params.instrument} ${item.params.fillId}`
5926
+ );
5927
+ const idx = this.retryQueue.indexOf(item);
5928
+ if (idx >= 0) this.retryQueue.splice(idx, 1);
5929
+ continue;
5930
+ }
5931
+ console.log(
5932
+ `Retrying prediction trade recording (attempt ${item.retries}/${MAX_RETRIES2}): ${item.params.instrument}`
5933
+ );
5934
+ const result = await this.submitRecord(item.params);
5935
+ if (result.success) {
5936
+ const idx = this.retryQueue.indexOf(item);
5937
+ if (idx >= 0) this.retryQueue.splice(idx, 1);
5938
+ }
5939
+ }
5940
+ }
5941
+ // ============================================================
5942
+ // CONVERSION HELPERS
5943
+ // ============================================================
5944
+ /**
5945
+ * Calculate notional USD from a fill (6-decimal).
5946
+ * notionalUSD = price * size * 1e6
5947
+ */
5948
+ calculateNotionalUSD(fill) {
5949
+ const price = parseFloat(fill.price);
5950
+ const size = parseFloat(fill.size);
5951
+ return BigInt(Math.round(price * size * 1e6));
5952
+ }
5953
+ };
5954
+
5955
+ // src/prediction/market-browser.ts
5956
+ var MARKET_CATEGORIES = [
5957
+ "crypto",
5958
+ "politics",
5959
+ "sports",
5960
+ "entertainment",
5961
+ "science",
5962
+ "business",
5963
+ "culture",
5964
+ "weather",
5965
+ "tech"
5966
+ ];
5967
+ var MarketBrowser = class {
5968
+ gammaUrl;
5969
+ allowedCategories;
5970
+ /** Cache for market listings */
5971
+ listCache = /* @__PURE__ */ new Map();
5972
+ CACHE_TTL_MS = 6e4;
5973
+ constructor(config) {
5974
+ const merged = { ...DEFAULT_PREDICTION_CONFIG, ...config };
5975
+ this.gammaUrl = merged.gammaApiUrl;
5976
+ this.allowedCategories = merged.allowedCategories || [];
5977
+ }
5978
+ // ============================================================
5979
+ // PUBLIC API
5980
+ // ============================================================
5981
+ /**
5982
+ * Get active markets, optionally filtered by category.
5983
+ * Results are sorted by volume (highest first).
5984
+ */
5985
+ async getActiveMarkets(params) {
5986
+ const limit = params?.limit || 20;
5987
+ const offset = params?.offset || 0;
5988
+ const cacheKey = `active:${params?.category || "all"}:${limit}:${offset}`;
5989
+ const cached = this.listCache.get(cacheKey);
5990
+ if (cached && Date.now() - cached.cachedAt < this.CACHE_TTL_MS) {
5991
+ return cached.data;
5992
+ }
5993
+ const query = new URLSearchParams({
5994
+ active: "true",
5995
+ closed: "false",
5996
+ limit: String(limit),
5997
+ offset: String(offset),
5998
+ order: "volume24hr",
5999
+ ascending: "false"
6000
+ });
6001
+ if (params?.category) query.set("tag", params.category);
6002
+ const markets = await this.fetchGamma(`/markets?${query.toString()}`);
6003
+ const parsed = markets.map((m) => this.parseMarket(m));
6004
+ const filtered = this.allowedCategories.length > 0 ? parsed.filter(
6005
+ (m) => this.allowedCategories.some((c) => m.category.toLowerCase().includes(c.toLowerCase()))
6006
+ ) : parsed;
6007
+ this.listCache.set(cacheKey, { data: filtered, cachedAt: Date.now() });
6008
+ return filtered;
6009
+ }
6010
+ /**
6011
+ * Search markets by query text.
6012
+ */
6013
+ async searchMarkets(query, limit = 20) {
6014
+ const params = new URLSearchParams({
6015
+ _q: query,
6016
+ limit: String(limit),
6017
+ active: "true"
6018
+ });
6019
+ const markets = await this.fetchGamma(`/markets?${params.toString()}`);
6020
+ return markets.map((m) => this.parseMarket(m));
6021
+ }
6022
+ /**
6023
+ * Get trending markets (highest 24h volume).
6024
+ */
6025
+ async getTrendingMarkets(limit = 10) {
6026
+ return this.getActiveMarkets({ limit });
6027
+ }
6028
+ /**
6029
+ * Get markets expiring soon (within N days).
6030
+ */
6031
+ async getExpiringMarkets(withinDays = 7, limit = 20) {
6032
+ const markets = await this.getActiveMarkets({ limit: 100 });
6033
+ const cutoff = Date.now() / 1e3 + withinDays * 86400;
6034
+ return markets.filter((m) => m.endDate > 0 && m.endDate < cutoff).sort((a, b) => a.endDate - b.endDate).slice(0, limit);
6035
+ }
6036
+ /**
6037
+ * Get recently resolved markets (for PnL tracking).
6038
+ */
6039
+ async getRecentlyResolved(limit = 20) {
6040
+ const params = new URLSearchParams({
6041
+ closed: "true",
6042
+ limit: String(limit),
6043
+ order: "endDate",
6044
+ ascending: "false"
6045
+ });
6046
+ const markets = await this.fetchGamma(`/markets?${params.toString()}`);
6047
+ return markets.map((m) => this.parseMarket(m)).filter((m) => m.resolved);
6048
+ }
6049
+ /**
6050
+ * Get a single market's details by condition ID.
6051
+ */
6052
+ async getMarketDetail(conditionId) {
6053
+ const markets = await this.fetchGamma(`/markets?condition_id=${conditionId}`);
6054
+ if (!markets || markets.length === 0) return null;
6055
+ return this.parseMarket(markets[0]);
6056
+ }
6057
+ /**
6058
+ * Build a concise market summary string for LLM context.
6059
+ * Keeps the prompt token count manageable.
6060
+ */
6061
+ formatMarketsForLLM(markets, maxMarkets = 15) {
6062
+ const subset = markets.slice(0, maxMarkets);
6063
+ const lines = subset.map((m, i) => {
6064
+ const prices = m.outcomePrices.map((p, j) => `${m.outcomes[j]}: ${(p * 100).toFixed(1)}%`).join(", ");
6065
+ const vol = m.volume24h >= 1e3 ? `$${(m.volume24h / 1e3).toFixed(1)}K` : `$${m.volume24h.toFixed(0)}`;
6066
+ const endStr = m.endDate > 0 ? new Date(m.endDate * 1e3).toISOString().split("T")[0] : "TBD";
6067
+ return `${i + 1}. [${m.category}] "${m.question}" \u2014 ${prices} | Vol: ${vol} | Ends: ${endStr} | ID: ${m.conditionId}`;
6068
+ });
6069
+ return lines.join("\n");
6070
+ }
6071
+ // ============================================================
6072
+ // PRIVATE
6073
+ // ============================================================
6074
+ async fetchGamma(path4) {
6075
+ const url = `${this.gammaUrl}${path4}`;
6076
+ const resp = await fetch(url);
6077
+ if (!resp.ok) {
6078
+ console.error(`Gamma API error: ${resp.status} for ${url}`);
6079
+ return [];
6080
+ }
6081
+ const data = await resp.json();
6082
+ return Array.isArray(data) ? data : [];
6083
+ }
6084
+ parseMarket(raw) {
6085
+ const outcomes = raw.outcomes ? JSON.parse(raw.outcomes) : ["Yes", "No"];
6086
+ const outcomePrices = raw.outcomePrices ? JSON.parse(raw.outcomePrices) : [0, 0];
6087
+ const outcomeTokenIds = raw.clobTokenIds ? JSON.parse(raw.clobTokenIds) : [];
6088
+ return {
6089
+ conditionId: raw.conditionId || raw.condition_id || "",
6090
+ question: raw.question || "",
6091
+ description: raw.description || "",
6092
+ category: raw.groupItemTitle || raw.category || "Other",
6093
+ outcomes,
6094
+ outcomeTokenIds,
6095
+ outcomePrices: outcomePrices.map((p) => parseFloat(p) || 0),
6096
+ volume24h: parseFloat(raw.volume24hr || raw.volume24h || "0"),
6097
+ liquidity: parseFloat(raw.liquidity || "0"),
6098
+ endDate: raw.endDate ? new Date(raw.endDate).getTime() / 1e3 : 0,
6099
+ active: raw.active !== false && raw.closed !== true,
6100
+ resolved: raw.resolved === true,
6101
+ winningOutcome: raw.winningOutcome,
6102
+ resolutionSource: raw.resolutionSource || void 0,
6103
+ uniqueTraders: raw.uniqueTraders || void 0
6104
+ };
6105
+ }
6106
+ };
6107
+
6108
+ // src/prediction/funding.ts
6109
+ var import_viem9 = require("viem");
6110
+ var import_chains6 = require("viem/chains");
6111
+ var import_accounts6 = require("viem/accounts");
6112
+ var CONTRACTS = {
6113
+ // Base
6114
+ BASE_USDC: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
6115
+ BASE_TOKEN_MESSENGER_V2: "0x28b5a0e9C621a5BadaA536219b3a228C8168cf5d",
6116
+ BASE_MESSAGE_TRANSMITTER_V2: "0x81D40F21F12A8F0E3252Bccb954D722d4c464B64",
6117
+ // Polygon
6118
+ POLYGON_USDC: POLYGON_USDC_ADDRESS,
6119
+ POLYGON_TOKEN_MESSENGER_V2,
6120
+ POLYGON_MESSAGE_TRANSMITTER_V2
6121
+ };
6122
+ var IRIS_API_URL = "https://iris-api.circle.com/v1/attestations";
6123
+ var MIN_BRIDGE_AMOUNT = 1000000n;
6124
+ var ERC20_ABI2 = (0, import_viem9.parseAbi)([
6125
+ "function approve(address spender, uint256 amount) external returns (bool)",
6126
+ "function balanceOf(address account) external view returns (uint256)",
6127
+ "function allowance(address owner, address spender) external view returns (uint256)"
6128
+ ]);
6129
+ var TOKEN_MESSENGER_V2_ABI2 = (0, import_viem9.parseAbi)([
6130
+ "function depositForBurn(uint256 amount, uint32 destinationDomain, bytes32 mintRecipient, address burnToken) external returns (uint64 nonce)",
6131
+ "event DepositForBurn(uint64 indexed nonce, address indexed burnToken, uint256 amount, address indexed depositor, bytes32 mintRecipient, uint32 destinationDomain, bytes32 destinationTokenMessenger, bytes32 destinationCaller)"
6132
+ ]);
6133
+ var MESSAGE_TRANSMITTER_V2_ABI2 = (0, import_viem9.parseAbi)([
6134
+ "function receiveMessage(bytes message, bytes attestation) external returns (bool success)",
6135
+ "event MessageSent(bytes message)"
6136
+ ]);
6137
+ var PredictionFunding = class {
6138
+ basePublic;
6139
+ baseWallet;
6140
+ polygonPublic;
6141
+ polygonWallet;
6142
+ account;
6143
+ /** Override recipient for deposits to Polygon (vault address in vault mode) */
6144
+ polygonRecipient;
6145
+ constructor(config) {
6146
+ this.account = (0, import_accounts6.privateKeyToAccount)(config.privateKey);
6147
+ this.polygonRecipient = config.polygonRecipient;
6148
+ const baseTransport = (0, import_viem9.http)(config.baseRpcUrl || "https://mainnet.base.org", { timeout: 6e4 });
6149
+ const polygonTransport = (0, import_viem9.http)(config.polygonRpcUrl || "https://polygon-rpc.com", { timeout: 6e4 });
6150
+ this.basePublic = (0, import_viem9.createPublicClient)({ chain: import_chains6.base, transport: baseTransport });
6151
+ this.baseWallet = (0, import_viem9.createWalletClient)({ chain: import_chains6.base, transport: baseTransport, account: this.account });
6152
+ this.polygonPublic = (0, import_viem9.createPublicClient)({ chain: import_chains6.polygon, transport: polygonTransport });
6153
+ this.polygonWallet = (0, import_viem9.createWalletClient)({ chain: import_chains6.polygon, transport: polygonTransport, account: this.account });
6154
+ }
6155
+ // ============================================================
6156
+ // BALANCE QUERIES
6157
+ // ============================================================
6158
+ /** Get USDC balance on Base (6-decimal) */
6159
+ async getBaseUSDCBalance() {
6160
+ return this.basePublic.readContract({
6161
+ address: CONTRACTS.BASE_USDC,
6162
+ abi: ERC20_ABI2,
6163
+ functionName: "balanceOf",
6164
+ args: [this.account.address]
6165
+ });
6166
+ }
6167
+ /** Get USDC balance on Polygon (6-decimal) */
6168
+ async getPolygonUSDCBalance() {
6169
+ return this.polygonPublic.readContract({
6170
+ address: CONTRACTS.POLYGON_USDC,
6171
+ abi: ERC20_ABI2,
6172
+ functionName: "balanceOf",
6173
+ args: [this.account.address]
6174
+ });
6175
+ }
6176
+ /** Get MATIC balance on Polygon for gas (wei) */
6177
+ async getPolygonGasBalance() {
6178
+ return this.polygonPublic.getBalance({ address: this.account.address });
6179
+ }
6180
+ // ============================================================
6181
+ // DEPOSIT: Base → Polygon
6182
+ // ============================================================
6183
+ /**
6184
+ * Bridge USDC from Base to Polygon via CCTP V2.
6185
+ *
6186
+ * Steps:
6187
+ * 1. Approve USDC on Base for TokenMessengerV2
6188
+ * 2. Call depositForBurn (Base → Polygon)
6189
+ * 3. Wait for Circle attestation (~1-5 minutes)
6190
+ * 4. Call receiveMessage on Polygon
6191
+ */
6192
+ async depositToPolygon(amount, onStep) {
6193
+ const startTime = Date.now();
6194
+ if (amount < MIN_BRIDGE_AMOUNT) {
6195
+ return { success: false, amount, error: "Amount below minimum (1 USDC)" };
6196
+ }
6197
+ try {
6198
+ onStep?.("approve_usdc");
6199
+ await this.approveIfNeeded(
6200
+ this.basePublic,
6201
+ this.baseWallet,
6202
+ CONTRACTS.BASE_USDC,
6203
+ CONTRACTS.BASE_TOKEN_MESSENGER_V2,
6204
+ amount
6205
+ );
6206
+ onStep?.("cctp_burn");
6207
+ const recipient = this.polygonRecipient ?? this.account.address;
6208
+ const mintRecipient = this.addressToBytes32(recipient);
6209
+ const burnHash = await this.baseWallet.writeContract({
6210
+ address: CONTRACTS.BASE_TOKEN_MESSENGER_V2,
6211
+ abi: TOKEN_MESSENGER_V2_ABI2,
6212
+ functionName: "depositForBurn",
6213
+ args: [amount, CCTP_DOMAIN_POLYGON, mintRecipient, CONTRACTS.BASE_USDC]
6214
+ });
6215
+ const burnReceipt = await this.basePublic.waitForTransactionReceipt({ hash: burnHash });
6216
+ const messageBytes = this.extractMessageFromReceipt(burnReceipt);
6217
+ if (!messageBytes) {
6218
+ return { success: false, amount, burnTxHash: burnHash, error: "Failed to extract CCTP message from burn tx" };
6219
+ }
6220
+ onStep?.("wait_attestation");
6221
+ const attestation = await this.waitForAttestation(messageBytes);
6222
+ if (!attestation) {
6223
+ return { success: false, amount, burnTxHash: burnHash, error: "Attestation timeout (15 min)" };
6224
+ }
6225
+ onStep?.("cctp_receive");
6226
+ const receiveHash = await this.polygonWallet.writeContract({
6227
+ address: CONTRACTS.POLYGON_MESSAGE_TRANSMITTER_V2,
6228
+ abi: MESSAGE_TRANSMITTER_V2_ABI2,
6229
+ functionName: "receiveMessage",
6230
+ args: [messageBytes, attestation]
6231
+ });
6232
+ await this.polygonPublic.waitForTransactionReceipt({ hash: receiveHash });
6233
+ console.log(`CCTP deposit complete: ${Number(amount) / 1e6} USDC Base \u2192 Polygon in ${((Date.now() - startTime) / 1e3).toFixed(0)}s`);
6234
+ return {
6235
+ success: true,
6236
+ amount,
6237
+ burnTxHash: burnHash,
6238
+ receiveTxHash: receiveHash,
6239
+ duration: Date.now() - startTime
6240
+ };
6241
+ } catch (error) {
6242
+ const message = error instanceof Error ? error.message : String(error);
6243
+ console.error("CCTP deposit to Polygon failed:", message);
6244
+ return { success: false, amount, error: message, duration: Date.now() - startTime };
6245
+ }
6246
+ }
6247
+ // ============================================================
6248
+ // WITHDRAW: Polygon → Base
6249
+ // ============================================================
6250
+ /**
6251
+ * Bridge USDC from Polygon back to Base via CCTP V2.
6252
+ * Used for fee collection (bridge accumulated fees to Base treasury).
6253
+ */
6254
+ async withdrawToBase(amount, onStep) {
6255
+ const startTime = Date.now();
6256
+ if (amount < MIN_BRIDGE_AMOUNT) {
6257
+ return { success: false, amount, error: "Amount below minimum (1 USDC)" };
6258
+ }
6259
+ try {
6260
+ onStep?.("approve_usdc");
6261
+ await this.approveIfNeeded(
6262
+ this.polygonPublic,
6263
+ this.polygonWallet,
6264
+ CONTRACTS.POLYGON_USDC,
6265
+ CONTRACTS.POLYGON_TOKEN_MESSENGER_V2,
6266
+ amount
6267
+ );
6268
+ onStep?.("cctp_burn");
6269
+ const mintRecipient = this.addressToBytes32(this.account.address);
6270
+ const burnHash = await this.polygonWallet.writeContract({
6271
+ address: CONTRACTS.POLYGON_TOKEN_MESSENGER_V2,
6272
+ abi: TOKEN_MESSENGER_V2_ABI2,
6273
+ functionName: "depositForBurn",
6274
+ args: [amount, CCTP_DOMAIN_BASE, mintRecipient, CONTRACTS.POLYGON_USDC]
6275
+ });
6276
+ const burnReceipt = await this.polygonPublic.waitForTransactionReceipt({ hash: burnHash });
6277
+ const messageBytes = this.extractMessageFromReceipt(burnReceipt);
6278
+ if (!messageBytes) {
6279
+ return { success: false, amount, burnTxHash: burnHash, error: "Failed to extract CCTP message" };
6280
+ }
6281
+ onStep?.("wait_attestation");
6282
+ const attestation = await this.waitForAttestation(messageBytes);
6283
+ if (!attestation) {
6284
+ return { success: false, amount, burnTxHash: burnHash, error: "Attestation timeout" };
6285
+ }
6286
+ onStep?.("cctp_receive");
6287
+ const receiveHash = await this.baseWallet.writeContract({
6288
+ address: CONTRACTS.BASE_MESSAGE_TRANSMITTER_V2,
6289
+ abi: MESSAGE_TRANSMITTER_V2_ABI2,
6290
+ functionName: "receiveMessage",
6291
+ args: [messageBytes, attestation]
6292
+ });
6293
+ await this.basePublic.waitForTransactionReceipt({ hash: receiveHash });
6294
+ console.log(`CCTP withdrawal complete: ${Number(amount) / 1e6} USDC Polygon \u2192 Base in ${((Date.now() - startTime) / 1e3).toFixed(0)}s`);
6295
+ return {
6296
+ success: true,
6297
+ amount,
6298
+ burnTxHash: burnHash,
6299
+ receiveTxHash: receiveHash,
6300
+ duration: Date.now() - startTime
6301
+ };
6302
+ } catch (error) {
6303
+ const message = error instanceof Error ? error.message : String(error);
6304
+ console.error("CCTP withdrawal to Base failed:", message);
6305
+ return { success: false, amount, error: message, duration: Date.now() - startTime };
6306
+ }
6307
+ }
6308
+ // ============================================================
6309
+ // PRIVATE HELPERS
6310
+ // ============================================================
6311
+ /** Convert address to bytes32 (left-padded) for CCTP mintRecipient */
6312
+ addressToBytes32(address) {
6313
+ return `0x000000000000000000000000${address.slice(2)}`;
6314
+ }
6315
+ /** Approve USDC spend if needed (maxUint256 pattern) */
6316
+ async approveIfNeeded(publicClient, walletClient, token, spender, amount) {
6317
+ const allowance = await publicClient.readContract({
6318
+ address: token,
6319
+ abi: ERC20_ABI2,
6320
+ functionName: "allowance",
6321
+ args: [this.account.address, spender]
6322
+ });
6323
+ if (allowance < amount) {
6324
+ const maxUint256 = 2n ** 256n - 1n;
6325
+ const hash = await walletClient.writeContract({
6326
+ address: token,
6327
+ abi: ERC20_ABI2,
6328
+ functionName: "approve",
6329
+ args: [spender, maxUint256]
6330
+ });
6331
+ await publicClient.waitForTransactionReceipt({ hash });
6332
+ }
6333
+ }
6334
+ /** Extract MessageSent bytes from a CCTP burn transaction receipt */
6335
+ extractMessageFromReceipt(receipt) {
6336
+ for (const log of receipt.logs || []) {
6337
+ try {
6338
+ if (log.topics[0] === "0x8c5261668696ce22758910d05bab8f186d6eb247ceac2af2e82c7dc17669b036") {
6339
+ return log.data;
6340
+ }
6341
+ } catch {
6342
+ continue;
6343
+ }
6344
+ }
6345
+ return null;
6346
+ }
6347
+ /** Wait for Circle attestation (polls Iris API) */
6348
+ async waitForAttestation(messageBytes, timeoutMs = 9e5) {
6349
+ const { keccak256: keccak2565 } = await import("viem");
6350
+ const messageHash = keccak2565(messageBytes);
6351
+ const start = Date.now();
6352
+ const pollInterval = 5e3;
6353
+ while (Date.now() - start < timeoutMs) {
6354
+ try {
6355
+ const resp = await fetch(`${IRIS_API_URL}/${messageHash}`);
6356
+ if (resp.ok) {
6357
+ const data = await resp.json();
6358
+ if (data.status === "complete" && data.attestation) {
6359
+ return data.attestation;
6360
+ }
6361
+ }
6362
+ } catch {
6363
+ }
6364
+ await new Promise((resolve) => setTimeout(resolve, pollInterval));
6365
+ }
6366
+ return null;
6367
+ }
6368
+ };
6369
+
6370
+ // src/prediction/vault-manager.ts
6371
+ var import_viem10 = require("viem");
6372
+ var import_chains7 = require("viem/chains");
6373
+ var import_accounts7 = require("viem/accounts");
6374
+ var ERC20_ABI3 = (0, import_viem10.parseAbi)([
6375
+ "function approve(address spender, uint256 amount) external returns (bool)",
6376
+ "function balanceOf(address account) external view returns (uint256)",
6377
+ "function allowance(address owner, address spender) external view returns (uint256)"
6378
+ ]);
6379
+ var VAULT_FACTORY_ABI2 = (0, import_viem10.parseAbi)([
6380
+ "function createVault(uint256 agentId, address agentWallet, uint256 seedAmount, string name, string symbol, address feeRecipient, bool isVerified, bytes agentSig) external returns (address)",
6381
+ "function vaults(uint256 agentId) external view returns (address)",
6382
+ "function isFactoryVault(address vault) external view returns (bool)",
6383
+ "function MIN_SEED_AMOUNT() external view returns (uint256)"
6384
+ ]);
6385
+ var VAULT_ABI2 = (0, import_viem10.parseAbi)([
6386
+ // ERC-4626
6387
+ "function deposit(uint256 assets, address receiver) external returns (uint256)",
6388
+ "function withdraw(uint256 assets, address receiver, address owner) external returns (uint256)",
6389
+ "function redeem(uint256 shares, address receiver, address owner) external returns (uint256)",
6390
+ "function redeemMax(address receiver, address owner) external returns (uint256)",
6391
+ "function totalAssets() external view returns (uint256)",
6392
+ "function totalSupply() external view returns (uint256)",
6393
+ "function balanceOf(address account) external view returns (uint256)",
6394
+ "function convertToAssets(uint256 shares) external view returns (uint256)",
6395
+ "function convertToShares(uint256 assets) external view returns (uint256)",
6396
+ // Vault-specific views
6397
+ "function sharePrice() external view returns (uint256)",
6398
+ "function availableTradingBalance() external view returns (uint256)",
6399
+ "function currentExposureBps() external view returns (uint256)",
6400
+ "function cachedCtfValue() external view returns (uint256)",
6401
+ "function lastCtfUpdate() external view returns (uint256)",
6402
+ "function depositCap() external view returns (uint256)",
6403
+ "function agentSeedAmount() external view returns (uint256)",
6404
+ "function circuitBreakerActive() external view returns (bool)",
6405
+ "function paused() external view returns (bool)",
6406
+ "function pendingTradingVolume() external view returns (uint256)",
6407
+ "function getTrackedTokenCount() external view returns (uint256)",
6408
+ "function agentWallet() external view returns (address)",
6409
+ "function agentId() external view returns (uint256)",
6410
+ "function isVerified() external view returns (bool)",
6411
+ "function maxExposureBps() external view returns (uint256)",
6412
+ // Keeper functions
6413
+ "function updatePositionValues(uint256[] tokenIds, uint256[] values) external",
6414
+ "function collectTradingFees(uint256 volume) external",
6415
+ // Emergency
6416
+ "function emergencyWithdraw() external returns (uint256)"
6417
+ ]);
6418
+ var PredictionVaultManager = class {
6419
+ config;
6420
+ publicClient;
6421
+ walletClient;
6422
+ account;
6423
+ /** Resolved vault address (discovered or created) */
6424
+ vaultAddress = null;
6425
+ /** NAV update timer */
6426
+ navTimer = null;
6427
+ /** Fee collection timer */
6428
+ feeTimer = null;
6429
+ /** Last known accumulated trading volume (for delta tracking) */
6430
+ lastReportedVolume = 0n;
6431
+ constructor(privateKey, config) {
6432
+ this.config = config;
6433
+ this.account = (0, import_accounts7.privateKeyToAccount)(privateKey);
6434
+ const transport = (0, import_viem10.http)(config.polygonRpcUrl, { timeout: 6e4 });
6435
+ this.publicClient = (0, import_viem10.createPublicClient)({ chain: import_chains7.polygon, transport });
6436
+ this.walletClient = (0, import_viem10.createWalletClient)({
6437
+ chain: import_chains7.polygon,
6438
+ transport,
6439
+ account: this.account
6440
+ });
6441
+ if (config.vaultAddress) {
6442
+ this.vaultAddress = config.vaultAddress;
6443
+ }
6444
+ }
6445
+ // ============================================================
6446
+ // INITIALIZATION
6447
+ // ============================================================
6448
+ /**
6449
+ * Initialize the vault manager — discover existing vault or prepare for creation.
6450
+ */
6451
+ async initialize() {
6452
+ if (!this.vaultAddress) {
6453
+ try {
6454
+ const vaultAddr = await this.publicClient.readContract({
6455
+ address: this.config.factoryAddress,
6456
+ abi: VAULT_FACTORY_ABI2,
6457
+ functionName: "vaults",
6458
+ args: [this.config.agentId]
6459
+ });
6460
+ if (vaultAddr && vaultAddr !== "0x0000000000000000000000000000000000000000") {
6461
+ this.vaultAddress = vaultAddr;
6462
+ console.log(`Discovered existing prediction vault: ${this.vaultAddress}`);
6463
+ }
6464
+ } catch {
6465
+ console.log("No existing prediction vault found \u2014 create one via createVault()");
6466
+ }
6467
+ }
6468
+ if (this.vaultAddress) {
6469
+ console.log(`Prediction vault manager initialized for vault: ${this.vaultAddress}`);
6470
+ }
6471
+ }
6472
+ /**
6473
+ * Get the vault address (null if no vault exists).
6474
+ */
6475
+ getVaultAddress() {
6476
+ return this.vaultAddress;
6477
+ }
6478
+ /**
6479
+ * Whether a vault has been created/discovered.
6480
+ */
6481
+ get hasVault() {
6482
+ return this.vaultAddress !== null;
6483
+ }
6484
+ // ============================================================
6485
+ // VAULT CREATION
6486
+ // ============================================================
6487
+ /**
6488
+ * Create a new prediction vault via PredictionVaultFactory.
6489
+ *
6490
+ * Deploys PredictionVault on Polygon with:
6491
+ * - Agent's EOA as the authorized signer for ERC-1271
6492
+ * - Seed USDC transferred from agent wallet to vault
6493
+ * - Deposit cap = seed × multiplier (10x unverified, 100x verified)
6494
+ */
6495
+ async createVault(seedAmount, name, symbol) {
6496
+ if (this.vaultAddress) {
6497
+ return { success: false, error: `Vault already exists: ${this.vaultAddress}` };
6498
+ }
6499
+ try {
6500
+ const message = (0, import_viem10.keccak256)(
6501
+ (0, import_viem10.encodePacked)(
6502
+ ["uint256", "address", "address"],
6503
+ [this.config.agentId, this.account.address, this.config.factoryAddress]
6504
+ )
6505
+ );
6506
+ const agentSig = await this.account.signMessage({ message: { raw: message } });
6507
+ await this.approveIfNeeded(
6508
+ POLYGON_USDC_ADDRESS,
6509
+ this.config.factoryAddress,
6510
+ seedAmount
6511
+ );
6512
+ const hash = await this.walletClient.writeContract({
6513
+ address: this.config.factoryAddress,
6514
+ abi: VAULT_FACTORY_ABI2,
6515
+ functionName: "createVault",
6516
+ args: [
6517
+ this.config.agentId,
6518
+ this.account.address,
6519
+ seedAmount,
6520
+ name,
6521
+ symbol,
6522
+ this.config.feeRecipient,
6523
+ this.config.isVerified ?? false,
6524
+ agentSig
6525
+ ]
6526
+ });
6527
+ const receipt = await this.publicClient.waitForTransactionReceipt({ hash });
6528
+ const vaultAddr = await this.publicClient.readContract({
6529
+ address: this.config.factoryAddress,
6530
+ abi: VAULT_FACTORY_ABI2,
6531
+ functionName: "vaults",
6532
+ args: [this.config.agentId]
6533
+ });
6534
+ this.vaultAddress = vaultAddr;
6535
+ console.log(`Prediction vault created: ${this.vaultAddress} (seed: ${(0, import_viem10.formatUnits)(seedAmount, 6)} USDC)`);
6536
+ return {
6537
+ success: true,
6538
+ vaultAddress: this.vaultAddress,
6539
+ txHash: hash
6540
+ };
6541
+ } catch (error) {
6542
+ const message = error instanceof Error ? error.message : String(error);
6543
+ console.error("Failed to create prediction vault:", message);
6544
+ return { success: false, error: message };
6545
+ }
6546
+ }
6547
+ // ============================================================
6548
+ // DEPOSITS & WITHDRAWALS
6549
+ // ============================================================
6550
+ /**
6551
+ * Deposit USDC into the prediction vault.
6552
+ * Returns the number of shares received.
6553
+ */
6554
+ async deposit(amount) {
6555
+ if (!this.vaultAddress) {
6556
+ return { success: false, error: "No vault exists" };
6557
+ }
6558
+ try {
6559
+ await this.approveIfNeeded(
6560
+ POLYGON_USDC_ADDRESS,
6561
+ this.vaultAddress,
6562
+ amount
6563
+ );
6564
+ const hash = await this.walletClient.writeContract({
6565
+ address: this.vaultAddress,
6566
+ abi: VAULT_ABI2,
6567
+ functionName: "deposit",
6568
+ args: [amount, this.account.address]
6569
+ });
6570
+ const receipt = await this.publicClient.waitForTransactionReceipt({ hash });
6571
+ const shares = await this.publicClient.readContract({
6572
+ address: this.vaultAddress,
6573
+ abi: VAULT_ABI2,
6574
+ functionName: "balanceOf",
6575
+ args: [this.account.address]
6576
+ });
6577
+ console.log(`Deposited ${(0, import_viem10.formatUnits)(amount, 6)} USDC into prediction vault`);
6578
+ return { success: true, shares, txHash: hash };
6579
+ } catch (error) {
6580
+ const message = error instanceof Error ? error.message : String(error);
6581
+ console.error("Vault deposit failed:", message);
6582
+ return { success: false, error: message };
6583
+ }
6584
+ }
6585
+ /**
6586
+ * Withdraw USDC from the prediction vault.
6587
+ * Redeems all shares and returns the USDC received (net of fees).
6588
+ */
6589
+ async withdrawAll() {
6590
+ if (!this.vaultAddress) {
6591
+ return { success: false, error: "No vault exists" };
6592
+ }
6593
+ try {
6594
+ const hash = await this.walletClient.writeContract({
6595
+ address: this.vaultAddress,
6596
+ abi: VAULT_ABI2,
6597
+ functionName: "redeemMax",
6598
+ args: [this.account.address, this.account.address]
6599
+ });
6600
+ const receipt = await this.publicClient.waitForTransactionReceipt({ hash });
6601
+ const balance = await this.publicClient.readContract({
6602
+ address: POLYGON_USDC_ADDRESS,
6603
+ abi: ERC20_ABI3,
6604
+ functionName: "balanceOf",
6605
+ args: [this.account.address]
6606
+ });
6607
+ console.log("Withdrew all shares from prediction vault");
6608
+ return { success: true, assets: balance, txHash: hash };
6609
+ } catch (error) {
6610
+ const message = error instanceof Error ? error.message : String(error);
6611
+ console.error("Vault withdrawal failed:", message);
6612
+ return { success: false, error: message };
6613
+ }
6614
+ }
6615
+ /**
6616
+ * Emergency withdraw with penalty.
6617
+ */
6618
+ async emergencyWithdraw() {
6619
+ if (!this.vaultAddress) {
6620
+ return { success: false, error: "No vault exists" };
6621
+ }
6622
+ try {
6623
+ const hash = await this.walletClient.writeContract({
6624
+ address: this.vaultAddress,
6625
+ abi: VAULT_ABI2,
6626
+ functionName: "emergencyWithdraw",
6627
+ args: []
6628
+ });
6629
+ await this.publicClient.waitForTransactionReceipt({ hash });
6630
+ console.log("Emergency withdrew from prediction vault (penalty applied)");
6631
+ return { success: true, txHash: hash };
6632
+ } catch (error) {
6633
+ const message = error instanceof Error ? error.message : String(error);
6634
+ return { success: false, error: message };
6635
+ }
6636
+ }
6637
+ // ============================================================
6638
+ // VAULT STATUS
6639
+ // ============================================================
6640
+ /**
6641
+ * Get comprehensive vault status.
6642
+ */
6643
+ async getVaultStatus() {
6644
+ if (!this.vaultAddress) {
6645
+ return {
6646
+ vaultAddress: "0x0000000000000000000000000000000000000000",
6647
+ hasVault: false,
6648
+ totalAssets: 0n,
6649
+ totalSupply: 0n,
6650
+ sharePrice: 0,
6651
+ usdcBalance: 0n,
6652
+ cachedCtfValue: 0n,
6653
+ currentExposureBps: 0,
6654
+ depositCap: 0n,
6655
+ seedAmount: 0n,
6656
+ circuitBreakerActive: false,
6657
+ isPaused: false,
6658
+ lastCtfUpdate: 0,
6659
+ pendingTradingVolume: 0n,
6660
+ trackedTokenCount: 0
6661
+ };
6662
+ }
6663
+ try {
6664
+ const [
6665
+ totalAssets,
6666
+ totalSupply,
6667
+ sharePrice,
6668
+ usdcBalance,
6669
+ cachedCtfValue,
6670
+ currentExposureBps,
6671
+ depositCap,
6672
+ seedAmount,
6673
+ circuitBreakerActive,
6674
+ isPaused,
6675
+ lastCtfUpdate,
6676
+ pendingTradingVolume,
6677
+ trackedTokenCount
6678
+ ] = await Promise.all([
6679
+ this.publicClient.readContract({ address: this.vaultAddress, abi: VAULT_ABI2, functionName: "totalAssets" }),
6680
+ this.publicClient.readContract({ address: this.vaultAddress, abi: VAULT_ABI2, functionName: "totalSupply" }),
6681
+ this.publicClient.readContract({ address: this.vaultAddress, abi: VAULT_ABI2, functionName: "sharePrice" }),
6682
+ this.publicClient.readContract({ address: POLYGON_USDC_ADDRESS, abi: ERC20_ABI3, functionName: "balanceOf", args: [this.vaultAddress] }),
6683
+ this.publicClient.readContract({ address: this.vaultAddress, abi: VAULT_ABI2, functionName: "cachedCtfValue" }),
6684
+ this.publicClient.readContract({ address: this.vaultAddress, abi: VAULT_ABI2, functionName: "currentExposureBps" }),
6685
+ this.publicClient.readContract({ address: this.vaultAddress, abi: VAULT_ABI2, functionName: "depositCap" }),
6686
+ this.publicClient.readContract({ address: this.vaultAddress, abi: VAULT_ABI2, functionName: "agentSeedAmount" }),
6687
+ this.publicClient.readContract({ address: this.vaultAddress, abi: VAULT_ABI2, functionName: "circuitBreakerActive" }),
6688
+ this.publicClient.readContract({ address: this.vaultAddress, abi: VAULT_ABI2, functionName: "paused" }),
6689
+ this.publicClient.readContract({ address: this.vaultAddress, abi: VAULT_ABI2, functionName: "lastCtfUpdate" }),
6690
+ this.publicClient.readContract({ address: this.vaultAddress, abi: VAULT_ABI2, functionName: "pendingTradingVolume" }),
6691
+ this.publicClient.readContract({ address: this.vaultAddress, abi: VAULT_ABI2, functionName: "getTrackedTokenCount" })
6692
+ ]);
6693
+ return {
6694
+ vaultAddress: this.vaultAddress,
6695
+ hasVault: true,
6696
+ totalAssets,
6697
+ totalSupply,
6698
+ sharePrice: Number(sharePrice) / 1e6,
6699
+ usdcBalance,
6700
+ cachedCtfValue,
6701
+ currentExposureBps: Number(currentExposureBps),
6702
+ depositCap,
6703
+ seedAmount,
6704
+ circuitBreakerActive,
6705
+ isPaused,
6706
+ lastCtfUpdate: Number(lastCtfUpdate),
6707
+ pendingTradingVolume,
6708
+ trackedTokenCount: Number(trackedTokenCount)
6709
+ };
6710
+ } catch (error) {
6711
+ const message = error instanceof Error ? error.message : String(error);
6712
+ console.error("Failed to read vault status:", message);
6713
+ return {
6714
+ vaultAddress: this.vaultAddress,
6715
+ hasVault: true,
6716
+ totalAssets: 0n,
6717
+ totalSupply: 0n,
6718
+ sharePrice: 0,
6719
+ usdcBalance: 0n,
6720
+ cachedCtfValue: 0n,
6721
+ currentExposureBps: 0,
6722
+ depositCap: 0n,
6723
+ seedAmount: 0n,
6724
+ circuitBreakerActive: false,
6725
+ isPaused: false,
6726
+ lastCtfUpdate: 0,
6727
+ pendingTradingVolume: 0n,
6728
+ trackedTokenCount: 0
6729
+ };
6730
+ }
6731
+ }
6732
+ // ============================================================
6733
+ // KEEPER: NAV UPDATES
6734
+ // ============================================================
6735
+ /**
6736
+ * Update cached CTF position values (keeper function).
6737
+ * Fetches CLOB midpoint prices for all tracked tokens and updates the vault.
6738
+ *
6739
+ * @param tokenIds - CTF token IDs
6740
+ * @param values - USD values in 6-decimal (midpoint price × balance for each token)
6741
+ */
6742
+ async updatePositionValues(tokenIds, values) {
6743
+ if (!this.vaultAddress) {
6744
+ return { success: false, error: "No vault exists" };
6745
+ }
6746
+ try {
6747
+ const hash = await this.walletClient.writeContract({
6748
+ address: this.vaultAddress,
6749
+ abi: VAULT_ABI2,
6750
+ functionName: "updatePositionValues",
6751
+ args: [tokenIds, values]
6752
+ });
6753
+ await this.publicClient.waitForTransactionReceipt({ hash });
6754
+ console.log(`Updated ${tokenIds.length} position values in prediction vault`);
6755
+ return { success: true, txHash: hash };
6756
+ } catch (error) {
6757
+ const message = error instanceof Error ? error.message : String(error);
6758
+ console.error("Failed to update position values:", message);
6759
+ return { success: false, error: message };
6760
+ }
6761
+ }
6762
+ /**
6763
+ * Collect trading fees based on reported volume (keeper function).
6764
+ * Transfers 0.2% of volume as fees to PredictionFeeCollector.
6765
+ *
6766
+ * @param volume - Trading volume to report (6-decimal USDC)
6767
+ */
6768
+ async collectTradingFees(volume) {
6769
+ if (!this.vaultAddress) {
6770
+ return { success: false, error: "No vault exists" };
6771
+ }
6772
+ try {
6773
+ const hash = await this.walletClient.writeContract({
6774
+ address: this.vaultAddress,
6775
+ abi: VAULT_ABI2,
6776
+ functionName: "collectTradingFees",
6777
+ args: [volume]
6778
+ });
6779
+ await this.publicClient.waitForTransactionReceipt({ hash });
6780
+ const feeAmount = volume * 20n / 10000n;
6781
+ console.log(`Collected trading fees: ${(0, import_viem10.formatUnits)(feeAmount, 6)} USDC from ${(0, import_viem10.formatUnits)(volume, 6)} USDC volume`);
6782
+ return { success: true, txHash: hash };
6783
+ } catch (error) {
6784
+ const message = error instanceof Error ? error.message : String(error);
6785
+ console.error("Failed to collect trading fees:", message);
6786
+ return { success: false, error: message };
6787
+ }
6788
+ }
6789
+ // ============================================================
6790
+ // KEEPER LOOPS
6791
+ // ============================================================
6792
+ /**
6793
+ * Start the keeper loops (NAV updates + fee collection).
6794
+ * These run on timers and automatically update vault state.
6795
+ *
6796
+ * @param getPositionValues - Callback that returns current CTF position values
6797
+ * (token IDs and their midpoint-price USD values)
6798
+ * @param getAccumulatedVolume - Callback that returns accumulated trading volume
6799
+ * since last fee collection
6800
+ */
6801
+ startKeeperLoops(getPositionValues, getAccumulatedVolume) {
6802
+ if (!this.vaultAddress) {
6803
+ console.log("Cannot start keeper loops \u2014 no vault exists");
6804
+ return;
6805
+ }
6806
+ const navInterval = this.config.navUpdateIntervalMs ?? 15 * 60 * 1e3;
6807
+ this.navTimer = setInterval(async () => {
6808
+ try {
6809
+ const { tokenIds, values } = await getPositionValues();
6810
+ if (tokenIds.length > 0) {
6811
+ await this.updatePositionValues(tokenIds, values);
6812
+ }
6813
+ } catch (error) {
6814
+ console.error("Keeper NAV update failed:", error instanceof Error ? error.message : error);
6815
+ }
6816
+ }, navInterval);
6817
+ const feeInterval = this.config.feeCollectionIntervalMs ?? 24 * 60 * 60 * 1e3;
6818
+ this.feeTimer = setInterval(async () => {
6819
+ try {
6820
+ const currentVolume = getAccumulatedVolume();
6821
+ const volumeDelta = currentVolume - this.lastReportedVolume;
6822
+ if (volumeDelta > 0n) {
6823
+ await this.collectTradingFees(volumeDelta);
6824
+ this.lastReportedVolume = currentVolume;
6825
+ }
6826
+ } catch (error) {
6827
+ console.error("Keeper fee collection failed:", error instanceof Error ? error.message : error);
6828
+ }
6829
+ }, feeInterval);
6830
+ console.log(`Keeper loops started (NAV: every ${navInterval / 6e4}min, fees: every ${feeInterval / 36e5}h)`);
6831
+ }
6832
+ /**
6833
+ * Stop keeper loops.
6834
+ */
6835
+ stopKeeperLoops() {
6836
+ if (this.navTimer) {
6837
+ clearInterval(this.navTimer);
6838
+ this.navTimer = null;
6839
+ }
6840
+ if (this.feeTimer) {
6841
+ clearInterval(this.feeTimer);
6842
+ this.feeTimer = null;
6843
+ }
6844
+ console.log("Keeper loops stopped");
6845
+ }
6846
+ // ============================================================
6847
+ // CLEANUP
6848
+ // ============================================================
6849
+ /**
6850
+ * Stop all timers and clean up.
6851
+ */
6852
+ stop() {
6853
+ this.stopKeeperLoops();
6854
+ }
6855
+ // ============================================================
6856
+ // PRIVATE HELPERS
6857
+ // ============================================================
6858
+ /** Approve USDC spend if needed (maxUint256 pattern) */
6859
+ async approveIfNeeded(token, spender, amount) {
6860
+ const allowance = await this.publicClient.readContract({
6861
+ address: token,
6862
+ abi: ERC20_ABI3,
6863
+ functionName: "allowance",
6864
+ args: [this.account.address, spender]
6865
+ });
6866
+ if (allowance < amount) {
6867
+ const maxUint256 = 2n ** 256n - 1n;
6868
+ const hash = await this.walletClient.writeContract({
6869
+ address: token,
6870
+ abi: ERC20_ABI3,
6871
+ functionName: "approve",
6872
+ args: [spender, maxUint256]
6873
+ });
6874
+ await this.publicClient.waitForTransactionReceipt({ hash });
6875
+ }
6876
+ }
6877
+ };
6878
+
6879
+ // src/runtime.ts
6880
+ var FUNDS_LOW_THRESHOLD = 5e-3;
6881
+ var FUNDS_CRITICAL_THRESHOLD = 1e-3;
6882
+ var AgentRuntime = class {
6883
+ config;
6884
+ client;
6885
+ llm;
6886
+ strategy;
6887
+ executor;
6888
+ riskManager;
6889
+ marketData;
6890
+ vaultManager;
6891
+ relay = null;
6892
+ isRunning = false;
6893
+ mode = "idle";
6894
+ configHash;
6895
+ pendingConfigHash = null;
6896
+ lastConfigCheckAt = 0;
6897
+ // Timestamp of last pending config RPC check
6898
+ cycleCount = 0;
6899
+ lastCycleAt = 0;
6900
+ lastPortfolioValue = 0;
6901
+ lastEthBalance = "0";
6902
+ lastPrices = {};
6903
+ processAlive = true;
6904
+ riskUniverse = 0;
6905
+ allowedTokens = /* @__PURE__ */ new Set();
6906
+ strategyContext;
6907
+ positionTracker;
6908
+ // Paper trading components (null when not in paper mode)
6909
+ paperExecutor = null;
6910
+ paperPortfolio = null;
6911
+ /** Whether agent was started directly in paper mode via CLI */
6912
+ startInPaperMode = false;
6913
+ // Perp trading components (null if perp not enabled)
6914
+ perpClient = null;
6915
+ perpSigner = null;
6916
+ perpOrders = null;
6917
+ perpPositions = null;
6918
+ perpWebSocket = null;
6919
+ perpRecorder = null;
6920
+ perpOnboarding = null;
6921
+ perpStrategy = null;
6922
+ // Two-layer perp control:
6923
+ // perpConnected = Hyperliquid infrastructure is initialized (WS, signer, recorder ready)
6924
+ // perpTradingActive = Dedicated perp trading cycle is mandated to run
6925
+ // When perpConnected && !perpTradingActive: agent's strategy can optionally return perp signals
6926
+ // When perpConnected && perpTradingActive: dedicated runPerpCycle() runs every interval
6927
+ perpConnected = false;
6928
+ perpTradingActive = false;
6929
+ // Cached perp account data for synchronous heartbeat inclusion (refreshed async)
6930
+ cachedPerpEquity = 0;
6931
+ cachedPerpUnrealizedPnl = 0;
6932
+ cachedPerpMarginUsed = 0;
6933
+ cachedPerpLeverage = 0;
6934
+ cachedPerpOpenPositions = 0;
6935
+ // Prediction market components (null if prediction not enabled)
6936
+ predictionClient = null;
6937
+ predictionOrders = null;
6938
+ predictionPositions = null;
6939
+ predictionRecorder = null;
6940
+ predictionFunding = null;
6941
+ predictionBrowser = null;
6942
+ predictionStrategy = null;
6943
+ // Two-layer prediction control (mirrors perp pattern):
6944
+ // predictionConnected = Polymarket infrastructure initialized (CLOB client, recorder ready)
6945
+ // predictionTradingActive = Dedicated prediction trading cycle is mandated to run
6946
+ predictionConnected = false;
6947
+ predictionTradingActive = false;
6948
+ // Cached prediction data for synchronous heartbeat inclusion
6949
+ cachedPredictionPolygonUSDC = 0;
6950
+ cachedPredictionBaseUSDC = 0;
6951
+ cachedPredictionExposure = 0;
6952
+ cachedPredictionOpenMarkets = 0;
6953
+ cachedPredictionOpenPositions = 0;
6954
+ cachedPredictionUnrealizedPnl = 0;
6955
+ // Prediction vault manager (null if vault mode not enabled)
6956
+ predictionVaultManager = null;
6957
+ // Cached vault status for synchronous heartbeat
6958
+ cachedPredictionVaultStatus = void 0;
6959
+ constructor(config, options) {
6960
+ this.config = config;
6961
+ this.startInPaperMode = options?.paperMode ?? false;
6962
+ this._paperInitialBalances = options?.paperBalances;
6963
+ }
6964
+ /** Initial balances override for paper mode (from CLI --initial-eth/--initial-usdc) */
6965
+ _paperInitialBalances;
6966
+ /**
6967
+ * Initialize the agent runtime
6968
+ */
6969
+ async initialize() {
6970
+ console.log(`Initializing agent: ${this.config.name} (ID: ${this.config.agentId})`);
6971
+ this.client = new import_sdk.ExagentClient({
6972
+ privateKey: this.config.privateKey,
6973
+ network: this.config.network
6974
+ });
6975
+ console.log(`Wallet: ${this.client.address}`);
6976
+ const agent = await this.client.registry.getAgent(BigInt(this.config.agentId));
6977
+ if (!agent) {
6978
+ throw new Error(`Agent ID ${this.config.agentId} not found on-chain. Please register first.`);
6979
+ }
6980
+ console.log(`Agent verified: ${agent.name}`);
6981
+ await this.ensureWalletLinked();
6982
+ await this.loadTradingRestrictions();
6983
+ console.log(`Initializing LLM: ${this.config.llm.provider}`);
6984
+ this.llm = await createLLMAdapter(this.config.llm);
6985
+ const llmMeta = this.llm.getMetadata();
6986
+ console.log(`LLM ready: ${llmMeta.provider} (${llmMeta.model})`);
6987
+ await this.syncConfigHash();
6988
+ this.strategy = await loadStrategy();
6989
+ const store = new FileStore();
6990
+ this.strategyContext = {
6991
+ store,
6992
+ agentId: Number(this.config.agentId),
6993
+ walletAddress: this.client.address
6994
+ };
6995
+ this.positionTracker = new PositionTracker(store, { maxTradeHistory: 50 });
6996
+ this.executor = new TradeExecutor(this.client, this.config, () => this.getConfigHash());
6997
+ this.riskManager = new RiskManager(this.config.trading, this.riskUniverse);
6998
+ this.marketData = new MarketDataService(this.getRpcUrl(), store);
6999
+ setGlobalResolver(this.marketData.getResolver());
7000
+ const savedRisk = this.positionTracker.getRiskState();
7001
+ if (savedRisk.lastResetDate) {
7002
+ this.riskManager.restoreState(savedRisk);
7003
+ }
7004
+ await this.initializeVaultManager();
7005
+ await this.initializePerp();
7006
+ await this.initializePrediction();
7007
+ await this.initializeRelay();
7008
+ console.log("Agent initialized successfully");
7009
+ }
7010
+ /**
7011
+ * Initialize the relay client for command center connectivity
7012
+ */
7013
+ async initializeRelay() {
7014
+ const relayConfig = this.config.relay;
7015
+ const relayEnabled = process.env.EXAGENT_RELAY_ENABLED !== "false";
7016
+ if (!relayConfig?.enabled || !relayEnabled) {
7017
+ console.log("Relay: Disabled");
7018
+ return;
7019
+ }
7020
+ const apiUrl = process.env.EXAGENT_API_URL || relayConfig.apiUrl;
7021
+ if (!apiUrl) {
7022
+ console.log("Relay: No API URL configured, skipping");
7023
+ return;
7024
+ }
7025
+ this.relay = new RelayClient({
7026
+ agentId: String(this.config.agentId),
7027
+ privateKey: this.config.privateKey,
7028
+ relay: {
7029
+ ...relayConfig,
7030
+ apiUrl
7031
+ },
7032
+ onCommand: (cmd) => this.handleCommand(cmd)
7033
+ });
7034
+ try {
7035
+ await this.relay.connect();
7036
+ console.log("Relay: Connected to command center");
7037
+ this.sendRelayStatus();
7038
+ } catch (error) {
7039
+ console.warn(
7040
+ "Relay: Failed to connect (agent will work locally):",
7041
+ error instanceof Error ? error.message : error
7042
+ );
7043
+ }
7044
+ }
7045
+ /**
7046
+ * Initialize the vault manager based on config
7047
+ */
7048
+ async initializeVaultManager() {
7049
+ const vaultConfig = this.config.vault || { policy: "disabled", preferVaultTrading: false };
7050
+ this.vaultManager = new VaultManager({
7051
+ agentId: BigInt(this.config.agentId),
7052
+ agentName: this.config.name,
7053
+ network: this.config.network,
7054
+ walletKey: this.config.privateKey,
7055
+ vaultConfig
7056
+ });
7057
+ console.log(`Vault policy: ${vaultConfig.policy}`);
7058
+ const status = await this.vaultManager.getVaultStatus();
7059
+ if (status.hasVault) {
7060
+ console.log(`Vault exists: ${status.vaultAddress}`);
7061
+ console.log(`Vault TVL: ${Number(status.totalAssets) / 1e6} USDC`);
7062
+ } else {
7063
+ console.log("No vault exists for this agent");
7064
+ if (vaultConfig.policy === "manual") {
7065
+ console.log("Vault creation is manual \u2014 use the command center to create one");
7066
+ }
7067
+ }
7068
+ }
7069
+ /**
7070
+ * Initialize Hyperliquid perp trading components.
7071
+ * Only initializes if perp is enabled in config AND risk universe >= 2.
7072
+ */
7073
+ async initializePerp() {
7074
+ const perpConfig = this.config.perp;
7075
+ if (!perpConfig?.enabled) {
7076
+ console.log("Perp trading: Disabled");
7077
+ return;
7078
+ }
7079
+ if (this.riskUniverse < 2) {
7080
+ console.warn(`Perp trading: Blocked by risk universe ${this.riskUniverse} (need >= 2)`);
7081
+ return;
7082
+ }
7083
+ try {
7084
+ const config = {
7085
+ enabled: true,
7086
+ apiUrl: perpConfig.apiUrl || "https://api.hyperliquid.xyz",
7087
+ wsUrl: perpConfig.wsUrl || "wss://api.hyperliquid.xyz/ws",
7088
+ builderAddress: perpConfig.builderAddress,
7089
+ builderFeeTenthsBps: perpConfig.builderFeeTenthsBps ?? 100,
7090
+ maxLeverage: perpConfig.maxLeverage ?? 10,
7091
+ maxNotionalUSD: perpConfig.maxNotionalUSD ?? 5e4,
7092
+ allowedInstruments: perpConfig.allowedInstruments
7093
+ };
7094
+ this.perpClient = new HyperliquidClient(config);
7095
+ const perpKey = perpConfig.perpRelayerKey || this.config.privateKey;
7096
+ const account = (0, import_accounts8.privateKeyToAccount)(perpKey);
7097
+ const walletClient = (0, import_viem11.createWalletClient)({
7098
+ chain: { id: 42161, name: "Arbitrum", nativeCurrency: { name: "ETH", symbol: "ETH", decimals: 18 }, rpcUrls: { default: { http: ["https://arb1.arbitrum.io/rpc"] } } },
7099
+ transport: (0, import_viem11.http)("https://arb1.arbitrum.io/rpc", { timeout: 6e4 }),
7100
+ account
7101
+ });
7102
+ this.perpSigner = new HyperliquidSigner(walletClient);
5189
7103
  this.perpOrders = new OrderManager(this.perpClient, this.perpSigner, config);
5190
7104
  this.perpPositions = new PositionManager(this.perpClient, this.perpSigner.getAddress());
5191
7105
  this.perpOnboarding = new PerpOnboarding(this.perpClient, this.perpSigner, config);
@@ -5258,6 +7172,101 @@ var AgentRuntime = class {
5258
7172
  console.warn("Perp trading will be disabled for this session");
5259
7173
  }
5260
7174
  }
7175
+ /**
7176
+ * Initialize Polymarket prediction market components.
7177
+ * Only initializes if prediction is enabled in config AND risk universe >= 2.
7178
+ */
7179
+ async initializePrediction() {
7180
+ const predictionConfig = this.config.prediction;
7181
+ if (!predictionConfig?.enabled) {
7182
+ console.log("Prediction markets: Disabled");
7183
+ return;
7184
+ }
7185
+ if (this.riskUniverse < 2) {
7186
+ console.warn(`Prediction markets: Blocked by risk universe ${this.riskUniverse} (need >= 2)`);
7187
+ return;
7188
+ }
7189
+ try {
7190
+ const config = {
7191
+ enabled: true,
7192
+ clobApiUrl: predictionConfig.clobApiUrl || "https://clob.polymarket.com",
7193
+ gammaApiUrl: predictionConfig.gammaApiUrl || "https://gamma-api.polymarket.com",
7194
+ polygonRpcUrl: predictionConfig.polygonRpcUrl || "https://polygon-rpc.com",
7195
+ maxNotionalUSD: predictionConfig.maxNotionalUSD ?? 500,
7196
+ maxTotalExposureUSD: predictionConfig.maxTotalExposureUSD ?? 5e3,
7197
+ polygonAllocationUSD: predictionConfig.polygonAllocationUSD ?? 100,
7198
+ allowedCategories: predictionConfig.allowedCategories,
7199
+ feeBridgeThresholdUSD: predictionConfig.feeBridgeThresholdUSD ?? 10
7200
+ };
7201
+ const vaultConfig = predictionConfig.vault;
7202
+ let vaultOpts;
7203
+ if (vaultConfig?.enabled && vaultConfig.factoryAddress) {
7204
+ this.predictionVaultManager = new PredictionVaultManager(
7205
+ this.config.privateKey,
7206
+ {
7207
+ enabled: true,
7208
+ factoryAddress: vaultConfig.factoryAddress,
7209
+ feeCollectorAddress: vaultConfig.feeCollectorAddress || "0x0000000000000000000000000000000000000000",
7210
+ polygonRpcUrl: predictionConfig.polygonRpcUrl || "https://polygon-rpc.com",
7211
+ vaultAddress: vaultConfig.vaultAddress,
7212
+ agentId: BigInt(this.config.agentId),
7213
+ feeRecipient: vaultConfig.feeRecipient || this.client.address,
7214
+ isVerified: vaultConfig.isVerified ?? false
7215
+ }
7216
+ );
7217
+ await this.predictionVaultManager.initialize();
7218
+ if (this.predictionVaultManager.hasVault) {
7219
+ const vaultAddr = this.predictionVaultManager.getVaultAddress();
7220
+ vaultOpts = { vaultMode: true, vaultAddress: vaultAddr };
7221
+ console.log(` Vault mode: ENABLED \u2014 vault ${vaultAddr}`);
7222
+ } else {
7223
+ console.log(" Vault mode: configured but no vault exists \u2014 create one via command center");
7224
+ }
7225
+ }
7226
+ this.predictionClient = new PolymarketClient(this.config.privateKey, config, vaultOpts);
7227
+ await this.predictionClient.initialize();
7228
+ this.predictionOrders = new PredictionOrderManager(this.predictionClient, config);
7229
+ this.predictionPositions = new PredictionPositionManager(this.predictionClient, config);
7230
+ this.predictionBrowser = new MarketBrowser(config);
7231
+ const recorderKey = predictionConfig.predictionRelayerKey || this.config.privateKey;
7232
+ this.predictionRecorder = new PredictionTradeRecorder({
7233
+ privateKey: recorderKey,
7234
+ rpcUrl: this.getRpcUrl(),
7235
+ agentId: BigInt(this.config.agentId),
7236
+ configHash: this.configHash
7237
+ });
7238
+ this.predictionFunding = new PredictionFunding({
7239
+ privateKey: this.config.privateKey,
7240
+ baseRpcUrl: this.getRpcUrl(),
7241
+ polygonRpcUrl: predictionConfig.polygonRpcUrl,
7242
+ ...vaultOpts?.vaultMode && vaultOpts?.vaultAddress && {
7243
+ polygonRecipient: vaultOpts.vaultAddress
7244
+ }
7245
+ });
7246
+ this.predictionConnected = true;
7247
+ console.log(`Polymarket: Connected`);
7248
+ console.log(` CLOB: ${config.clobApiUrl}`);
7249
+ console.log(` Max notional: $${config.maxNotionalUSD}, Max exposure: $${config.maxTotalExposureUSD}`);
7250
+ if (config.allowedCategories?.length) {
7251
+ console.log(` Categories: ${config.allowedCategories.join(", ")}`);
7252
+ }
7253
+ try {
7254
+ const polygonBalance = await this.predictionFunding.getPolygonUSDCBalance();
7255
+ const polygonUSDC = Number(polygonBalance) / 1e6;
7256
+ this.cachedPredictionPolygonUSDC = polygonUSDC;
7257
+ console.log(` Polygon USDC: $${polygonUSDC.toFixed(2)}`);
7258
+ if (polygonUSDC < 1) {
7259
+ console.warn(" No USDC on Polygon \u2014 bridge required before trading");
7260
+ }
7261
+ } catch {
7262
+ console.warn(" Could not check Polygon USDC balance");
7263
+ }
7264
+ } catch (error) {
7265
+ const message = error instanceof Error ? error.message : String(error);
7266
+ console.error(`Prediction initialization failed: ${message}`);
7267
+ console.warn("Prediction market trading will be disabled for this session");
7268
+ }
7269
+ }
5261
7270
  /**
5262
7271
  * Ensure the current wallet is linked to the agent.
5263
7272
  * If the trading wallet differs from the owner, enters a recovery loop
@@ -5422,9 +7431,9 @@ var AgentRuntime = class {
5422
7431
  const message = error instanceof Error ? error.message : String(error);
5423
7432
  if (message.includes("insufficient funds") || message.includes("intrinsic gas too low") || message.includes("exceeds the balance")) {
5424
7433
  const ccUrl = `https://exagent.io/agents/${encodeURIComponent(this.config.name)}/command-center`;
5425
- const publicClientInstance = (0, import_viem7.createPublicClient)({
5426
- chain: import_chains5.base,
5427
- transport: (0, import_viem7.http)(this.getRpcUrl(), { timeout: 6e4 })
7434
+ const publicClientInstance = (0, import_viem11.createPublicClient)({
7435
+ chain: import_chains8.base,
7436
+ transport: (0, import_viem11.http)(this.getRpcUrl(), { timeout: 6e4 })
5428
7437
  });
5429
7438
  console.log("");
5430
7439
  console.log("=== ETH NEEDED FOR GAS ===");
@@ -5792,6 +7801,224 @@ var AgentRuntime = class {
5792
7801
  }
5793
7802
  break;
5794
7803
  }
7804
+ case "enable_polymarket":
7805
+ if (this.predictionConnected) {
7806
+ this.relay?.sendCommandResult(cmd.id, true, "Polymarket already connected");
7807
+ break;
7808
+ }
7809
+ if (!this.config.prediction?.enabled) {
7810
+ this.relay?.sendCommandResult(cmd.id, false, "Prediction trading not configured in agent config");
7811
+ break;
7812
+ }
7813
+ if (this.riskUniverse < 2) {
7814
+ this.relay?.sendCommandResult(cmd.id, false, `Risk universe ${this.riskUniverse} too low (need >= 2)`);
7815
+ break;
7816
+ }
7817
+ try {
7818
+ await this.initializePrediction();
7819
+ if (this.predictionConnected) {
7820
+ this.relay?.sendCommandResult(cmd.id, true, "Polymarket connected");
7821
+ this.relay?.sendMessage(
7822
+ "system",
7823
+ "success",
7824
+ "Polymarket Enabled",
7825
+ 'Polymarket infrastructure connected. Use "Start Prediction Trading" to begin a dedicated prediction cycle.'
7826
+ );
7827
+ } else {
7828
+ this.relay?.sendCommandResult(cmd.id, false, "Failed to connect to Polymarket");
7829
+ }
7830
+ } catch (error) {
7831
+ const msg = error instanceof Error ? error.message : String(error);
7832
+ this.relay?.sendCommandResult(cmd.id, false, `Polymarket init failed: ${msg}`);
7833
+ }
7834
+ this.sendRelayStatus();
7835
+ break;
7836
+ case "disable_polymarket":
7837
+ if (!this.predictionConnected) {
7838
+ this.relay?.sendCommandResult(cmd.id, true, "Polymarket already disconnected");
7839
+ break;
7840
+ }
7841
+ this.predictionTradingActive = false;
7842
+ this.predictionConnected = false;
7843
+ this.predictionClient = null;
7844
+ this.predictionOrders = null;
7845
+ this.predictionPositions = null;
7846
+ this.predictionRecorder = null;
7847
+ this.predictionFunding = null;
7848
+ this.predictionBrowser = null;
7849
+ console.log("Polymarket disabled via command center");
7850
+ this.relay?.sendCommandResult(cmd.id, true, "Polymarket disconnected");
7851
+ this.relay?.sendMessage(
7852
+ "system",
7853
+ "info",
7854
+ "Polymarket Disabled",
7855
+ "Polymarket infrastructure disconnected."
7856
+ );
7857
+ this.sendRelayStatus();
7858
+ break;
7859
+ case "start_prediction_trading":
7860
+ if (!this.predictionConnected) {
7861
+ this.relay?.sendCommandResult(cmd.id, false, "Polymarket not connected. Enable Polymarket first.");
7862
+ break;
7863
+ }
7864
+ if (this.predictionTradingActive) {
7865
+ this.relay?.sendCommandResult(cmd.id, true, "Prediction trading already active");
7866
+ break;
7867
+ }
7868
+ this.predictionTradingActive = true;
7869
+ if (this.mode !== "trading") {
7870
+ this.mode = "trading";
7871
+ this.isRunning = true;
7872
+ }
7873
+ console.log("Prediction trading mandated via command center");
7874
+ this.relay?.sendCommandResult(cmd.id, true, "Prediction trading cycle active");
7875
+ this.relay?.sendMessage(
7876
+ "system",
7877
+ "success",
7878
+ "Prediction Trading Active",
7879
+ "Dedicated prediction trading cycle is now running every interval."
7880
+ );
7881
+ this.sendRelayStatus();
7882
+ break;
7883
+ case "stop_prediction_trading":
7884
+ if (!this.predictionTradingActive) {
7885
+ this.relay?.sendCommandResult(cmd.id, true, "Prediction trading already stopped");
7886
+ break;
7887
+ }
7888
+ this.predictionTradingActive = false;
7889
+ console.log("Prediction trading cycle stopped via command center");
7890
+ this.relay?.sendCommandResult(cmd.id, true, "Prediction trading cycle stopped");
7891
+ this.relay?.sendMessage(
7892
+ "system",
7893
+ "info",
7894
+ "Prediction Trading Stopped",
7895
+ "Dedicated prediction cycle stopped. Polymarket remains connected."
7896
+ );
7897
+ this.sendRelayStatus();
7898
+ break;
7899
+ case "update_prediction_params": {
7900
+ const predParams = cmd.params || {};
7901
+ let predUpdated = false;
7902
+ if (this.config.prediction && predParams.maxNotionalUSD !== void 0) {
7903
+ const val = Number(predParams.maxNotionalUSD);
7904
+ if (val >= 1) {
7905
+ this.config.prediction.maxNotionalUSD = val;
7906
+ predUpdated = true;
7907
+ }
7908
+ }
7909
+ if (this.config.prediction && predParams.maxTotalExposureUSD !== void 0) {
7910
+ const val = Number(predParams.maxTotalExposureUSD);
7911
+ if (val >= 1) {
7912
+ this.config.prediction.maxTotalExposureUSD = val;
7913
+ predUpdated = true;
7914
+ }
7915
+ }
7916
+ if (predUpdated) {
7917
+ this.relay?.sendCommandResult(cmd.id, true, "Prediction parameters updated");
7918
+ this.relay?.sendMessage(
7919
+ "config_updated",
7920
+ "info",
7921
+ "Prediction Params Updated",
7922
+ `Max notional: $${this.config.prediction?.maxNotionalUSD}, Max exposure: $${this.config.prediction?.maxTotalExposureUSD}`
7923
+ );
7924
+ } else {
7925
+ this.relay?.sendCommandResult(cmd.id, false, "No valid prediction parameters provided");
7926
+ }
7927
+ break;
7928
+ }
7929
+ case "create_prediction_vault": {
7930
+ if (!this.predictionVaultManager) {
7931
+ this.relay?.sendCommandResult(cmd.id, false, "Vault mode not configured. Set prediction.vault config.");
7932
+ break;
7933
+ }
7934
+ if (this.predictionVaultManager.hasVault) {
7935
+ const addr = this.predictionVaultManager.getVaultAddress();
7936
+ this.relay?.sendCommandResult(cmd.id, true, `Vault already exists: ${addr}`);
7937
+ break;
7938
+ }
7939
+ try {
7940
+ const vaultParams = cmd.params || {};
7941
+ const seedAmount = BigInt(Number(vaultParams.seedAmount || 100) * 1e6);
7942
+ const name = vaultParams.name || `Prediction Fund ${this.config.agentId}`;
7943
+ const symbol = vaultParams.symbol || `pxAGT${this.config.agentId}`;
7944
+ const result = await this.predictionVaultManager.createVault(seedAmount, name, symbol);
7945
+ if (result.success) {
7946
+ this.relay?.sendCommandResult(cmd.id, true, `Vault created: ${result.vaultAddress}`);
7947
+ this.relay?.sendMessage(
7948
+ "prediction_vault_created",
7949
+ "success",
7950
+ "Prediction Vault Created",
7951
+ `Vault deployed at ${result.vaultAddress} with ${Number(seedAmount) / 1e6} USDC seed`,
7952
+ { vaultAddress: result.vaultAddress, txHash: result.txHash }
7953
+ );
7954
+ } else {
7955
+ this.relay?.sendCommandResult(cmd.id, false, result.error || "Vault creation failed");
7956
+ }
7957
+ } catch (error) {
7958
+ const msg = error instanceof Error ? error.message : String(error);
7959
+ this.relay?.sendCommandResult(cmd.id, false, `Vault creation failed: ${msg}`);
7960
+ }
7961
+ this.sendRelayStatus();
7962
+ break;
7963
+ }
7964
+ case "deposit_prediction_vault": {
7965
+ if (!this.predictionVaultManager?.hasVault) {
7966
+ this.relay?.sendCommandResult(cmd.id, false, "No prediction vault exists");
7967
+ break;
7968
+ }
7969
+ try {
7970
+ const amount = BigInt(Number(cmd.params?.amount || 0) * 1e6);
7971
+ if (amount <= 0n) {
7972
+ this.relay?.sendCommandResult(cmd.id, false, "Amount must be positive");
7973
+ break;
7974
+ }
7975
+ const result = await this.predictionVaultManager.deposit(amount);
7976
+ if (result.success) {
7977
+ this.relay?.sendCommandResult(cmd.id, true, `Deposited ${Number(amount) / 1e6} USDC`);
7978
+ this.relay?.sendMessage(
7979
+ "prediction_vault_deposit",
7980
+ "success",
7981
+ "Vault Deposit",
7982
+ `${Number(amount) / 1e6} USDC deposited into prediction vault`,
7983
+ { amount: Number(amount) / 1e6, txHash: result.txHash }
7984
+ );
7985
+ } else {
7986
+ this.relay?.sendCommandResult(cmd.id, false, result.error || "Deposit failed");
7987
+ }
7988
+ } catch (error) {
7989
+ const msg = error instanceof Error ? error.message : String(error);
7990
+ this.relay?.sendCommandResult(cmd.id, false, `Deposit failed: ${msg}`);
7991
+ }
7992
+ this.sendRelayStatus();
7993
+ break;
7994
+ }
7995
+ case "withdraw_prediction_vault": {
7996
+ if (!this.predictionVaultManager?.hasVault) {
7997
+ this.relay?.sendCommandResult(cmd.id, false, "No prediction vault exists");
7998
+ break;
7999
+ }
8000
+ try {
8001
+ const isEmergency = cmd.params?.emergency === true;
8002
+ const result = isEmergency ? await this.predictionVaultManager.emergencyWithdraw() : await this.predictionVaultManager.withdrawAll();
8003
+ if (result.success) {
8004
+ this.relay?.sendCommandResult(cmd.id, true, isEmergency ? "Emergency withdrawn (penalty applied)" : "Withdrawn all shares");
8005
+ this.relay?.sendMessage(
8006
+ "prediction_vault_withdraw",
8007
+ isEmergency ? "warning" : "success",
8008
+ isEmergency ? "Emergency Withdrawal" : "Vault Withdrawal",
8009
+ isEmergency ? "Emergency withdrawn with penalty" : "All shares redeemed from prediction vault",
8010
+ { emergency: isEmergency, txHash: result.txHash }
8011
+ );
8012
+ } else {
8013
+ this.relay?.sendCommandResult(cmd.id, false, result.error || "Withdrawal failed");
8014
+ }
8015
+ } catch (error) {
8016
+ const msg = error instanceof Error ? error.message : String(error);
8017
+ this.relay?.sendCommandResult(cmd.id, false, `Withdrawal failed: ${msg}`);
8018
+ }
8019
+ this.sendRelayStatus();
8020
+ break;
8021
+ }
5795
8022
  case "start_paper_trading":
5796
8023
  if (this.mode === "paper") {
5797
8024
  this.relay?.sendCommandResult(cmd.id, true, "Already paper trading");
@@ -5919,7 +8146,7 @@ var AgentRuntime = class {
5919
8146
  try {
5920
8147
  const onChain = await this.client.registry.getConfigHash(BigInt(this.config.agentId));
5921
8148
  if (onChain === this.pendingConfigHash) {
5922
- this.configHash = this.pendingConfigHash;
8149
+ this.updateConfigHashEverywhere(this.pendingConfigHash);
5923
8150
  this.pendingConfigHash = null;
5924
8151
  console.log("Config verified on-chain! Your agent will now appear on the leaderboard.");
5925
8152
  this.relay?.sendMessage(
@@ -5932,6 +8159,34 @@ var AgentRuntime = class {
5932
8159
  } catch {
5933
8160
  }
5934
8161
  }
8162
+ /**
8163
+ * Periodically sync config hash from on-chain to detect external changes.
8164
+ * This catches cases where the config was changed via the website or another
8165
+ * session while this agent is running. Without this, all trades and recordings
8166
+ * fail with ConfigMismatch until the agent is restarted.
8167
+ *
8168
+ * Called from sendRelayStatus at most every 5 minutes (timestamp-throttled).
8169
+ */
8170
+ lastConfigSyncAt = 0;
8171
+ async syncConfigHashFromChain() {
8172
+ try {
8173
+ const onChain = await this.client.registry.getConfigHash(BigInt(this.config.agentId));
8174
+ if (onChain && onChain !== this.configHash && onChain !== "0x0000000000000000000000000000000000000000000000000000000000000000") {
8175
+ console.log(`Config hash changed on-chain (external update detected): ${this.configHash} \u2192 ${onChain}`);
8176
+ this.updateConfigHashEverywhere(onChain);
8177
+ }
8178
+ } catch {
8179
+ }
8180
+ }
8181
+ /**
8182
+ * Propagate a config hash update to runtime + all consumers (recorders).
8183
+ * Ensures executor, perp recorder, and prediction recorder all stay in sync.
8184
+ */
8185
+ updateConfigHashEverywhere(newHash) {
8186
+ this.configHash = newHash;
8187
+ this.perpRecorder?.updateConfigHash(newHash);
8188
+ this.predictionRecorder?.updateConfigHash(newHash);
8189
+ }
5935
8190
  /**
5936
8191
  * Send current status to the relay
5937
8192
  */
@@ -5942,6 +8197,11 @@ var AgentRuntime = class {
5942
8197
  this.lastConfigCheckAt = Date.now();
5943
8198
  this.checkPendingConfigApproval();
5944
8199
  }
8200
+ const CONFIG_SYNC_INTERVAL_MS = 3e5;
8201
+ if (!this.pendingConfigHash && Date.now() - this.lastConfigSyncAt >= CONFIG_SYNC_INTERVAL_MS) {
8202
+ this.lastConfigSyncAt = Date.now();
8203
+ this.syncConfigHashFromChain();
8204
+ }
5945
8205
  const vaultConfig = this.config.vault || { policy: "disabled" };
5946
8206
  const status = {
5947
8207
  mode: this.mode,
@@ -5979,6 +8239,19 @@ var AgentRuntime = class {
5979
8239
  effectiveLeverage: this.cachedPerpLeverage,
5980
8240
  pendingRecords: this.perpRecorder?.pendingRetries ?? 0
5981
8241
  } : void 0,
8242
+ prediction: this.predictionConnected ? {
8243
+ enabled: true,
8244
+ trading: this.predictionTradingActive,
8245
+ polygonUSDC: this.cachedPredictionPolygonUSDC,
8246
+ baseUSDC: this.cachedPredictionBaseUSDC,
8247
+ totalExposure: this.cachedPredictionExposure,
8248
+ openMarkets: this.cachedPredictionOpenMarkets,
8249
+ openPositions: this.cachedPredictionOpenPositions,
8250
+ unrealizedPnl: this.cachedPredictionUnrealizedPnl,
8251
+ pendingFees: this.predictionRecorder?.pendingFeesUSD ?? 0,
8252
+ pendingRecords: this.predictionRecorder?.pendingRetries ?? 0,
8253
+ vault: this.cachedPredictionVaultStatus
8254
+ } : void 0,
5982
8255
  positions: this.positionTracker ? this.positionTracker.getPositionSummary(this.lastPrices) : void 0,
5983
8256
  paper: this.mode === "paper" && this.paperPortfolio ? (() => {
5984
8257
  const summary = this.paperPortfolio.getSummary(this.lastPrices);
@@ -6021,6 +8294,39 @@ var AgentRuntime = class {
6021
8294
  }).catch(() => {
6022
8295
  });
6023
8296
  }
8297
+ if (this.predictionConnected && this.predictionPositions && this.predictionFunding) {
8298
+ this.predictionPositions.getAccountSummary().then((summary) => {
8299
+ this.cachedPredictionExposure = summary.totalExposure;
8300
+ this.cachedPredictionOpenMarkets = summary.openMarketCount;
8301
+ this.cachedPredictionOpenPositions = summary.openPositionCount;
8302
+ this.cachedPredictionUnrealizedPnl = summary.totalUnrealizedPnl;
8303
+ }).catch(() => {
8304
+ });
8305
+ this.predictionFunding.getPolygonUSDCBalance().then((bal) => {
8306
+ this.cachedPredictionPolygonUSDC = Number(bal) / 1e6;
8307
+ }).catch(() => {
8308
+ });
8309
+ this.predictionFunding.getBaseUSDCBalance().then((bal) => {
8310
+ this.cachedPredictionBaseUSDC = Number(bal) / 1e6;
8311
+ }).catch(() => {
8312
+ });
8313
+ if (this.predictionVaultManager?.hasVault) {
8314
+ this.predictionVaultManager.getVaultStatus().then((vs) => {
8315
+ if (vs.hasVault) {
8316
+ this.cachedPredictionVaultStatus = {
8317
+ address: vs.vaultAddress,
8318
+ totalAssets: Number(vs.totalAssets) / 1e6,
8319
+ sharePrice: vs.sharePrice,
8320
+ cachedCtfValue: Number(vs.cachedCtfValue) / 1e6,
8321
+ currentExposureBps: vs.currentExposureBps,
8322
+ circuitBreakerActive: vs.circuitBreakerActive,
8323
+ isPaused: vs.isPaused
8324
+ };
8325
+ }
8326
+ }).catch(() => {
8327
+ });
8328
+ }
8329
+ }
6024
8330
  }
6025
8331
  /**
6026
8332
  * Run a single trading cycle
@@ -6218,6 +8524,15 @@ var AgentRuntime = class {
6218
8524
  this.relay?.sendMessage("system", "error", "Perp Cycle Error", message);
6219
8525
  }
6220
8526
  }
8527
+ if (!isPaper && this.predictionConnected && this.predictionTradingActive) {
8528
+ try {
8529
+ await this.runPredictionCycle();
8530
+ } catch (error) {
8531
+ const message = error instanceof Error ? error.message : String(error);
8532
+ console.error("Error in prediction cycle:", message);
8533
+ this.relay?.sendMessage("system", "error", "Prediction Cycle Error", message);
8534
+ }
8535
+ }
6221
8536
  this.sendRelayStatus();
6222
8537
  }
6223
8538
  /**
@@ -6278,6 +8593,101 @@ var AgentRuntime = class {
6278
8593
  }
6279
8594
  }
6280
8595
  }
8596
+ /**
8597
+ * Run a single prediction market trading cycle.
8598
+ * Fetches active markets, positions, calls prediction strategy, executes signals.
8599
+ * Fills are recorded on Base via Router.recordPerpTrade() with POLY: prefix.
8600
+ */
8601
+ async runPredictionCycle() {
8602
+ if (!this.predictionClient || !this.predictionOrders || !this.predictionPositions || !this.predictionRecorder || !this.predictionBrowser || !this.predictionConnected) return;
8603
+ const predictionConfig = this.config.prediction;
8604
+ if (!predictionConfig?.enabled) return;
8605
+ console.log(" [POLY] Running prediction cycle...");
8606
+ try {
8607
+ const newFills = await this.predictionOrders.pollNewFills();
8608
+ if (newFills.length > 0) {
8609
+ console.log(` [POLY] ${newFills.length} new fill(s) detected`);
8610
+ for (const fill of newFills) {
8611
+ this.predictionPositions.processFill(fill);
8612
+ const result = await this.predictionRecorder.recordFill(fill);
8613
+ if (result.success) {
8614
+ this.relay?.sendMessage(
8615
+ "prediction_fill",
8616
+ "success",
8617
+ "Prediction Fill",
8618
+ `${fill.side} ${fill.size} @ $${parseFloat(fill.price).toFixed(2)} \u2014 market ${fill.marketConditionId.slice(0, 12)}...`,
8619
+ { tradeId: fill.tradeId, side: fill.side, size: fill.size, price: fill.price, txHash: result.txHash }
8620
+ );
8621
+ }
8622
+ }
8623
+ }
8624
+ } catch (error) {
8625
+ const message = error instanceof Error ? error.message : String(error);
8626
+ console.warn(" [POLY] Fill polling failed:", message);
8627
+ }
8628
+ const account = await this.predictionPositions.getAccountSummary();
8629
+ const positions = await this.predictionPositions.getPositions();
8630
+ console.log(` [POLY] Polygon USDC: $${account.polygonUSDC.toFixed(2)}, Positions: ${positions.length}, Exposure: $${account.totalExposure.toFixed(2)}`);
8631
+ if (!this.predictionStrategy) {
8632
+ return;
8633
+ }
8634
+ let markets;
8635
+ try {
8636
+ const category = predictionConfig.allowedCategories?.[0];
8637
+ markets = await this.predictionBrowser.getActiveMarkets({
8638
+ limit: 50,
8639
+ category
8640
+ });
8641
+ } catch (error) {
8642
+ const message = error instanceof Error ? error.message : String(error);
8643
+ console.error(" [POLY] Market fetch error:", message);
8644
+ return;
8645
+ }
8646
+ let signals;
8647
+ try {
8648
+ signals = await this.predictionStrategy(
8649
+ markets,
8650
+ positions,
8651
+ account,
8652
+ this.llm,
8653
+ this.config
8654
+ );
8655
+ } catch (error) {
8656
+ const message = error instanceof Error ? error.message : String(error);
8657
+ console.error(" [POLY] Strategy error:", message);
8658
+ return;
8659
+ }
8660
+ console.log(` [POLY] Strategy generated ${signals.length} signals`);
8661
+ for (const signal of signals) {
8662
+ if (signal.action === "hold") continue;
8663
+ const result = await this.predictionOrders.executeSignal(signal);
8664
+ if (result.success) {
8665
+ console.log(` [POLY] Order placed: ${signal.action} "${signal.marketQuestion.slice(0, 50)}..." \u2014 ${result.status}`);
8666
+ } else {
8667
+ console.warn(` [POLY] Order failed: ${signal.action} \u2014 ${result.error}`);
8668
+ }
8669
+ }
8670
+ const pendingFees = this.predictionRecorder.pendingFeesUSD;
8671
+ const threshold = predictionConfig.feeBridgeThresholdUSD ?? 10;
8672
+ if (pendingFees >= threshold && this.predictionFunding) {
8673
+ console.log(` [POLY] Bridging $${pendingFees.toFixed(2)} in accumulated fees to Base...`);
8674
+ const feeAmount = BigInt(Math.floor(pendingFees * 1e6));
8675
+ try {
8676
+ const bridgeResult = await this.predictionFunding.withdrawToBase(feeAmount, (step) => {
8677
+ console.log(` [POLY] Fee bridge: ${step}`);
8678
+ });
8679
+ if (bridgeResult.success) {
8680
+ this.predictionRecorder.resetAccumulatedFees();
8681
+ console.log(` [POLY] Fee bridge complete: $${pendingFees.toFixed(2)} \u2192 Base`);
8682
+ } else {
8683
+ console.warn(` [POLY] Fee bridge failed: ${bridgeResult.error}`);
8684
+ }
8685
+ } catch (error) {
8686
+ const message = error instanceof Error ? error.message : String(error);
8687
+ console.warn(` [POLY] Fee bridge error: ${message}`);
8688
+ }
8689
+ }
8690
+ }
6281
8691
  /**
6282
8692
  * Check if ETH balance is below threshold and notify.
6283
8693
  * Returns true if trading should continue, false if ETH is critically low.
@@ -6329,6 +8739,14 @@ var AgentRuntime = class {
6329
8739
  if (this.perpRecorder) {
6330
8740
  this.perpRecorder.stop();
6331
8741
  }
8742
+ this.predictionConnected = false;
8743
+ this.predictionTradingActive = false;
8744
+ if (this.predictionVaultManager) {
8745
+ this.predictionVaultManager.stop();
8746
+ }
8747
+ if (this.predictionRecorder) {
8748
+ this.predictionRecorder.stop();
8749
+ }
6332
8750
  if (this.relay) {
6333
8751
  this.relay.disconnect();
6334
8752
  }
@@ -6346,8 +8764,8 @@ var AgentRuntime = class {
6346
8764
  * for Frontier agents trading outside the whitelist) stay visible.
6347
8765
  */
6348
8766
  getTokensToTrack() {
6349
- const base6 = this.config.allowedTokens || this.getDefaultTokens();
6350
- const baseSet = new Set(base6.map((t) => t.toLowerCase()));
8767
+ const base8 = this.config.allowedTokens || this.getDefaultTokens();
8768
+ const baseSet = new Set(base8.map((t) => t.toLowerCase()));
6351
8769
  const trackedPositions = this.positionTracker.getPositions();
6352
8770
  const extras = [];
6353
8771
  for (const pos of trackedPositions) {
@@ -6370,7 +8788,7 @@ var AgentRuntime = class {
6370
8788
  console.log(`Auto-tracking ${extras.length} additional token(s) from position history`);
6371
8789
  }
6372
8790
  const resolver = this.marketData.getResolver();
6373
- const allTokens = [...base6, ...extras];
8791
+ const allTokens = [...base8, ...extras];
6374
8792
  const filtered = allTokens.filter((t) => !resolver.isUnresolvable(t.toLowerCase()));
6375
8793
  const dropped = allTokens.length - filtered.length;
6376
8794
  if (dropped > 0) {
@@ -6707,7 +9125,7 @@ function loadSecureEnv(basePath, passphrase) {
6707
9125
  }
6708
9126
 
6709
9127
  // src/index.ts
6710
- var AGENT_VERSION = "0.1.39";
9128
+ var AGENT_VERSION = "0.1.40";
6711
9129
  // Annotate the CommonJS export names for ESM import in node:
6712
9130
  0 && (module.exports = {
6713
9131
  AGENT_VERSION,
@@ -6725,6 +9143,8 @@ var AGENT_VERSION = "0.1.39";
6725
9143
  HyperliquidWebSocket,
6726
9144
  LLMConfigSchema,
6727
9145
  LLMProviderSchema,
9146
+ MARKET_CATEGORIES,
9147
+ MarketBrowser,
6728
9148
  MarketDataService,
6729
9149
  MistralAdapter,
6730
9150
  OllamaAdapter,
@@ -6733,8 +9153,14 @@ var AGENT_VERSION = "0.1.39";
6733
9153
  PerpConfigSchema,
6734
9154
  PerpOnboarding,
6735
9155
  PerpTradeRecorder,
9156
+ PolymarketClient,
6736
9157
  PositionManager,
6737
9158
  PositionTracker,
9159
+ PredictionConfigSchema,
9160
+ PredictionFunding,
9161
+ PredictionOrderManager,
9162
+ PredictionPositionManager,
9163
+ PredictionTradeRecorder,
6738
9164
  RelayClient,
6739
9165
  RelayConfigSchema,
6740
9166
  RiskManager,
@@ -6746,9 +9172,12 @@ var AGENT_VERSION = "0.1.39";
6746
9172
  VaultConfigSchema,
6747
9173
  VaultManager,
6748
9174
  VaultPolicySchema,
9175
+ calculatePredictionFee,
6749
9176
  createLLMAdapter,
6750
9177
  createSampleConfig,
9178
+ decodePredictionInstrument,
6751
9179
  decryptEnvFile,
9180
+ encodePredictionInstrument,
6752
9181
  encryptEnvFile,
6753
9182
  fillHashToBytes32,
6754
9183
  fillOidToBytes32,
@@ -6758,6 +9187,8 @@ var AGENT_VERSION = "0.1.39";
6758
9187
  loadConfig,
6759
9188
  loadSecureEnv,
6760
9189
  loadStrategy,
9190
+ orderIdToBytes32,
9191
+ tradeIdToBytes32,
6761
9192
  validateConfig,
6762
9193
  validateStrategy
6763
9194
  });