|
@@ -1,4 +1,4 @@
|
|
|
-import { HexString, PriceFeed, PriceStatus } from "@pythnetwork/pyth-sdk-js";
|
|
|
|
|
|
|
+import { HexString, PriceFeed } from "@pythnetwork/pyth-sdk-js";
|
|
|
import { Server } from "http";
|
|
import { Server } from "http";
|
|
|
import { WebSocket, WebSocketServer } from "ws";
|
|
import { WebSocket, WebSocketServer } from "ws";
|
|
|
import { sleep } from "../helpers";
|
|
import { sleep } from "../helpers";
|
|
@@ -33,12 +33,12 @@ function dummyPriceMetadata(
|
|
|
function dummyPriceInfo(
|
|
function dummyPriceInfo(
|
|
|
id: HexString,
|
|
id: HexString,
|
|
|
vaa: HexString,
|
|
vaa: HexString,
|
|
|
- priceMetadata: any
|
|
|
|
|
|
|
+ dummyPriceMetadataValue: any
|
|
|
): PriceInfo {
|
|
): PriceInfo {
|
|
|
return {
|
|
return {
|
|
|
- seqNum: priceMetadata.sequence_number,
|
|
|
|
|
- attestationTime: priceMetadata.attestation_time,
|
|
|
|
|
- emitterChainId: priceMetadata.emitter_chain,
|
|
|
|
|
|
|
+ seqNum: dummyPriceMetadataValue.sequence_number,
|
|
|
|
|
+ attestationTime: dummyPriceMetadataValue.attestation_time,
|
|
|
|
|
+ emitterChainId: dummyPriceMetadataValue.emitter_chain,
|
|
|
priceFeed: dummyPriceFeed(id),
|
|
priceFeed: dummyPriceFeed(id),
|
|
|
vaaBytes: Buffer.from(vaa, "hex").toString("binary"),
|
|
vaaBytes: Buffer.from(vaa, "hex").toString("binary"),
|
|
|
};
|
|
};
|
|
@@ -46,20 +46,19 @@ function dummyPriceInfo(
|
|
|
|
|
|
|
|
function dummyPriceFeed(id: string): PriceFeed {
|
|
function dummyPriceFeed(id: string): PriceFeed {
|
|
|
return PriceFeed.fromJson({
|
|
return PriceFeed.fromJson({
|
|
|
- conf: "0",
|
|
|
|
|
- ema_conf: "1",
|
|
|
|
|
- ema_price: "2",
|
|
|
|
|
- expo: 3,
|
|
|
|
|
|
|
+ ema_price: {
|
|
|
|
|
+ conf: "1",
|
|
|
|
|
+ expo: 2,
|
|
|
|
|
+ price: "3",
|
|
|
|
|
+ publish_time: 4,
|
|
|
|
|
+ },
|
|
|
id,
|
|
id,
|
|
|
- max_num_publishers: 5,
|
|
|
|
|
- num_publishers: 6,
|
|
|
|
|
- prev_conf: "7",
|
|
|
|
|
- prev_price: "8",
|
|
|
|
|
- prev_publish_time: 9,
|
|
|
|
|
- price: "10",
|
|
|
|
|
- product_id: "def456",
|
|
|
|
|
- publish_time: 12,
|
|
|
|
|
- status: PriceStatus.Trading,
|
|
|
|
|
|
|
+ price: {
|
|
|
|
|
+ conf: "5",
|
|
|
|
|
+ expo: 6,
|
|
|
|
|
+ price: "7",
|
|
|
|
|
+ publish_time: 8,
|
|
|
|
|
+ },
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -101,11 +100,10 @@ beforeAll(async () => {
|
|
|
dummyPriceInfo(expandTo64Len("6789"), "bidbidbid", priceMetadata),
|
|
dummyPriceInfo(expandTo64Len("6789"), "bidbidbid", priceMetadata),
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
- let priceInfo: PriceStore = {
|
|
|
|
|
|
|
+ const priceInfo: PriceStore = {
|
|
|
getLatestPriceInfo: (_priceFeedId: string) => undefined,
|
|
getLatestPriceInfo: (_priceFeedId: string) => undefined,
|
|
|
addUpdateListener: (_callback: (priceInfo: PriceInfo) => any) => undefined,
|
|
addUpdateListener: (_callback: (priceInfo: PriceInfo) => any) => undefined,
|
|
|
- getPriceIds: () =>
|
|
|
|
|
- new Set(priceInfos.map((priceInfo) => priceInfo.priceFeed.id)),
|
|
|
|
|
|
|
+ getPriceIds: () => new Set(priceInfos.map((info) => info.priceFeed.id)),
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
api = new WebSocketAPI(priceInfo);
|
|
api = new WebSocketAPI(priceInfo);
|
|
@@ -123,9 +121,9 @@ afterAll(async () => {
|
|
|
|
|
|
|
|
describe("Client receives data", () => {
|
|
describe("Client receives data", () => {
|
|
|
test("When subscribes with valid ids without verbose flag, returns correct price feed", async () => {
|
|
test("When subscribes with valid ids without verbose flag, returns correct price feed", async () => {
|
|
|
- let [client, serverMessages] = await createSocketClient();
|
|
|
|
|
|
|
+ const [client, serverMessages] = await createSocketClient();
|
|
|
|
|
|
|
|
- let message: ClientMessage = {
|
|
|
|
|
|
|
+ const message: ClientMessage = {
|
|
|
ids: [priceInfos[0].priceFeed.id, priceInfos[1].priceFeed.id],
|
|
ids: [priceInfos[0].priceFeed.id, priceInfos[1].priceFeed.id],
|
|
|
type: "subscribe",
|
|
type: "subscribe",
|
|
|
};
|
|
};
|
|
@@ -162,9 +160,9 @@ describe("Client receives data", () => {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
test("When subscribes with valid ids and verbose flag set to true, returns correct price feed with metadata", async () => {
|
|
test("When subscribes with valid ids and verbose flag set to true, returns correct price feed with metadata", async () => {
|
|
|
- let [client, serverMessages] = await createSocketClient();
|
|
|
|
|
|
|
+ const [client, serverMessages] = await createSocketClient();
|
|
|
|
|
|
|
|
- let message: ClientMessage = {
|
|
|
|
|
|
|
+ const message: ClientMessage = {
|
|
|
ids: [priceInfos[0].priceFeed.id, priceInfos[1].priceFeed.id],
|
|
ids: [priceInfos[0].priceFeed.id, priceInfos[1].priceFeed.id],
|
|
|
type: "subscribe",
|
|
type: "subscribe",
|
|
|
verbose: true,
|
|
verbose: true,
|
|
@@ -208,9 +206,9 @@ describe("Client receives data", () => {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
test("When subscribes with valid ids and verbose flag set to false, returns correct price feed without metadata", async () => {
|
|
test("When subscribes with valid ids and verbose flag set to false, returns correct price feed without metadata", async () => {
|
|
|
- let [client, serverMessages] = await createSocketClient();
|
|
|
|
|
|
|
+ const [client, serverMessages] = await createSocketClient();
|
|
|
|
|
|
|
|
- let message: ClientMessage = {
|
|
|
|
|
|
|
+ const message: ClientMessage = {
|
|
|
ids: [priceInfos[0].priceFeed.id, priceInfos[1].priceFeed.id],
|
|
ids: [priceInfos[0].priceFeed.id, priceInfos[1].priceFeed.id],
|
|
|
type: "subscribe",
|
|
type: "subscribe",
|
|
|
verbose: false,
|
|
verbose: false,
|
|
@@ -248,9 +246,9 @@ describe("Client receives data", () => {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
test("When subscribes with invalid ids, returns error", async () => {
|
|
test("When subscribes with invalid ids, returns error", async () => {
|
|
|
- let [client, serverMessages] = await createSocketClient();
|
|
|
|
|
|
|
+ const [client, serverMessages] = await createSocketClient();
|
|
|
|
|
|
|
|
- let message: ClientMessage = {
|
|
|
|
|
|
|
+ const message: ClientMessage = {
|
|
|
ids: [expandTo64Len("aaaa")],
|
|
ids: [expandTo64Len("aaaa")],
|
|
|
type: "subscribe",
|
|
type: "subscribe",
|
|
|
};
|
|
};
|
|
@@ -268,9 +266,9 @@ describe("Client receives data", () => {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
test("When subscribes for Price Feed A, doesn't receive updates for Price Feed B", async () => {
|
|
test("When subscribes for Price Feed A, doesn't receive updates for Price Feed B", async () => {
|
|
|
- let [client, serverMessages] = await createSocketClient();
|
|
|
|
|
|
|
+ const [client, serverMessages] = await createSocketClient();
|
|
|
|
|
|
|
|
- let message: ClientMessage = {
|
|
|
|
|
|
|
+ const message: ClientMessage = {
|
|
|
ids: [priceInfos[0].priceFeed.id],
|
|
ids: [priceInfos[0].priceFeed.id],
|
|
|
type: "subscribe",
|
|
type: "subscribe",
|
|
|
};
|
|
};
|
|
@@ -305,7 +303,7 @@ describe("Client receives data", () => {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
test("When subscribes for Price Feed A, receives updated and when unsubscribes stops receiving", async () => {
|
|
test("When subscribes for Price Feed A, receives updated and when unsubscribes stops receiving", async () => {
|
|
|
- let [client, serverMessages] = await createSocketClient();
|
|
|
|
|
|
|
+ const [client, serverMessages] = await createSocketClient();
|
|
|
|
|
|
|
|
let message: ClientMessage = {
|
|
let message: ClientMessage = {
|
|
|
ids: [priceInfos[0].priceFeed.id],
|
|
ids: [priceInfos[0].priceFeed.id],
|
|
@@ -355,9 +353,9 @@ describe("Client receives data", () => {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
test("Unsubscribe on not subscribed price feed is ok", async () => {
|
|
test("Unsubscribe on not subscribed price feed is ok", async () => {
|
|
|
- let [client, serverMessages] = await createSocketClient();
|
|
|
|
|
|
|
+ const [client, serverMessages] = await createSocketClient();
|
|
|
|
|
|
|
|
- let message: ClientMessage = {
|
|
|
|
|
|
|
+ const message: ClientMessage = {
|
|
|
ids: [priceInfos[0].priceFeed.id],
|
|
ids: [priceInfos[0].priceFeed.id],
|
|
|
type: "unsubscribe",
|
|
type: "unsubscribe",
|
|
|
};
|
|
};
|
|
@@ -376,17 +374,17 @@ describe("Client receives data", () => {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
test("Multiple clients with different price feed works", async () => {
|
|
test("Multiple clients with different price feed works", async () => {
|
|
|
- let [client1, serverMessages1] = await createSocketClient();
|
|
|
|
|
- let [client2, serverMessages2] = await createSocketClient();
|
|
|
|
|
|
|
+ const [client1, serverMessages1] = await createSocketClient();
|
|
|
|
|
+ const [client2, serverMessages2] = await createSocketClient();
|
|
|
|
|
|
|
|
- let message1: ClientMessage = {
|
|
|
|
|
|
|
+ const message1: ClientMessage = {
|
|
|
ids: [priceInfos[0].priceFeed.id],
|
|
ids: [priceInfos[0].priceFeed.id],
|
|
|
type: "subscribe",
|
|
type: "subscribe",
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
client1.send(JSON.stringify(message1));
|
|
client1.send(JSON.stringify(message1));
|
|
|
|
|
|
|
|
- let message2: ClientMessage = {
|
|
|
|
|
|
|
+ const message2: ClientMessage = {
|
|
|
ids: [priceInfos[1].priceFeed.id],
|
|
ids: [priceInfos[1].priceFeed.id],
|
|
|
type: "subscribe",
|
|
type: "subscribe",
|
|
|
};
|
|
};
|