-
Notifications
You must be signed in to change notification settings - Fork 14
/
rateModel.ts
40 lines (35 loc) · 1.29 KB
/
rateModel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { RateModel } from "../lpFeeCalculator";
const expectedRateModelKeys = ["UBar", "R0", "R1", "R2"];
/**
* Helper method that returns parsed rate model from string, or throws.
* @param rateModelString Stringified rate model to parse.
* @returns Rate model object. Must conform to `expectedRateModelKeys` format.
*/
export const parseAndReturnRateModelFromString = (rateModelString: string): RateModel => {
const rateModelFromEvent = JSON.parse(rateModelString);
// Rate model must contain the exact same keys in `expectedRateModelKeys`.
for (const key of expectedRateModelKeys) {
if (!Object.keys(rateModelFromEvent).includes(key)) {
throw new Error(
`Rate model does not contain all expected keys. Expected keys: [${expectedRateModelKeys}], actual keys: [${Object.keys(
rateModelFromEvent
)}]`
);
}
}
for (const key of Object.keys(rateModelFromEvent)) {
if (!expectedRateModelKeys.includes(key)) {
throw new Error(
`Rate model contains unexpected keys. Expected keys: [${expectedRateModelKeys}], actual keys: [${Object.keys(
rateModelFromEvent
)}]`
);
}
}
return {
UBar: rateModelFromEvent.UBar,
R0: rateModelFromEvent.R0,
R1: rateModelFromEvent.R1,
R2: rateModelFromEvent.R2,
};
};