-
Notifications
You must be signed in to change notification settings - Fork 1
/
blocks.tsx
72 lines (62 loc) · 1.89 KB
/
blocks.tsx
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
61
62
63
64
65
66
67
68
69
70
71
72
import config from './../../../../config/config/environment';
export type ID = string;
export type Level = string;
// should be used as ISO string
export type Timestamp = string;
export interface Block {
id: ID;
level: Level;
timestamp: Timestamp;
}
export const GET_BLOCKS_URL = `${config.API_URL}/v1/blocks`;
export interface LoadBlockAtIdentifierResponse {
level: string;
timestamp: string;
}
// universal identifier for a block used by the api
// could be Timestamp or Level in our case
export type BlockIdentifier = Timestamp | Level;
/**
*
* @param identifier
* @returns
*/
export const fetchBlockAtIdentifier = async (
identifier: BlockIdentifier
): Promise<Block> => {
const response = await fetch(GET_BLOCKS_URL + `/${identifier}`);
if (!response.ok) throw new Error(await response.text());
const body =
await (response.json() as Promise<LoadBlockAtIdentifierResponse>);
const block: Block = {
id: body.level,
level: body.level,
// convert ISO datetime to UNIX timestamp
timestamp: new Date(body.timestamp).getTime().toString(),
};
return block;
};
export const GET_LATEST_BLOCK_COUNT_URL = GET_BLOCKS_URL + `/count`;
/**
* Fetches the current block count (NOT LEVEL, level is count - 1)
* @returns Promise<string>
*/
export const fetchBlockCount = async (): Promise<string> => {
// load the block count
const response = await fetch(GET_LATEST_BLOCK_COUNT_URL);
if (!response.ok) throw new Error(await response.text());
const body = await response.text();
const count = body.toString();
return count;
};
/**
* Loads the latest available block from the API
* @returns Promise<Block>
*/
export const fetchLatestBlock = async (): Promise<Block> => {
const count = await fetchBlockCount();
const level = `${Number(count) - 1}`;
// load the block from the level loaded previously
const block = await fetchBlockAtIdentifier(level);
return block;
};