Skip to content

Commit 378e99b

Browse files
authored
add binary query string to /latest_price_feeds (#445)
* add binary query string to /latest_price_feeds * fix precommit * fix both rest and ws tests * bump package version * fix tests to use the latest schema
1 parent cdf99f4 commit 378e99b

File tree

5 files changed

+52
-26
lines changed

5 files changed

+52
-26
lines changed

third_party/pyth/price-service/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

third_party/pyth/price-service/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pythnetwork/pyth-price-service",
3-
"version": "2.2.3",
3+
"version": "2.2.4",
44
"description": "Pyth Price Service",
55
"main": "index.js",
66
"scripts": {

third_party/pyth/price-service/src/__tests__/rest.test.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { HexString, PriceFeed, Price } from "@pythnetwork/pyth-sdk-js";
2-
import { PriceStore, PriceInfo } from "../listen";
3-
import { RestAPI } from "../rest";
1+
import { HexString, Price, PriceFeed } from "@pythnetwork/pyth-sdk-js";
42
import { Express } from "express";
5-
import request from "supertest";
63
import { StatusCodes } from "http-status-codes";
4+
import request from "supertest";
5+
import { PriceInfo, PriceStore } from "../listen";
6+
import { RestAPI } from "../rest";
77

88
let app: Express;
99
let priceInfoMap: Map<string, PriceInfo>;
@@ -82,6 +82,23 @@ describe("Latest Price Feed Endpoint", () => {
8282
expect(resp.body).toContainEqual(dummyPriceFeed(ids[1]).toJson());
8383
});
8484

85+
test("When called with valid ids and binary flag set to true, returns correct price feed with binary vaa", async () => {
86+
const ids = [expandTo64Len("abcd"), expandTo64Len("3456")];
87+
const resp = await request(app)
88+
.get("/api/latest_price_feeds")
89+
.query({ ids, binary: true });
90+
expect(resp.status).toBe(StatusCodes.OK);
91+
expect(resp.body.length).toBe(2);
92+
expect(resp.body).toContainEqual({
93+
...priceInfoMap.get(ids[0])!.priceFeed.toJson(),
94+
vaa: priceInfoMap.get(ids[0])!.vaa.toString("base64"),
95+
});
96+
expect(resp.body).toContainEqual({
97+
...priceInfoMap.get(ids[1])!.priceFeed.toJson(),
98+
vaa: priceInfoMap.get(ids[1])!.vaa.toString("base64"),
99+
});
100+
});
101+
85102
test("When called with some non-existent ids within ids, returns error mentioning non-existent ids", async () => {
86103
const ids = [
87104
expandTo64Len("ab01"),

third_party/pyth/price-service/src/__tests__/ws.test.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HexString, PriceFeed } from "@pythnetwork/pyth-sdk-js";
1+
import { HexString, Price, PriceFeed } from "@pythnetwork/pyth-sdk-js";
22
import { Server } from "http";
33
import { WebSocket, WebSocketServer } from "ws";
44
import { sleep } from "../helpers";
@@ -49,20 +49,20 @@ function dummyPriceInfo(
4949
}
5050

5151
function dummyPriceFeed(id: string): PriceFeed {
52-
return PriceFeed.fromJson({
53-
ema_price: {
52+
return new PriceFeed({
53+
emaPrice: new Price({
5454
conf: "1",
5555
expo: 2,
5656
price: "3",
57-
publish_time: 4,
58-
},
57+
publishTime: 4,
58+
}),
5959
id,
60-
price: {
60+
price: new Price({
6161
conf: "5",
6262
expo: 6,
6363
price: "7",
64-
publish_time: 8,
65-
},
64+
publishTime: 8,
65+
}),
6666
});
6767
}
6868

@@ -273,7 +273,10 @@ describe("Client receives data", () => {
273273

274274
expect(serverMessages[1]).toEqual({
275275
type: "price_update",
276-
price_feed: priceInfos[0].priceFeed.toJson(),
276+
price_feed: {
277+
...priceInfos[0].priceFeed.toJson(),
278+
vaa: priceInfos[0].vaa.toString("base64"),
279+
},
277280
});
278281

279282
api.dispatchPriceFeedUpdate(priceInfos[1]);

third_party/pyth/price-service/src/rest.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1+
import { HexString } from "@pythnetwork/pyth-sdk-js";
12
import cors from "cors";
23
import express, { NextFunction, Request, Response } from "express";
34
import { Joi, schema, validate, ValidationError } from "express-validation";
45
import { Server } from "http";
56
import { StatusCodes } from "http-status-codes";
67
import morgan from "morgan";
7-
import responseTime from "response-time";
8-
import { DurationInMs, DurationInSec, TimestampInSec } from "./helpers";
8+
import { TimestampInSec } from "./helpers";
99
import { PriceStore } from "./listen";
1010
import { logger } from "./logging";
1111
import { PromClient } from "./promClient";
12-
import { HexString } from "@pythnetwork/pyth-sdk-js";
1312

1413
const MORGAN_LOG_FORMAT =
1514
':remote-addr - :remote-user ":method :url HTTP/:http-version"' +
@@ -120,6 +119,7 @@ export class RestAPI {
120119
.items(Joi.string().regex(/^(0x)?[a-f0-9]{64}$/))
121120
.required(),
122121
verbose: Joi.boolean(),
122+
binary: Joi.boolean(),
123123
}).required(),
124124
};
125125
app.get(
@@ -129,6 +129,8 @@ export class RestAPI {
129129
const priceIds = req.query.ids as string[];
130130
// verbose is optional, default to false
131131
const verbose = req.query.verbose === "true";
132+
// binary is optional, default to false
133+
const binary = req.query.binary === "true";
132134

133135
const responseJson = [];
134136

@@ -146,20 +148,21 @@ export class RestAPI {
146148
continue;
147149
}
148150

149-
if (verbose) {
150-
responseJson.push({
151-
...latestPriceInfo.priceFeed.toJson(),
151+
responseJson.push({
152+
...latestPriceInfo.priceFeed.toJson(),
153+
...(verbose && {
152154
metadata: {
153155
emitter_chain: latestPriceInfo.emitterChainId,
154156
attestation_time: latestPriceInfo.attestationTime,
155157
sequence_number: latestPriceInfo.seqNum,
156158
price_service_receive_time:
157159
latestPriceInfo.priceServiceReceiveTime,
158160
},
159-
});
160-
} else {
161-
responseJson.push(latestPriceInfo.priceFeed.toJson());
162-
}
161+
}),
162+
...(binary && {
163+
vaa: latestPriceInfo.vaa.toString("base64"),
164+
}),
165+
});
163166
}
164167

165168
if (notFoundIds.length > 0) {
@@ -175,6 +178,9 @@ export class RestAPI {
175178
endpoints.push(
176179
"api/latest_price_feeds?ids[]=<price_feed_id>&ids[]=<price_feed_id_2>&..&verbose=true"
177180
);
181+
endpoints.push(
182+
"api/latest_price_feeds?ids[]=<price_feed_id>&ids[]=<price_feed_id_2>&..&verbose=true&binary=true"
183+
);
178184

179185
app.get("/api/price_feed_ids", (req: Request, res: Response) => {
180186
const availableIds = this.priceFeedVaaInfo.getPriceIds();

0 commit comments

Comments
 (0)