Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lazer/sdk/js/examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ await client.subscribe({
subscriptionId: 1,
priceFeedIds: [1, 2],
properties: ["price"],
chains: ["solana"],
formats: ["solana"],
deliveryFormat: "binary",
channel: "fixed_rate@200ms",
parsed: false,
Expand All @@ -63,7 +63,7 @@ await client.subscribe({
subscriptionId: 2,
priceFeedIds: [1, 2, 3, 4, 5],
properties: ["price", "exponent", "publisherCount", "confidence"],
chains: ["evm"],
formats: ["evm"],
deliveryFormat: "json",
channel: "fixed_rate@200ms",
parsed: true,
Expand Down
2 changes: 1 addition & 1 deletion lazer/sdk/js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pythnetwork/pyth-lazer-sdk",
"version": "0.3.3",
"version": "0.4.0",
"description": "Pyth Lazer SDK",
"publishConfig": {
"access": "public"
Expand Down
24 changes: 14 additions & 10 deletions lazer/sdk/js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import WebSocket from "isomorphic-ws";
import { dummyLogger, type Logger } from "ts-log";

import {
BINARY_UPDATE_FORMAT_MAGIC,
EVM_FORMAT_MAGIC,
PARSED_FORMAT_MAGIC,
BINARY_UPDATE_FORMAT_MAGIC_LE,
FORMAT_MAGICS_LE,
type ParsedPayload,
type Request,
type Response,
SOLANA_FORMAT_MAGIC_BE,
} from "./protocol.js";
import { WebSocketPool } from "./socket/websocket-pool.js";

Expand All @@ -17,6 +15,8 @@ export type BinaryResponse = {
evm?: Buffer | undefined;
solana?: Buffer | undefined;
parsed?: ParsedPayload | undefined;
leEcdsa?: Buffer | undefined;
leUnsigned?: Buffer | undefined;
};
export type JsonOrBinaryResponse =
| {
Expand Down Expand Up @@ -64,9 +64,9 @@ export class PythLazerClient {
});
} else if (Buffer.isBuffer(data)) {
let pos = 0;
const magic = data.subarray(pos, pos + UINT32_NUM_BYTES).readUint32BE();
const magic = data.subarray(pos, pos + UINT32_NUM_BYTES).readUint32LE();
pos += UINT32_NUM_BYTES;
if (magic != BINARY_UPDATE_FORMAT_MAGIC) {
if (magic != BINARY_UPDATE_FORMAT_MAGIC_LE) {
throw new Error("binary update format magic mismatch");
}
// TODO: some uint64 values may not be representable as Number.
Expand All @@ -81,12 +81,16 @@ export class PythLazerClient {
pos += UINT16_NUM_BYTES;
const magic = data
.subarray(pos, pos + UINT32_NUM_BYTES)
.readUint32BE();
if (magic == EVM_FORMAT_MAGIC) {
.readUint32LE();
if (magic == FORMAT_MAGICS_LE.EVM) {
value.evm = data.subarray(pos, pos + len);
} else if (magic == SOLANA_FORMAT_MAGIC_BE) {
} else if (magic == FORMAT_MAGICS_LE.SOLANA) {
value.solana = data.subarray(pos, pos + len);
} else if (magic == PARSED_FORMAT_MAGIC) {
} else if (magic == FORMAT_MAGICS_LE.LE_ECDSA) {
value.leEcdsa = data.subarray(pos, pos + len);
} else if (magic == FORMAT_MAGICS_LE.LE_UNSIGNED) {
value.leUnsigned = data.subarray(pos, pos + len);
} else if (magic == FORMAT_MAGICS_LE.JSON) {
value.parsed = JSON.parse(
data.subarray(pos + UINT32_NUM_BYTES, pos + len).toString()
) as ParsedPayload;
Expand Down
19 changes: 13 additions & 6 deletions lazer/sdk/js/src/protocol.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type Chain = "evm" | "solana";
export type Format = "evm" | "solana" | "leEcdsa" | "leUnsigned";
export type DeliveryFormat = "json" | "binary";
export type JsonBinaryEncoding = "base64" | "hex";
export type PriceFeedProperty =
Expand All @@ -16,7 +16,7 @@ export type Request =
subscriptionId: number;
priceFeedIds: number[];
properties: PriceFeedProperty[];
chains: Chain[];
formats: Format[];
deliveryFormat?: DeliveryFormat;
jsonBinaryEncoding?: JsonBinaryEncoding;
parsed?: boolean;
Expand Down Expand Up @@ -71,9 +71,16 @@ export type Response =
parsed?: ParsedPayload | undefined;
evm?: JsonBinaryData | undefined;
solana?: JsonBinaryData | undefined;
leEcdsa?: JsonBinaryData | undefined;
leUnsigned?: JsonBinaryData | undefined;
};

export const BINARY_UPDATE_FORMAT_MAGIC = 1_937_213_467;
export const PARSED_FORMAT_MAGIC = 2_584_795_844;
export const EVM_FORMAT_MAGIC = 706_910_618;
export const SOLANA_FORMAT_MAGIC_BE = 3_103_857_282;
export const BINARY_UPDATE_FORMAT_MAGIC_LE = 461_928_307;

export const FORMAT_MAGICS_LE = {
JSON: 3_302_625_434,
EVM: 2_593_727_018,
SOLANA: 2_182_742_457,
LE_ECDSA: 1_296_547_300,
LE_UNSIGNED: 1_499_680_012,
};
Loading