forked from ERrijul8/redstone-cache-layer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.ts
60 lines (48 loc) · 1.53 KB
/
utils.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { getProviders } from "./providers";
import { PriceWithParams } from "./routes/prices";
export const priceParamsToPriceObj = (price: PriceWithParams) => {
const priceObj = {
...price,
minutes: getMinutesFromTimestamp(price.timestamp),
};
for (const signatureProp of ["signature", "evmSignature", "liteEvmSignature"]) {
if (price[signatureProp]) {
priceObj[signatureProp] = Buffer.from(price[signatureProp], "base64");
}
}
return priceObj;
}
export const getMinutesFromTimestamp = (timestamp: number) => {
return new Date(timestamp).getMinutes();
}
export const getProviderFromParams = async (params: { provider: string; }) => {
// TODO: load this mapping directly from arweave
// TODO: we also can implement caching
const providers = getProviders();
if (providers[params.provider]) {
return providers[params.provider];
} else {
return {
address: params.provider,
publicKey: ""
};
// TODO: [email protected] disabled error throwing here
// to allow external providers use redstone-api
// throw new Error(
// `Provider details not found for: "${params.provider}". `
// + `Params: ${JSON.stringify(params)}`);
}
}
export const formatDate = (date: number) => {
const d = new Date(Number(date));
const year = String(d.getFullYear());
let month = String((d.getMonth() + 1));
let day = String(d.getDate());
if (month.length < 2) {
month = '0' + month;
}
if (day.length < 2) {
day = '0' + day;
}
return `${year}-${month}-${day}`;
}