Skip to content
This repository was archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
Update PriceFeed schema and methods + v1 release (#8)
Browse files Browse the repository at this point in the history
* Update PriceFeed schema and methods + v1 release

* Address comments + make schema simpler
  • Loading branch information
ali-bahjati authored Oct 4, 2022
1 parent 7732c43 commit c771175
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 423 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pythnetwork/pyth-sdk-js",
"version": "0.3.0",
"version": "1.0.0",
"description": "Pyth Network SDK in JS",
"homepage": "https://pyth.network",
"main": "lib/index.js",
Expand Down
133 changes: 41 additions & 92 deletions src/__tests__/PriceFeed.test.ts
Original file line number Diff line number Diff line change
@@ -1,125 +1,74 @@
import { Price, PriceFeed, PriceFeedMetadata, PriceStatus } from "../index";
import { Price, PriceFeed, PriceFeedMetadata } from "../index";

beforeAll(() => {
jest.useFakeTimers();
});

test("Parsing Price Feed works as expected", () => {
const data = {
conf: "1",
ema_conf: "2",
ema_price: "3",
expo: 4,
ema_price: {
conf: "2",
expo: 4,
price: "3",
publish_time: 11,
},
id: "abcdef0123456789",
max_num_publishers: 6,
num_publishers: 5,
prev_conf: "7",
prev_price: "8",
prev_publish_time: 9,
price: "10",
product_id: "0123456789abcdef",
publish_time: 11,
status: PriceStatus.Trading,
price: {
conf: "1",
expo: 4,
price: "10",
publish_time: 11,
},
};

const priceFeed = PriceFeed.fromJson(data);
expect(priceFeed.status).toBe(PriceStatus.Trading);
expect(priceFeed.expo).toBe(4);
expect(priceFeed.id).toBe("abcdef0123456789");
expect(priceFeed.maxNumPublishers).toBe(6);
expect(priceFeed.numPublishers).toBe(5);
expect(priceFeed.productId).toBe("0123456789abcdef");
expect(priceFeed.publishTime).toBe(11);
expect(priceFeed.getCurrentPrice()).toStrictEqual(new Price("1", 4, "10"));
expect(priceFeed.getEmaPrice()).toStrictEqual(new Price("2", 4, "3"));
expect(priceFeed.getLatestAvailablePriceUnchecked()).toStrictEqual([
new Price("1", 4, "10"),
11,
]);

jest.setSystemTime(20000);
expect(priceFeed.getLatestAvailablePriceWithinDuration(15)).toStrictEqual(
new Price("1", 4, "10")
);
expect(priceFeed.getLatestAvailablePriceWithinDuration(5)).toBeUndefined();
expect(priceFeed.toJson()).toEqual(data);
});

test("getCurrentPrice returns undefined if status is not Trading", () => {
const data = {
const expectedPrice = new Price({
conf: "1",
ema_conf: "2",
ema_price: "3",
expo: 4,
id: "abcdef0123456789",
max_num_publishers: 6,
num_publishers: 5,
prev_conf: "7",
prev_price: "8",
prev_publish_time: 9,
price: "10",
product_id: "0123456789abcdef",
publish_time: 11,
status: PriceStatus.Unknown,
};

const priceFeed = PriceFeed.fromJson(data);
expect(priceFeed.getCurrentPrice()).toBeUndefined();
});
publishTime: 11,
});
expect(priceFeed.getPriceUnchecked()).toStrictEqual(expectedPrice);

test("getLatestAvailablePrice returns prevPrice when status is not Trading", () => {
const data = {
conf: "1",
ema_conf: "2",
ema_price: "3",
const expectedEmaPrice = new Price({
conf: "2",
expo: 4,
id: "abcdef0123456789",
max_num_publishers: 6,
num_publishers: 5,
prev_conf: "7",
prev_price: "8",
prev_publish_time: 9,
price: "10",
product_id: "0123456789abcdef",
publish_time: 11,
status: PriceStatus.Unknown,
};

const priceFeed = PriceFeed.fromJson(data);

expect(priceFeed.getLatestAvailablePriceUnchecked()).toStrictEqual([
new Price("7", 4, "8"),
9,
]);
price: "3",
publishTime: 11,
});
expect(priceFeed.getEmaPriceUnchecked()).toStrictEqual(expectedEmaPrice);

jest.setSystemTime(20000);
expect(priceFeed.getLatestAvailablePriceWithinDuration(15)).toStrictEqual(
new Price("7", 4, "8")
);
expect(priceFeed.getLatestAvailablePriceWithinDuration(10)).toBeUndefined();
expect(priceFeed.getPriceNoOlderThan(15)).toStrictEqual(expectedPrice);
expect(priceFeed.getPriceNoOlderThan(5)).toBeUndefined();
expect(priceFeed.getEmaPriceNoOlderThan(15)).toStrictEqual(expectedEmaPrice);
expect(priceFeed.getEmaPriceNoOlderThan(5)).toBeUndefined();

expect(priceFeed.toJson()).toEqual(data);
});

test("getMetadata returns PriceFeedMetadata as expected", () => {
const data = {
conf: "1",
ema_conf: "2",
ema_price: "3",
expo: 4,
ema_price: {
conf: "2",
expo: 4,
price: "3",
publish_time: 11,
},
id: "abcdef0123456789",
max_num_publishers: 6,
price: {
conf: "1",
expo: 4,
price: "10",
publish_time: 11,
},
metadata: {
attestation_time: 7,
emitter_chain: 8,
sequence_number: 9,
},
num_publishers: 10,
prev_conf: "11",
prev_price: "12",
prev_publish_time: 13,
price: "14",
product_id: "0123456789abcdef",
publish_time: 16,
status: PriceStatus.Unknown,
};

const priceFeed = PriceFeed.fromJson(data);
Expand Down
Loading

0 comments on commit c771175

Please sign in to comment.