Skip to content

Commit

Permalink
Mobile integration (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpuri authored Jul 11, 2023
1 parent 0e20ba0 commit 3afc9ff
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 244 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"dist/"
],
"scripts": {
"build": "tsc --project tsconfig.build.json && cp node_modules/@blockaid/ppom-mock/dist/ppom_bg.wasm dist/ppom_bg.wasm",
"build": "tsc --project tsconfig.build.json",
"build:clean": "rimraf dist && yarn build",
"build:docs": "typedoc",
"lint": "yarn lint:eslint && yarn lint:constraints && yarn lint:misc --check && yarn lint:dependencies --check && yarn lint:changelog",
Expand All @@ -31,7 +31,6 @@
"test:watch": "jest --watch"
},
"dependencies": {
"@blockaid/ppom-mock": "^1.0.0",
"@metamask/base-controller": "^3.0.0",
"@metamask/controller-utils": "^4.0.0",
"await-semaphore": "^0.1.3"
Expand Down
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export * from './ppom-controller';

export type { StorageBackend, StorageKey } from './ppom-storage';

export * from './ppom-middleware';
119 changes: 68 additions & 51 deletions src/ppom-controller.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { PPOM } from '@blockaid/ppom-mock';

import {
VERSION_INFO,
buildFetchSpy,
Expand Down Expand Up @@ -28,31 +26,6 @@ async function flushPromises() {
return new Promise(jest.requireActual('timers').setImmediate);
}

jest.mock('@blockaid/ppom-mock', () => ({
PPOM: class PPOMClass {
#jsonRpcRequest;

constructor(jsonRpcRequest: any) {
this.#jsonRpcRequest = jsonRpcRequest;
}

validateJsonRpc = async () => {
return Promise.resolve();
};

free = () => undefined;

testJsonRPCRequest = async (args: any) =>
await this.#jsonRpcRequest({
method: 'eth_blockNumber',
...args,
});
},
// eslint-disable-next-line @typescript-eslint/naming-convention
__esModule: true,
default: () => undefined,
}));

describe('PPOMController', () => {
let ppomController: any;

Expand Down Expand Up @@ -95,7 +68,7 @@ describe('PPOMController', () => {
ppomController = buildPPOMController();
jest.runOnlyPendingTimers();

await ppomController.usePPOM(async (ppom: PPOM) => {
await ppomController.usePPOM(async (ppom: any) => {
expect(ppom).toBeDefined();
return Promise.resolve();
});
Expand All @@ -106,7 +79,7 @@ describe('PPOMController', () => {
ppomController = buildPPOMController();
jest.runOnlyPendingTimers();

const result = await ppomController.usePPOM(async (ppom: PPOM) => {
const result = await ppomController.usePPOM(async (ppom: any) => {
expect(ppom).toBeDefined();
return Promise.resolve('DUMMY_VALUE');
});
Expand Down Expand Up @@ -158,6 +131,44 @@ describe('PPOMController', () => {
expect(spy).toHaveBeenCalledTimes(10);
});

it('should re-initialise ppom to use files fetched with scheduled job', async () => {
buildFetchSpy();
const freeMock = jest.fn();
ppomController = buildPPOMController({
ppomProvider: {
ppomInit: () => undefined,
PPOM: class PPOMClass {
#jsonRpcRequest;

constructor(jsonRpcRequest: any) {
this.#jsonRpcRequest = jsonRpcRequest;
}

validateJsonRpc = async () => {
return Promise.resolve();
};

free = freeMock;

testJsonRPCRequest = async (args2: any) =>
await this.#jsonRpcRequest({
method: 'eth_blockNumber',
...args2,
});
},
},
});
jest.runOnlyPendingTimers();
await ppomController.usePPOM(async () => {
return Promise.resolve();
});
jest.runOnlyPendingTimers();
await ppomController.usePPOM(async () => {
return Promise.resolve();
});
expect(freeMock).toHaveBeenCalledTimes(1);
});

it('should pass instance of provider to ppom to enable it to send JSON RPC request on it', async () => {
buildFetchSpy();
ppomController = buildPPOMController({
Expand All @@ -169,8 +180,8 @@ describe('PPOMController', () => {
});
jest.runOnlyPendingTimers();

await ppomController.usePPOM(async (ppom: PPOM) => {
const result = await (ppom as any).testJsonRPCRequest();
await ppomController.usePPOM(async (ppom: any) => {
const result = await ppom.testJsonRPCRequest();
expect(result).toBe('DUMMY_VALUE');
});
});
Expand All @@ -185,8 +196,8 @@ describe('PPOMController', () => {
},
});
jest.runOnlyPendingTimers();
await ppomController.usePPOM(async (ppom: PPOM) => {
(ppom as any).testJsonRPCRequest().catch((exp: any) => {
await ppomController.usePPOM(async (ppom: any) => {
ppom.testJsonRPCRequest().catch((exp: any) => {
// eslint-disable-next-line jest/no-conditional-expect
expect(exp).toBe('DUMMY_ERROR');
});
Expand All @@ -203,8 +214,8 @@ describe('PPOMController', () => {
},
});
jest.runOnlyPendingTimers();
await ppomController.usePPOM(async (ppom: PPOM) => {
(ppom as any)
await ppomController.usePPOM(async (ppom: any) => {
ppom
.testJsonRPCRequest({ method: 'DUMMY_METHOD' })
.catch((exp: any) => {
// eslint-disable-next-line jest/no-conditional-expect
Expand All @@ -226,20 +237,18 @@ describe('PPOMController', () => {
});
jest.runOnlyPendingTimers();

await ppomController.usePPOM(async (ppom: PPOM) => {
await (ppom as any).testJsonRPCRequest();
await (ppom as any).testJsonRPCRequest();
await (ppom as any).testJsonRPCRequest();
await (ppom as any).testJsonRPCRequest();
await (ppom as any).testJsonRPCRequest();
const result = await (ppom as any)
.testJsonRPCRequest()
.catch((exp: any) => {
// eslint-disable-next-line jest/no-conditional-expect
expect(exp.toString()).toBe(
'Error: Number of request to provider from PPOM exceed rate limit',
);
});
await ppomController.usePPOM(async (ppom: any) => {
await ppom.testJsonRPCRequest();
await ppom.testJsonRPCRequest();
await ppom.testJsonRPCRequest();
await ppom.testJsonRPCRequest();
await ppom.testJsonRPCRequest();
const result = await ppom.testJsonRPCRequest().catch((exp: any) => {
// eslint-disable-next-line jest/no-conditional-expect
expect(exp.toString()).toBe(
'Error: Number of request to provider from PPOM exceed rate limit',
);
});
expect(result).toBeUndefined();
});
});
Expand Down Expand Up @@ -465,7 +474,9 @@ describe('PPOMController', () => {
// is helping fetch new files as value of fileScheduleInterval is adjusted to be able to fetch all data files
const spy = buildFetchSpy();
ppomController = buildPPOMController({
fileScheduleInterval: REFRESH_TIME_INTERVAL * 100,
state: {
fileScheduleInterval: REFRESH_TIME_INTERVAL * 100,
},
});
expect(spy).toHaveBeenCalledTimes(0);
jest.advanceTimersByTime(REFRESH_TIME_INTERVAL);
Expand All @@ -479,11 +490,17 @@ describe('PPOMController', () => {

it('should delete network more than a week old from chainStatus', async () => {
buildFetchSpy();
ppomController = buildPPOMController();
let callBack: any;
ppomController = buildPPOMController({
onNetworkChange: (func: any) => {
callBack = func;
},
});
jest.runOnlyPendingTimers();
await flushPromises();
const chainIdData1 = ppomController.state.chainStatus['0x1'];
expect(chainIdData1).toBeDefined();
callBack({ providerConfig: { chainId: '0x2' } });

jest.advanceTimersByTime(NETWORK_CACHE_DURATION);
jest.runOnlyPendingTimers();
Expand Down
Loading

0 comments on commit 3afc9ff

Please sign in to comment.