Skip to content

Commit

Permalink
polish
Browse files Browse the repository at this point in the history
  • Loading branch information
shunjizhan committed Nov 14, 2023
1 parent 6848c04 commit 239baf5
Showing 1 changed file with 10 additions and 21 deletions.
31 changes: 10 additions & 21 deletions scripts/e2e-tests/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { hexZeroPad } from 'ethers/lib/utils';
import WebSocket from 'ws';

export class SubsManager {
curReqId = 0;
msgs: any[] = [];
ws: WebSocket;
isReady: Promise<void>;
id = 0;

constructor(url: string) {
this.ws = new WebSocket(url);
Expand All @@ -25,11 +25,11 @@ export class SubsManager {
this.msgs.length = 0;
}

buildRequest(id: number, params: any[]) {
buildRequest(params: any[], method = 'eth_subscribe') {
return JSON.stringify({
jsonrpc: '2.0',
method: 'eth_subscribe',
id,
id: ++this.curReqId,
method,
params,
})
}
Expand Down Expand Up @@ -72,40 +72,29 @@ export class SubsManager {
}

async subscribeNewHeads() {
const reqId = this.id++;

this.ws.send(
this.buildRequest(reqId, ['newHeads'])
this.buildRequest(['newHeads'])
);

const resp = await this.waitForSubs(reqId);
const resp = await this.waitForSubs(this.curReqId);
return resp.result as string;
}

async subscribeLogs(params: any) {
const reqId = this.id++;

this.ws.send(
this.buildRequest(reqId, ['logs', params])
this.buildRequest(['logs', params])
);

const resp = await this.waitForSubs(reqId);
const resp = await this.waitForSubs(this.curReqId);
return resp.result as string;
}

async unSubscribe(subId: string) {
const reqId = this.id++;

this.ws.send(
JSON.stringify({
jsonrpc: '2.0',
id: reqId,
method: 'eth_unsubscribe',
params: [subId],
})
this.buildRequest([subId], 'eth_unsubscribe')
);

const resp = await this.waitForSubs(reqId);
const resp = await this.waitForSubs(this.curReqId);
return resp.result as string;
}

Expand Down

0 comments on commit 239baf5

Please sign in to comment.