pmxtjs 0.3.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/API_REFERENCE.md +230 -1
  2. package/dist/BaseExchange.d.ts +36 -2
  3. package/dist/BaseExchange.js +43 -1
  4. package/dist/exchanges/kalshi/auth.d.ts +23 -0
  5. package/dist/exchanges/kalshi/auth.js +99 -0
  6. package/dist/exchanges/kalshi/fetchMarkets.d.ts +3 -0
  7. package/dist/exchanges/kalshi/fetchMarkets.js +110 -0
  8. package/dist/exchanges/kalshi/fetchOHLCV.d.ts +3 -0
  9. package/dist/exchanges/kalshi/fetchOHLCV.js +78 -0
  10. package/dist/exchanges/kalshi/fetchOrderBook.d.ts +2 -0
  11. package/dist/exchanges/kalshi/fetchOrderBook.js +32 -0
  12. package/dist/exchanges/kalshi/fetchTrades.d.ts +3 -0
  13. package/dist/exchanges/kalshi/fetchTrades.js +31 -0
  14. package/dist/exchanges/kalshi/getMarketsBySlug.d.ts +7 -0
  15. package/dist/exchanges/kalshi/getMarketsBySlug.js +62 -0
  16. package/dist/exchanges/kalshi/index.d.ts +21 -0
  17. package/dist/exchanges/kalshi/index.js +273 -0
  18. package/dist/exchanges/kalshi/kalshi.test.d.ts +1 -0
  19. package/dist/exchanges/kalshi/kalshi.test.js +309 -0
  20. package/dist/exchanges/kalshi/searchMarkets.d.ts +3 -0
  21. package/dist/exchanges/kalshi/searchMarkets.js +28 -0
  22. package/dist/exchanges/kalshi/utils.d.ts +5 -0
  23. package/dist/exchanges/kalshi/utils.js +85 -0
  24. package/dist/exchanges/polymarket/auth.d.ts +32 -0
  25. package/dist/exchanges/polymarket/auth.js +98 -0
  26. package/dist/exchanges/polymarket/fetchMarkets.d.ts +3 -0
  27. package/dist/exchanges/polymarket/fetchMarkets.js +75 -0
  28. package/dist/exchanges/polymarket/fetchOHLCV.d.ts +7 -0
  29. package/dist/exchanges/polymarket/fetchOHLCV.js +73 -0
  30. package/dist/exchanges/polymarket/fetchOrderBook.d.ts +6 -0
  31. package/dist/exchanges/polymarket/fetchOrderBook.js +38 -0
  32. package/dist/exchanges/polymarket/fetchPositions.d.ts +2 -0
  33. package/dist/exchanges/polymarket/fetchPositions.js +27 -0
  34. package/dist/exchanges/polymarket/fetchTrades.d.ts +11 -0
  35. package/dist/exchanges/polymarket/fetchTrades.js +59 -0
  36. package/dist/exchanges/polymarket/getMarketsBySlug.d.ts +7 -0
  37. package/dist/exchanges/polymarket/getMarketsBySlug.js +39 -0
  38. package/dist/exchanges/polymarket/index.d.ts +23 -0
  39. package/dist/exchanges/polymarket/index.js +216 -0
  40. package/dist/exchanges/polymarket/searchMarkets.d.ts +3 -0
  41. package/dist/exchanges/polymarket/searchMarkets.js +35 -0
  42. package/dist/exchanges/polymarket/utils.d.ts +7 -0
  43. package/dist/exchanges/polymarket/utils.js +95 -0
  44. package/dist/index.d.ts +4 -4
  45. package/dist/index.js +8 -8
  46. package/dist/types.d.ts +38 -0
  47. package/package.json +5 -1
  48. package/readme.md +37 -3
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.fetchOHLCV = fetchOHLCV;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ const utils_1 = require("./utils");
9
+ /**
10
+ * Fetch historical price data (OHLCV candles) for a specific token.
11
+ * @param id - The CLOB token ID (e.g., outcome token ID)
12
+ */
13
+ async function fetchOHLCV(id, params) {
14
+ // ID Validation: Polymarket CLOB requires a Token ID (long numeric string) not a Market ID
15
+ if (id.length < 10 && /^\d+$/.test(id)) {
16
+ throw new Error(`Invalid ID for Polymarket history: "${id}". You provided a Market ID, but Polymarket's CLOB API requires a Token ID. Ensure you are using 'outcome.id'.`);
17
+ }
18
+ try {
19
+ const fidelity = (0, utils_1.mapIntervalToFidelity)(params.resolution);
20
+ const nowTs = Math.floor(Date.now() / 1000);
21
+ // 1. Smart Lookback Calculation
22
+ // If start/end not provided, calculate window based on limit * resolution
23
+ let startTs = params.start ? Math.floor(params.start.getTime() / 1000) : 0;
24
+ let endTs = params.end ? Math.floor(params.end.getTime() / 1000) : nowTs;
25
+ if (!params.start) {
26
+ // Default limit is usually 20 in the example, but safety margin is good.
27
+ // If limit is not set, we default to 100 candles.
28
+ const count = params.limit || 100;
29
+ // fidelity is in minutes.
30
+ const durationSeconds = count * fidelity * 60;
31
+ startTs = endTs - durationSeconds;
32
+ }
33
+ const queryParams = {
34
+ market: id,
35
+ fidelity: fidelity,
36
+ startTs: startTs,
37
+ endTs: endTs
38
+ };
39
+ const response = await axios_1.default.get(`${utils_1.CLOB_API_URL}/prices-history`, {
40
+ params: queryParams
41
+ });
42
+ const history = response.data.history || [];
43
+ // 2. Align Timestamps (Snap to Grid)
44
+ // Polymarket returns random tick timestamps (e.g. 1:00:21).
45
+ // We want to normalize this to the start of the bucket (1:00:00).
46
+ const resolutionMs = fidelity * 60 * 1000;
47
+ const candles = history.map((item) => {
48
+ const rawMs = item.t * 1000;
49
+ const snappedMs = Math.floor(rawMs / resolutionMs) * resolutionMs;
50
+ return {
51
+ timestamp: snappedMs, // Aligned timestamp
52
+ open: item.p,
53
+ high: item.p,
54
+ low: item.p,
55
+ close: item.p,
56
+ volume: undefined
57
+ };
58
+ });
59
+ // Apply limit if specified
60
+ if (params.limit && candles.length > params.limit) {
61
+ return candles.slice(-params.limit);
62
+ }
63
+ return candles;
64
+ }
65
+ catch (error) {
66
+ if (axios_1.default.isAxiosError(error) && error.response) {
67
+ const apiError = error.response.data?.error || error.response.data?.message || "Unknown API Error";
68
+ throw new Error(`Polymarket History API Error (${error.response.status}): ${apiError}. Used ID: ${id}`);
69
+ }
70
+ console.error(`Unexpected error fetching Polymarket history for ${id}:`, error);
71
+ throw error;
72
+ }
73
+ }
@@ -0,0 +1,6 @@
1
+ import { OrderBook } from '../../types';
2
+ /**
3
+ * Fetch the current order book for a specific token.
4
+ * @param id - The CLOB token ID
5
+ */
6
+ export declare function fetchOrderBook(id: string): Promise<OrderBook>;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.fetchOrderBook = fetchOrderBook;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ const utils_1 = require("./utils");
9
+ /**
10
+ * Fetch the current order book for a specific token.
11
+ * @param id - The CLOB token ID
12
+ */
13
+ async function fetchOrderBook(id) {
14
+ try {
15
+ const response = await axios_1.default.get(`${utils_1.CLOB_API_URL}/book`, {
16
+ params: { token_id: id }
17
+ });
18
+ const data = response.data;
19
+ // Response format: { bids: [{price: "0.52", size: "100"}], asks: [...] }
20
+ const bids = (data.bids || []).map((level) => ({
21
+ price: parseFloat(level.price),
22
+ size: parseFloat(level.size)
23
+ })).sort((a, b) => b.price - a.price); // Sort Bids Descending (Best/Highest first)
24
+ const asks = (data.asks || []).map((level) => ({
25
+ price: parseFloat(level.price),
26
+ size: parseFloat(level.size)
27
+ })).sort((a, b) => a.price - b.price); // Sort Asks Ascending (Best/Lowest first)
28
+ return {
29
+ bids,
30
+ asks,
31
+ timestamp: data.timestamp ? new Date(data.timestamp).getTime() : Date.now()
32
+ };
33
+ }
34
+ catch (error) {
35
+ console.error(`Error fetching Polymarket orderbook for ${id}:`, error);
36
+ return { bids: [], asks: [] };
37
+ }
38
+ }
@@ -0,0 +1,2 @@
1
+ import { Position } from '../../types';
2
+ export declare function fetchPositions(userAddress: string): Promise<Position[]>;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.fetchPositions = fetchPositions;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ const DATA_API_URL = 'https://data-api.polymarket.com';
9
+ async function fetchPositions(userAddress) {
10
+ const response = await axios_1.default.get(`${DATA_API_URL}/positions`, {
11
+ params: {
12
+ user: userAddress,
13
+ limit: 100
14
+ }
15
+ });
16
+ const data = Array.isArray(response.data) ? response.data : [];
17
+ return data.map((p) => ({
18
+ marketId: p.conditionId,
19
+ outcomeId: p.asset,
20
+ outcomeLabel: p.outcome || 'Unknown',
21
+ size: parseFloat(p.size),
22
+ entryPrice: parseFloat(p.avgPrice),
23
+ currentPrice: parseFloat(p.curPrice || '0'),
24
+ unrealizedPnL: parseFloat(p.cashPnl || '0'),
25
+ realizedPnL: parseFloat(p.realizedPnl || '0')
26
+ }));
27
+ }
@@ -0,0 +1,11 @@
1
+ import { HistoryFilterParams } from '../../BaseExchange';
2
+ import { Trade } from '../../types';
3
+ /**
4
+ * Fetch raw trade history for a specific token.
5
+ * @param id - The CLOB token ID
6
+ *
7
+ * NOTE: Polymarket's /trades endpoint currently requires L2 Authentication (API Key).
8
+ * This method will return an empty array if an API key is not provided in headers.
9
+ * Use fetchOHLCV for public historical price data instead.
10
+ */
11
+ export declare function fetchTrades(id: string, params: HistoryFilterParams): Promise<Trade[]>;
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.fetchTrades = fetchTrades;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ const utils_1 = require("./utils");
9
+ /**
10
+ * Fetch raw trade history for a specific token.
11
+ * @param id - The CLOB token ID
12
+ *
13
+ * NOTE: Polymarket's /trades endpoint currently requires L2 Authentication (API Key).
14
+ * This method will return an empty array if an API key is not provided in headers.
15
+ * Use fetchOHLCV for public historical price data instead.
16
+ */
17
+ async function fetchTrades(id, params) {
18
+ // ID Validation
19
+ if (id.length < 10 && /^\d+$/.test(id)) {
20
+ throw new Error(`Invalid ID for Polymarket trades: "${id}". You provided a Market ID, but Polymarket's CLOB API requires a Token ID.`);
21
+ }
22
+ try {
23
+ const queryParams = {
24
+ market: id
25
+ };
26
+ // Add time filters if provided
27
+ if (params.start) {
28
+ queryParams.after = Math.floor(params.start.getTime() / 1000);
29
+ }
30
+ if (params.end) {
31
+ queryParams.before = Math.floor(params.end.getTime() / 1000);
32
+ }
33
+ const response = await axios_1.default.get(`${utils_1.CLOB_API_URL}/trades`, {
34
+ params: queryParams
35
+ });
36
+ // Response is an array of trade objects
37
+ const trades = response.data || [];
38
+ const mappedTrades = trades.map((trade) => ({
39
+ id: trade.id || `${trade.timestamp}-${trade.price}`,
40
+ timestamp: trade.timestamp * 1000, // Convert to milliseconds
41
+ price: parseFloat(trade.price),
42
+ amount: parseFloat(trade.size || trade.amount || 0),
43
+ side: trade.side === 'BUY' ? 'buy' : trade.side === 'SELL' ? 'sell' : 'unknown'
44
+ }));
45
+ // Apply limit if specified
46
+ if (params.limit && mappedTrades.length > params.limit) {
47
+ return mappedTrades.slice(-params.limit); // Return most recent N trades
48
+ }
49
+ return mappedTrades;
50
+ }
51
+ catch (error) {
52
+ if (axios_1.default.isAxiosError(error) && error.response) {
53
+ const apiError = error.response.data?.error || error.response.data?.message || "Unknown API Error";
54
+ throw new Error(`Polymarket Trades API Error (${error.response.status}): ${apiError}. Used ID: ${id}`);
55
+ }
56
+ console.error(`Unexpected error fetching Polymarket trades for ${id}:`, error);
57
+ throw error;
58
+ }
59
+ }
@@ -0,0 +1,7 @@
1
+ import { UnifiedMarket } from '../../types';
2
+ /**
3
+ * Fetch specific markets by their URL slug.
4
+ * Useful for looking up a specific event from a URL.
5
+ * @param slug - The event slug (e.g. "will-fed-cut-rates-in-march")
6
+ */
7
+ export declare function getMarketsBySlug(slug: string): Promise<UnifiedMarket[]>;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getMarketsBySlug = getMarketsBySlug;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ const utils_1 = require("./utils");
9
+ /**
10
+ * Fetch specific markets by their URL slug.
11
+ * Useful for looking up a specific event from a URL.
12
+ * @param slug - The event slug (e.g. "will-fed-cut-rates-in-march")
13
+ */
14
+ async function getMarketsBySlug(slug) {
15
+ try {
16
+ const response = await axios_1.default.get(utils_1.GAMMA_API_URL, {
17
+ params: { slug: slug }
18
+ });
19
+ const events = response.data;
20
+ if (!events || events.length === 0)
21
+ return [];
22
+ const unifiedMarkets = [];
23
+ for (const event of events) {
24
+ if (!event.markets)
25
+ continue;
26
+ for (const market of event.markets) {
27
+ const unifiedMarket = (0, utils_1.mapMarketToUnified)(event, market, { useQuestionAsCandidateFallback: true });
28
+ if (unifiedMarket) {
29
+ unifiedMarkets.push(unifiedMarket);
30
+ }
31
+ }
32
+ }
33
+ return unifiedMarkets;
34
+ }
35
+ catch (error) {
36
+ console.error(`Error fetching Polymarket slug ${slug}:`, error);
37
+ return [];
38
+ }
39
+ }
@@ -0,0 +1,23 @@
1
+ import { PredictionMarketExchange, MarketFilterParams, HistoryFilterParams, ExchangeCredentials } from '../../BaseExchange';
2
+ import { UnifiedMarket, PriceCandle, OrderBook, Trade, Order, Position, Balance, CreateOrderParams } from '../../types';
3
+ export declare class PolymarketExchange extends PredictionMarketExchange {
4
+ private auth?;
5
+ constructor(credentials?: ExchangeCredentials);
6
+ get name(): string;
7
+ fetchMarkets(params?: MarketFilterParams): Promise<UnifiedMarket[]>;
8
+ searchMarkets(query: string, params?: MarketFilterParams): Promise<UnifiedMarket[]>;
9
+ getMarketsBySlug(slug: string): Promise<UnifiedMarket[]>;
10
+ fetchOHLCV(id: string, params: HistoryFilterParams): Promise<PriceCandle[]>;
11
+ fetchOrderBook(id: string): Promise<OrderBook>;
12
+ fetchTrades(id: string, params: HistoryFilterParams): Promise<Trade[]>;
13
+ /**
14
+ * Ensure authentication is initialized before trading operations.
15
+ */
16
+ private ensureAuth;
17
+ createOrder(params: CreateOrderParams): Promise<Order>;
18
+ cancelOrder(orderId: string): Promise<Order>;
19
+ fetchOrder(orderId: string): Promise<Order>;
20
+ fetchOpenOrders(marketId?: string): Promise<Order[]>;
21
+ fetchPositions(): Promise<Position[]>;
22
+ fetchBalance(): Promise<Balance[]>;
23
+ }
@@ -0,0 +1,216 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PolymarketExchange = void 0;
4
+ const BaseExchange_1 = require("../../BaseExchange");
5
+ const fetchMarkets_1 = require("./fetchMarkets");
6
+ const searchMarkets_1 = require("./searchMarkets");
7
+ const getMarketsBySlug_1 = require("./getMarketsBySlug");
8
+ const fetchOHLCV_1 = require("./fetchOHLCV");
9
+ const fetchOrderBook_1 = require("./fetchOrderBook");
10
+ const fetchTrades_1 = require("./fetchTrades");
11
+ const fetchPositions_1 = require("./fetchPositions");
12
+ const auth_1 = require("./auth");
13
+ const clob_client_1 = require("@polymarket/clob-client");
14
+ class PolymarketExchange extends BaseExchange_1.PredictionMarketExchange {
15
+ constructor(credentials) {
16
+ super(credentials);
17
+ // Initialize auth if credentials are provided
18
+ if (credentials?.privateKey) {
19
+ this.auth = new auth_1.PolymarketAuth(credentials);
20
+ }
21
+ }
22
+ get name() {
23
+ return 'Polymarket';
24
+ }
25
+ async fetchMarkets(params) {
26
+ return (0, fetchMarkets_1.fetchMarkets)(params);
27
+ }
28
+ async searchMarkets(query, params) {
29
+ return (0, searchMarkets_1.searchMarkets)(query, params);
30
+ }
31
+ async getMarketsBySlug(slug) {
32
+ return (0, getMarketsBySlug_1.getMarketsBySlug)(slug);
33
+ }
34
+ async fetchOHLCV(id, params) {
35
+ return (0, fetchOHLCV_1.fetchOHLCV)(id, params);
36
+ }
37
+ async fetchOrderBook(id) {
38
+ return (0, fetchOrderBook_1.fetchOrderBook)(id);
39
+ }
40
+ async fetchTrades(id, params) {
41
+ return (0, fetchTrades_1.fetchTrades)(id, params);
42
+ }
43
+ // ----------------------------------------------------------------------------
44
+ // Trading Methods
45
+ // ----------------------------------------------------------------------------
46
+ /**
47
+ * Ensure authentication is initialized before trading operations.
48
+ */
49
+ ensureAuth() {
50
+ if (!this.auth) {
51
+ throw new Error('Trading operations require authentication. ' +
52
+ 'Initialize PolymarketExchange with credentials: new PolymarketExchange({ privateKey: "0x..." })');
53
+ }
54
+ return this.auth;
55
+ }
56
+ async createOrder(params) {
57
+ const auth = this.ensureAuth();
58
+ const client = await auth.getClobClient();
59
+ // Map side to Polymarket enum
60
+ const side = params.side.toUpperCase() === 'BUY' ? clob_client_1.Side.BUY : clob_client_1.Side.SELL;
61
+ // For limit orders, price is required
62
+ if (params.type === 'limit' && !params.price) {
63
+ throw new Error('Price is required for limit orders');
64
+ }
65
+ // For market orders, use max slippage: 0.99 for BUY (willing to pay up to 99%), 0.01 for SELL (willing to accept down to 1%)
66
+ const price = params.price || (side === clob_client_1.Side.BUY ? 0.99 : 0.01);
67
+ try {
68
+ // We use createAndPostOrder which handles signing and posting
69
+ const response = await client.createAndPostOrder({
70
+ tokenID: params.outcomeId,
71
+ price: price,
72
+ side: side,
73
+ size: params.amount,
74
+ feeRateBps: 0,
75
+ }, {
76
+ tickSize: "0.01"
77
+ });
78
+ if (!response || !response.success) {
79
+ throw new Error(response?.errorMsg || 'Order placement failed');
80
+ }
81
+ return {
82
+ id: response.orderID,
83
+ marketId: params.marketId,
84
+ outcomeId: params.outcomeId,
85
+ side: params.side,
86
+ type: params.type,
87
+ price: price,
88
+ amount: params.amount,
89
+ status: 'open',
90
+ filled: 0,
91
+ remaining: params.amount,
92
+ timestamp: Date.now()
93
+ };
94
+ }
95
+ catch (error) {
96
+ console.error("Polymarket createOrder error:", error);
97
+ throw error;
98
+ }
99
+ }
100
+ async cancelOrder(orderId) {
101
+ const auth = this.ensureAuth();
102
+ const client = await auth.getClobClient();
103
+ try {
104
+ await client.cancelOrder({ orderID: orderId });
105
+ return {
106
+ id: orderId,
107
+ marketId: 'unknown',
108
+ outcomeId: 'unknown',
109
+ side: 'buy',
110
+ type: 'limit',
111
+ amount: 0,
112
+ status: 'cancelled',
113
+ filled: 0,
114
+ remaining: 0,
115
+ timestamp: Date.now()
116
+ };
117
+ }
118
+ catch (error) {
119
+ console.error("Polymarket cancelOrder error:", error);
120
+ throw error;
121
+ }
122
+ }
123
+ async fetchOrder(orderId) {
124
+ const auth = this.ensureAuth();
125
+ const client = await auth.getClobClient();
126
+ try {
127
+ const order = await client.getOrder(orderId);
128
+ return {
129
+ id: order.id,
130
+ marketId: order.market || 'unknown',
131
+ outcomeId: order.asset_id,
132
+ side: order.side.toLowerCase(),
133
+ type: order.order_type === 'GTC' ? 'limit' : 'market',
134
+ price: parseFloat(order.price),
135
+ amount: parseFloat(order.original_size),
136
+ status: order.status, // Needs precise mapping
137
+ filled: parseFloat(order.size_matched),
138
+ remaining: parseFloat(order.original_size) - parseFloat(order.size_matched),
139
+ timestamp: order.created_at * 1000
140
+ };
141
+ }
142
+ catch (error) {
143
+ console.error("Polymarket fetchOrder error:", error);
144
+ throw error;
145
+ }
146
+ }
147
+ async fetchOpenOrders(marketId) {
148
+ const auth = this.ensureAuth();
149
+ const client = await auth.getClobClient();
150
+ try {
151
+ const orders = await client.getOpenOrders({
152
+ market: marketId
153
+ });
154
+ return orders.map((o) => ({
155
+ id: o.id,
156
+ marketId: o.market || 'unknown',
157
+ outcomeId: o.asset_id,
158
+ side: o.side.toLowerCase(),
159
+ type: 'limit',
160
+ price: parseFloat(o.price),
161
+ amount: parseFloat(o.original_size),
162
+ status: 'open',
163
+ filled: parseFloat(o.size_matched),
164
+ remaining: parseFloat(o.size_left || (parseFloat(o.original_size) - parseFloat(o.size_matched))),
165
+ timestamp: o.created_at * 1000
166
+ }));
167
+ }
168
+ catch (error) {
169
+ console.error("Polymarket fetchOpenOrders error:", error);
170
+ return [];
171
+ }
172
+ }
173
+ async fetchPositions() {
174
+ const auth = this.ensureAuth();
175
+ const address = auth.getAddress();
176
+ return (0, fetchPositions_1.fetchPositions)(address);
177
+ }
178
+ async fetchBalance() {
179
+ const auth = this.ensureAuth();
180
+ const client = await auth.getClobClient();
181
+ try {
182
+ // 1. Fetch raw collateral balance (USDC)
183
+ // Polymarket relies strictly on USDC (Polygon) which has 6 decimals.
184
+ const USDC_DECIMALS = 6;
185
+ const balRes = await client.getBalanceAllowance({
186
+ asset_type: clob_client_1.AssetType.COLLATERAL
187
+ });
188
+ const rawBalance = parseFloat(balRes.balance);
189
+ const total = rawBalance / Math.pow(10, USDC_DECIMALS);
190
+ // 2. Fetch open orders to calculate locked funds
191
+ // We only care about BUY orders for USDC balance locking
192
+ const openOrders = await client.getOpenOrders({});
193
+ let locked = 0;
194
+ if (openOrders && Array.isArray(openOrders)) {
195
+ for (const order of openOrders) {
196
+ if (order.side === clob_client_1.Side.BUY) {
197
+ const remainingSize = parseFloat(order.original_size) - parseFloat(order.size_matched);
198
+ const price = parseFloat(order.price);
199
+ locked += remainingSize * price;
200
+ }
201
+ }
202
+ }
203
+ return [{
204
+ currency: 'USDC',
205
+ total: total,
206
+ available: total - locked, // Available for new trades
207
+ locked: locked
208
+ }];
209
+ }
210
+ catch (error) {
211
+ console.error("Polymarket fetchBalance error:", error);
212
+ throw error;
213
+ }
214
+ }
215
+ }
216
+ exports.PolymarketExchange = PolymarketExchange;
@@ -0,0 +1,3 @@
1
+ import { MarketFilterParams } from '../../BaseExchange';
2
+ import { UnifiedMarket } from '../../types';
3
+ export declare function searchMarkets(query: string, params?: MarketFilterParams): Promise<UnifiedMarket[]>;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.searchMarkets = searchMarkets;
4
+ const fetchMarkets_1 = require("./fetchMarkets");
5
+ async function searchMarkets(query, params) {
6
+ // Polymarket Gamma API doesn't support native search
7
+ // Fetch all active markets and filter client-side
8
+ const searchLimit = 100000; // Fetch all markets for comprehensive search
9
+ try {
10
+ // Fetch markets with a higher limit
11
+ const markets = await (0, fetchMarkets_1.fetchMarkets)({
12
+ ...params,
13
+ limit: searchLimit
14
+ });
15
+ // Client-side text filtering
16
+ const lowerQuery = query.toLowerCase();
17
+ const searchIn = params?.searchIn || 'title'; // Default to title-only search
18
+ const filtered = markets.filter(market => {
19
+ const titleMatch = (market.title || '').toLowerCase().includes(lowerQuery);
20
+ const descMatch = (market.description || '').toLowerCase().includes(lowerQuery);
21
+ if (searchIn === 'title')
22
+ return titleMatch;
23
+ if (searchIn === 'description')
24
+ return descMatch;
25
+ return titleMatch || descMatch; // 'both'
26
+ });
27
+ // Apply limit to filtered results
28
+ const limit = params?.limit || 20;
29
+ return filtered.slice(0, limit);
30
+ }
31
+ catch (error) {
32
+ console.error("Error searching Polymarket data:", error);
33
+ return [];
34
+ }
35
+ }
@@ -0,0 +1,7 @@
1
+ import { UnifiedMarket, CandleInterval } from '../../types';
2
+ export declare const GAMMA_API_URL = "https://gamma-api.polymarket.com/events";
3
+ export declare const CLOB_API_URL = "https://clob.polymarket.com";
4
+ export declare function mapMarketToUnified(event: any, market: any, options?: {
5
+ useQuestionAsCandidateFallback?: boolean;
6
+ }): UnifiedMarket | null;
7
+ export declare function mapIntervalToFidelity(interval: CandleInterval): number;
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CLOB_API_URL = exports.GAMMA_API_URL = void 0;
4
+ exports.mapMarketToUnified = mapMarketToUnified;
5
+ exports.mapIntervalToFidelity = mapIntervalToFidelity;
6
+ exports.GAMMA_API_URL = 'https://gamma-api.polymarket.com/events';
7
+ exports.CLOB_API_URL = 'https://clob.polymarket.com';
8
+ function mapMarketToUnified(event, market, options = {}) {
9
+ if (!market)
10
+ return null;
11
+ const outcomes = [];
12
+ // Polymarket Gamma often returns 'outcomes' and 'outcomePrices' as stringified JSON keys.
13
+ let outcomeLabels = [];
14
+ let outcomePrices = [];
15
+ try {
16
+ outcomeLabels = typeof market.outcomes === 'string' ? JSON.parse(market.outcomes) : (market.outcomes || []);
17
+ outcomePrices = typeof market.outcomePrices === 'string' ? JSON.parse(market.outcomePrices) : (market.outcomePrices || []);
18
+ }
19
+ catch (e) {
20
+ console.warn(`Error parsing outcomes for market ${market.id}:`, e);
21
+ }
22
+ // Extract CLOB token IDs for granular operations
23
+ let clobTokenIds = [];
24
+ try {
25
+ clobTokenIds = typeof market.clobTokenIds === 'string' ? JSON.parse(market.clobTokenIds) : (market.clobTokenIds || []);
26
+ }
27
+ catch (e) {
28
+ // console.warn(`Error parsing clobTokenIds for market ${market.id}`, e);
29
+ }
30
+ // Extract candidate/option name from market question for better outcome labels
31
+ let candidateName = null;
32
+ if (market.groupItemTitle) {
33
+ candidateName = market.groupItemTitle;
34
+ }
35
+ else if (market.question && options.useQuestionAsCandidateFallback) {
36
+ // Fallback or sometimes question is the candidate name in nested structures
37
+ // Only used if explicitly requested (e.g. for getMarketsBySlug)
38
+ candidateName = market.question;
39
+ }
40
+ if (outcomeLabels.length > 0) {
41
+ outcomeLabels.forEach((label, index) => {
42
+ const rawPrice = outcomePrices[index] || "0";
43
+ // For Yes/No markets with specific candidates, use the candidate name
44
+ let outcomeLabel = label;
45
+ if (candidateName && label.toLowerCase() === 'yes') {
46
+ outcomeLabel = candidateName;
47
+ }
48
+ else if (candidateName && label.toLowerCase() === 'no') {
49
+ outcomeLabel = `Not ${candidateName}`;
50
+ }
51
+ // 24h Price Change
52
+ // Polymarket API provides 'oneDayPriceChange' on the market object
53
+ let priceChange = 0;
54
+ if (index === 0 || label.toLowerCase() === 'yes' || (candidateName && label === candidateName)) {
55
+ priceChange = Number(market.oneDayPriceChange || 0);
56
+ }
57
+ outcomes.push({
58
+ id: clobTokenIds[index] || String(index), // Use CLOB Token ID as the primary ID
59
+ label: outcomeLabel,
60
+ price: parseFloat(rawPrice) || 0,
61
+ priceChange24h: priceChange,
62
+ metadata: {
63
+ // clobTokenId is now the main ID, but keeping it in metadata for backward compat if needed
64
+ clobTokenId: clobTokenIds[index]
65
+ }
66
+ });
67
+ });
68
+ }
69
+ return {
70
+ id: market.id,
71
+ title: market.question ? `${event.title} - ${market.question}` : event.title,
72
+ description: market.description || event.description,
73
+ outcomes: outcomes,
74
+ resolutionDate: market.endDate ? new Date(market.endDate) : (market.end_date_iso ? new Date(market.end_date_iso) : new Date()),
75
+ volume24h: Number(market.volume24hr || market.volume_24h || 0),
76
+ volume: Number(market.volume || 0),
77
+ liquidity: Number(market.liquidity || market.rewards?.liquidity || 0),
78
+ openInterest: Number(market.openInterest || market.open_interest || 0),
79
+ url: `https://polymarket.com/event/${event.slug}`,
80
+ image: event.image || market.image || `https://polymarket.com/api/og?slug=${event.slug}`,
81
+ category: event.category || event.tags?.[0]?.label,
82
+ tags: event.tags?.map((t) => t.label) || []
83
+ };
84
+ }
85
+ function mapIntervalToFidelity(interval) {
86
+ const mapping = {
87
+ '1m': 1,
88
+ '5m': 5,
89
+ '15m': 15,
90
+ '1h': 60,
91
+ '6h': 360,
92
+ '1d': 1440
93
+ };
94
+ return mapping[interval];
95
+ }