@k256/sdk 0.1.4 → 0.1.7

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 CHANGED
@@ -25,7 +25,7 @@ const client = new K256WebSocketClient({
25
25
 
26
26
  // Message callbacks
27
27
  onPoolUpdate: (update) => console.log('Pool:', update.data.poolAddress),
28
- onPriorityFees: (fees) => console.log('Fees:', fees.data.recommended),
28
+ onFeeMarket: (fees) => console.log('Fees:', fees.data.recommended),
29
29
  onBlockhash: (bh) => console.log('Blockhash:', bh.data.blockhash),
30
30
 
31
31
  // Connection callbacks
@@ -38,7 +38,6 @@ const client = new K256WebSocketClient({
38
38
  await client.connect();
39
39
  client.subscribe({
40
40
  channels: ['pools', 'priority_fees', 'blockhash'],
41
- protocols: ['Raydium AMM', 'Orca Whirlpool'], // Optional filter
42
41
  });
43
42
 
44
43
  // Streaming quotes
@@ -68,7 +67,7 @@ client.disconnect();
68
67
  | Type | Description |
69
68
  |------|-------------|
70
69
  | `pool_update` | DEX pool state change |
71
- | `priority_fees` | Fee estimates (~400ms updates) |
70
+ | `fee_market` | Per-writable-account fee market (~400ms updates) |
72
71
  | `blockhash` | Latest blockhash (~400ms updates) |
73
72
  | `quote` | Streaming quote update |
74
73
  | `subscribed` | Subscription confirmed |
@@ -162,14 +161,14 @@ git clone https://github.com/k256-xyz/k256-sdks.git
162
161
  cd k256-sdks/typescript/examples
163
162
  npm install
164
163
 
165
- # Run with your API key
164
+ # Run with your API key (all channels)
166
165
  K256_API_KEY=your-key npx tsx websocket.ts
167
166
 
168
167
  # Subscribe to specific channel
169
168
  K256_API_KEY=your-key npx tsx websocket.ts --channel=priority_fees
170
169
 
171
- # Filter by protocol
172
- K256_API_KEY=your-key npx tsx websocket.ts --protocol="Raydium AMM"
170
+ # Subscribe to blockhash only
171
+ K256_API_KEY=your-key npx tsx websocket.ts --channel=blockhash
173
172
  ```
174
173
 
175
174
  See [examples/](https://github.com/k256-xyz/k256-sdks/tree/main/typescript/examples) for all available examples.
package/dist/index.cjs CHANGED
@@ -23,8 +23,15 @@ function base58Encode(bytes) {
23
23
  return leadingZeros + digits.reverse().map((d) => BASE58_ALPHABET[d]).join("");
24
24
  }
25
25
  function base58Decode(str) {
26
+ if (str.length === 0) {
27
+ return new Uint8Array(0);
28
+ }
29
+ let leadingZeros = 0;
30
+ for (let i = 0; i < str.length && str[i] === "1"; i++) {
31
+ leadingZeros++;
32
+ }
26
33
  const bytes = [0];
27
- for (let i = 0; i < str.length; i++) {
34
+ for (let i = leadingZeros; i < str.length; i++) {
28
35
  const char = str[i];
29
36
  const value = BASE58_ALPHABET.indexOf(char);
30
37
  if (value === -1) {
@@ -41,10 +48,19 @@ function base58Decode(str) {
41
48
  carry >>= 8;
42
49
  }
43
50
  }
44
- for (let i = 0; i < str.length && str[i] === "1"; i++) {
45
- bytes.push(0);
51
+ bytes.reverse();
52
+ if (leadingZeros > 0 && bytes.length === 1 && bytes[0] === 0) {
53
+ return new Uint8Array(leadingZeros);
54
+ }
55
+ const startIdx = bytes.length > 1 && bytes[0] === 0 ? 1 : 0;
56
+ const result = new Uint8Array(leadingZeros + bytes.length - startIdx);
57
+ for (let i = 0; i < leadingZeros; i++) {
58
+ result[i] = 0;
46
59
  }
47
- return new Uint8Array(bytes.reverse());
60
+ for (let i = startIdx; i < bytes.length; i++) {
61
+ result[leadingZeros + i - startIdx] = bytes[i];
62
+ }
63
+ return result;
48
64
  }
49
65
  function isValidPubkey(address) {
50
66
  if (address.length < 32 || address.length > 44) {
@@ -133,55 +149,54 @@ function decodeMessage(data) {
133
149
  return { type: "error", data: { message: text } };
134
150
  }
135
151
  case MessageType.PriorityFees: {
136
- if (payload.byteLength < 24) return null;
152
+ if (payload.byteLength < 42) return null;
137
153
  const slot = Number(payloadView.getBigUint64(0, true));
138
154
  const timestampMs = Number(payloadView.getBigUint64(8, true));
139
155
  const recommended = Number(payloadView.getBigUint64(16, true));
140
- const state = payload.byteLength > 24 ? payloadView.getUint8(24) : 1;
141
- const isStale = payload.byteLength > 25 ? payloadView.getUint8(25) !== 0 : false;
142
- let swapP50 = 0, swapP75 = 0, swapP90 = 0, swapP99 = 0;
143
- if (payload.byteLength >= 58) {
144
- swapP50 = Number(payloadView.getBigUint64(26, true));
145
- swapP75 = Number(payloadView.getBigUint64(34, true));
146
- swapP90 = Number(payloadView.getBigUint64(42, true));
147
- swapP99 = Number(payloadView.getBigUint64(50, true));
148
- }
149
- let swapSamples = 0;
150
- let landingP50Fee = 0, landingP75Fee = 0, landingP90Fee = 0, landingP99Fee = 0;
151
- let top10Fee = 0, top25Fee = 0;
152
- let spikeDetected = false, spikeFee = 0;
153
- if (payload.byteLength >= 119) {
154
- swapSamples = payloadView.getUint32(58, true);
155
- landingP50Fee = Number(payloadView.getBigUint64(62, true));
156
- landingP75Fee = Number(payloadView.getBigUint64(70, true));
157
- landingP90Fee = Number(payloadView.getBigUint64(78, true));
158
- landingP99Fee = Number(payloadView.getBigUint64(86, true));
159
- top10Fee = Number(payloadView.getBigUint64(94, true));
160
- top25Fee = Number(payloadView.getBigUint64(102, true));
161
- spikeDetected = payloadView.getUint8(110) !== 0;
162
- spikeFee = Number(payloadView.getBigUint64(111, true));
156
+ const state = payloadView.getUint8(24);
157
+ const isStale = payloadView.getUint8(25) !== 0;
158
+ const blockUtilizationPct = payloadView.getFloat32(26, true);
159
+ const blocksInWindow = payloadView.getUint32(30, true);
160
+ const accountCount = Number(payloadView.getBigUint64(34, true));
161
+ const accounts = [];
162
+ let offset = 42;
163
+ for (let i = 0; i < accountCount && offset + 92 <= payload.byteLength; i++) {
164
+ const pubkeyBytes = new Uint8Array(payload, offset, 32);
165
+ const pubkey = base58Encode(pubkeyBytes);
166
+ const totalTxs = payloadView.getUint32(offset + 32, true);
167
+ const activeSlots = payloadView.getUint32(offset + 36, true);
168
+ const cuConsumed = Number(payloadView.getBigUint64(offset + 40, true));
169
+ const utilizationPct = payloadView.getFloat32(offset + 48, true);
170
+ const p25 = Number(payloadView.getBigUint64(offset + 52, true));
171
+ const p50 = Number(payloadView.getBigUint64(offset + 60, true));
172
+ const p75 = Number(payloadView.getBigUint64(offset + 68, true));
173
+ const p90 = Number(payloadView.getBigUint64(offset + 76, true));
174
+ const minNonzeroPrice = Number(payloadView.getBigUint64(offset + 84, true));
175
+ accounts.push({
176
+ pubkey,
177
+ totalTxs,
178
+ activeSlots,
179
+ cuConsumed,
180
+ utilizationPct,
181
+ p25,
182
+ p50,
183
+ p75,
184
+ p90,
185
+ minNonzeroPrice
186
+ });
187
+ offset += 92;
163
188
  }
164
189
  return {
165
- type: "priority_fees",
190
+ type: "fee_market",
166
191
  data: {
167
192
  slot,
168
193
  timestampMs,
169
194
  recommended,
170
195
  state,
171
196
  isStale,
172
- swapP50,
173
- swapP75,
174
- swapP90,
175
- swapP99,
176
- swapSamples,
177
- landingP50Fee,
178
- landingP75Fee,
179
- landingP90Fee,
180
- landingP99Fee,
181
- top10Fee,
182
- top25Fee,
183
- spikeDetected,
184
- spikeFee
197
+ blockUtilizationPct,
198
+ blocksInWindow,
199
+ accounts
185
200
  }
186
201
  };
187
202
  }
@@ -293,8 +308,6 @@ function decodePoolUpdate(payload, payloadView) {
293
308
  tokenDecimals.push(payloadView.getInt32(offset, true));
294
309
  offset += 4;
295
310
  }
296
- const isValid = offset < payload.byteLength ? payloadView.getUint8(offset) !== 0 : true;
297
- offset += 1;
298
311
  return {
299
312
  type: "pool_update",
300
313
  data: {
@@ -305,25 +318,11 @@ function decodePoolUpdate(payload, payloadView) {
305
318
  poolAddress: base58Encode(poolAddr),
306
319
  tokenMints,
307
320
  tokenBalances,
308
- tokenDecimals,
309
- isValid
321
+ tokenDecimals
310
322
  }
311
323
  };
312
324
  } catch {
313
- return {
314
- type: "pool_update",
315
- data: {
316
- sequence: 0,
317
- slot: 0,
318
- writeVersion: 0,
319
- protocol: "unknown",
320
- poolAddress: "",
321
- tokenMints: [],
322
- tokenBalances: [],
323
- tokenDecimals: [],
324
- isValid: false
325
- }
326
- };
325
+ return null;
327
326
  }
328
327
  }
329
328
  function decodeQuote(payload, payloadView) {
@@ -394,25 +393,7 @@ function decodeQuote(payload, payloadView) {
394
393
  }
395
394
  };
396
395
  } catch {
397
- return {
398
- type: "quote",
399
- data: {
400
- topicId: "",
401
- timestampMs: 0,
402
- sequence: 0,
403
- inputMint: "",
404
- outputMint: "",
405
- inAmount: "0",
406
- outAmount: "0",
407
- priceImpactBps: 0,
408
- contextSlot: 0,
409
- algorithm: "",
410
- isImprovement: false,
411
- isCached: false,
412
- isStale: false,
413
- routePlan: null
414
- }
415
- };
396
+ return null;
416
397
  }
417
398
  }
418
399
 
@@ -714,8 +695,8 @@ var K256WebSocketClient = class {
714
695
  case "pool_update":
715
696
  this.config.onPoolUpdate?.(decoded);
716
697
  break;
717
- case "priority_fees":
718
- this.config.onPriorityFees?.(decoded);
698
+ case "fee_market":
699
+ this.config.onFeeMarket?.(decoded);
719
700
  break;
720
701
  case "blockhash":
721
702
  this.config.onBlockhash?.(decoded);