Skip to content

Commit

Permalink
feat(lazer): add new formats to js sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
Riateche committed Feb 27, 2025
1 parent 77c3b29 commit c202413
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
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
32 changes: 18 additions & 14 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,20 +15,22 @@ export type BinaryResponse = {
evm?: Buffer | undefined;
solana?: Buffer | undefined;
parsed?: ParsedPayload | undefined;
leEcdsa?: Buffer | undefined;
leUnsigned?: Buffer | undefined;
};
export type JsonOrBinaryResponse =
| {
type: "json";
value: Response;
}
type: "json";
value: Response;
}
| { type: "binary"; value: BinaryResponse };

const UINT16_NUM_BYTES = 2;
const UINT32_NUM_BYTES = 4;
const UINT64_NUM_BYTES = 8;

export class PythLazerClient {
private constructor(private readonly wsp: WebSocketPool) {}
private constructor(private readonly wsp: WebSocketPool) { }

Check failure on line 33 in lazer/sdk/js/src/client.ts

View workflow job for this annotation

GitHub Actions / test

Do not add spaces between braces

/**
* Creates a new PythLazerClient instance.
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,
};

0 comments on commit c202413

Please sign in to comment.