pmxt-core 2.7.0 → 2.9.0

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 (85) hide show
  1. package/dist/BaseExchange.d.ts +59 -0
  2. package/dist/BaseExchange.js +155 -0
  3. package/dist/exchanges/kalshi/api.d.ts +1763 -0
  4. package/dist/exchanges/kalshi/api.js +2554 -0
  5. package/dist/exchanges/kalshi/fetchEvents.d.ts +3 -2
  6. package/dist/exchanges/kalshi/fetchEvents.js +9 -16
  7. package/dist/exchanges/kalshi/fetchMarkets.d.ts +3 -2
  8. package/dist/exchanges/kalshi/fetchMarkets.js +27 -35
  9. package/dist/exchanges/kalshi/fetchOHLCV.d.ts +1 -2
  10. package/dist/exchanges/kalshi/fetchOHLCV.js +9 -11
  11. package/dist/exchanges/kalshi/index.d.ts +2 -1
  12. package/dist/exchanges/kalshi/index.js +161 -183
  13. package/dist/exchanges/kalshi/kalshi.test.js +51 -31
  14. package/dist/exchanges/limitless/api.d.ts +555 -0
  15. package/dist/exchanges/limitless/api.js +863 -0
  16. package/dist/exchanges/limitless/fetchEvents.d.ts +1 -2
  17. package/dist/exchanges/limitless/fetchEvents.js +9 -15
  18. package/dist/exchanges/limitless/fetchMarkets.d.ts +1 -2
  19. package/dist/exchanges/limitless/fetchMarkets.js +11 -18
  20. package/dist/exchanges/limitless/fetchOHLCV.d.ts +1 -2
  21. package/dist/exchanges/limitless/fetchOHLCV.js +2 -11
  22. package/dist/exchanges/limitless/fetchOrderBook.d.ts +1 -2
  23. package/dist/exchanges/limitless/fetchOrderBook.js +2 -11
  24. package/dist/exchanges/limitless/index.d.ts +1 -0
  25. package/dist/exchanges/limitless/index.js +28 -7
  26. package/dist/exchanges/limitless/websocket.d.ts +2 -1
  27. package/dist/exchanges/limitless/websocket.js +6 -4
  28. package/dist/exchanges/myriad/api.d.ts +294 -0
  29. package/dist/exchanges/myriad/api.js +690 -0
  30. package/dist/exchanges/myriad/fetchOHLCV.d.ts +1 -2
  31. package/dist/exchanges/myriad/fetchOHLCV.js +3 -11
  32. package/dist/exchanges/myriad/fetchOrderBook.d.ts +1 -2
  33. package/dist/exchanges/myriad/fetchOrderBook.js +3 -11
  34. package/dist/exchanges/myriad/index.d.ts +2 -0
  35. package/dist/exchanges/myriad/index.js +116 -103
  36. package/dist/exchanges/myriad/websocket.d.ts +2 -2
  37. package/dist/exchanges/myriad/websocket.js +28 -6
  38. package/dist/exchanges/polymarket/api-clob.d.ts +346 -0
  39. package/dist/exchanges/polymarket/api-clob.js +517 -0
  40. package/dist/exchanges/polymarket/api-data.d.ts +789 -0
  41. package/dist/exchanges/polymarket/api-data.js +860 -0
  42. package/dist/exchanges/polymarket/api-gamma.d.ts +556 -0
  43. package/dist/exchanges/polymarket/api-gamma.js +1161 -0
  44. package/dist/exchanges/polymarket/fetchEvents.js +0 -68
  45. package/dist/exchanges/polymarket/fetchMarkets.js +2 -2
  46. package/dist/exchanges/polymarket/fetchOHLCV.d.ts +1 -2
  47. package/dist/exchanges/polymarket/fetchOHLCV.js +4 -10
  48. package/dist/exchanges/polymarket/fetchOrderBook.d.ts +1 -2
  49. package/dist/exchanges/polymarket/fetchOrderBook.js +2 -10
  50. package/dist/exchanges/polymarket/fetchTrades.d.ts +1 -2
  51. package/dist/exchanges/polymarket/fetchTrades.js +2 -11
  52. package/dist/exchanges/polymarket/index.d.ts +10 -0
  53. package/dist/exchanges/polymarket/index.js +110 -5
  54. package/dist/exchanges/probable/api.d.ts +605 -0
  55. package/dist/exchanges/probable/api.js +887 -0
  56. package/dist/exchanges/probable/fetchEvents.d.ts +3 -3
  57. package/dist/exchanges/probable/fetchEvents.js +28 -25
  58. package/dist/exchanges/probable/fetchMarkets.d.ts +1 -1
  59. package/dist/exchanges/probable/fetchMarkets.js +25 -21
  60. package/dist/exchanges/probable/index.d.ts +1 -0
  61. package/dist/exchanges/probable/index.js +92 -10
  62. package/dist/exchanges/probable/utils.d.ts +1 -2
  63. package/dist/exchanges/probable/utils.js +4 -11
  64. package/dist/index.d.ts +1 -0
  65. package/dist/index.js +3 -1
  66. package/dist/types.d.ts +1 -0
  67. package/dist/utils/openapi.d.ts +9 -0
  68. package/dist/utils/openapi.js +59 -0
  69. package/package.json +4 -3
  70. package/dist/exchanges/kalshi/fetchOrderBook.d.ts +0 -3
  71. package/dist/exchanges/kalshi/fetchOrderBook.js +0 -59
  72. package/dist/exchanges/kalshi/fetchTrades.d.ts +0 -4
  73. package/dist/exchanges/kalshi/fetchTrades.js +0 -31
  74. package/dist/exchanges/limitless/fetchPositions.d.ts +0 -2
  75. package/dist/exchanges/limitless/fetchPositions.js +0 -34
  76. package/dist/exchanges/myriad/fetchTrades.d.ts +0 -4
  77. package/dist/exchanges/myriad/fetchTrades.js +0 -62
  78. package/dist/exchanges/polymarket/fetchPositions.d.ts +0 -2
  79. package/dist/exchanges/polymarket/fetchPositions.js +0 -34
  80. package/dist/exchanges/probable/fetchOHLCV.d.ts +0 -4
  81. package/dist/exchanges/probable/fetchOHLCV.js +0 -83
  82. package/dist/exchanges/probable/fetchOrderBook.d.ts +0 -3
  83. package/dist/exchanges/probable/fetchOrderBook.js +0 -37
  84. package/dist/exchanges/probable/fetchPositions.d.ts +0 -2
  85. package/dist/exchanges/probable/fetchPositions.js +0 -33
@@ -1,6 +1,22 @@
1
1
  import { UnifiedMarket, UnifiedEvent, PriceCandle, CandleInterval, OrderBook, Trade, Order, Position, Balance, CreateOrderParams } from './types';
2
2
  import { ExecutionPriceResult } from './utils/math';
3
3
  import { AxiosInstance } from 'axios';
4
+ export interface ApiEndpoint {
5
+ method: string;
6
+ path: string;
7
+ isPrivate?: boolean;
8
+ operationId?: string;
9
+ }
10
+ export interface ApiDescriptor {
11
+ baseUrl: string;
12
+ endpoints: Record<string, ApiEndpoint>;
13
+ }
14
+ export interface ImplicitApiMethodInfo {
15
+ name: string;
16
+ method: string;
17
+ path: string;
18
+ isPrivate: boolean;
19
+ }
4
20
  export interface MarketFilterParams {
5
21
  limit?: number;
6
22
  offset?: number;
@@ -120,12 +136,26 @@ export interface ExchangeCredentials {
120
136
  funderAddress?: string;
121
137
  }
122
138
  export declare abstract class PredictionMarketExchange {
139
+ [key: string]: any;
123
140
  protected credentials?: ExchangeCredentials;
124
141
  verbose: boolean;
125
142
  http: AxiosInstance;
143
+ markets: Record<string, UnifiedMarket>;
144
+ marketsBySlug: Record<string, UnifiedMarket>;
145
+ loadedMarkets: boolean;
146
+ protected apiDescriptor?: ApiDescriptor;
147
+ private apiDescriptors;
126
148
  readonly has: ExchangeHas;
127
149
  constructor(credentials?: ExchangeCredentials);
128
150
  abstract get name(): string;
151
+ /**
152
+ * Load and cache markets from the exchange.
153
+ * This method populates `this.markets` and `this.marketsBySlug`.
154
+ *
155
+ * @param reload - Force a reload of markets from the API even if already loaded
156
+ * @returns Dictionary of markets indexed by marketId
157
+ */
158
+ loadMarkets(reload?: boolean): Promise<Record<string, UnifiedMarket>>;
129
159
  /**
130
160
  * Fetch markets with optional filtering, search, or slug lookup.
131
161
  * This is the primary method for retrieving markets.
@@ -584,4 +614,33 @@ export declare abstract class PredictionMarketExchange {
584
614
  * exchange.close()
585
615
  */
586
616
  close(): Promise<void>;
617
+ /**
618
+ * Call an implicit API method by its operationId (or auto-generated name).
619
+ * Provides a typed entry point so unified methods can delegate to the implicit API
620
+ * without casting to `any` everywhere.
621
+ */
622
+ protected callApi(operationId: string, params?: Record<string, any>): Promise<any>;
623
+ /**
624
+ * Parse an API descriptor and generate callable methods on this instance.
625
+ * Existing methods (unified API) are never overwritten.
626
+ */
627
+ protected defineImplicitApi(descriptor: ApiDescriptor): void;
628
+ /**
629
+ * Creates an async function for an implicit API endpoint.
630
+ */
631
+ private createImplicitMethod;
632
+ /**
633
+ * Returns auth headers for a private API call.
634
+ * Exchanges should override this to provide authentication.
635
+ */
636
+ protected sign(_method: string, _path: string, _params: Record<string, any>): Record<string, string>;
637
+ /**
638
+ * Maps errors from implicit API calls through the exchange's error mapper.
639
+ * Exchanges should override this to use their specific error mapper.
640
+ */
641
+ protected mapImplicitApiError(error: any): any;
642
+ /**
643
+ * Introspection getter: returns info about all implicit API methods.
644
+ */
645
+ get implicitApi(): ImplicitApiMethodInfo[];
587
646
  }
@@ -14,6 +14,13 @@ class PredictionMarketExchange {
14
14
  credentials;
15
15
  verbose = false;
16
16
  http;
17
+ // Market Cache
18
+ markets = {};
19
+ marketsBySlug = {};
20
+ loadedMarkets = false;
21
+ // Implicit API (merged across multiple defineImplicitApi calls)
22
+ apiDescriptor;
23
+ apiDescriptors = [];
17
24
  has = {
18
25
  fetchMarkets: false,
19
26
  fetchEvents: false,
@@ -63,6 +70,32 @@ class PredictionMarketExchange {
63
70
  return Promise.reject(error);
64
71
  });
65
72
  }
73
+ /**
74
+ * Load and cache markets from the exchange.
75
+ * This method populates `this.markets` and `this.marketsBySlug`.
76
+ *
77
+ * @param reload - Force a reload of markets from the API even if already loaded
78
+ * @returns Dictionary of markets indexed by marketId
79
+ */
80
+ async loadMarkets(reload = false) {
81
+ if (this.loadedMarkets && !reload) {
82
+ return this.markets;
83
+ }
84
+ // Fetch all markets (implementation dependent, usually fetches active markets)
85
+ const markets = await this.fetchMarkets();
86
+ // Reset caches
87
+ this.markets = {};
88
+ this.marketsBySlug = {};
89
+ for (const market of markets) {
90
+ this.markets[market.marketId] = market;
91
+ // Some exchanges provide slugs, if so cache them
92
+ if (market.slug) {
93
+ this.marketsBySlug[market.slug] = market;
94
+ }
95
+ }
96
+ this.loadedMarkets = true;
97
+ return this.markets;
98
+ }
66
99
  /**
67
100
  * Fetch markets with optional filtering, search, or slug lookup.
68
101
  * This is the primary method for retrieving markets.
@@ -142,6 +175,15 @@ class PredictionMarketExchange {
142
175
  * market = exchange.fetch_market(market_id='663583')
143
176
  */
144
177
  async fetchMarket(params) {
178
+ // Try to fetch from cache first if we have loaded markets and have an ID/slug
179
+ if (this.loadedMarkets) {
180
+ if (params?.marketId && this.markets[params.marketId]) {
181
+ return this.markets[params.marketId];
182
+ }
183
+ if (params?.slug && this.marketsBySlug[params.slug]) {
184
+ return this.marketsBySlug[params.slug];
185
+ }
186
+ }
145
187
  const markets = await this.fetchMarkets(params);
146
188
  if (markets.length === 0) {
147
189
  const identifier = params?.marketId || params?.outcomeId || params?.slug || params?.eventId || params?.query || 'unknown';
@@ -786,5 +828,118 @@ class PredictionMarketExchange {
786
828
  // Default implementation: no-op
787
829
  // Exchanges with WebSocket support should override this
788
830
  }
831
+ // ----------------------------------------------------------------------------
832
+ // Implicit API (OpenAPI-driven method generation)
833
+ // ----------------------------------------------------------------------------
834
+ /**
835
+ * Call an implicit API method by its operationId (or auto-generated name).
836
+ * Provides a typed entry point so unified methods can delegate to the implicit API
837
+ * without casting to `any` everywhere.
838
+ */
839
+ async callApi(operationId, params) {
840
+ const method = this[operationId];
841
+ if (typeof method !== 'function') {
842
+ throw new Error(`Implicit API method "${operationId}" not found on ${this.name}`);
843
+ }
844
+ return method.call(this, params);
845
+ }
846
+ /**
847
+ * Parse an API descriptor and generate callable methods on this instance.
848
+ * Existing methods (unified API) are never overwritten.
849
+ */
850
+ defineImplicitApi(descriptor) {
851
+ this.apiDescriptors.push(descriptor);
852
+ // Merge into a single apiDescriptor for the implicitApi getter
853
+ if (!this.apiDescriptor) {
854
+ this.apiDescriptor = { baseUrl: descriptor.baseUrl, endpoints: { ...descriptor.endpoints } };
855
+ }
856
+ else {
857
+ Object.assign(this.apiDescriptor.endpoints, descriptor.endpoints);
858
+ }
859
+ for (const [name, endpoint] of Object.entries(descriptor.endpoints)) {
860
+ // Never overwrite existing methods (unified API wins)
861
+ if (name in this) {
862
+ continue;
863
+ }
864
+ this[name] = this.createImplicitMethod(name, endpoint, descriptor.baseUrl);
865
+ }
866
+ }
867
+ /**
868
+ * Creates an async function for an implicit API endpoint.
869
+ */
870
+ createImplicitMethod(name, endpoint, baseUrl) {
871
+ return async (params) => {
872
+ const allParams = { ...(params || {}) };
873
+ // Substitute path parameters like {ticker} from params
874
+ let resolvedPath = endpoint.path.replace(/\{([^}]+)\}/g, (_match, key) => {
875
+ const value = allParams[key];
876
+ if (value === undefined) {
877
+ throw new Error(`Missing required path parameter "${key}" for ${name}(). ` +
878
+ `Path: ${endpoint.path}`);
879
+ }
880
+ delete allParams[key];
881
+ return encodeURIComponent(String(value));
882
+ });
883
+ // Get auth headers for private endpoints
884
+ let headers = {};
885
+ if (endpoint.isPrivate) {
886
+ headers = this.sign(endpoint.method, resolvedPath, allParams);
887
+ }
888
+ const url = `${baseUrl}${resolvedPath}`;
889
+ const method = endpoint.method.toUpperCase();
890
+ try {
891
+ let response;
892
+ if (method === 'GET' || method === 'DELETE') {
893
+ // Remaining params go to query string
894
+ response = await this.http.request({
895
+ method: method,
896
+ url,
897
+ params: Object.keys(allParams).length > 0 ? allParams : undefined,
898
+ headers,
899
+ });
900
+ }
901
+ else {
902
+ // POST/PUT/PATCH: remaining params go to JSON body
903
+ response = await this.http.request({
904
+ method: method,
905
+ url,
906
+ data: Object.keys(allParams).length > 0 ? allParams : undefined,
907
+ headers: { 'Content-Type': 'application/json', ...headers },
908
+ });
909
+ }
910
+ return response.data;
911
+ }
912
+ catch (error) {
913
+ throw this.mapImplicitApiError(error);
914
+ }
915
+ };
916
+ }
917
+ /**
918
+ * Returns auth headers for a private API call.
919
+ * Exchanges should override this to provide authentication.
920
+ */
921
+ sign(_method, _path, _params) {
922
+ return {};
923
+ }
924
+ /**
925
+ * Maps errors from implicit API calls through the exchange's error mapper.
926
+ * Exchanges should override this to use their specific error mapper.
927
+ */
928
+ mapImplicitApiError(error) {
929
+ throw error;
930
+ }
931
+ /**
932
+ * Introspection getter: returns info about all implicit API methods.
933
+ */
934
+ get implicitApi() {
935
+ if (!this.apiDescriptor)
936
+ return [];
937
+ return Object.entries(this.apiDescriptor.endpoints).map(([name, endpoint]) => ({
938
+ name,
939
+ method: endpoint.method,
940
+ path: endpoint.path,
941
+ isPrivate: !!endpoint.isPrivate,
942
+ }));
943
+ }
789
944
  }
790
945
  exports.PredictionMarketExchange = PredictionMarketExchange;