Skip to content

Commit

Permalink
DLOBApiClient init
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney committed Apr 21, 2023
1 parent 28e8d36 commit 006b269
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 33 additions & 0 deletions sdk/src/dlob/DLOBApiClient.ts
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;
}
}
2 changes: 1 addition & 1 deletion sdk/src/dlob/DLOBSubscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class DLOBSubscriber {
this.intervalId = setInterval(async () => {
await this.updateDLOB();
this.eventEmitter.emit('update', this.dlob);
});
}, this.updateFrequency);
}

async updateDLOB(): Promise<void> {
Expand Down

0 comments on commit 006b269

Please sign in to comment.