Skip to content

Commit

Permalink
Merge pull request #133 from OpenZeppelin/feature/account-usage
Browse files Browse the repository at this point in the history
Add support for retrieving account usage via sdk
  • Loading branch information
zeljkoX authored Oct 19, 2023
2 parents 837bf9c + 9511184 commit fa02288
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 1 deletion.
17 changes: 17 additions & 0 deletions examples/get-usage/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require('dotenv').config();

const { Defender } = require('@openzeppelin/defender-sdk');

async function main() {
const creds = { apiKey: process.env.API_KEY, apiSecret: process.env.API_SECRET };
const client = new Defender(creds);

// List Account Usage
const usage = await client.account.getUsage();

console.log(usage);
}

if (require.main === module) {
main().catch(console.error);
}
15 changes: 15 additions & 0 deletions examples/get-usage/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@openzeppelin/defender-sdk-example-get-usage",
"version": "1.3.0",
"private": true,
"main": "index.js",
"author": "Zeljko Markovic <[email protected]>",
"license": "MIT",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"@openzeppelin/defender-sdk": "1.3.0",
"dotenv": "^16.3.1"
}
}
5 changes: 5 additions & 0 deletions packages/account/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Defender SDK Account Client

The OpenZeppelin Defender provides a security operations (SecOps) platform for Ethereum with built-in best practices. Development teams implement Defender to ship faster and minimize security risks.

This library provides methods related to networks. See Examples for usage.
1 change: 1 addition & 0 deletions packages/account/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../jest.config');
30 changes: 30 additions & 0 deletions packages/account/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@openzeppelin/defender-sdk-account-client",
"version": "1.3.0",
"description": "",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"scripts": {
"build": "rm -rf lib && tsc",
"test": "npm run test:unit",
"test:unit": "jest --verbose --passWithNoTests --forceExit",
"watch": "tsc -w"
},
"files": [
"lib",
"!*.test.js",
"!*.test.js.map",
"!*.test.d.ts",
"!*__mocks__"
],
"author": "OpenZeppelin Defender <[email protected]>",
"license": "MIT",
"dependencies": {
"@openzeppelin/defender-sdk-base-client": "^1.3.0",
"axios": "^1.4.0",
"lodash": "^4.17.21"
},
"publishConfig": {
"access": "public"
}
}
27 changes: 27 additions & 0 deletions packages/account/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { BaseApiClient } from '@openzeppelin/defender-sdk-base-client';
import { AccountUsageResponse } from '../models/account';

const PATH = '/account';

export class AccountClient extends BaseApiClient {
protected getPoolId(): string {
return process.env.DEFENDER_POOL_ID ?? 'us-west-2_94f3puJWv';
}

protected getPoolClientId(): string {
return process.env.DEFENDER_POOL_CLIENT_ID ?? '40e58hbc7pktmnp9i26hh5nsav';
}

protected getApiUrl(): string {
return process.env.DEFENDER_API_URL ?? 'https://defender-api.openzeppelin.com/v2/';
}

public async getUsage(params?: { date?: string | Date; quotas: string[] }): Promise<AccountUsageResponse> {
const searchParams = new URLSearchParams({
...(params?.quotas && { quotas: params.quotas.join(',') }),
...(params?.date && { date: new Date(params.date).toISOString() }),
});

return this.apiCall(async (api) => api.get(`${PATH}/usage?${searchParams.toString()}`));
}
}
5 changes: 5 additions & 0 deletions packages/account/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { AccountClient } from './api';
export { AccountUsageResponse } from './models/account';

// eslint-disable-next-line @typescript-eslint/no-var-requires
export const VERSION = require('../package.json').version;
16 changes: 16 additions & 0 deletions packages/account/src/models/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
type AccountUsage =
| {
name: string;
description: string;
used: number;
limit: number;
overage?: number;
remaining: number;
period: 'hour' | 'month' | 'total';
}
| {
name: string;
error: string;
};

export type AccountUsageResponse = Record<string, AccountUsage>;
11 changes: 11 additions & 0 deletions packages/account/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "code-style/tsconfig.json",
"compilerOptions": {
"declaration": true,
"outDir": "./lib",
"skipLibCheck": true,
"sourceMap": false
},
"include": ["./src"],
"exclude": ["**/*.test.ts", "**/__mocks__/*"]
}
3 changes: 2 additions & 1 deletion packages/defender-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"@openzeppelin/defender-sdk-deploy-client": "^1.3.0",
"@openzeppelin/defender-sdk-notification-channel-client": "^1.3.0",
"@openzeppelin/defender-sdk-relay-signer-client": "^1.3.0",
"@openzeppelin/defender-sdk-network-client": "^1.3.0"
"@openzeppelin/defender-sdk-network-client": "^1.3.0",
"@openzeppelin/defender-sdk-account-client": "^1.3.0"
},
"publishConfig": {
"access": "public"
Expand Down
5 changes: 5 additions & 0 deletions packages/defender-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ProposalClient } from '@openzeppelin/defender-sdk-proposal-client';
import { DeployClient } from '@openzeppelin/defender-sdk-deploy-client';
import { NotificationChannelClient } from '@openzeppelin/defender-sdk-notification-channel-client';
import { NetworkClient } from '@openzeppelin/defender-sdk-network-client';
import { AccountClient } from '@openzeppelin/defender-sdk-account-client';

import { Newable, ClientParams } from './types';
import { ActionRelayerParams, Relayer as RelaySignerClient } from '@openzeppelin/defender-sdk-relay-signer-client';
Expand Down Expand Up @@ -57,6 +58,10 @@ export class Defender {
return getClient(NetworkClient, { apiKey: this.apiKey, apiSecret: this.apiSecret });
}

get account() {
return getClient(AccountClient, { apiKey: this.apiKey, apiSecret: this.apiSecret });
}

get monitor() {
return getClient(MonitorClient, { apiKey: this.apiKey, apiSecret: this.apiSecret });
}
Expand Down
24 changes: 24 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fa02288

Please sign in to comment.