Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add v2 endpoints to js client #1316

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
44 changes: 43 additions & 1 deletion price_service/client/js/src/PriceServiceConnection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HexString, PriceFeed } from "@pythnetwork/price-service-sdk";
import { HexString, PriceFeed, PriceUpdate, PriceUpdateConvert } from "@pythnetwork/price-service-sdk";
import axios, { AxiosInstance } from "axios";
import axiosRetry from "axios-retry";
import * as WebSocket from "isomorphic-ws";
Expand Down Expand Up @@ -236,6 +236,48 @@ export class PriceServiceConnection {
return response.data;
}

/**
* Fetch the latest PriceUpdates of the given price ids
* This will throw an axios error if there is a network problem or the price service returns a non-ok response (e.g: Invalid price ids)
*
* @param priceIds Hex-encoded price ids.
* @returns PriceUpdate
*/
async getLatestPriceUpdates(
priceIds: HexString[],
): Promise<PriceUpdate> {
const response = await this.httpClient.get("/v2/updates/price/latest", {
params: {
ids: priceIds,
},
});

return PriceUpdateConvert.toPriceUpdate(response.data)
}

/**
* Fetch the PriceUpdates of the given price ids that is published since the given publish time.
* This will throw an error if the given publish time is in the future, or if the publish time
* is old and the price service endpoint does not have a db backend for historical requests.
* This will throw an axios error if there is a network problem or the price service returns a non-ok response (e.g: Invalid price id)
*
* @param priceIds Hex-encoded price ids.
* @param publishTime Epoch timestamp in seconds.
* @returns PriceUpdate
*/
async getPriceUpdates(
priceIds: HexString[],
publishTime: EpochTimeStamp
): Promise<PriceUpdate> {
const response = await this.httpClient.get(`/v2/updates/price/${publishTime}`, {
params: {
ids: priceIds,
},
});

return PriceUpdateConvert.toPriceUpdate(response.data)
}

/**
* Subscribe to updates for given price ids.
*
Expand Down
39 changes: 39 additions & 0 deletions price_service/client/js/src/__tests__/connection.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,45 @@ describe("Test http endpoints", () => {
expect(vaa.length).toBeGreaterThan(0);
expect(vaaPublishTime).toBeGreaterThanOrEqual(publishTime10SecAgo);
});

test("Get latest price updates works", async () => {
const connection = new PriceServiceConnection(PRICE_SERVICE_ENDPOINT, {
priceFeedRequestConfig: { binary: true },
});

const ids = await connection.getPriceFeedIds();
expect(ids.length).toBeGreaterThan(0);

const update = await connection.getLatestPriceUpdates(
ids.slice(0, 10),
);

expect(update.binary.data.length).toBeGreaterThan(0);
expect(update.parsed).toBeDefined();
expect(update.parsed!.length).toBeGreaterThanOrEqual(0);
});

test("Get price updates works", async () => {
const connection = new PriceServiceConnection(PRICE_SERVICE_ENDPOINT, {
priceFeedRequestConfig: { binary: true },
});

const ids = await connection.getPriceFeedIds();
expect(ids.length).toBeGreaterThan(0);

const publishTime10SecAgo = Math.floor(new Date().getTime() / 1000) - 10;
const update = await connection.getPriceUpdates(
ids.slice(0, 10),
publishTime10SecAgo
);

expect(update.binary.data.length).toBeGreaterThan(0);
expect(update.parsed).toBeDefined();
expect(update.parsed!.length).toBeGreaterThanOrEqual(0);
for (const parsedUpdate of update.parsed!) {
expect(parsedUpdate.price.publish_time).toBeGreaterThanOrEqual(publishTime10SecAgo)
}
});
});

describe("Test websocket endpoints", () => {
Expand Down
5 changes: 5 additions & 0 deletions price_service/sdk/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import {
PriceFeedMetadata as JsonPriceFeedMetadata,
} from "./schemas/PriceFeed";

export {
PriceUpdate,
Convert as PriceUpdateConvert
} from './schemas/PriceUpdate';

export type UnixTimestamp = number;
export type DurationInSeconds = number;
export type HexString = string;
Expand Down
Loading