@human-protocol/sdk 1.1.13 → 1.1.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/statistics.ts CHANGED
@@ -20,6 +20,43 @@ import { IStatisticsParams } from './interfaces';
20
20
  import { NetworkData } from './types';
21
21
  import { throwError } from './utils';
22
22
 
23
+ /**
24
+ * ## Introduction
25
+ *
26
+ * This client enables to obtain statistical information from the subgraph.
27
+ *
28
+ * Unlikely from the other SDK clients, `StatisticsClient` does not require `signer` or `provider` to be provided.
29
+ * We just need to create client object using relevant network data.
30
+ *
31
+ * ```ts
32
+ * constructor(network: NetworkData)
33
+ * ```
34
+ *
35
+ * A `Signer` or a `Provider` should be passed depending on the use case of this module:
36
+ *
37
+ * - **Signer**: when the user wants to use this model in order to send transactions caling the contract functions.
38
+ * - **Provider**: when the user wants to use this model in order to get information from the contracts or subgraph.
39
+ *
40
+ * ## Installation
41
+ *
42
+ * ### npm
43
+ * ```bash
44
+ * npm install @human-protocol/sdk
45
+ * ```
46
+ *
47
+ * ### yarn
48
+ * ```bash
49
+ * yarn install @human-protocol/sdk
50
+ * ```
51
+ *
52
+ * ## Code example
53
+ *
54
+ * ```ts
55
+ * import { StatisticsClient, ChainId, NETWORKS } from '@human-protocol/sdk';
56
+ *
57
+ * const statisticsClient = new StatisticsClient(NETWORKS[ChainId.POLYGON_MUMBAI]);
58
+ * ```
59
+ */
23
60
  export class StatisticsClient {
24
61
  public network: NetworkData;
25
62
 
@@ -33,11 +70,53 @@ export class StatisticsClient {
33
70
  }
34
71
 
35
72
  /**
36
- * Returns the escrow statistics data for the given date range
73
+ * This function returns the statistical data of escrows.
37
74
  *
38
- * @param {IStatisticsParams} params - Filter parameters.
39
- * @returns {Promise<EscrowStatistics>}
40
- * @throws {Error} - An error object if an error occurred.
75
+ *
76
+ * **Input parameters**
77
+ *
78
+ * ```ts
79
+ * interface IStatisticsParams {
80
+ * from?: Date;
81
+ * to?: Date;
82
+ * limit?: number;
83
+ * }
84
+ * ```
85
+ *
86
+ * ```ts
87
+ * type DailyEscrowsData = {
88
+ * timestamp: Date;
89
+ * escrowsTotal: number;
90
+ * escrowsPending: number;
91
+ * escrowsSolved: number;
92
+ * escrowsPaid: number;
93
+ * escrowsCancelled: number;
94
+ * };
95
+ *
96
+ * type EscrowStatistics = {
97
+ * totalEscrows: number;
98
+ * dailyEscrowsData: DailyEscrowsData[];
99
+ * };
100
+ * ```
101
+ *
102
+ *
103
+ * @param {IStatisticsParams} params Statistics params with duration data
104
+ * @returns {EscrowStatistics} Escrow statistics data.
105
+ *
106
+ *
107
+ * **Code example**
108
+ *
109
+ * ```ts
110
+ * import { StatisticsClient, ChainId, NETWORKS } from '@human-protocol/sdk';
111
+ *
112
+ * const statisticsClient = new StatisticsClient(NETWORKS[ChainId.POLYGON_MUMBAI]);
113
+ *
114
+ * const escrowStatistics = await statisticsClient.getEscrowStatistics();
115
+ * const escrowStatisticsApril = await statisticsClient.getEscrowStatistics({
116
+ * from: new Date('2021-04-01'),
117
+ * to: new Date('2021-04-30'),
118
+ * });
119
+ * ```
41
120
  */
42
121
  async getEscrowStatistics(
43
122
  params: IStatisticsParams = {}
@@ -71,11 +150,48 @@ export class StatisticsClient {
71
150
  }
72
151
 
73
152
  /**
74
- * Returns the worker statistics data for the given date range
153
+ * This function returns the statistical data of workers.
154
+ *
155
+ *
156
+ * **Input parameters**
75
157
  *
76
- * @param {IStatisticsParams} params - Filter parameters.
77
- * @returns {Promise<WorkerStatistics>}
78
- * @throws {Error} - An error object if an error occurred.
158
+ * ```ts
159
+ * interface IStatisticsParams {
160
+ * from?: Date;
161
+ * to?: Date;
162
+ * limit?: number;
163
+ * }
164
+ * ```
165
+ *
166
+ * ```ts
167
+ * type DailyWorkerData = {
168
+ * timestamp: Date;
169
+ * activeWorkers: number;
170
+ * };
171
+ *
172
+ * type WorkerStatistics = {
173
+ * dailyWorkersData: DailyWorkerData[];
174
+ * };
175
+ * ```
176
+ *
177
+ *
178
+ * @param {IStatisticsParams} params Statistics params with duration data
179
+ * @returns {WorkerStatistics} Worker statistics data.
180
+ *
181
+ *
182
+ * **Code example**
183
+ *
184
+ * ```ts
185
+ * import { StatisticsClient, ChainId, NETWORKS } from '@human-protocol/sdk';
186
+ *
187
+ * const statisticsClient = new StatisticsClient(NETWORKS[ChainId.POLYGON_MUMBAI]);
188
+ *
189
+ * const workerStatistics = await statisticsClient.getWorkerStatistics();
190
+ * const workerStatisticsApril = await statisticsClient.getWorkerStatistics({
191
+ * from: new Date('2021-04-01'),
192
+ * to: new Date('2021-04-30'),
193
+ * });
194
+ * ```
79
195
  */
80
196
  async getWorkerStatistics(
81
197
  params: IStatisticsParams = {}
@@ -100,11 +216,71 @@ export class StatisticsClient {
100
216
  }
101
217
 
102
218
  /**
103
- * Returns the payment statistics data for the given date range
219
+ * This function returns the statistical data of payments.
220
+ *
221
+ *
222
+ * **Input parameters**
223
+ *
224
+ * ```ts
225
+ * interface IStatisticsParams {
226
+ * from?: Date;
227
+ * to?: Date;
228
+ * limit?: number;
229
+ * }
230
+ * ```
104
231
  *
105
- * @param {IStatisticsParams} params - Filter parameters.
106
- * @returns {Promise<PaymentStatistics>}
107
- * @throws {Error} - An error object if an error occurred.
232
+ * ```ts
233
+ * type DailyPaymentData = {
234
+ * timestamp: Date;
235
+ * totalAmountPaid: BigNumber;
236
+ * totalCount: number;
237
+ * averageAmountPerWorker: BigNumber;
238
+ * };
239
+ *
240
+ * type PaymentStatistics = {
241
+ * dailyPaymentsData: DailyPaymentData[];
242
+ * };
243
+ * ```
244
+ *
245
+ *
246
+ * @param {IStatisticsParams} params Statistics params with duration data
247
+ * @returns {PaymentStatistics} Payment statistics data.
248
+ *
249
+ *
250
+ * **Code example**
251
+ *
252
+ * ```ts
253
+ * import { StatisticsClient, ChainId, NETWORKS } from '@human-protocol/sdk';
254
+ *
255
+ * const statisticsClient = new StatisticsClient(NETWORKS[ChainId.POLYGON_MUMBAI]);
256
+ *
257
+ * console.log(
258
+ * 'Payment statistics:',
259
+ * (await statisticsClient.getPaymentStatistics()).dailyPaymentsData.map(
260
+ * (p) => ({
261
+ * ...p,
262
+ * totalAmountPaid: p.totalAmountPaid.toString(),
263
+ * averageAmountPerJob: p.averageAmountPerJob.toString(),
264
+ * averageAmountPerWorker: p.averageAmountPerWorker.toString(),
265
+ * })
266
+ * )
267
+ * );
268
+ *
269
+ * console.log(
270
+ * 'Payment statistics from 5/8 - 6/8:',
271
+ * (
272
+ * await statisticsClient.getPaymentStatistics({
273
+ * from: new Date(2023, 4, 8),
274
+ * to: new Date(2023, 5, 8),
275
+ * })
276
+ * ).dailyPaymentsData.map((p) => ({
277
+ * ...p,
278
+ * totalAmountPaid: p.totalAmountPaid.toString(),
279
+ * averageAmountPerJob: p.averageAmountPerJob.toString(),
280
+ * averageAmountPerWorker: p.averageAmountPerWorker.toString(),
281
+ * }))
282
+ * );
283
+ * ```
108
284
  */
109
285
  async getPaymentStatistics(
110
286
  params: IStatisticsParams = {}
@@ -136,11 +312,85 @@ export class StatisticsClient {
136
312
  }
137
313
 
138
314
  /**
139
- * Returns the HMToken statistics data for the given date range
315
+ * This function returns the statistical data of HMToken.
316
+ *
317
+ *
318
+ * **Input parameters**
319
+ *
320
+ * ```ts
321
+ * interface IStatisticsParams {
322
+ * from?: Date;
323
+ * to?: Date;
324
+ * limit?: number;
325
+ * }
326
+ * ```
327
+ *
328
+ * ```ts
329
+ * type HMTHolder = {
330
+ * address: string;
331
+ * balance: BigNumber;
332
+ * }
333
+ *
334
+ * type DailyHMTData = {
335
+ * timestamp: Date;
336
+ * totalTransactionAmount: BigNumber;
337
+ * totalTransactionCount: number;
338
+ * };
339
+ *
340
+ * type HMTStatistics = {
341
+ * totalTransferAmount: BigNumber;
342
+ * totalTransferCount: BigNumber;
343
+ * totalHolders: number;
344
+ * holders: HMTHolder[];
345
+ * dailyHMTData: DailyHMTData[];
346
+ * };
347
+ * ```
348
+ *
349
+ *
350
+ * @param {IStatisticsParams} params Statistics params with duration data
351
+ * @returns {HMTStatistics} HMToken statistics data.
352
+ *
353
+ *
354
+ * **Code example**
355
+ *
356
+ * ```ts
357
+ * import { StatisticsClient, ChainId, NETWORKS } from '@human-protocol/sdk';
358
+ *
359
+ * const statisticsClient = new StatisticsClient(NETWORKS[ChainId.POLYGON_MUMBAI]);
360
+ *
361
+ * const hmtStatistics = await statisticsClient.getHMTStatistics();
362
+ *
363
+ * console.log('HMT statistics:', {
364
+ * ...hmtStatistics,
365
+ * totalTransferAmount: hmtStatistics.totalTransferAmount.toString(),
366
+ * holders: hmtStatistics.holders.map((h) => ({
367
+ * ...h,
368
+ * balance: h.balance.toString(),
369
+ * })),
370
+ * dailyHMTData: hmtStatistics.dailyHMTData.map((d) => ({
371
+ * ...d,
372
+ * totalTransactionAmount: d.totalTransactionAmount.toString(),
373
+ * })),
374
+ * });
375
+ *
376
+ * const hmtStatisticsRange = await statisticsClient.getHMTStatistics({
377
+ * from: new Date(2023, 4, 8),
378
+ * to: new Date(2023, 5, 8),
379
+ * });
140
380
  *
141
- * @param {IStatisticsParams} params - Filter parameters.
142
- * @returns {Promise<HMTStatistics>}
143
- * @throws {Error} - An error object if an error occurred.
381
+ * console.log('HMT statistics from 5/8 - 6/8:', {
382
+ * ...hmtStatisticsRange,
383
+ * totalTransferAmount: hmtStatisticsRange.totalTransferAmount.toString(),
384
+ * holders: hmtStatisticsRange.holders.map((h) => ({
385
+ * ...h,
386
+ * balance: h.balance.toString(),
387
+ * })),
388
+ * dailyHMTData: hmtStatisticsRange.dailyHMTData.map((d) => ({
389
+ * ...d,
390
+ * totalTransactionAmount: d.totalTransactionAmount.toString(),
391
+ * })),
392
+ * });
393
+ * ```
144
394
  */
145
395
  async getHMTStatistics(
146
396
  params: IStatisticsParams = {}
package/src/storage.ts CHANGED
@@ -14,7 +14,51 @@ import { isValidUrl } from './utils';
14
14
  import { HttpStatus } from './constants';
15
15
 
16
16
  /**
17
+ *
17
18
  * @deprecated StorageClient is deprecated. Use Minio.Client directly.
19
+ *
20
+ * ## Introduction
21
+ *
22
+ * This client enables to interact with S3 cloud storage services like Amazon S3 Bucket, Google Cloud Storage and others.
23
+ *
24
+ * The instance creation of `StorageClient` should be made using its constructor:
25
+ *
26
+ * ```ts
27
+ * constructor(params: StorageParams, credentials?: StorageCredentials)
28
+ * ```
29
+ *
30
+ * > If credentials is not provided, it uses an anonymous access to the bucket for downloading files.
31
+ *
32
+ * ## Installation
33
+ *
34
+ * ### npm
35
+ * ```bash
36
+ * npm install @human-protocol/sdk
37
+ * ```
38
+ *
39
+ * ### yarn
40
+ * ```bash
41
+ * yarn install @human-protocol/sdk
42
+ * ```
43
+ *
44
+ * ## Code example
45
+ *
46
+ * ```ts
47
+ * import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
48
+ *
49
+ * const credentials: StorageCredentials = {
50
+ * accessKey: 'ACCESS_KEY',
51
+ * secretKey: 'SECRET_KEY',
52
+ * };
53
+ * const params: StorageParams = {
54
+ * endPoint: 'http://localhost',
55
+ * port: 9000,
56
+ * useSSL: false,
57
+ * region: 'us-east-1'
58
+ * };
59
+ *
60
+ * const storageClient = new StorageClient(params, credentials);
61
+ * ```
18
62
  */
19
63
  export class StorageClient {
20
64
  private client: Minio.Client;
@@ -41,10 +85,30 @@ export class StorageClient {
41
85
  }
42
86
 
43
87
  /**
44
- * **Download files from cloud storage**
88
+ * This function downloads files from a bucket.
45
89
  *
46
- * @param {string} keys - Keys of files
47
- * @returns {Promise<File>} - Downloaded file
90
+ * @param {string[]} keys Array of filenames to download.
91
+ * @param {string} bucket Bucket name.
92
+ * @returns {any[]} Returns an array of json files downloaded and parsed into objects.
93
+ *
94
+ *
95
+ * **Code example**
96
+ *
97
+ * ```ts
98
+ * import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
99
+ *
100
+ * const params: StorageParams = {
101
+ * endPoint: 'http://localhost',
102
+ * port: 9000,
103
+ * useSSL: false,
104
+ * region: 'us-east-1'
105
+ * };
106
+ *
107
+ * const storageClient = new StorageClient(params);
108
+ *
109
+ * const keys = ['file1.json', 'file2.json'];
110
+ * const files = await storageClient.downloadFiles(keys, 'bucket-name');
111
+ * ```
48
112
  */
49
113
  public async downloadFiles(keys: string[], bucket: string): Promise<any[]> {
50
114
  const isBucketExists = await this.client.bucketExists(bucket);
@@ -67,10 +131,19 @@ export class StorageClient {
67
131
  }
68
132
 
69
133
  /**
70
- * **Download files from cloud storage.*
134
+ * This function downloads files from a Url.
135
+ *
136
+ * @param {string} url Url of the file to download.
137
+ * @returns {any} Returns the JSON file downloaded and parsed into object.
71
138
  *
72
- * @param {string} url - URL to the file
73
- * @returns {Promise<File>} - Downloaded file
139
+ *
140
+ * **Code example**
141
+ *
142
+ * ```ts
143
+ * import { StorageClient } from '@human-protocol/sdk';
144
+ *
145
+ * const file = await storageClient.downloadFileFromUrl('http://localhost/file.json');
146
+ * ```
74
147
  */
75
148
  public static async downloadFileFromUrl(url: string): Promise<any> {
76
149
  if (!isValidUrl(url)) {
@@ -95,11 +168,35 @@ export class StorageClient {
95
168
  }
96
169
 
97
170
  /**
98
- * **Upload file to cloud storage**
171
+ * This function uploads files to a bucket.
172
+ *
173
+ * @param {any[]} files Array of objects to upload serialized into json.
174
+ * @param {string} bucket Bucket name.
175
+ * @returns {UploadFile[]} Returns an array of json files downloaded and parsed into objects.
176
+ *
177
+ *
178
+ * **Code example**
99
179
  *
100
- * @param {File[]} files - Files to upload
101
- * @param {string} bucket - Bucket name
102
- * @returns {Promise<UploadFile>} - Uploaded file with key/hash
180
+ * ```ts
181
+ * import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
182
+ *
183
+ * const credentials: StorageCredentials = {
184
+ * accessKey: 'ACCESS_KEY',
185
+ * secretKey: 'SECRET_KEY',
186
+ * };
187
+ * const params: StorageParams = {
188
+ * endPoint: 'http://localhost',
189
+ * port: 9000,
190
+ * useSSL: false,
191
+ * region: 'us-east-1'
192
+ * };
193
+ *
194
+ * const storageClient = new StorageClient(params, credentials);
195
+ * const file1 = { name: 'file1', description: 'description of file1' };
196
+ * const file2 = { name: 'file2', description: 'description of file2' };
197
+ * const files = [file1, file2];
198
+ * const uploadedFiles = await storageClient.uploadFiles(files, 'bucket-name');
199
+ * ```
103
200
  */
104
201
  public async uploadFiles(
105
202
  files: any[],
@@ -139,20 +236,62 @@ export class StorageClient {
139
236
  }
140
237
 
141
238
  /**
142
- * **Checks if a bucket exists**
239
+ * This function checks if a bucket exists.
240
+ *
241
+ * @param {string} bucket Bucket name.
242
+ * @returns {boolean} Returns `true` if exists, `false` if it doesn't.
243
+ *
244
+ *
245
+ * **Code example**
246
+ *
247
+ * ```ts
248
+ * import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
143
249
  *
144
- * @param {string} bucket - Name of the bucket
145
- * @returns {Promise<boolean>} - True if bucket exists, false otherwise
250
+ * const credentials: StorageCredentials = {
251
+ * accessKey: 'ACCESS_KEY',
252
+ * secretKey: 'SECRET_KEY',
253
+ * };
254
+ * const params: StorageParams = {
255
+ * endPoint: 'http://localhost',
256
+ * port: 9000,
257
+ * useSSL: false,
258
+ * region: 'us-east-1'
259
+ * };
260
+ *
261
+ * const storageClient = new StorageClient(params, credentials);
262
+ * const exists = await storageClient.bucketExists('bucket-name');
263
+ * ```
146
264
  */
147
265
  public async bucketExists(bucket: string): Promise<boolean> {
148
266
  return this.client.bucketExists(bucket);
149
267
  }
150
268
 
151
269
  /**
152
- * **Checks if a bucket exists**
270
+ * This function list all file names contained in the bucket.
271
+ *
272
+ * @param {string} bucket Bucket name.
273
+ * @returns {boolean} Returns the list of file names contained in the bucket.
274
+ *
275
+ *
276
+ * **Code example**
277
+ *
278
+ * ```ts
279
+ * import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
280
+ *
281
+ * const credentials: StorageCredentials = {
282
+ * accessKey: 'ACCESS_KEY',
283
+ * secretKey: 'SECRET_KEY',
284
+ * };
285
+ * const params: StorageParams = {
286
+ * endPoint: 'http://localhost',
287
+ * port: 9000,
288
+ * useSSL: false,
289
+ * region: 'us-east-1'
290
+ * };
153
291
  *
154
- * @param {string} bucket - Name of the bucket
155
- * @returns {Promise<string[]>} - A list of filenames with their extensions in the bucket
292
+ * const storageClient = new StorageClient(params, credentials);
293
+ * const fileNames = await storageClient.listObjects('bucket-name');
294
+ * ```
156
295
  */
157
296
  public async listObjects(bucket: string): Promise<string[]> {
158
297
  const isBucketExists = await this.client.bucketExists(bucket);
@@ -165,7 +304,7 @@ export class StorageClient {
165
304
  const keys: string[] = [];
166
305
  const stream = this.client.listObjectsV2(bucket, '', true, '');
167
306
 
168
- stream.on('data', (obj) => keys.push(obj.name));
307
+ stream.on('data', (obj: { name: string }) => keys.push(obj.name));
169
308
  stream.on('error', reject);
170
309
  stream.on('end', () => {
171
310
  resolve(keys);