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

Commit 5388085

Browse files
committed
refactor
1 parent e32f44a commit 5388085

File tree

4 files changed

+52
-22
lines changed

4 files changed

+52
-22
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"test": "jest",
1414
"build": "tsc",
1515
"format": "prettier --write \"src/**/*.ts\"",
16-
"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\"",
16+
"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\"",
1717
"lint": "eslint src/",
1818
"prepare": "npm run build",
1919
"prepublishOnly": "npm test && npm run lint",

src/__tests__/PriceFeed.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ test("getMetadata returns PriceFeedMetadata as expected", () => {
107107
expo: 4,
108108
id: "abcdef0123456789",
109109
max_num_publishers: 6,
110-
metadata: new PriceFeedMetadata({
110+
metadata: {
111111
attestation_time: 7,
112112
emitter_chain: 8,
113113
sequence_number: 9,
114-
}),
114+
},
115115
num_publishers: 10,
116116
prev_conf: "11",
117117
prev_price: "12",
@@ -125,7 +125,7 @@ test("getMetadata returns PriceFeedMetadata as expected", () => {
125125
const priceFeed = PriceFeed.fromJson(data);
126126

127127
expect(priceFeed.getMetadata()).toStrictEqual(
128-
new PriceFeedMetadata({
128+
PriceFeedMetadata.fromJson({
129129
attestation_time: 7,
130130
emitter_chain: 8,
131131
sequence_number: 9,

src/index.ts

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { Convert, PriceFeed as JsonPriceFeed } from "./schemas/PriceFeed";
1+
import {
2+
Convert,
3+
PriceFeed as JsonPriceFeed,
4+
PriceFeedMetadata as JsonPriceFeedMetadata,
5+
} from "./schemas/PriceFeed";
26

37
export type UnixTimestamp = number;
48
export type DurationInSeconds = number;
@@ -62,24 +66,46 @@ export class PriceFeedMetadata {
6266
/**
6367
* Attestation time of the price
6468
*/
65-
attestation_time: number;
69+
attestationTime: number;
6670
/**
6771
* Chain of the emitter
6872
*/
69-
emitter_chain: number;
73+
emitterChain: number;
7074
/**
7175
* Sequence number of the price
7276
*/
73-
sequence_number: number;
77+
sequenceNumber: number;
7478

7579
constructor(metadata: {
76-
attestation_time: number;
77-
emitter_chain: number;
78-
sequence_number: number;
80+
attestationTime: number;
81+
emitterChain: number;
82+
sequenceNumber: number;
7983
}) {
80-
this.attestation_time = metadata.attestation_time;
81-
this.emitter_chain = metadata.emitter_chain;
82-
this.sequence_number = metadata.sequence_number;
84+
this.attestationTime = metadata.attestationTime;
85+
this.emitterChain = metadata.emitterChain;
86+
this.sequenceNumber = metadata.sequenceNumber;
87+
}
88+
89+
static fromJson(json: any): PriceFeedMetadata | undefined {
90+
if (json === undefined) {
91+
return undefined;
92+
}
93+
const jsonFeed: JsonPriceFeedMetadata = Convert.toPriceFeedMetadata(json);
94+
return new PriceFeedMetadata({
95+
attestationTime: jsonFeed.attestation_time,
96+
emitterChain: jsonFeed.emitter_chain,
97+
sequenceNumber: jsonFeed.sequence_number,
98+
});
99+
}
100+
101+
toJson(): any {
102+
const jsonFeed: JsonPriceFeedMetadata = {
103+
attestation_time: this.attestationTime,
104+
emitter_chain: this.emitterChain,
105+
sequence_number: this.sequenceNumber,
106+
};
107+
// this is done to avoid sending undefined values to the server
108+
return Convert.priceFeedMetadataToJson(jsonFeed);
83109
}
84110
}
85111

@@ -194,7 +220,7 @@ export class PriceFeed {
194220
expo: jsonFeed.expo,
195221
id: jsonFeed.id,
196222
maxNumPublishers: jsonFeed.max_num_publishers,
197-
metadata: jsonFeed.metadata,
223+
metadata: PriceFeedMetadata.fromJson(jsonFeed.metadata),
198224
numPublishers: jsonFeed.num_publishers,
199225
prevConf: jsonFeed.prev_conf,
200226
prevPrice: jsonFeed.prev_price,
@@ -214,7 +240,7 @@ export class PriceFeed {
214240
expo: this.expo,
215241
id: this.id,
216242
max_num_publishers: this.maxNumPublishers,
217-
metadata: this.metadata,
243+
metadata: this.metadata?.toJson(),
218244
num_publishers: this.numPublishers,
219245
prev_conf: this.prevConf,
220246
prev_price: this.prevPrice,
@@ -224,7 +250,6 @@ export class PriceFeed {
224250
publish_time: this.publishTime,
225251
status: this.status,
226252
};
227-
// this is done to avoid sending undefined values to the server
228253
return Convert.priceFeedToJson(jsonFeed);
229254
}
230255

@@ -318,13 +343,10 @@ export class PriceFeed {
318343
/**
319344
* Get the price feed metadata.
320345
*
321-
* @returns a struct containing the attestation time, emitter chain, and the sequence number.
346+
* @returns a struct containing the attestation time, emitter chain, and the sequence number.
322347
* Returns `undefined` if metadata is currently unavailable.
323348
*/
324349
getMetadata(): PriceFeedMetadata | undefined {
325-
if (this.metadata === undefined) {
326-
return undefined;
327-
}
328-
return new PriceFeedMetadata(this.metadata);
350+
return this.metadata;
329351
}
330352
}

src/schemas/PriceFeed.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ export class Convert {
115115
public static priceFeedToJson(value: PriceFeed): any {
116116
return uncast(value, r("PriceFeed"));
117117
}
118+
119+
public static toPriceFeedMetadata(json: any): PriceFeedMetadata {
120+
return cast(json, r("PriceFeedMetadata"));
121+
}
122+
123+
public static priceFeedMetadataToJson(value: PriceFeedMetadata): any {
124+
return uncast(value, r("PriceFeedMetadata"));
125+
}
118126
}
119127

120128
function invalidValue(typ: any, val: any, key: any = ""): never {

0 commit comments

Comments
 (0)