@human-protocol/sdk 3.0.1 → 3.0.2
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/dist/constants.d.ts +1 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +1 -0
- package/dist/escrow.d.ts +0 -6
- package/dist/escrow.d.ts.map +1 -1
- package/dist/escrow.js +0 -6
- package/dist/graphql/queries/escrow.d.ts.map +1 -1
- package/dist/graphql/queries/escrow.js +0 -3
- package/dist/graphql/queries/kvstore.d.ts +1 -0
- package/dist/graphql/queries/kvstore.d.ts.map +1 -1
- package/dist/graphql/queries/kvstore.js +12 -1
- package/dist/graphql/queries/operator.js +1 -1
- package/dist/graphql/queries/statistics.d.ts +2 -2
- package/dist/graphql/queries/statistics.d.ts.map +1 -1
- package/dist/graphql/queries/statistics.js +11 -7
- package/dist/graphql/types.d.ts +0 -5
- package/dist/graphql/types.d.ts.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/interfaces.d.ts +3 -5
- package/dist/interfaces.d.ts.map +1 -1
- package/dist/kvstore.d.ts +69 -106
- package/dist/kvstore.d.ts.map +1 -1
- package/dist/kvstore.js +123 -159
- package/dist/statistics.d.ts +72 -66
- package/dist/statistics.d.ts.map +1 -1
- package/dist/statistics.js +121 -89
- package/package.json +1 -1
- package/src/constants.ts +1 -0
- package/src/escrow.ts +0 -6
- package/src/graphql/queries/escrow.ts +0 -3
- package/src/graphql/queries/kvstore.ts +11 -0
- package/src/graphql/queries/operator.ts +1 -1
- package/src/graphql/queries/statistics.ts +13 -9
- package/src/graphql/types.ts +0 -5
- package/src/index.ts +2 -1
- package/src/interfaces.ts +3 -5
- package/src/kvstore.ts +145 -158
- package/src/statistics.ts +138 -100
package/src/kvstore.ts
CHANGED
|
@@ -19,7 +19,10 @@ import {
|
|
|
19
19
|
import gqlFetch from 'graphql-request';
|
|
20
20
|
import { NetworkData } from './types';
|
|
21
21
|
import { getSubgraphUrl, isValidUrl } from './utils';
|
|
22
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
GET_KVSTORE_BY_ADDRESS_AND_KEY_QUERY,
|
|
24
|
+
GET_KVSTORE_BY_ADDRESS_QUERY,
|
|
25
|
+
} from './graphql/queries/kvstore';
|
|
23
26
|
import { KVStoreData } from './graphql';
|
|
24
27
|
import { IKVStore } from './interfaces';
|
|
25
28
|
/**
|
|
@@ -277,71 +280,162 @@ export class KVStoreClient extends BaseEthersClient {
|
|
|
277
280
|
throw Error(`Failed to set URL and hash: ${e.message}`);
|
|
278
281
|
}
|
|
279
282
|
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* ## Introduction
|
|
287
|
+
*
|
|
288
|
+
* Utility class for KVStore-related operations.
|
|
289
|
+
*
|
|
290
|
+
* ## Installation
|
|
291
|
+
*
|
|
292
|
+
* ### npm
|
|
293
|
+
* ```bash
|
|
294
|
+
* npm install @human-protocol/sdk
|
|
295
|
+
* ```
|
|
296
|
+
*
|
|
297
|
+
* ### yarn
|
|
298
|
+
* ```bash
|
|
299
|
+
* yarn install @human-protocol/sdk
|
|
300
|
+
* ```
|
|
301
|
+
*
|
|
302
|
+
* ## Code example
|
|
303
|
+
*
|
|
304
|
+
* ### Signer
|
|
305
|
+
*
|
|
306
|
+
* **Using private key (backend)**
|
|
307
|
+
*
|
|
308
|
+
* ```ts
|
|
309
|
+
* import { ChainId, KVStoreUtils } from '@human-protocol/sdk';
|
|
310
|
+
*
|
|
311
|
+
* const KVStoreAddresses = new KVStoreUtils.getKVStoreData({
|
|
312
|
+
* ChainId.POLYGON_AMOY,
|
|
313
|
+
* "0x1234567890123456789012345678901234567890",
|
|
314
|
+
* );
|
|
315
|
+
* ```
|
|
316
|
+
*/
|
|
317
|
+
export class KVStoreUtils {
|
|
318
|
+
/**
|
|
319
|
+
* This function returns the KVStore data for a given address.
|
|
320
|
+
*
|
|
321
|
+
* @param {ChainId} chainId Network in which the KVStore is deployed
|
|
322
|
+
* @param {string} address Address of the KVStore
|
|
323
|
+
* @returns {Promise<IKVStore[]>} KVStore data
|
|
324
|
+
* @throws {ErrorUnsupportedChainID} - Thrown if the network's chainId is not supported
|
|
325
|
+
* @throws {ErrorInvalidAddress} - Thrown if the Address sent is invalid
|
|
326
|
+
*
|
|
327
|
+
* **Code example**
|
|
328
|
+
*
|
|
329
|
+
* ```ts
|
|
330
|
+
* import { ChainId, KVStoreUtils } from '@human-protocol/sdk';
|
|
331
|
+
*
|
|
332
|
+
* const kvStoreData = await KVStoreUtils.getKVStoreData(ChainId.POLYGON_AMOY, "0x1234567890123456789012345678901234567890");
|
|
333
|
+
* console.log(kvStoreData);
|
|
334
|
+
* ```
|
|
335
|
+
*/
|
|
336
|
+
public static async getKVStoreData(
|
|
337
|
+
chainId: ChainId,
|
|
338
|
+
address: string
|
|
339
|
+
): Promise<IKVStore[]> {
|
|
340
|
+
const networkData = NETWORKS[chainId];
|
|
341
|
+
|
|
342
|
+
if (!networkData) {
|
|
343
|
+
throw ErrorUnsupportedChainID;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
if (address && !ethers.isAddress(address)) {
|
|
347
|
+
throw ErrorInvalidAddress;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const { kvstores } = await gqlFetch<{ kvstores: KVStoreData[] }>(
|
|
351
|
+
getSubgraphUrl(networkData),
|
|
352
|
+
GET_KVSTORE_BY_ADDRESS_QUERY(),
|
|
353
|
+
{ address: address.toLowerCase() }
|
|
354
|
+
);
|
|
355
|
+
|
|
356
|
+
const kvStoreData = kvstores.map((item) => ({
|
|
357
|
+
key: item.key,
|
|
358
|
+
value: item.value,
|
|
359
|
+
}));
|
|
360
|
+
|
|
361
|
+
return kvStoreData || [];
|
|
362
|
+
}
|
|
280
363
|
|
|
281
364
|
/**
|
|
282
|
-
* Gets the value of a key-value pair in the
|
|
365
|
+
* Gets the value of a key-value pair in the KVStore using the subgraph.
|
|
283
366
|
*
|
|
367
|
+
* @param {ChainId} chainId Network in which the KVStore is deployed
|
|
284
368
|
* @param {string} address Address from which to get the key value.
|
|
285
369
|
* @param {string} key Key to obtain the value.
|
|
286
|
-
* @returns {string} Value of the key.
|
|
287
|
-
*
|
|
370
|
+
* @returns {Promise<string>} Value of the key.
|
|
371
|
+
* @throws {ErrorUnsupportedChainID} - Thrown if the network's chainId is not supported
|
|
372
|
+
* @throws {ErrorInvalidAddress} - Thrown if the Address sent is invalid
|
|
373
|
+
* @throws {ErrorKVStoreEmptyKey} - Thrown if the key is empty
|
|
288
374
|
*
|
|
289
375
|
* **Code example**
|
|
290
376
|
*
|
|
291
|
-
* > Need to have available stake.
|
|
292
|
-
*
|
|
293
377
|
* ```ts
|
|
294
|
-
* import {
|
|
295
|
-
* import { KVStoreClient } from '@human-protocol/sdk';
|
|
296
|
-
*
|
|
297
|
-
* const rpcUrl = 'YOUR_RPC_URL';
|
|
378
|
+
* import { ChainId, KVStoreUtils } from '@human-protocol/sdk';
|
|
298
379
|
*
|
|
299
|
-
* const
|
|
300
|
-
* const
|
|
380
|
+
* const chainId = ChainId.POLYGON_AMOY;
|
|
381
|
+
* const address = '0x1234567890123456789012345678901234567890';
|
|
382
|
+
* const key = 'role';
|
|
301
383
|
*
|
|
302
|
-
* const value = await
|
|
384
|
+
* const value = await KVStoreUtils.get(chainId, address, key);
|
|
385
|
+
* console.log(value);
|
|
303
386
|
* ```
|
|
304
387
|
*/
|
|
305
|
-
public async get(
|
|
388
|
+
public static async get(
|
|
389
|
+
chainId: ChainId,
|
|
390
|
+
address: string,
|
|
391
|
+
key: string
|
|
392
|
+
): Promise<string> {
|
|
306
393
|
if (key === '') throw ErrorKVStoreEmptyKey;
|
|
307
394
|
if (!ethers.isAddress(address)) throw ErrorInvalidAddress;
|
|
308
395
|
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
396
|
+
const networkData = NETWORKS[chainId];
|
|
397
|
+
|
|
398
|
+
if (!networkData) {
|
|
399
|
+
throw ErrorUnsupportedChainID;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const { kvstores } = await gqlFetch<{ kvstores: KVStoreData[] }>(
|
|
403
|
+
getSubgraphUrl(networkData),
|
|
404
|
+
GET_KVSTORE_BY_ADDRESS_AND_KEY_QUERY(),
|
|
405
|
+
{ address: address.toLowerCase(), key }
|
|
406
|
+
);
|
|
407
|
+
|
|
408
|
+
if (!kvstores || kvstores.length === 0) {
|
|
409
|
+
throw new Error(`Key "${key}" not found for address ${address}`);
|
|
315
410
|
}
|
|
411
|
+
|
|
412
|
+
const value = kvstores[0].value;
|
|
413
|
+
|
|
414
|
+
return value;
|
|
316
415
|
}
|
|
317
416
|
|
|
318
417
|
/**
|
|
319
|
-
* Gets the URL value of the given entity, and
|
|
418
|
+
* Gets the URL value of the given entity, and verifies its hash.
|
|
320
419
|
*
|
|
420
|
+
* @param {ChainId} chainId Network in which the KVStore is deployed
|
|
321
421
|
* @param {string} address Address from which to get the URL value.
|
|
322
|
-
* @param {string} urlKey
|
|
323
|
-
* @returns {string} URL value for the given address if exists, and the content is valid
|
|
324
|
-
*
|
|
422
|
+
* @param {string} urlKey Configurable URL key. `url` by default.
|
|
423
|
+
* @returns {Promise<string>} URL value for the given address if it exists, and the content is valid
|
|
325
424
|
*
|
|
326
425
|
* **Code example**
|
|
327
426
|
*
|
|
328
427
|
* ```ts
|
|
329
|
-
* import {
|
|
330
|
-
* import { KVStoreClient } from '@human-protocol/sdk';
|
|
428
|
+
* import { ChainId, KVStoreUtils } from '@human-protocol/sdk';
|
|
331
429
|
*
|
|
332
|
-
* const
|
|
430
|
+
* const chainId = ChainId.POLYGON_AMOY;
|
|
431
|
+
* const address = '0x1234567890123456789012345678901234567890';
|
|
333
432
|
*
|
|
334
|
-
* const
|
|
335
|
-
*
|
|
336
|
-
*
|
|
337
|
-
* const url = await kvstoreClient.getFileUrlAndVerifyHash('0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266');
|
|
338
|
-
* const linkedinUrl = await kvstoreClient.getFileUrlAndVerifyHash(
|
|
339
|
-
* '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
|
|
340
|
-
* 'linkedin_url'
|
|
341
|
-
* );
|
|
433
|
+
* const url = await KVStoreUtils.getFileUrlAndVerifyHash(chainId, address);
|
|
434
|
+
* console.log(url);
|
|
342
435
|
* ```
|
|
343
436
|
*/
|
|
344
|
-
public async getFileUrlAndVerifyHash(
|
|
437
|
+
public static async getFileUrlAndVerifyHash(
|
|
438
|
+
chainId: ChainId,
|
|
345
439
|
address: string,
|
|
346
440
|
urlKey = 'url'
|
|
347
441
|
): Promise<string> {
|
|
@@ -352,7 +446,7 @@ export class KVStoreClient extends BaseEthersClient {
|
|
|
352
446
|
hash = '';
|
|
353
447
|
|
|
354
448
|
try {
|
|
355
|
-
url = await this.
|
|
449
|
+
url = await this.get(chainId, address, urlKey);
|
|
356
450
|
} catch (e) {
|
|
357
451
|
if (e instanceof Error) throw Error(`Failed to get URL: ${e.message}`);
|
|
358
452
|
}
|
|
@@ -363,7 +457,7 @@ export class KVStoreClient extends BaseEthersClient {
|
|
|
363
457
|
}
|
|
364
458
|
|
|
365
459
|
try {
|
|
366
|
-
hash = await this.
|
|
460
|
+
hash = await this.get(chainId, address, hashKey);
|
|
367
461
|
} catch (e) {
|
|
368
462
|
if (e instanceof Error) throw Error(`Failed to get Hash: ${e.message}`);
|
|
369
463
|
}
|
|
@@ -379,28 +473,30 @@ export class KVStoreClient extends BaseEthersClient {
|
|
|
379
473
|
}
|
|
380
474
|
|
|
381
475
|
/**
|
|
382
|
-
* Gets the public key of the given entity, and
|
|
476
|
+
* Gets the public key of the given entity, and verifies its hash.
|
|
383
477
|
*
|
|
478
|
+
* @param {ChainId} chainId Network in which the KVStore is deployed
|
|
384
479
|
* @param {string} address Address from which to get the public key.
|
|
385
|
-
* @returns {string} Public key for the given address if exists, and the content is valid
|
|
386
|
-
*
|
|
480
|
+
* @returns {Promise<string>} Public key for the given address if it exists, and the content is valid
|
|
387
481
|
*
|
|
388
482
|
* **Code example**
|
|
389
483
|
*
|
|
390
484
|
* ```ts
|
|
391
|
-
* import {
|
|
392
|
-
* import { KVStoreClient } from '@human-protocol/sdk';
|
|
393
|
-
*
|
|
394
|
-
* const rpcUrl = 'YOUR_RPC_URL';
|
|
485
|
+
* import { ChainId, KVStoreUtils } from '@human-protocol/sdk';
|
|
395
486
|
*
|
|
396
|
-
* const
|
|
397
|
-
* const
|
|
487
|
+
* const chainId = ChainId.POLYGON_AMOY;
|
|
488
|
+
* const address = '0x1234567890123456789012345678901234567890';
|
|
398
489
|
*
|
|
399
|
-
* const publicKey = await
|
|
490
|
+
* const publicKey = await KVStoreUtils.getPublicKey(chainId, address);
|
|
491
|
+
* console.log(publicKey);
|
|
400
492
|
* ```
|
|
401
493
|
*/
|
|
402
|
-
public async getPublicKey(
|
|
494
|
+
public static async getPublicKey(
|
|
495
|
+
chainId: ChainId,
|
|
496
|
+
address: string
|
|
497
|
+
): Promise<string> {
|
|
403
498
|
const publicKeyUrl = await this.getFileUrlAndVerifyHash(
|
|
499
|
+
chainId,
|
|
404
500
|
address,
|
|
405
501
|
KVStoreKeys.publicKey
|
|
406
502
|
);
|
|
@@ -414,112 +510,3 @@ export class KVStoreClient extends BaseEthersClient {
|
|
|
414
510
|
return publicKey;
|
|
415
511
|
}
|
|
416
512
|
}
|
|
417
|
-
|
|
418
|
-
/**
|
|
419
|
-
* ## Introduction
|
|
420
|
-
*
|
|
421
|
-
* Utility class for KVStore-related operations.
|
|
422
|
-
*
|
|
423
|
-
* ## Installation
|
|
424
|
-
*
|
|
425
|
-
* ### npm
|
|
426
|
-
* ```bash
|
|
427
|
-
* npm install @human-protocol/sdk
|
|
428
|
-
* ```
|
|
429
|
-
*
|
|
430
|
-
* ### yarn
|
|
431
|
-
* ```bash
|
|
432
|
-
* yarn install @human-protocol/sdk
|
|
433
|
-
* ```
|
|
434
|
-
*
|
|
435
|
-
* ## Code example
|
|
436
|
-
*
|
|
437
|
-
* ### Signer
|
|
438
|
-
*
|
|
439
|
-
* **Using private key (backend)**
|
|
440
|
-
*
|
|
441
|
-
* ```ts
|
|
442
|
-
* import { ChainId, KVStoreUtils } from '@human-protocol/sdk';
|
|
443
|
-
*
|
|
444
|
-
* const KVStoreAddresses = new KVStoreUtils.getData({
|
|
445
|
-
* network: ChainId.POLYGON_AMOY
|
|
446
|
-
* });
|
|
447
|
-
* ```
|
|
448
|
-
*/
|
|
449
|
-
export class KVStoreUtils {
|
|
450
|
-
/**
|
|
451
|
-
* This function returns the KVStore data for a given address.
|
|
452
|
-
*
|
|
453
|
-
* > This uses Subgraph
|
|
454
|
-
*
|
|
455
|
-
* **Input parameters**
|
|
456
|
-
*
|
|
457
|
-
* ```ts
|
|
458
|
-
* enum ChainId {
|
|
459
|
-
* ALL = -1,
|
|
460
|
-
* MAINNET = 1,
|
|
461
|
-
* RINKEBY = 4,
|
|
462
|
-
* GOERLI = 5,
|
|
463
|
-
* BSC_MAINNET = 56,
|
|
464
|
-
* BSC_TESTNET = 97,
|
|
465
|
-
* POLYGON = 137,
|
|
466
|
-
* POLYGON_MUMBAI = 80001,
|
|
467
|
-
* POLYGON_AMOY = 80002,
|
|
468
|
-
* MOONBEAM = 1284,
|
|
469
|
-
* MOONBASE_ALPHA = 1287,
|
|
470
|
-
* AVALANCHE = 43114,
|
|
471
|
-
* AVALANCHE_TESTNET = 43113,
|
|
472
|
-
* CELO = 42220,
|
|
473
|
-
* CELO_ALFAJORES = 44787,
|
|
474
|
-
* LOCALHOST = 1338,
|
|
475
|
-
* }
|
|
476
|
-
* ```
|
|
477
|
-
*
|
|
478
|
-
* ```ts
|
|
479
|
-
* interface IKVStore {
|
|
480
|
-
* key: string;
|
|
481
|
-
* value: string;
|
|
482
|
-
* }
|
|
483
|
-
* ```
|
|
484
|
-
*
|
|
485
|
-
* @param {ChainId} chainId Network in which the KVStore is deployed
|
|
486
|
-
* @param {string} address Address of the KVStore
|
|
487
|
-
* @returns {Promise<IKVStore[]>} KVStore data
|
|
488
|
-
*
|
|
489
|
-
* **Code example**
|
|
490
|
-
*
|
|
491
|
-
* ```ts
|
|
492
|
-
* import { ChainId, KVStoreUtils } from '@human-protocol/sdk';
|
|
493
|
-
*
|
|
494
|
-
* const kvStoreData = await KVStoreUtils.getKVStoreData(ChainId.POLYGON_AMOY, "0x1234567890123456789012345678901234567890");
|
|
495
|
-
* console.log(kvStoreData);
|
|
496
|
-
* ```
|
|
497
|
-
*/
|
|
498
|
-
public static async getKVStoreData(
|
|
499
|
-
chainId: ChainId,
|
|
500
|
-
address: string
|
|
501
|
-
): Promise<IKVStore[]> {
|
|
502
|
-
const networkData = NETWORKS[chainId];
|
|
503
|
-
|
|
504
|
-
if (!networkData) {
|
|
505
|
-
throw ErrorUnsupportedChainID;
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
if (address && !ethers.isAddress(address)) {
|
|
509
|
-
throw ErrorInvalidAddress;
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
const { kvstores } = await gqlFetch<{ kvstores: KVStoreData[] }>(
|
|
513
|
-
getSubgraphUrl(networkData),
|
|
514
|
-
GET_KVSTORE_BY_ADDRESS_QUERY(),
|
|
515
|
-
{ address: address.toLowerCase() }
|
|
516
|
-
);
|
|
517
|
-
|
|
518
|
-
const kvStoreData = kvstores.map((item) => ({
|
|
519
|
-
key: item.key,
|
|
520
|
-
value: item.value,
|
|
521
|
-
}));
|
|
522
|
-
|
|
523
|
-
return kvStoreData || [];
|
|
524
|
-
}
|
|
525
|
-
}
|