@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.
- package/LICENSE +21 -0
- package/README.md +159 -0
- package/dist/index.cjs +949 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +938 -0
- package/dist/index.js.map +1 -0
- package/dist/types/index.cjs +14 -0
- package/dist/types/index.cjs.map +1 -0
- package/dist/types/index.d.cts +202 -0
- package/dist/types/index.d.ts +202 -0
- package/dist/types/index.js +12 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/index.cjs +70 -0
- package/dist/utils/index.cjs.map +1 -0
- package/dist/utils/index.d.cts +48 -0
- package/dist/utils/index.d.ts +48 -0
- package/dist/utils/index.js +66 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/ws/index.cjs +896 -0
- package/dist/ws/index.cjs.map +1 -0
- package/dist/ws/index.d.cts +516 -0
- package/dist/ws/index.d.ts +516 -0
- package/dist/ws/index.js +889 -0
- package/dist/ws/index.js.map +1 -0
- package/package.json +92 -0
- package/src/index.ts +34 -0
- package/src/types/index.ts +212 -0
- package/src/utils/base58.ts +123 -0
- package/src/utils/index.ts +7 -0
- package/src/ws/client.ts +786 -0
- package/src/ws/decoder.ts +490 -0
- package/src/ws/index.ts +55 -0
- package/src/ws/types.ts +227 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-2026 K256
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# @k256/sdk
|
|
2
|
+
|
|
3
|
+
Official TypeScript SDK for [K256](https://k256.xyz) - the fastest Solana swap aggregator.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @k256/sdk
|
|
9
|
+
# or
|
|
10
|
+
yarn add @k256/sdk
|
|
11
|
+
# or
|
|
12
|
+
pnpm add @k256/sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { K256WebSocketClient } from '@k256/sdk';
|
|
19
|
+
|
|
20
|
+
const client = new K256WebSocketClient({
|
|
21
|
+
apiKey: 'your-api-key',
|
|
22
|
+
mode: 'binary', // 'binary' (default, fastest) or 'json' (debugging)
|
|
23
|
+
|
|
24
|
+
// Message callbacks
|
|
25
|
+
onPoolUpdate: (update) => console.log('Pool:', update.data.poolAddress),
|
|
26
|
+
onPriorityFees: (fees) => console.log('Fees:', fees.data.recommended),
|
|
27
|
+
onBlockhash: (bh) => console.log('Blockhash:', bh.data.blockhash),
|
|
28
|
+
|
|
29
|
+
// Connection callbacks
|
|
30
|
+
onConnect: () => console.log('Connected'),
|
|
31
|
+
onDisconnect: (code, reason) => console.log(`Disconnected: ${code}`),
|
|
32
|
+
onError: (error) => console.error('Error:', error.message),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// Connect and subscribe
|
|
36
|
+
await client.connect();
|
|
37
|
+
client.subscribe({
|
|
38
|
+
channels: ['pools', 'priority_fees', 'blockhash'],
|
|
39
|
+
protocols: ['Raydium AMM', 'Orca Whirlpool'], // Optional filter
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// Streaming quotes
|
|
43
|
+
client.subscribeQuote({
|
|
44
|
+
inputMint: 'So11111111111111111111111111111111111111112', // SOL
|
|
45
|
+
outputMint: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', // USDC
|
|
46
|
+
amount: 1_000_000_000, // 1 SOL in lamports
|
|
47
|
+
slippageBps: 50,
|
|
48
|
+
refreshIntervalMs: 1000,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// Graceful disconnect
|
|
52
|
+
client.disconnect();
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Features
|
|
56
|
+
|
|
57
|
+
- **Binary Protocol**: Direct decoding of K2's high-performance bincode format
|
|
58
|
+
- **Automatic Reconnection**: Exponential backoff with configurable limits
|
|
59
|
+
- **Ping/Pong Keepalive**: Automatic connection health monitoring
|
|
60
|
+
- **Heartbeat Tracking**: Server status and connection stats
|
|
61
|
+
- **Type-Safe Events**: Full TypeScript types for all message types
|
|
62
|
+
- **RFC 6455 Close Codes**: Proper WebSocket close code handling
|
|
63
|
+
|
|
64
|
+
## Message Types
|
|
65
|
+
|
|
66
|
+
| Type | Description |
|
|
67
|
+
|------|-------------|
|
|
68
|
+
| `pool_update` | DEX pool state change |
|
|
69
|
+
| `priority_fees` | Fee estimates (~400ms updates) |
|
|
70
|
+
| `blockhash` | Latest blockhash (~400ms updates) |
|
|
71
|
+
| `quote` | Streaming quote update |
|
|
72
|
+
| `subscribed` | Subscription confirmed |
|
|
73
|
+
| `heartbeat` | Connection stats |
|
|
74
|
+
| `error` | Server error message |
|
|
75
|
+
|
|
76
|
+
## Connection States
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
type ConnectionState =
|
|
80
|
+
| 'disconnected' // Not connected
|
|
81
|
+
| 'connecting' // Initial connection in progress
|
|
82
|
+
| 'connected' // Active connection
|
|
83
|
+
| 'reconnecting' // Auto-reconnecting after disconnect
|
|
84
|
+
| 'closed'; // Explicitly closed, won't reconnect
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Error Handling
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
import { K256WebSocketError } from '@k256/sdk';
|
|
91
|
+
|
|
92
|
+
client.onError = (error: K256WebSocketError) => {
|
|
93
|
+
console.error(`[${error.code}] ${error.message}`);
|
|
94
|
+
|
|
95
|
+
if (error.isAuthError) {
|
|
96
|
+
// Invalid API key - don't retry
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (error.isRecoverable) {
|
|
100
|
+
// Will auto-reconnect
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Low-Level Decoder
|
|
106
|
+
|
|
107
|
+
For advanced usage, you can use the decoder directly:
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
import { decodeMessage, decodePoolUpdateBatch, MessageType } from '@k256/sdk/ws';
|
|
111
|
+
|
|
112
|
+
const ws = new WebSocket('wss://gateway.k256.xyz/v1/ws?apiKey=...');
|
|
113
|
+
ws.binaryType = 'arraybuffer';
|
|
114
|
+
|
|
115
|
+
ws.onmessage = (event) => {
|
|
116
|
+
if (event.data instanceof ArrayBuffer) {
|
|
117
|
+
const message = decodeMessage(event.data);
|
|
118
|
+
|
|
119
|
+
// Handle batched pool updates
|
|
120
|
+
if (message && new DataView(event.data).getUint8(0) === MessageType.PoolUpdateBatch) {
|
|
121
|
+
const updates = decodePoolUpdateBatch(event.data.slice(1));
|
|
122
|
+
for (const update of updates) {
|
|
123
|
+
console.log(update.data.poolAddress);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Configuration
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
interface K256WebSocketClientConfig {
|
|
134
|
+
apiKey: string;
|
|
135
|
+
url?: string; // Default: wss://gateway.k256.xyz/v1/ws
|
|
136
|
+
mode?: 'binary' | 'json'; // Default: 'binary'
|
|
137
|
+
|
|
138
|
+
// Reconnection
|
|
139
|
+
autoReconnect?: boolean; // Default: true
|
|
140
|
+
reconnectDelayMs?: number; // Default: 1000
|
|
141
|
+
maxReconnectDelayMs?: number; // Default: 30000
|
|
142
|
+
maxReconnectAttempts?: number; // Default: Infinity
|
|
143
|
+
|
|
144
|
+
// Keepalive
|
|
145
|
+
pingIntervalMs?: number; // Default: 30000
|
|
146
|
+
pongTimeoutMs?: number; // Default: 10000
|
|
147
|
+
heartbeatTimeoutMs?: number; // Default: 15000
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Links
|
|
152
|
+
|
|
153
|
+
- [K256 Website](https://k256.xyz)
|
|
154
|
+
- [API Documentation](https://docs.k256.xyz)
|
|
155
|
+
- [GitHub](https://github.com/k256-xyz)
|
|
156
|
+
|
|
157
|
+
## License
|
|
158
|
+
|
|
159
|
+
MIT
|