From 5388085bd7010c99bb4232abf1ae6fcab15fe616 Mon Sep 17 00:00:00 2001 From: Daniel Chew Date: Tue, 6 Sep 2022 19:05:46 +0800 Subject: [PATCH] refactor --- package.json | 2 +- src/__tests__/PriceFeed.test.ts | 6 ++-- src/index.ts | 58 +++++++++++++++++++++++---------- src/schemas/PriceFeed.ts | 8 +++++ 4 files changed, 52 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index dce4508..6d582f7 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "test": "jest", "build": "tsc", "format": "prettier --write \"src/**/*.ts\"", - "gen-ts-schema": "quicktype --src-lang schema src/schemas/price_feed.json -o src/schemas/PriceFeed.ts --raw-type any && prettier --write \"src/schemas/*.ts\"", + "gen-ts-schema": "quicktype --src-lang schema src/schemas/price_feed.json -o src/schemas/PriceFeed.ts --raw-type any --converters all-objects && prettier --write \"src/schemas/*.ts\"", "lint": "eslint src/", "prepare": "npm run build", "prepublishOnly": "npm test && npm run lint", diff --git a/src/__tests__/PriceFeed.test.ts b/src/__tests__/PriceFeed.test.ts index 536412a..b599e66 100644 --- a/src/__tests__/PriceFeed.test.ts +++ b/src/__tests__/PriceFeed.test.ts @@ -107,11 +107,11 @@ test("getMetadata returns PriceFeedMetadata as expected", () => { expo: 4, id: "abcdef0123456789", max_num_publishers: 6, - metadata: new PriceFeedMetadata({ + metadata: { attestation_time: 7, emitter_chain: 8, sequence_number: 9, - }), + }, num_publishers: 10, prev_conf: "11", prev_price: "12", @@ -125,7 +125,7 @@ test("getMetadata returns PriceFeedMetadata as expected", () => { const priceFeed = PriceFeed.fromJson(data); expect(priceFeed.getMetadata()).toStrictEqual( - new PriceFeedMetadata({ + PriceFeedMetadata.fromJson({ attestation_time: 7, emitter_chain: 8, sequence_number: 9, diff --git a/src/index.ts b/src/index.ts index ecf0334..d549b92 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,8 @@ -import { Convert, PriceFeed as JsonPriceFeed } from "./schemas/PriceFeed"; +import { + Convert, + PriceFeed as JsonPriceFeed, + PriceFeedMetadata as JsonPriceFeedMetadata, +} from "./schemas/PriceFeed"; export type UnixTimestamp = number; export type DurationInSeconds = number; @@ -62,24 +66,46 @@ export class PriceFeedMetadata { /** * Attestation time of the price */ - attestation_time: number; + attestationTime: number; /** * Chain of the emitter */ - emitter_chain: number; + emitterChain: number; /** * Sequence number of the price */ - sequence_number: number; + sequenceNumber: number; constructor(metadata: { - attestation_time: number; - emitter_chain: number; - sequence_number: number; + attestationTime: number; + emitterChain: number; + sequenceNumber: number; }) { - this.attestation_time = metadata.attestation_time; - this.emitter_chain = metadata.emitter_chain; - this.sequence_number = metadata.sequence_number; + this.attestationTime = metadata.attestationTime; + this.emitterChain = metadata.emitterChain; + this.sequenceNumber = metadata.sequenceNumber; + } + + static fromJson(json: any): PriceFeedMetadata | undefined { + if (json === undefined) { + return undefined; + } + const jsonFeed: JsonPriceFeedMetadata = Convert.toPriceFeedMetadata(json); + return new PriceFeedMetadata({ + attestationTime: jsonFeed.attestation_time, + emitterChain: jsonFeed.emitter_chain, + sequenceNumber: jsonFeed.sequence_number, + }); + } + + toJson(): any { + const jsonFeed: JsonPriceFeedMetadata = { + attestation_time: this.attestationTime, + emitter_chain: this.emitterChain, + sequence_number: this.sequenceNumber, + }; + // this is done to avoid sending undefined values to the server + return Convert.priceFeedMetadataToJson(jsonFeed); } } @@ -194,7 +220,7 @@ export class PriceFeed { expo: jsonFeed.expo, id: jsonFeed.id, maxNumPublishers: jsonFeed.max_num_publishers, - metadata: jsonFeed.metadata, + metadata: PriceFeedMetadata.fromJson(jsonFeed.metadata), numPublishers: jsonFeed.num_publishers, prevConf: jsonFeed.prev_conf, prevPrice: jsonFeed.prev_price, @@ -214,7 +240,7 @@ export class PriceFeed { expo: this.expo, id: this.id, max_num_publishers: this.maxNumPublishers, - metadata: this.metadata, + metadata: this.metadata?.toJson(), num_publishers: this.numPublishers, prev_conf: this.prevConf, prev_price: this.prevPrice, @@ -224,7 +250,6 @@ export class PriceFeed { publish_time: this.publishTime, status: this.status, }; - // this is done to avoid sending undefined values to the server return Convert.priceFeedToJson(jsonFeed); } @@ -318,13 +343,10 @@ export class PriceFeed { /** * Get the price feed metadata. * - * @returns a struct containing the attestation time, emitter chain, and the sequence number. + * @returns a struct containing the attestation time, emitter chain, and the sequence number. * Returns `undefined` if metadata is currently unavailable. */ getMetadata(): PriceFeedMetadata | undefined { - if (this.metadata === undefined) { - return undefined; - } - return new PriceFeedMetadata(this.metadata); + return this.metadata; } } diff --git a/src/schemas/PriceFeed.ts b/src/schemas/PriceFeed.ts index 32085cc..40b61e2 100644 --- a/src/schemas/PriceFeed.ts +++ b/src/schemas/PriceFeed.ts @@ -115,6 +115,14 @@ export class Convert { public static priceFeedToJson(value: PriceFeed): any { return uncast(value, r("PriceFeed")); } + + public static toPriceFeedMetadata(json: any): PriceFeedMetadata { + return cast(json, r("PriceFeedMetadata")); + } + + public static priceFeedMetadataToJson(value: PriceFeedMetadata): any { + return uncast(value, r("PriceFeedMetadata")); + } } function invalidValue(typ: any, val: any, key: any = ""): never {