@vesper85/strategy-sdk 0.1.0 → 0.1.3
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/README.md +339 -209
- package/dist/clients/polymarket-client.d.ts +1 -18
- package/dist/engine/event-runner.d.ts +33 -3
- package/dist/engine/polymarket-event-runner.d.ts +205 -0
- package/dist/engine/strategy-runner.d.ts +3 -4
- package/dist/index.d.ts +5 -2
- package/dist/index.js +1738 -204
- package/dist/rtds/index.d.ts +9 -0
- package/dist/rtds/osiris-rtds.service.d.ts +134 -0
- package/dist/rtds/polymarket-rtds.service.d.ts +117 -0
- package/dist/rtds/unified-rtds.service.d.ts +146 -0
- package/dist/types/event-types.d.ts +281 -11
- package/dist/types/gamma.d.ts +135 -0
- package/dist/types/osiris.d.ts +3 -40
- package/dist/utils/index.d.ts +3 -0
- package/package.json +6 -4
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { GammaMarket, type Event as GammaEvent, type Tag, type Team, type Sport, type Series, type Comment as GammaComment, type SearchResults, type PaginatedResponse, type MarketFilters, type EventFilters, type EventPaginationFilters, type PaginationParams, type SearchParams } from "polymarket-gamma";
|
|
2
2
|
import type { PolymarketAPI, PolymarketClientOptions, PolymarketLimitOrderParams, PolymarketMarketOrderParams, PolymarketOrderResponse, PolymarketOpenOrder, PolymarketCancelResponse, PolymarketOrderBook, PolymarketApprovalResult, SignerAPI } from "../types/osiris";
|
|
3
3
|
import type { Logger } from "../utils/logger";
|
|
4
4
|
export declare class PolymarketClient implements PolymarketAPI {
|
|
@@ -16,7 +16,6 @@ export declare class PolymarketClient implements PolymarketAPI {
|
|
|
16
16
|
constructor(logger: Logger, signer: SignerAPI, userAddress: string, options?: PolymarketClientOptions);
|
|
17
17
|
initializeTradingClient(): Promise<void>;
|
|
18
18
|
isTradingClientInitialized(): boolean;
|
|
19
|
-
private getPrivateKeyFromSigner;
|
|
20
19
|
private ensureTradingClient;
|
|
21
20
|
private ensureMcpInstance;
|
|
22
21
|
/**
|
|
@@ -87,22 +86,6 @@ export declare class PolymarketClient implements PolymarketAPI {
|
|
|
87
86
|
private ensureApprovals;
|
|
88
87
|
buy(tokenId: string, size: number): Promise<void>;
|
|
89
88
|
sell(tokenId: string, size: number): Promise<void>;
|
|
90
|
-
/**
|
|
91
|
-
* Place a limit buy order
|
|
92
|
-
*/
|
|
93
|
-
buyLimit(tokenId: string, price: number, size: number): Promise<PolymarketOrderResponse>;
|
|
94
|
-
/**
|
|
95
|
-
* Place a limit sell order
|
|
96
|
-
*/
|
|
97
|
-
sellLimit(tokenId: string, price: number, size: number): Promise<PolymarketOrderResponse>;
|
|
98
|
-
/**
|
|
99
|
-
* Place a market buy order
|
|
100
|
-
*/
|
|
101
|
-
buyMarket(tokenId: string, amount: number, slippage?: number): Promise<PolymarketOrderResponse>;
|
|
102
|
-
/**
|
|
103
|
-
* Place a market sell order
|
|
104
|
-
*/
|
|
105
|
-
sellMarket(tokenId: string, size: number, slippage?: number): Promise<PolymarketOrderResponse>;
|
|
106
89
|
getPrice(ticker: string): Promise<number>;
|
|
107
90
|
getIndicator(ticker: string, indicator: string, params?: any): Promise<number>;
|
|
108
91
|
search(params: SearchParams): Promise<SearchResults>;
|
|
@@ -1,16 +1,24 @@
|
|
|
1
1
|
import type { CodeStrategy } from "../types/strategy";
|
|
2
2
|
import type { OsirisContext } from "../types/osiris";
|
|
3
3
|
import type { Logger } from "../utils/logger";
|
|
4
|
+
import { PolymarketEventRunner, type PolymarketClobAuth } from "./polymarket-event-runner";
|
|
5
|
+
export type EventRunner = OsirisEventRunner | PolymarketEventRunner;
|
|
4
6
|
/**
|
|
5
7
|
* Configuration for the EventRunner
|
|
6
8
|
*/
|
|
7
9
|
export interface EventRunnerConfig {
|
|
8
10
|
/** Logger instance for logging */
|
|
9
11
|
logger: Logger;
|
|
10
|
-
/** WebSocket URL for the event source server */
|
|
11
|
-
eventSourceUrl
|
|
12
|
+
/** WebSocket URL for the event source server (Required for 'osiris' source) */
|
|
13
|
+
eventSourceUrl?: string;
|
|
12
14
|
/** Optional strategy ID for logging (defaults to 'unnamed-strategy') */
|
|
13
15
|
strategyId?: string;
|
|
16
|
+
/** Polymarket specific configuration */
|
|
17
|
+
polymarket?: {
|
|
18
|
+
clobAuth?: PolymarketClobAuth;
|
|
19
|
+
walletAddress?: string;
|
|
20
|
+
pingIntervalMs?: number;
|
|
21
|
+
};
|
|
14
22
|
/** Reconnection settings */
|
|
15
23
|
reconnect?: {
|
|
16
24
|
/** Maximum number of reconnection attempts (default: 10) */
|
|
@@ -41,7 +49,12 @@ export interface EventRunnerConfig {
|
|
|
41
49
|
* runner.stop();
|
|
42
50
|
* ```
|
|
43
51
|
*/
|
|
44
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Osiris Event Runner (Generic)
|
|
54
|
+
*
|
|
55
|
+
* Connects to a generic Osiris WebSocket event source.
|
|
56
|
+
*/
|
|
57
|
+
export declare class OsirisEventRunner {
|
|
45
58
|
private strategy;
|
|
46
59
|
private config;
|
|
47
60
|
private context;
|
|
@@ -87,7 +100,19 @@ export declare class EventRunner {
|
|
|
87
100
|
/**
|
|
88
101
|
* Subscribe to events based on strategy's subscriptions
|
|
89
102
|
*/
|
|
103
|
+
/**
|
|
104
|
+
* Subscribe to events based on strategy's subscriptions
|
|
105
|
+
* Maps SDK subscriptions to Osiris Pub/Sub protocol
|
|
106
|
+
*/
|
|
90
107
|
private subscribeToEvents;
|
|
108
|
+
/**
|
|
109
|
+
* Map EventSubscription to Osiris Pub/Sub topic string
|
|
110
|
+
* Uses discriminated union to properly handle each subscription type
|
|
111
|
+
*/
|
|
112
|
+
private mapSubscriptionToTopic;
|
|
113
|
+
/**
|
|
114
|
+
* Unsubscribe from events before disconnecting
|
|
115
|
+
*/
|
|
91
116
|
/**
|
|
92
117
|
* Unsubscribe from events before disconnecting
|
|
93
118
|
*/
|
|
@@ -96,6 +121,11 @@ export declare class EventRunner {
|
|
|
96
121
|
* Handle incoming WebSocket messages
|
|
97
122
|
*/
|
|
98
123
|
private handleMessage;
|
|
124
|
+
/**
|
|
125
|
+
* Map incoming Osiris message to StrategyEvent
|
|
126
|
+
* Uses proper fields for each event type (market vs wallet)
|
|
127
|
+
*/
|
|
128
|
+
private mapMessageToStrategyEvent;
|
|
99
129
|
/**
|
|
100
130
|
* Validate that a message is a valid StrategyEvent
|
|
101
131
|
*/
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import type { CodeStrategy } from "../types/strategy";
|
|
2
|
+
import type { OsirisContext } from "../types/osiris";
|
|
3
|
+
import type { Logger } from "../utils/logger";
|
|
4
|
+
/**
|
|
5
|
+
* Polymarket RTDS subscription topics
|
|
6
|
+
*/
|
|
7
|
+
export type PolymarketRTDSTopic = 'crypto_prices' | 'comments' | 'activity' | 'orders' | 'trades' | 'profile';
|
|
8
|
+
/**
|
|
9
|
+
* CLOB authentication for trading-related subscriptions
|
|
10
|
+
*/
|
|
11
|
+
export interface PolymarketClobAuth {
|
|
12
|
+
key: string;
|
|
13
|
+
secret: string;
|
|
14
|
+
passphrase: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Gamma authentication for user-specific data
|
|
18
|
+
*/
|
|
19
|
+
export interface PolymarketGammaAuth {
|
|
20
|
+
address: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Polymarket RTDS subscription configuration
|
|
24
|
+
*/
|
|
25
|
+
export interface PolymarketRTDSSubscription {
|
|
26
|
+
topic: PolymarketRTDSTopic;
|
|
27
|
+
type?: string;
|
|
28
|
+
filters?: string;
|
|
29
|
+
clob_auth?: PolymarketClobAuth;
|
|
30
|
+
gamma_auth?: PolymarketGammaAuth;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Configuration for the PolymarketEventRunner
|
|
34
|
+
*/
|
|
35
|
+
export interface PolymarketEventRunnerConfig {
|
|
36
|
+
/** Logger instance for logging */
|
|
37
|
+
logger: Logger;
|
|
38
|
+
/** Optional strategy ID for logging (defaults to 'unnamed-strategy') */
|
|
39
|
+
strategyId?: string;
|
|
40
|
+
/** CLOB API credentials (required for order/trade subscriptions) */
|
|
41
|
+
clobAuth?: PolymarketClobAuth;
|
|
42
|
+
/** Wallet address for user-specific subscriptions */
|
|
43
|
+
walletAddress?: string;
|
|
44
|
+
/** Reconnection settings */
|
|
45
|
+
reconnect?: {
|
|
46
|
+
/** Maximum number of reconnection attempts (default: 10) */
|
|
47
|
+
maxAttempts?: number;
|
|
48
|
+
/** Base delay between reconnection attempts in ms (default: 1000) */
|
|
49
|
+
delayMs?: number;
|
|
50
|
+
/** Maximum delay between reconnection attempts in ms (default: 30000) */
|
|
51
|
+
maxDelayMs?: number;
|
|
52
|
+
};
|
|
53
|
+
/** Ping interval in ms (default: 5000) - Polymarket recommends 5 seconds */
|
|
54
|
+
pingIntervalMs?: number;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Polymarket Event Runner for event-based strategy execution
|
|
58
|
+
*
|
|
59
|
+
* Connects directly to Polymarket's Real-Time Data Socket (RTDS) and dispatches
|
|
60
|
+
* events to the strategy's onEvent handler.
|
|
61
|
+
*
|
|
62
|
+
* @see https://docs.polymarket.com/developers/RTDS/RTDS-overview
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* const runner = createPolymarketEventRunner(strategy, {
|
|
67
|
+
* strategyId: 'price-alert-strategy',
|
|
68
|
+
* logger: createConsoleLogger(),
|
|
69
|
+
* walletAddress: '0x...',
|
|
70
|
+
* }, context);
|
|
71
|
+
*
|
|
72
|
+
* runner.start();
|
|
73
|
+
* // ... later
|
|
74
|
+
* runner.stop();
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export declare class PolymarketEventRunner {
|
|
78
|
+
private strategy;
|
|
79
|
+
private config;
|
|
80
|
+
private context;
|
|
81
|
+
private ws;
|
|
82
|
+
private isRunning;
|
|
83
|
+
private reconnectAttempts;
|
|
84
|
+
private reconnectTimeout;
|
|
85
|
+
private pingInterval;
|
|
86
|
+
private isIntentionallyClosed;
|
|
87
|
+
private readonly maxReconnectAttempts;
|
|
88
|
+
private readonly baseReconnectDelay;
|
|
89
|
+
private readonly maxReconnectDelay;
|
|
90
|
+
private readonly pingIntervalMs;
|
|
91
|
+
constructor(strategy: CodeStrategy, config: PolymarketEventRunnerConfig, context: OsirisContext);
|
|
92
|
+
/**
|
|
93
|
+
* Start listening to events
|
|
94
|
+
* Connects to Polymarket RTDS and subscribes to events
|
|
95
|
+
*/
|
|
96
|
+
start(): void;
|
|
97
|
+
/**
|
|
98
|
+
* Stop listening and disconnect from WebSocket
|
|
99
|
+
*/
|
|
100
|
+
stop(): void;
|
|
101
|
+
/**
|
|
102
|
+
* Check if the runner is currently active
|
|
103
|
+
*/
|
|
104
|
+
isActive(): boolean;
|
|
105
|
+
/**
|
|
106
|
+
* Get current connection status
|
|
107
|
+
*/
|
|
108
|
+
getStatus(): {
|
|
109
|
+
running: boolean;
|
|
110
|
+
connected: boolean;
|
|
111
|
+
reconnectAttempts: number;
|
|
112
|
+
subscriptionCount: number;
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* Connect to Polymarket RTDS WebSocket
|
|
116
|
+
*/
|
|
117
|
+
private connect;
|
|
118
|
+
/**
|
|
119
|
+
* Start ping interval to maintain connection (Polymarket recommends 5 seconds)
|
|
120
|
+
*/
|
|
121
|
+
private startPingInterval;
|
|
122
|
+
/**
|
|
123
|
+
* Attempt to reconnect with exponential backoff
|
|
124
|
+
*/
|
|
125
|
+
private attemptReconnect;
|
|
126
|
+
/**
|
|
127
|
+
* Map strategy subscriptions to Polymarket RTDS subscriptions
|
|
128
|
+
* Only MarketSubscription types are supported by Polymarket RTDS
|
|
129
|
+
* WalletSubscription and OpportunitySubscription are Osiris-only features
|
|
130
|
+
*/
|
|
131
|
+
private mapToRTDSSubscriptions;
|
|
132
|
+
/**
|
|
133
|
+
* Get the message type for a given RTDS topic
|
|
134
|
+
* Each topic has specific message types as per the Polymarket RTDS documentation
|
|
135
|
+
*
|
|
136
|
+
* Note: Only 'crypto_prices' and 'comments' are documented RTDS topics.
|
|
137
|
+
* 'trades' and 'orders' subscriptions require the CLOB WebSocket API, not RTDS.
|
|
138
|
+
*/
|
|
139
|
+
private getMessageTypeForTopic;
|
|
140
|
+
/**
|
|
141
|
+
* Map EventType to Polymarket RTDS topic
|
|
142
|
+
*/
|
|
143
|
+
private mapEventTypeToTopic;
|
|
144
|
+
/**
|
|
145
|
+
* Subscribe to events based on strategy's subscriptions
|
|
146
|
+
*/
|
|
147
|
+
private subscribeToEvents;
|
|
148
|
+
/**
|
|
149
|
+
* Unsubscribe from events before disconnecting
|
|
150
|
+
*/
|
|
151
|
+
private unsubscribeFromEvents;
|
|
152
|
+
/**
|
|
153
|
+
* Handle incoming WebSocket messages
|
|
154
|
+
*/
|
|
155
|
+
private handleMessage;
|
|
156
|
+
/**
|
|
157
|
+
* Validate that a message is a valid Polymarket RTDS message
|
|
158
|
+
*/
|
|
159
|
+
private isValidRTDSMessage;
|
|
160
|
+
/**
|
|
161
|
+
* Convert Polymarket RTDS message to StrategyEvent
|
|
162
|
+
*/
|
|
163
|
+
private convertToStrategyEvent;
|
|
164
|
+
/**
|
|
165
|
+
* Map Polymarket RTDS topic to EventType
|
|
166
|
+
*/
|
|
167
|
+
private mapTopicToEventType;
|
|
168
|
+
/**
|
|
169
|
+
* Extract event data from Polymarket RTDS payload
|
|
170
|
+
*/
|
|
171
|
+
private extractEventData;
|
|
172
|
+
/**
|
|
173
|
+
* Dispatch an event to the strategy's onEvent handler
|
|
174
|
+
*/
|
|
175
|
+
private dispatchEvent;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Create a PolymarketEventRunner instance for event-based strategy execution
|
|
179
|
+
*
|
|
180
|
+
* Connects directly to Polymarket's Real-Time Data Socket (RTDS) without
|
|
181
|
+
* requiring an external event source server.
|
|
182
|
+
*
|
|
183
|
+
* @param strategy - The strategy to run (must have subscriptions and onEvent)
|
|
184
|
+
* @param config - Runner configuration
|
|
185
|
+
* @param context - Osiris context with market APIs
|
|
186
|
+
* @returns PolymarketEventRunner instance
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* ```typescript
|
|
190
|
+
* const runner = createPolymarketEventRunner(myStrategy, {
|
|
191
|
+
* strategyId: 'price-alert-strategy',
|
|
192
|
+
* logger: createConsoleLogger(),
|
|
193
|
+
* walletAddress: '0x...', // For user-specific events
|
|
194
|
+
* clobAuth: { // For order/trade events
|
|
195
|
+
* key: 'your-api-key',
|
|
196
|
+
* secret: 'your-api-secret',
|
|
197
|
+
* passphrase: 'your-passphrase',
|
|
198
|
+
* },
|
|
199
|
+
* }, context);
|
|
200
|
+
*
|
|
201
|
+
* runner.start();
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
export declare function createPolymarketEventRunner(strategy: CodeStrategy, config: PolymarketEventRunnerConfig, context: OsirisContext): PolymarketEventRunner;
|
|
205
|
+
//# sourceMappingURL=polymarket-event-runner.d.ts.map
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import type { CodeStrategy } from "../types/strategy";
|
|
2
2
|
import type { OsirisContext } from "../types/osiris";
|
|
3
|
-
import type
|
|
3
|
+
import { type Logger } from "../utils/logger";
|
|
4
4
|
export interface StrategyRunnerConfig {
|
|
5
|
-
/** Logger instance for logging */
|
|
6
|
-
logger: Logger;
|
|
7
5
|
/** Optional strategy ID for logging (defaults to 'unnamed-strategy') */
|
|
8
6
|
strategyId?: string;
|
|
7
|
+
logger?: Logger;
|
|
9
8
|
}
|
|
10
9
|
export interface StrategyEngineConfig extends StrategyRunnerConfig {
|
|
11
10
|
/** Interval between ticks in milliseconds (default: 60000) */
|
|
@@ -22,7 +21,7 @@ export declare function runStrategy(strategy: CodeStrategy, config: StrategyRunn
|
|
|
22
21
|
* Create a strategy engine that runs a strategy on a tick interval
|
|
23
22
|
* Returns a controller object to start/stop the engine
|
|
24
23
|
*/
|
|
25
|
-
export declare function createStrategyEngine(strategy: CodeStrategy,
|
|
24
|
+
export declare function createStrategyEngine(strategy: CodeStrategy, context: OsirisContext, config: StrategyEngineConfig): StrategyEngine;
|
|
26
25
|
/**
|
|
27
26
|
* Strategy Engine that runs a strategy on a tick interval
|
|
28
27
|
*/
|
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
export type { CodeStrategy, } from './types/strategy';
|
|
2
2
|
export type { OsirisContext, OsirisState, PolymarketAPI, HyperliquidAPI, TechnicalAnalysisAPI, SignerAPI, PolymarketOrderSide, PolymarketLimitOrderParams, PolymarketMarketOrderParams, PolymarketOrderResponse, PolymarketOpenOrder, PolymarketCancelResponse, PolymarketOrderBook, PolymarketOrderBookEntry, PolymarketApprovalResult, PolymarketClientOptions, } from './types/osiris';
|
|
3
|
-
export type { EventType, EventSubscription, EventConditions, StrategyEvent, EventData, EventSourceSubscribeMessage, EventSourceUnsubscribeMessage, EventSourceMessage, } from './types/event-types';
|
|
3
|
+
export type { EventType, EventSourceType, EventSubscription, EventConditions, StrategyEvent, EventData, EventSourceSubscribeMessage, EventSourceUnsubscribeMessage, EventSourceMessage, MarketSubscription, WalletSubscription, OpportunitySubscription, CustomSubscription, ClobMarketSubscription, ClobUserSubscription, ActivitySubscription, CommentsSubscription, CryptoPricesSubscription, RfqSubscription, } from './types/event-types';
|
|
4
|
+
export { isMarketSubscription, isWalletSubscription, isOpportunitySubscription, isCustomSubscription, isClobMarketSubscription, isClobUserSubscription, isActivitySubscription, isCommentsSubscription, isCryptoPricesSubscription, isRfqSubscription, isPolymarketSubscription, isOsirisSubscription, } from './types/event-types';
|
|
4
5
|
export { PolymarketClient, } from './clients/polymarket-client';
|
|
5
6
|
export { HyperliquidClient, } from './clients/hyperliquid-client';
|
|
6
7
|
export { MemoryStateManager, } from './state/memory-state';
|
|
7
8
|
export { RedisStateManager, } from './state/redis-state';
|
|
8
9
|
export { createOsirisContext, type OsirisContextConfig, } from './context/osiris-context';
|
|
9
10
|
export { runStrategy, createStrategyEngine, StrategyEngine, isEventBasedStrategy, isTickBasedStrategy, validateStrategy, type StrategyRunnerConfig, type StrategyEngineConfig, } from './engine/strategy-runner';
|
|
10
|
-
export {
|
|
11
|
+
export { OsirisEventRunner, createEventRunner, type EventRunner, type EventRunnerConfig, } from './engine/event-runner';
|
|
12
|
+
export { PolymarketEventRunner, createPolymarketEventRunner, type PolymarketEventRunnerConfig, type PolymarketClobAuth, type PolymarketGammaAuth, type PolymarketRTDSSubscription, type PolymarketRTDSTopic, } from './engine/polymarket-event-runner';
|
|
13
|
+
export { UnifiedRTDSService, createUnifiedRTDSService, PolymarketRTDSService, createPolymarketRTDSService, OsirisRTDSService, createOsirisRTDSService, DEFAULT_OSIRIS_RTDS_URL, type UnifiedRTDSConfig, type PolymarketRTDSConfig, type OsirisRTDSConfig, type ClobApiKeyCreds, } from './rtds';
|
|
11
14
|
export { createConsoleLogger, type Logger, } from './utils/logger';
|
|
12
15
|
export { Signer, OsirisSigner, PrivateKeySigner, type ISigner, type SignerConfig, type OsirisSignerConfig, type PrivateKeySignerConfig, } from './signer';
|
|
13
16
|
//# sourceMappingURL=index.d.ts.map
|