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

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
cctdaniel committed Sep 6, 2022
1 parent e32f44a commit 5388085
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/PriceFeed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
Expand Down
58 changes: 40 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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);
}

Expand Down Expand Up @@ -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;
}
}
8 changes: 8 additions & 0 deletions src/schemas/PriceFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 5388085

Please sign in to comment.