@vesper85/strategy-sdk 0.1.0 → 0.1.2
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 +202 -213
- package/dist/clients/polymarket-client.d.ts +0 -16
- package/dist/engine/event-runner.d.ts +33 -3
- package/dist/engine/polymarket-event-runner.d.ts +202 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.js +661 -176
- package/dist/types/event-types.d.ts +111 -11
- package/dist/types/osiris.d.ts +2 -39
- package/package.json +3 -2
|
@@ -0,0 +1,202 @@
|
|
|
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
|
+
private getMessageTypeForTopic;
|
|
137
|
+
/**
|
|
138
|
+
* Map EventType to Polymarket RTDS topic
|
|
139
|
+
*/
|
|
140
|
+
private mapEventTypeToTopic;
|
|
141
|
+
/**
|
|
142
|
+
* Subscribe to events based on strategy's subscriptions
|
|
143
|
+
*/
|
|
144
|
+
private subscribeToEvents;
|
|
145
|
+
/**
|
|
146
|
+
* Unsubscribe from events before disconnecting
|
|
147
|
+
*/
|
|
148
|
+
private unsubscribeFromEvents;
|
|
149
|
+
/**
|
|
150
|
+
* Handle incoming WebSocket messages
|
|
151
|
+
*/
|
|
152
|
+
private handleMessage;
|
|
153
|
+
/**
|
|
154
|
+
* Validate that a message is a valid Polymarket RTDS message
|
|
155
|
+
*/
|
|
156
|
+
private isValidRTDSMessage;
|
|
157
|
+
/**
|
|
158
|
+
* Convert Polymarket RTDS message to StrategyEvent
|
|
159
|
+
*/
|
|
160
|
+
private convertToStrategyEvent;
|
|
161
|
+
/**
|
|
162
|
+
* Map Polymarket RTDS topic to EventType
|
|
163
|
+
*/
|
|
164
|
+
private mapTopicToEventType;
|
|
165
|
+
/**
|
|
166
|
+
* Extract event data from Polymarket RTDS payload
|
|
167
|
+
*/
|
|
168
|
+
private extractEventData;
|
|
169
|
+
/**
|
|
170
|
+
* Dispatch an event to the strategy's onEvent handler
|
|
171
|
+
*/
|
|
172
|
+
private dispatchEvent;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Create a PolymarketEventRunner instance for event-based strategy execution
|
|
176
|
+
*
|
|
177
|
+
* Connects directly to Polymarket's Real-Time Data Socket (RTDS) without
|
|
178
|
+
* requiring an external event source server.
|
|
179
|
+
*
|
|
180
|
+
* @param strategy - The strategy to run (must have subscriptions and onEvent)
|
|
181
|
+
* @param config - Runner configuration
|
|
182
|
+
* @param context - Osiris context with market APIs
|
|
183
|
+
* @returns PolymarketEventRunner instance
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```typescript
|
|
187
|
+
* const runner = createPolymarketEventRunner(myStrategy, {
|
|
188
|
+
* strategyId: 'price-alert-strategy',
|
|
189
|
+
* logger: createConsoleLogger(),
|
|
190
|
+
* walletAddress: '0x...', // For user-specific events
|
|
191
|
+
* clobAuth: { // For order/trade events
|
|
192
|
+
* key: 'your-api-key',
|
|
193
|
+
* secret: 'your-api-secret',
|
|
194
|
+
* passphrase: 'your-passphrase',
|
|
195
|
+
* },
|
|
196
|
+
* }, context);
|
|
197
|
+
*
|
|
198
|
+
* runner.start();
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
export declare function createPolymarketEventRunner(strategy: CodeStrategy, config: PolymarketEventRunnerConfig, context: OsirisContext): PolymarketEventRunner;
|
|
202
|
+
//# sourceMappingURL=polymarket-event-runner.d.ts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
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, } from './types/event-types';
|
|
4
|
+
export { isMarketSubscription, isWalletSubscription, isOpportunitySubscription, isCustomSubscription, } 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';
|
|
11
13
|
export { createConsoleLogger, type Logger, } from './utils/logger';
|
|
12
14
|
export { Signer, OsirisSigner, PrivateKeySigner, type ISigner, type SignerConfig, type OsirisSignerConfig, type PrivateKeySignerConfig, } from './signer';
|
|
13
15
|
//# sourceMappingURL=index.d.ts.map
|