@human-protocol/sdk 5.2.0 → 6.1.0

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.
Files changed (69) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/base.d.ts +8 -7
  3. package/dist/base.d.ts.map +1 -1
  4. package/dist/base.js +18 -5
  5. package/dist/constants.d.ts +0 -1
  6. package/dist/constants.d.ts.map +1 -1
  7. package/dist/constants.js +7 -8
  8. package/dist/encryption.d.ts +68 -203
  9. package/dist/encryption.d.ts.map +1 -1
  10. package/dist/encryption.js +66 -202
  11. package/dist/error.d.ts +0 -24
  12. package/dist/error.d.ts.map +1 -1
  13. package/dist/error.js +2 -26
  14. package/dist/escrow.d.ts +438 -791
  15. package/dist/escrow.d.ts.map +1 -1
  16. package/dist/escrow.js +331 -705
  17. package/dist/graphql/queries/operator.d.ts.map +1 -1
  18. package/dist/graphql/queries/operator.js +3 -1
  19. package/dist/graphql/types.d.ts.map +1 -1
  20. package/dist/index.d.ts +3 -4
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +2 -4
  23. package/dist/interfaces.d.ts +2 -2
  24. package/dist/interfaces.d.ts.map +1 -1
  25. package/dist/kvstore.d.ts +124 -186
  26. package/dist/kvstore.d.ts.map +1 -1
  27. package/dist/kvstore.js +122 -185
  28. package/dist/operator.d.ts +59 -30
  29. package/dist/operator.d.ts.map +1 -1
  30. package/dist/operator.js +59 -30
  31. package/dist/staking.d.ts +142 -141
  32. package/dist/staking.d.ts.map +1 -1
  33. package/dist/staking.js +140 -139
  34. package/dist/statistics.d.ts +104 -134
  35. package/dist/statistics.d.ts.map +1 -1
  36. package/dist/statistics.js +119 -144
  37. package/dist/transaction.d.ts +38 -17
  38. package/dist/transaction.d.ts.map +1 -1
  39. package/dist/transaction.js +40 -19
  40. package/dist/types.d.ts +14 -55
  41. package/dist/types.d.ts.map +1 -1
  42. package/dist/utils.d.ts +32 -18
  43. package/dist/utils.d.ts.map +1 -1
  44. package/dist/utils.js +32 -19
  45. package/dist/worker.d.ts +35 -14
  46. package/dist/worker.d.ts.map +1 -1
  47. package/dist/worker.js +35 -14
  48. package/package.json +8 -25
  49. package/src/base.ts +42 -7
  50. package/src/constants.ts +6 -8
  51. package/src/encryption.ts +69 -203
  52. package/src/error.ts +0 -36
  53. package/src/escrow.ts +548 -891
  54. package/src/graphql/queries/operator.ts +3 -1
  55. package/src/graphql/types.ts +4 -2
  56. package/src/index.ts +4 -5
  57. package/src/interfaces.ts +2 -2
  58. package/src/kvstore.ts +142 -197
  59. package/src/operator.ts +59 -30
  60. package/src/staking.ts +177 -160
  61. package/src/statistics.ts +125 -146
  62. package/src/transaction.ts +40 -19
  63. package/src/types.ts +16 -58
  64. package/src/utils.ts +33 -23
  65. package/src/worker.ts +35 -14
  66. package/dist/storage.d.ts +0 -186
  67. package/dist/storage.d.ts.map +0 -1
  68. package/dist/storage.js +0 -319
  69. package/src/storage.ts +0 -313
package/src/utils.ts CHANGED
@@ -3,7 +3,6 @@ import { ethers } from 'ethers';
3
3
  import gqlFetch from 'graphql-request';
4
4
 
5
5
  import { isURL } from 'validator';
6
- import { SUBGRAPH_API_KEY_PLACEHOLDER } from './constants';
7
6
  import { ChainId } from './enums';
8
7
  import {
9
8
  ContractExecutionError,
@@ -17,14 +16,20 @@ import {
17
16
  TransactionReplaced,
18
17
  WarnSubgraphApiKeyNotProvided,
19
18
  } from './error';
20
- import { NetworkData } from './types';
21
19
  import { SubgraphOptions } from './interfaces';
20
+ import { NetworkData } from './types';
22
21
 
23
22
  /**
24
- * **Handle and throw the error.*
23
+ * Handles and throws appropriate error types based on the Ethereum error.
25
24
  *
26
- * @param {any} e
27
- * @returns
25
+ * @param e - The error to handle
26
+ * @throws {InvalidArgumentError} If the error is an invalid argument error
27
+ * @throws {ContractExecutionError} If the error is a contract execution error
28
+ * @throws {TransactionReplaced} If the transaction was replaced
29
+ * @throws {ReplacementUnderpriced} If the replacement transaction was underpriced
30
+ * @throws {NumericFault} If there's a numeric fault
31
+ * @throws {NonceExpired} If the nonce has expired
32
+ * @throws {EthereumError} For any other Ethereum-related error
28
33
  */
29
34
  export const throwError = (e: any) => {
30
35
  if (ethers.isError(e, 'INVALID_ARGUMENT')) {
@@ -45,10 +50,10 @@ export const throwError = (e: any) => {
45
50
  };
46
51
 
47
52
  /**
48
- * **URL validation.*
53
+ * Validates if a string is a valid URL.
49
54
  *
50
- * @param {string} url
51
- * @returns
55
+ * @param url - The URL string to validate
56
+ * @returns True if the URL is valid, false otherwise
52
57
  */
53
58
  export const isValidUrl = (url: string): boolean => {
54
59
  return isURL(url, {
@@ -59,10 +64,10 @@ export const isValidUrl = (url: string): boolean => {
59
64
  };
60
65
 
61
66
  /**
62
- * **Check if a string is a valid JSON.*
67
+ * Checks if a string is valid JSON.
63
68
  *
64
- * @param {string} input
65
- * @returns {boolean}
69
+ * @param input - The string to check
70
+ * @returns True if the string is valid JSON, false otherwise
66
71
  */
67
72
  export const isValidJson = (input: string): boolean => {
68
73
  try {
@@ -74,18 +79,15 @@ export const isValidJson = (input: string): boolean => {
74
79
  };
75
80
 
76
81
  /**
77
- * **Get the subgraph URL.*
82
+ * Gets the subgraph URL for the given network, using API key if available.
78
83
  *
79
- * @param {NetworkData} networkData
80
- * @returns
84
+ * @param networkData - The network data containing subgraph URLs
85
+ * @returns The subgraph URL with API key if available
81
86
  */
82
87
  export const getSubgraphUrl = (networkData: NetworkData) => {
83
88
  let subgraphUrl = networkData.subgraphUrl;
84
89
  if (process.env.SUBGRAPH_API_KEY) {
85
- subgraphUrl = networkData.subgraphUrlApiKey.replace(
86
- SUBGRAPH_API_KEY_PLACEHOLDER,
87
- process.env.SUBGRAPH_API_KEY
88
- );
90
+ subgraphUrl = networkData.subgraphUrlApiKey;
89
91
  } else if (networkData.chainId !== ChainId.LOCALHOST) {
90
92
  // eslint-disable-next-line no-console
91
93
  console.warn(WarnSubgraphApiKeyNotProvided);
@@ -95,10 +97,10 @@ export const getSubgraphUrl = (networkData: NetworkData) => {
95
97
  };
96
98
 
97
99
  /**
98
- * **Convert a date to Unix timestamp (seconds since epoch).*
100
+ * Converts a Date object to Unix timestamp (seconds since epoch).
99
101
  *
100
- * @param {Date} date
101
- * @returns {number}
102
+ * @param date - The date to convert
103
+ * @returns Unix timestamp in seconds
102
104
  */
103
105
  export const getUnixTimestamp = (date: Date): number => {
104
106
  return Math.floor(date.getTime() / 1000);
@@ -127,8 +129,16 @@ const buildIndexerUrl = (baseUrl: string, indexerId?: string): string => {
127
129
  };
128
130
 
129
131
  /**
130
- * Execute a GraphQL request with automatic retry logic for bad indexer errors.
131
- * Only retries if options is provided.
132
+ * Executes a GraphQL request with automatic retry logic for bad indexer errors.
133
+ * Only retries if options is provided with maxRetries and baseDelay.
134
+ *
135
+ * @param url - The GraphQL endpoint URL
136
+ * @param query - The GraphQL query to execute
137
+ * @param variables - Variables for the GraphQL query (optional)
138
+ * @param options - Optional configuration for subgraph requests including retry logic
139
+ * @returns The response data from the GraphQL query
140
+ * @throws ErrorRetryParametersMissing If only one of maxRetries or baseDelay is provided
141
+ * @throws ErrorRoutingRequestsToIndexerRequiresApiKey If indexerId is provided without API key
132
142
  */
133
143
  export const customGqlFetch = async <T = any>(
134
144
  url: string,
package/src/worker.ts CHANGED
@@ -7,21 +7,40 @@ import { GET_WORKER_QUERY, GET_WORKERS_QUERY } from './graphql/queries/worker';
7
7
  import { IWorker, IWorkersFilter, SubgraphOptions } from './interfaces';
8
8
  import { getSubgraphUrl, customGqlFetch } from './utils';
9
9
 
10
+ /**
11
+ * Utility class for worker-related operations.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * import { WorkerUtils, ChainId } from '@human-protocol/sdk';
16
+ *
17
+ * const worker = await WorkerUtils.getWorker(
18
+ * ChainId.POLYGON_AMOY,
19
+ * '0x1234567890abcdef1234567890abcdef12345678'
20
+ * );
21
+ * console.log('Worker:', worker);
22
+ * ```
23
+ */
10
24
  export class WorkerUtils {
11
25
  /**
12
26
  * This function returns the worker data for the given address.
13
27
  *
14
- * @param {ChainId} chainId The chain ID.
15
- * @param {string} address The worker address.
16
- * @param {SubgraphOptions} options Optional configuration for subgraph requests.
17
- * @returns {Promise<IWorker | null>} - Returns the worker details or null if not found.
18
- *
19
- * **Code example**
28
+ * @param chainId - The chain ID.
29
+ * @param address - The worker address.
30
+ * @param options - Optional configuration for subgraph requests.
31
+ * @returns Returns the worker details or null if not found.
32
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
33
+ * @throws ErrorInvalidAddress If the address is invalid
20
34
  *
35
+ * @example
21
36
  * ```ts
22
37
  * import { WorkerUtils, ChainId } from '@human-protocol/sdk';
23
38
  *
24
- * const worker = await WorkerUtils.getWorker(ChainId.POLYGON, '0x1234567890abcdef1234567890abcdef12345678');
39
+ * const worker = await WorkerUtils.getWorker(
40
+ * ChainId.POLYGON_AMOY,
41
+ * '0x1234567890abcdef1234567890abcdef12345678'
42
+ * );
43
+ * console.log('Worker:', worker);
25
44
  * ```
26
45
  */
27
46
  public static async getWorker(
@@ -79,21 +98,23 @@ export class WorkerUtils {
79
98
  * };
80
99
  * ```
81
100
  *
82
- * @param {IWorkersFilter} filter Filter for the workers.
83
- * @param {SubgraphOptions} options Optional configuration for subgraph requests.
84
- * @returns {Promise<IWorker[]>} Returns an array with all the worker details.
85
- *
86
- * **Code example**
101
+ * @param filter - Filter for the workers.
102
+ * @param options - Optional configuration for subgraph requests.
103
+ * @returns Returns an array with all the worker details.
104
+ * @throws ErrorUnsupportedChainID If the chain ID is not supported
105
+ * @throws ErrorInvalidAddress If the filter address is invalid
87
106
  *
107
+ * @example
88
108
  * ```ts
89
109
  * import { WorkerUtils, ChainId } from '@human-protocol/sdk';
90
110
  *
91
- * const filter: IWorkersFilter = {
92
- * chainId: ChainId.POLYGON,
111
+ * const filter = {
112
+ * chainId: ChainId.POLYGON_AMOY,
93
113
  * first: 10,
94
114
  * skip: 0,
95
115
  * };
96
116
  * const workers = await WorkerUtils.getWorkers(filter);
117
+ * console.log('Workers:', workers.length);
97
118
  * ```
98
119
  */
99
120
  public static async getWorkers(
package/dist/storage.d.ts DELETED
@@ -1,186 +0,0 @@
1
- import { UploadFile, StorageCredentials, StorageParams } from './types';
2
- /**
3
- *
4
- * @deprecated StorageClient is deprecated. Use Minio.Client directly.
5
- *
6
- * ## Introduction
7
- *
8
- * This client enables interacting with S3 cloud storage services like Amazon S3 Bucket, Google Cloud Storage, and others.
9
- *
10
- * The instance creation of `StorageClient` should be made using its constructor:
11
- *
12
- * ```ts
13
- * constructor(params: StorageParams, credentials?: StorageCredentials)
14
- * ```
15
- *
16
- * > If credentials are not provided, it uses anonymous access to the bucket for downloading files.
17
- *
18
- * ## Installation
19
- *
20
- * ### npm
21
- * ```bash
22
- * npm install @human-protocol/sdk
23
- * ```
24
- *
25
- * ### yarn
26
- * ```bash
27
- * yarn install @human-protocol/sdk
28
- * ```
29
- *
30
- * ## Code example
31
- *
32
- * ```ts
33
- * import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
34
- *
35
- * const credentials: StorageCredentials = {
36
- * accessKey: 'ACCESS_KEY',
37
- * secretKey: 'SECRET_KEY',
38
- * };
39
- * const params: StorageParams = {
40
- * endPoint: 'http://localhost',
41
- * port: 9000,
42
- * useSSL: false,
43
- * region: 'us-east-1'
44
- * };
45
- *
46
- * const storageClient = new StorageClient(params, credentials);
47
- * ```
48
- */
49
- export declare class StorageClient {
50
- private client;
51
- private clientParams;
52
- /**
53
- * **Storage client constructor**
54
- *
55
- * @param {StorageParams} params - Cloud storage params
56
- * @param {StorageCredentials} credentials - Optional. Cloud storage access data. If credentials are not provided - use anonymous access to the bucket
57
- */
58
- constructor(params: StorageParams, credentials?: StorageCredentials);
59
- /**
60
- * This function downloads files from a bucket.
61
- *
62
- * @param {string[]} keys Array of filenames to download.
63
- * @param {string} bucket Bucket name.
64
- * @returns {Promise<any[]>} Returns an array of JSON files downloaded and parsed into objects.
65
- *
66
- * **Code example**
67
- *
68
- * ```ts
69
- * import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
70
- *
71
- * const params: StorageParams = {
72
- * endPoint: 'http://localhost',
73
- * port: 9000,
74
- * useSSL: false,
75
- * region: 'us-east-1'
76
- * };
77
- *
78
- * const storageClient = new StorageClient(params);
79
- *
80
- * const keys = ['file1.json', 'file2.json'];
81
- * const files = await storageClient.downloadFiles(keys, 'bucket-name');
82
- * ```
83
- */
84
- downloadFiles(keys: string[], bucket: string): Promise<any[]>;
85
- /**
86
- * This function downloads files from a URL.
87
- *
88
- * @param {string} url URL of the file to download.
89
- * @returns {Promise<any>} Returns the JSON file downloaded and parsed into an object.
90
- *
91
- * **Code example**
92
- *
93
- * ```ts
94
- * import { StorageClient } from '@human-protocol/sdk';
95
- *
96
- * const file = await StorageClient.downloadFileFromUrl('http://localhost/file.json');
97
- * ```
98
- */
99
- static downloadFileFromUrl(url: string): Promise<any>;
100
- /**
101
- * This function uploads files to a bucket.
102
- *
103
- * @param {any[]} files Array of objects to upload serialized into JSON.
104
- * @param {string} bucket Bucket name.
105
- * @returns {Promise<UploadFile[]>} Returns an array of uploaded file metadata.
106
- *
107
- * **Code example**
108
- *
109
- * ```ts
110
- * import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
111
- *
112
- * const credentials: StorageCredentials = {
113
- * accessKey: 'ACCESS_KEY',
114
- * secretKey: 'SECRET_KEY',
115
- * };
116
- * const params: StorageParams = {
117
- * endPoint: 'http://localhost',
118
- * port: 9000,
119
- * useSSL: false,
120
- * region: 'us-east-1'
121
- * };
122
- *
123
- * const storageClient = new StorageClient(params, credentials);
124
- * const file1 = { name: 'file1', description: 'description of file1' };
125
- * const file2 = { name: 'file2', description: 'description of file2' };
126
- * const files = [file1, file2];
127
- * const uploadedFiles = await storageClient.uploadFiles(files, 'bucket-name');
128
- * ```
129
- */
130
- uploadFiles(files: any[], bucket: string): Promise<UploadFile[]>;
131
- /**
132
- * This function checks if a bucket exists.
133
- *
134
- * @param {string} bucket Bucket name.
135
- * @returns {Promise<boolean>} Returns `true` if exists, `false` if it doesn't.
136
- *
137
- * **Code example**
138
- *
139
- * ```ts
140
- * import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
141
- *
142
- * const credentials: StorageCredentials = {
143
- * accessKey: 'ACCESS_KEY',
144
- * secretKey: 'SECRET_KEY',
145
- * };
146
- * const params: StorageParams = {
147
- * endPoint: 'http://localhost',
148
- * port: 9000,
149
- * useSSL: false,
150
- * region: 'us-east-1'
151
- * };
152
- *
153
- * const storageClient = new StorageClient(params, credentials);
154
- * const exists = await storageClient.bucketExists('bucket-name');
155
- * ```
156
- */
157
- bucketExists(bucket: string): Promise<boolean>;
158
- /**
159
- * This function lists all file names contained in the bucket.
160
- *
161
- * @param {string} bucket Bucket name.
162
- * @returns {Promise<string[]>} Returns the list of file names contained in the bucket.
163
- *
164
- * **Code example**
165
- *
166
- * ```ts
167
- * import { StorageClient, StorageCredentials, StorageParams } from '@human-protocol/sdk';
168
- *
169
- * const credentials: StorageCredentials = {
170
- * accessKey: 'ACCESS_KEY',
171
- * secretKey: 'SECRET_KEY',
172
- * };
173
- * const params: StorageParams = {
174
- * endPoint: 'http://localhost',
175
- * port: 9000,
176
- * useSSL: false,
177
- * region: 'us-east-1'
178
- * };
179
- *
180
- * const storageClient = new StorageClient(params, credentials);
181
- * const fileNames = await storageClient.listObjects('bucket-name');
182
- * ```
183
- */
184
- listObjects(bucket: string): Promise<string[]>;
185
- }
186
- //# sourceMappingURL=storage.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,UAAU,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAIxE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,qBAAa,aAAa;IACxB,OAAO,CAAC,MAAM,CAAe;IAC7B,OAAO,CAAC,YAAY,CAAgB;IAEpC;;;;;OAKG;gBACS,MAAM,EAAE,aAAa,EAAE,WAAW,CAAC,EAAE,kBAAkB;IAcnE;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACU,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;IAoB1E;;;;;;;;;;;;;OAaG;WACiB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC;IAsBlE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA6BG;IACU,WAAW,CACtB,KAAK,EAAE,GAAG,EAAE,EACZ,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,UAAU,EAAE,CAAC;IAmCxB;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACU,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAI3D;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACU,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;CAqB5D"}