-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28e8d36
commit 006b269
Showing
2 changed files
with
34 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
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); | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters