Skip to content

Commit

Permalink
hotfix: Minor RPC init optimization (#4195)
Browse files Browse the repository at this point in the history
* chore: Optimize provider initiation

* chore: We should only ever be instatiating the static provider
  • Loading branch information
garethfuller authored Sep 19, 2023
1 parent 80d3909 commit a8ca451
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 21 deletions.
21 changes: 2 additions & 19 deletions src/services/rpc-provider/rpc-provider.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,23 @@
import { JsonRpcProvider } from '@ethersproject/providers';

import RpcProviderService from '@/services/rpc-provider/rpc-provider.service';
import { StaticJsonRpcBatchProvider } from './static-json-rpc-batch-provider';

vi.mock('@ethersproject/providers', () => {
vi.mock('./static-json-rpc-batch-provider', () => {
return {
JsonRpcProvider: vi.fn().mockImplementation(() => {
StaticJsonRpcBatchProvider: vi.fn().mockImplementation(() => {
return {
once: vi.fn(),
};
}),
};
});

vi.mock('./static-json-rpc-batch-provider', () => {
return {
StaticJsonRpcBatchProvider: vi.fn().mockImplementation(() => {
return {};
}),
};
});

describe('RPC provider service', () => {
const MockedJsonRpcProvider = vi.mocked(JsonRpcProvider, true);
const MockedStaticJsonRpcBatchProvider = vi.mocked(
StaticJsonRpcBatchProvider,
true
);

beforeEach(() => {
MockedJsonRpcProvider.mockClear();
MockedStaticJsonRpcBatchProvider.mockClear();
});

Expand All @@ -42,9 +30,4 @@ describe('RPC provider service', () => {
new RpcProviderService();
expect(MockedStaticJsonRpcBatchProvider).toHaveBeenCalledTimes(1);
});

it('Calls the WebSocketProvider', () => {
new RpcProviderService().initBlockListener(() => ({}));
expect(MockedJsonRpcProvider).toHaveBeenCalledTimes(1);
});
});
3 changes: 1 addition & 2 deletions src/services/rpc-provider/rpc-provider.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ export default class RpcProviderService {
) {}

public initBlockListener(newBlockHandler: NewBlockHandler): void {
const blockProvider = new JsonRpcProvider(this.config.rpc);
blockProvider.once('block', newBlockNumber => {
this.jsonProvider.once('block', newBlockNumber => {
let currentBlockNumber = newBlockNumber;
newBlockHandler(currentBlockNumber);
setInterval(() => {
Expand Down

0 comments on commit a8ca451

Please sign in to comment.