-
Notifications
You must be signed in to change notification settings - Fork 81
/
DLOBApiClient.ts
39 lines (34 loc) · 995 Bytes
/
DLOBApiClient.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
import fetch from 'node-fetch';
import { DLOBOrdersCoder } from './DLOBOrders';
import { DLOB } from './DLOB';
type DLOBApiClientConfig = {
url: string;
};
export class DLOBApiClient {
url: string;
dlobCoder = DLOBOrdersCoder.create();
lastSeenDLOB: DLOB;
lastSeenSlot = 0;
constructor(config: DLOBApiClientConfig) {
this.url = config.url;
}
public async getDLOB(slot: number): Promise<DLOB> {
const r = await fetch(this.url);
if (!r.ok) {
throw new Error(
`Failed to fetch DLOB from ${this.url}. Status: ${r.status}, ${r.statusText}`
);
}
const resp = await r.json();
const responseSlot = resp['slot'];
if (responseSlot > this.lastSeenSlot) {
const dlobOrdersBuffer = Buffer.from(resp['data'], 'base64');
const dlobOrders = this.dlobCoder.decode(Buffer.from(dlobOrdersBuffer));
const dlob = new DLOB();
dlob.initFromOrders(dlobOrders, slot);
this.lastSeenDLOB = dlob;
this.lastSeenSlot = responseSlot;
}
return this.lastSeenDLOB;
}
}