@k256/sdk 0.1.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.
@@ -0,0 +1,227 @@
1
+ /**
2
+ * WebSocket message types and interfaces
3
+ *
4
+ * Message type constants MUST match the K2 server values.
5
+ * See: https://github.com/k256-xyz for protocol documentation
6
+ */
7
+
8
+ /**
9
+ * WebSocket message type constants
10
+ *
11
+ * These are the byte prefixes used in the binary protocol.
12
+ * All SDKs across all languages MUST use these exact values.
13
+ */
14
+ export const MessageType = {
15
+ /** Single pool state update (bincode) */
16
+ PoolUpdate: 0x01,
17
+ /** Subscribe request (JSON) - Client → Server */
18
+ Subscribe: 0x02,
19
+ /** Subscription confirmed (JSON) - Server → Client */
20
+ Subscribed: 0x03,
21
+ /** Unsubscribe all - Client → Server */
22
+ Unsubscribe: 0x04,
23
+ /** Priority fee update (bincode) */
24
+ PriorityFees: 0x05,
25
+ /** Recent blockhash (bincode) */
26
+ Blockhash: 0x06,
27
+ /** Streaming quote update (bincode) */
28
+ Quote: 0x07,
29
+ /** Quote subscription confirmed (JSON) */
30
+ QuoteSubscribed: 0x08,
31
+ /** Subscribe to quote stream (JSON) - Client → Server */
32
+ SubscribeQuote: 0x09,
33
+ /** Unsubscribe from quote (JSON) - Client → Server */
34
+ UnsubscribeQuote: 0x0a,
35
+ /** Ping keepalive - Client → Server */
36
+ Ping: 0x0b,
37
+ /** Pong response (bincode u64 timestamp) */
38
+ Pong: 0x0c,
39
+ /** Connection heartbeat with stats (JSON) */
40
+ Heartbeat: 0x0d,
41
+ /** Batched pool updates for high throughput */
42
+ PoolUpdateBatch: 0x0e,
43
+ /** Error message (UTF-8 string) */
44
+ Error: 0xff,
45
+ } as const;
46
+
47
+ export type MessageTypeValue = typeof MessageType[keyof typeof MessageType];
48
+
49
+ /**
50
+ * Decoded pool update from binary message
51
+ */
52
+ export interface PoolUpdateMessage {
53
+ type: 'pool_update';
54
+ data: {
55
+ sequence: number;
56
+ slot: number;
57
+ writeVersion: number;
58
+ protocol: string;
59
+ poolAddress: string;
60
+ tokenMints: string[];
61
+ tokenBalances: string[];
62
+ tokenDecimals: number[];
63
+ isValid: boolean;
64
+ bestBid?: { price: string; size: string };
65
+ bestAsk?: { price: string; size: string };
66
+ };
67
+ }
68
+
69
+ /**
70
+ * Decoded priority fees from binary message
71
+ *
72
+ * All fields from K2 PriorityFeesWire
73
+ */
74
+ export interface PriorityFeesMessage {
75
+ type: 'priority_fees';
76
+ data: {
77
+ slot: number;
78
+ timestampMs: number;
79
+ /** Recommended priority fee in micro-lamports */
80
+ recommended: number;
81
+ /** Fee state: 0=low, 1=normal, 2=high, 3=extreme */
82
+ state: number;
83
+ /** True if data is stale (no recent samples) */
84
+ isStale: boolean;
85
+ // Swap fee percentiles (for ≥50K CU transactions)
86
+ swapP50: number;
87
+ swapP75: number;
88
+ swapP90: number;
89
+ swapP99: number;
90
+ /** Number of samples used for swap percentiles */
91
+ swapSamples: number;
92
+ // Landing probability fees (fee to land with X% probability)
93
+ landingP50Fee: number;
94
+ landingP75Fee: number;
95
+ landingP90Fee: number;
96
+ landingP99Fee: number;
97
+ // Top fee tiers
98
+ top10Fee: number;
99
+ top25Fee: number;
100
+ // Spike detection
101
+ spikeDetected: boolean;
102
+ spikeFee: number;
103
+ };
104
+ }
105
+
106
+ /**
107
+ * Decoded blockhash from binary message
108
+ */
109
+ export interface BlockhashMessage {
110
+ type: 'blockhash';
111
+ data: {
112
+ slot: number;
113
+ timestampMs: number;
114
+ blockhash: string;
115
+ blockHeight: number;
116
+ lastValidBlockHeight: number;
117
+ isStale: boolean;
118
+ };
119
+ }
120
+
121
+ /**
122
+ * Decoded quote from binary message
123
+ */
124
+ export interface QuoteMessage {
125
+ type: 'quote';
126
+ data: {
127
+ topicId: string;
128
+ timestampMs: number;
129
+ sequence: number;
130
+ inputMint: string;
131
+ outputMint: string;
132
+ inAmount: string;
133
+ outAmount: string;
134
+ priceImpactBps: number;
135
+ contextSlot: number;
136
+ algorithm: string;
137
+ isImprovement: boolean;
138
+ isCached: boolean;
139
+ isStale: boolean;
140
+ routePlan: unknown | null;
141
+ };
142
+ }
143
+
144
+ /**
145
+ * Decoded heartbeat from JSON message
146
+ */
147
+ export interface HeartbeatMessage {
148
+ type: 'heartbeat';
149
+ data: {
150
+ timestampMs: number;
151
+ uptimeSecs: number;
152
+ messagesSent: number;
153
+ poolUpdatesSent: number;
154
+ messagesDropped: number;
155
+ poolUpdatesEnabled: boolean;
156
+ subscribedChannels: string[];
157
+ serverSequence: number;
158
+ };
159
+ }
160
+
161
+ /**
162
+ * Subscription confirmed message
163
+ */
164
+ export interface SubscribedMessage {
165
+ type: 'subscribed';
166
+ data: {
167
+ channelCount: number;
168
+ channels: string[];
169
+ poolCount: number;
170
+ tokenPairCount: number;
171
+ protocolCount: number;
172
+ poolUpdatesEnabled: boolean;
173
+ timestampMs: number;
174
+ summary: string;
175
+ format?: 'binary' | 'json';
176
+ };
177
+ }
178
+
179
+ /**
180
+ * Quote subscription confirmed message
181
+ */
182
+ export interface QuoteSubscribedMessage {
183
+ type: 'quote_subscribed';
184
+ data: {
185
+ topicId: string;
186
+ inputMint: string;
187
+ outputMint: string;
188
+ amount: string;
189
+ slippageBps: number;
190
+ refreshIntervalMs: number;
191
+ timestampMs: number;
192
+ };
193
+ }
194
+
195
+ /**
196
+ * Error message from server
197
+ */
198
+ export interface ErrorMessage {
199
+ type: 'error';
200
+ data: {
201
+ message: string;
202
+ };
203
+ }
204
+
205
+ /**
206
+ * Pong response message
207
+ */
208
+ export interface PongMessage {
209
+ type: 'pong';
210
+ data: {
211
+ timestampMs: number;
212
+ };
213
+ }
214
+
215
+ /**
216
+ * Union of all decoded message types
217
+ */
218
+ export type DecodedMessage =
219
+ | PoolUpdateMessage
220
+ | PriorityFeesMessage
221
+ | BlockhashMessage
222
+ | QuoteMessage
223
+ | HeartbeatMessage
224
+ | SubscribedMessage
225
+ | QuoteSubscribedMessage
226
+ | ErrorMessage
227
+ | PongMessage;