Skip to content

Commit

Permalink
Add list tenant block watchers endpoint (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahnami authored Nov 16, 2023
1 parent 5eec5b0 commit 173fa83
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ module.exports = {
delete: jest.fn(),
})),
BaseApiClient: MockBaseApiClient,
isValidNetwork: jest.fn((network: string) => network !== 'invalid'),
};
10 changes: 10 additions & 0 deletions packages/monitor/src/api/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ describe('MonitorClient', () => {
beforeEach(() => {
monitor = (new MonitorClient({ apiKey: 'key', apiSecret: 'secret' }) as unknown) as TestMonitorClient;
createAuthenticatedApi.mockClear();

listBlockwatchersSpy = jest.spyOn(monitor, 'listBlockwatchers').mockImplementation(async () => [
{
blockWatcherId: 'i-am-the-watcher',
Expand Down Expand Up @@ -559,6 +560,15 @@ describe('MonitorClient', () => {
});
});

describe('listTenantBlockwatchers', () => {
it('calls API correctly', async () => {
listBlockwatchersSpy.mockRestore();
await monitor.listTenantBlockwatchers();
expect(monitor.api.get).toBeCalledWith('/blockwatchers/tenant');
expect(createAuthenticatedApi).toBeCalled();
});
});

describe('getBlockwatcherIdByNetwork', () => {
it('finds blockwatchers for network when there are available', async () => {
// Make sure the network provided is the network mocked above
Expand Down
17 changes: 15 additions & 2 deletions packages/monitor/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseApiClient, Network } from '@openzeppelin/defender-sdk-base-client';
import { BaseApiClient, Network, isValidNetwork } from '@openzeppelin/defender-sdk-base-client';
import {
ConditionSet,
CreateMonitorRequest,
Expand Down Expand Up @@ -129,6 +129,12 @@ export class MonitorClient extends BaseApiClient {
});
}

public async listTenantBlockwatchers(): Promise<BlockWatcher[]> {
return this.apiCall(async (api) => {
return await api.get(`/blockwatchers/tenant`);
});
}

public async createNotificationChannel(notification: CreateNotificationRequest): Promise<NotificationResponse> {
return this.apiCall(async (api) => {
return await api.post(`/notifications/${notification.type}`, notification);
Expand Down Expand Up @@ -185,7 +191,14 @@ export class MonitorClient extends BaseApiClient {
}

private async constructBlockMonitor(monitor: CreateBlockMonitorRequest): Promise<PartialCreateBlockMonitorRequest> {
const blockWatchers = await this.getBlockwatcherIdByNetwork(monitor.network);
let blockWatchers: BlockWatcher[] = [];
if (!isValidNetwork(monitor.network)) {
blockWatchers = (await this.listTenantBlockwatchers()).filter(
(blockwatcher) => blockwatcher.network === monitor.network,
);
} else {
blockWatchers = await this.getBlockwatcherIdByNetwork(monitor.network);
}

let blockWatcherId;

Expand Down

0 comments on commit 173fa83

Please sign in to comment.