@human-protocol/sdk 1.1.3 → 1.1.6
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/README.md +80 -1
- package/dist/constants.d.ts +52 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +222 -0
- package/dist/decorators.d.ts +2 -0
- package/dist/decorators.d.ts.map +1 -0
- package/dist/decorators.js +17 -0
- package/dist/encryption.d.ts +84 -0
- package/dist/encryption.d.ts.map +1 -0
- package/dist/encryption.js +202 -0
- package/dist/enums.d.ts +17 -0
- package/dist/enums.d.ts.map +1 -0
- package/dist/enums.js +20 -0
- package/dist/error.d.ts +196 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.js +229 -0
- package/dist/escrow.d.ts +201 -0
- package/dist/escrow.d.ts.map +1 -0
- package/dist/escrow.js +651 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +29 -0
- package/dist/interfaces.d.ts +42 -0
- package/dist/interfaces.d.ts.map +1 -0
- package/dist/interfaces.js +2 -0
- package/dist/kvstore.d.ts +51 -0
- package/dist/kvstore.d.ts.map +1 -0
- package/dist/kvstore.js +135 -0
- package/dist/queries.d.ts +5 -0
- package/dist/queries.d.ts.map +1 -0
- package/dist/queries.js +19 -0
- package/dist/staking.d.ts +130 -0
- package/dist/staking.d.ts.map +1 -0
- package/dist/staking.js +409 -0
- package/dist/storage.d.ts +49 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +171 -0
- package/dist/types.d.ts +131 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +35 -0
- package/dist/utils.d.ts +32 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +99 -0
- package/package.json +4 -1
- package/src/constants.ts +25 -6
- package/src/encryption.ts +223 -0
- package/src/error.ts +2 -4
- package/src/escrow.ts +120 -74
- package/src/index.ts +6 -12
- package/src/interfaces.ts +10 -13
- package/src/kvstore.ts +51 -14
- package/src/queries.ts +15 -7
- package/src/staking.ts +61 -24
- package/src/storage.ts +15 -3
- package/src/types.ts +8 -0
- package/src/init.ts +0 -45
package/src/escrow.ts
CHANGED
|
@@ -1,19 +1,26 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import { Provider } from '@ethersproject/abstract-provider';
|
|
3
|
+
import { Network } from '@ethersproject/networks';
|
|
1
4
|
import {
|
|
2
|
-
HMToken__factory,
|
|
3
|
-
HMToken,
|
|
4
5
|
Escrow,
|
|
5
6
|
EscrowFactory,
|
|
6
7
|
EscrowFactory__factory,
|
|
7
8
|
Escrow__factory,
|
|
9
|
+
HMToken,
|
|
10
|
+
HMToken__factory,
|
|
8
11
|
} from '@human-protocol/core/typechain-types';
|
|
9
|
-
import { BigNumber, ContractReceipt, Signer, ethers
|
|
10
|
-
import {
|
|
12
|
+
import { BigNumber, ContractReceipt, Signer, ethers } from 'ethers';
|
|
13
|
+
import { DEFAULT_TX_ID, NETWORKS } from './constants';
|
|
14
|
+
import { requiresSigner } from './decorators';
|
|
15
|
+
import { ChainId } from './enums';
|
|
11
16
|
import {
|
|
12
17
|
ErrorAmountMustBeGreaterThanZero,
|
|
13
18
|
ErrorAmountsCannotBeEmptyArray,
|
|
14
19
|
ErrorEscrowAddressIsNotProvidedByFactory,
|
|
15
20
|
ErrorEscrowDoesNotHaveEnoughBalance,
|
|
16
21
|
ErrorHashIsEmptyString,
|
|
22
|
+
ErrorProviderDoesNotExist,
|
|
23
|
+
ErrorUnsupportedChainID,
|
|
17
24
|
ErrorInvalidAddress,
|
|
18
25
|
ErrorInvalidEscrowAddressProvided,
|
|
19
26
|
ErrorInvalidRecordingOracleAddressProvided,
|
|
@@ -24,45 +31,67 @@ import {
|
|
|
24
31
|
ErrorListOfHandlersCannotBeEmpty,
|
|
25
32
|
ErrorRecipientAndAmountsMustBeSameLength,
|
|
26
33
|
ErrorRecipientCannotBeEmptyArray,
|
|
27
|
-
ErrorSigner,
|
|
28
34
|
ErrorTotalFeeMustBeLessThanHundred,
|
|
29
35
|
ErrorUrlIsEmptyString,
|
|
30
36
|
InvalidEthereumAddressError,
|
|
31
37
|
} from './error';
|
|
32
|
-
import {
|
|
33
|
-
IClientParams,
|
|
34
|
-
IEscrowConfig,
|
|
35
|
-
IEscrowsFilter,
|
|
36
|
-
ILauncherEscrowsResult,
|
|
37
|
-
} from './interfaces';
|
|
38
|
-
import { gqlFetch, isValidUrl, throwError } from './utils';
|
|
39
|
-
import { DEFAULT_TX_ID } from './constants';
|
|
38
|
+
import { IEscrowConfig, IEscrowsFilter } from './interfaces';
|
|
40
39
|
import {
|
|
41
40
|
RAW_LAUNCHED_ESCROWS_FILTERED_QUERY,
|
|
42
41
|
RAW_LAUNCHED_ESCROWS_QUERY,
|
|
43
42
|
} from './queries';
|
|
44
43
|
import { EscrowStatus, NetworkData } from './types';
|
|
45
|
-
import {
|
|
44
|
+
import { gqlFetch, isValidUrl, throwError } from './utils';
|
|
46
45
|
|
|
47
|
-
export
|
|
46
|
+
export class EscrowClient {
|
|
48
47
|
private escrowFactoryContract: EscrowFactory;
|
|
49
48
|
private escrowContract?: Escrow;
|
|
50
49
|
private signerOrProvider: Signer | Provider;
|
|
51
50
|
public network: NetworkData;
|
|
52
51
|
|
|
53
52
|
/**
|
|
54
|
-
* **
|
|
53
|
+
* **EscrowClient constructor**
|
|
55
54
|
*
|
|
56
|
-
*
|
|
55
|
+
* @param {Signer | Provider} signerOrProvider - The Signer or Provider object to interact with the Ethereum network
|
|
56
|
+
* @param {NetworkData} network - The network information required to connect to the Escrow contract
|
|
57
57
|
*/
|
|
58
|
-
constructor(
|
|
59
|
-
this.network = clientParams.network;
|
|
60
|
-
|
|
58
|
+
constructor(signerOrProvider: Signer | Provider, network: NetworkData) {
|
|
61
59
|
this.escrowFactoryContract = EscrowFactory__factory.connect(
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
network.factoryAddress,
|
|
61
|
+
signerOrProvider
|
|
64
62
|
);
|
|
65
|
-
this.
|
|
63
|
+
this.network = network;
|
|
64
|
+
this.signerOrProvider = signerOrProvider;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Creates an instance of EscrowClient from a Signer or Provider.
|
|
69
|
+
*
|
|
70
|
+
* @param {Signer | Provider} signerOrProvider - The Signer or Provider object to interact with the Ethereum network
|
|
71
|
+
* @returns {Promise<EscrowClient>} - An instance of EscrowClient
|
|
72
|
+
* @throws {ErrorProviderDoesNotExist} - Thrown if the provider does not exist for the provided Signer
|
|
73
|
+
* @throws {ErrorUnsupportedChainID} - Thrown if the network's chainId is not supported
|
|
74
|
+
*/
|
|
75
|
+
public static async build(signerOrProvider: Signer | Provider) {
|
|
76
|
+
let network: Network;
|
|
77
|
+
if (Signer.isSigner(signerOrProvider)) {
|
|
78
|
+
if (!signerOrProvider.provider) {
|
|
79
|
+
throw ErrorProviderDoesNotExist;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
network = await signerOrProvider.provider.getNetwork();
|
|
83
|
+
} else {
|
|
84
|
+
network = await signerOrProvider.getNetwork();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const chainId: ChainId = network.chainId;
|
|
88
|
+
const networkData = NETWORKS[chainId];
|
|
89
|
+
|
|
90
|
+
if (!networkData) {
|
|
91
|
+
throw ErrorUnsupportedChainID;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return new EscrowClient(signerOrProvider, networkData);
|
|
66
95
|
}
|
|
67
96
|
|
|
68
97
|
/**
|
|
@@ -96,11 +125,15 @@ export default class EscrowClient {
|
|
|
96
125
|
)
|
|
97
126
|
).wait();
|
|
98
127
|
|
|
99
|
-
|
|
128
|
+
const event = result.events?.find(({ topics }) =>
|
|
129
|
+
topics.includes(ethers.utils.id('Launched(address,address)'))
|
|
130
|
+
)?.args;
|
|
131
|
+
|
|
132
|
+
if (!event) {
|
|
100
133
|
throw ErrorLaunchedEventIsNotEmitted;
|
|
101
134
|
}
|
|
102
135
|
|
|
103
|
-
return
|
|
136
|
+
return event.escrow;
|
|
104
137
|
} catch (e: any) {
|
|
105
138
|
return throwError(e);
|
|
106
139
|
}
|
|
@@ -125,7 +158,7 @@ export default class EscrowClient {
|
|
|
125
158
|
recordingOracleFee,
|
|
126
159
|
reputationOracleFee,
|
|
127
160
|
manifestUrl,
|
|
128
|
-
|
|
161
|
+
manifestHash,
|
|
129
162
|
} = escrowConfig;
|
|
130
163
|
|
|
131
164
|
if (!ethers.utils.isAddress(recordingOracle)) {
|
|
@@ -156,7 +189,7 @@ export default class EscrowClient {
|
|
|
156
189
|
throw ErrorInvalidUrl;
|
|
157
190
|
}
|
|
158
191
|
|
|
159
|
-
if (!
|
|
192
|
+
if (!manifestHash) {
|
|
160
193
|
throw ErrorHashIsEmptyString;
|
|
161
194
|
}
|
|
162
195
|
|
|
@@ -170,16 +203,16 @@ export default class EscrowClient {
|
|
|
170
203
|
this.signerOrProvider
|
|
171
204
|
);
|
|
172
205
|
await this.escrowContract.setup(
|
|
173
|
-
recordingOracle,
|
|
174
206
|
reputationOracle,
|
|
175
|
-
|
|
207
|
+
recordingOracle,
|
|
176
208
|
reputationOracleFee,
|
|
209
|
+
recordingOracleFee,
|
|
177
210
|
manifestUrl,
|
|
178
|
-
|
|
211
|
+
manifestHash
|
|
179
212
|
);
|
|
180
213
|
|
|
181
214
|
return;
|
|
182
|
-
} catch (e
|
|
215
|
+
} catch (e) {
|
|
183
216
|
return throwError(e);
|
|
184
217
|
}
|
|
185
218
|
}
|
|
@@ -209,7 +242,7 @@ export default class EscrowClient {
|
|
|
209
242
|
await this.setup(escrowAddress, escrowConfig);
|
|
210
243
|
|
|
211
244
|
return escrowAddress;
|
|
212
|
-
} catch (e
|
|
245
|
+
} catch (e) {
|
|
213
246
|
return throwError(e);
|
|
214
247
|
}
|
|
215
248
|
}
|
|
@@ -252,7 +285,7 @@ export default class EscrowClient {
|
|
|
252
285
|
await tokenContract.transfer(escrowAddress, amount);
|
|
253
286
|
|
|
254
287
|
return;
|
|
255
|
-
} catch (e
|
|
288
|
+
} catch (e) {
|
|
256
289
|
return throwError(e);
|
|
257
290
|
}
|
|
258
291
|
}
|
|
@@ -301,7 +334,7 @@ export default class EscrowClient {
|
|
|
301
334
|
await this.escrowContract.storeResults(url, hash);
|
|
302
335
|
|
|
303
336
|
return;
|
|
304
|
-
} catch (e
|
|
337
|
+
} catch (e) {
|
|
305
338
|
return throwError(e);
|
|
306
339
|
}
|
|
307
340
|
}
|
|
@@ -330,7 +363,7 @@ export default class EscrowClient {
|
|
|
330
363
|
);
|
|
331
364
|
await this.escrowContract.complete();
|
|
332
365
|
return;
|
|
333
|
-
} catch (e
|
|
366
|
+
} catch (e) {
|
|
334
367
|
return throwError(e);
|
|
335
368
|
}
|
|
336
369
|
}
|
|
@@ -417,7 +450,7 @@ export default class EscrowClient {
|
|
|
417
450
|
DEFAULT_TX_ID
|
|
418
451
|
);
|
|
419
452
|
return;
|
|
420
|
-
} catch (e
|
|
453
|
+
} catch (e) {
|
|
421
454
|
return throwError(e);
|
|
422
455
|
}
|
|
423
456
|
}
|
|
@@ -446,7 +479,7 @@ export default class EscrowClient {
|
|
|
446
479
|
);
|
|
447
480
|
await this.escrowContract.cancel();
|
|
448
481
|
return;
|
|
449
|
-
} catch (e
|
|
482
|
+
} catch (e) {
|
|
450
483
|
return throwError(e);
|
|
451
484
|
}
|
|
452
485
|
}
|
|
@@ -475,7 +508,7 @@ export default class EscrowClient {
|
|
|
475
508
|
);
|
|
476
509
|
await this.escrowContract.abort();
|
|
477
510
|
return;
|
|
478
|
-
} catch (e
|
|
511
|
+
} catch (e) {
|
|
479
512
|
return throwError(e);
|
|
480
513
|
}
|
|
481
514
|
}
|
|
@@ -518,7 +551,7 @@ export default class EscrowClient {
|
|
|
518
551
|
);
|
|
519
552
|
await this.escrowContract.addTrustedHandlers(trustedHandlers);
|
|
520
553
|
return;
|
|
521
|
-
} catch (e
|
|
554
|
+
} catch (e) {
|
|
522
555
|
return throwError(e);
|
|
523
556
|
}
|
|
524
557
|
}
|
|
@@ -545,7 +578,7 @@ export default class EscrowClient {
|
|
|
545
578
|
this.signerOrProvider
|
|
546
579
|
);
|
|
547
580
|
return this.escrowContract.getBalance();
|
|
548
|
-
} catch (e
|
|
581
|
+
} catch (e) {
|
|
549
582
|
return throwError(e);
|
|
550
583
|
}
|
|
551
584
|
}
|
|
@@ -572,7 +605,7 @@ export default class EscrowClient {
|
|
|
572
605
|
this.signerOrProvider
|
|
573
606
|
);
|
|
574
607
|
return this.escrowContract.manifestUrl();
|
|
575
|
-
} catch (e
|
|
608
|
+
} catch (e) {
|
|
576
609
|
return throwError(e);
|
|
577
610
|
}
|
|
578
611
|
}
|
|
@@ -599,6 +632,33 @@ export default class EscrowClient {
|
|
|
599
632
|
this.signerOrProvider
|
|
600
633
|
);
|
|
601
634
|
return this.escrowContract.finalResultsUrl();
|
|
635
|
+
} catch (e) {
|
|
636
|
+
return throwError(e);
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/**
|
|
641
|
+
* Returns the intermediate results file URL.
|
|
642
|
+
*
|
|
643
|
+
* @param {string} escrowAddress - Address of the escrow.
|
|
644
|
+
* @returns {Promise<void>}
|
|
645
|
+
* @throws {Error} - An error object if an error occurred.
|
|
646
|
+
*/
|
|
647
|
+
async getIntermediateResultsUrl(escrowAddress: string): Promise<string> {
|
|
648
|
+
if (!ethers.utils.isAddress(escrowAddress)) {
|
|
649
|
+
throw ErrorInvalidEscrowAddressProvided;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
|
|
653
|
+
throw ErrorEscrowAddressIsNotProvidedByFactory;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
try {
|
|
657
|
+
this.escrowContract = Escrow__factory.connect(
|
|
658
|
+
escrowAddress,
|
|
659
|
+
this.signerOrProvider
|
|
660
|
+
);
|
|
661
|
+
return this.escrowContract.intermediateResultsUrl();
|
|
602
662
|
} catch (e: any) {
|
|
603
663
|
return throwError(e);
|
|
604
664
|
}
|
|
@@ -626,7 +686,7 @@ export default class EscrowClient {
|
|
|
626
686
|
this.signerOrProvider
|
|
627
687
|
);
|
|
628
688
|
return this.escrowContract.token();
|
|
629
|
-
} catch (e
|
|
689
|
+
} catch (e) {
|
|
630
690
|
return throwError(e);
|
|
631
691
|
}
|
|
632
692
|
}
|
|
@@ -653,21 +713,19 @@ export default class EscrowClient {
|
|
|
653
713
|
this.signerOrProvider
|
|
654
714
|
);
|
|
655
715
|
return this.escrowContract.status();
|
|
656
|
-
} catch (e
|
|
716
|
+
} catch (e) {
|
|
657
717
|
return throwError(e);
|
|
658
718
|
}
|
|
659
719
|
}
|
|
660
720
|
|
|
661
721
|
/**
|
|
662
|
-
* Returns the
|
|
722
|
+
* Returns the escrow addresses created by a job requester.
|
|
663
723
|
*
|
|
664
724
|
* @param {IEscrowsFilter} requesterAddress - Address of the requester.
|
|
665
|
-
* @returns {Promise<
|
|
725
|
+
* @returns {Promise<string[]>}
|
|
666
726
|
* @throws {Error} - An error object if an error occurred.
|
|
667
727
|
*/
|
|
668
|
-
async getLaunchedEscrows(
|
|
669
|
-
requesterAddress: string
|
|
670
|
-
): Promise<ILauncherEscrowsResult[]> {
|
|
728
|
+
async getLaunchedEscrows(requesterAddress: string): Promise<string[]> {
|
|
671
729
|
if (!ethers.utils.isAddress(requesterAddress)) {
|
|
672
730
|
throw ErrorInvalidAddress;
|
|
673
731
|
}
|
|
@@ -675,51 +733,39 @@ export default class EscrowClient {
|
|
|
675
733
|
try {
|
|
676
734
|
const { data } = await gqlFetch(
|
|
677
735
|
this.network.subgraphUrl,
|
|
678
|
-
RAW_LAUNCHED_ESCROWS_QUERY()
|
|
679
|
-
{
|
|
680
|
-
address: requesterAddress,
|
|
681
|
-
}
|
|
736
|
+
RAW_LAUNCHED_ESCROWS_QUERY(requesterAddress)
|
|
682
737
|
);
|
|
683
738
|
|
|
684
|
-
return data;
|
|
739
|
+
return data.data.launchedEscrows.map((escrow: any) => escrow.id);
|
|
685
740
|
} catch (e: any) {
|
|
686
741
|
return throwError(e);
|
|
687
742
|
}
|
|
688
743
|
}
|
|
689
744
|
|
|
690
745
|
/**
|
|
691
|
-
* Returns the
|
|
746
|
+
* Returns the escrow addresses based on a specified filter.
|
|
692
747
|
*
|
|
693
|
-
* @param {
|
|
694
|
-
* @
|
|
695
|
-
* @returns {Promise<void>}
|
|
748
|
+
* @param {IEscrowsFilter} filter - Filter parameters.
|
|
749
|
+
* @returns {Promise<string[]>}
|
|
696
750
|
* @throws {Error} - An error object if an error occurred.
|
|
697
751
|
*/
|
|
698
|
-
async getEscrowsFiltered(
|
|
699
|
-
|
|
700
|
-
filter: IEscrowsFilter
|
|
701
|
-
): Promise<ILauncherEscrowsResult[]> {
|
|
702
|
-
if (!ethers.utils.isAddress(escrowAddress)) {
|
|
752
|
+
async getEscrowsFiltered(filter: IEscrowsFilter): Promise<string[]> {
|
|
753
|
+
if (filter?.address && !ethers.utils.isAddress(filter?.address)) {
|
|
703
754
|
throw ErrorInvalidAddress;
|
|
704
755
|
}
|
|
705
756
|
|
|
706
|
-
if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
|
|
707
|
-
throw ErrorEscrowAddressIsNotProvidedByFactory;
|
|
708
|
-
}
|
|
709
|
-
|
|
710
757
|
try {
|
|
711
758
|
const { data } = await gqlFetch(
|
|
712
759
|
this.network.subgraphUrl,
|
|
713
|
-
RAW_LAUNCHED_ESCROWS_FILTERED_QUERY(
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
}
|
|
760
|
+
RAW_LAUNCHED_ESCROWS_FILTERED_QUERY(
|
|
761
|
+
filter.address,
|
|
762
|
+
filter.status,
|
|
763
|
+
filter.from,
|
|
764
|
+
filter.to
|
|
765
|
+
)
|
|
720
766
|
);
|
|
721
767
|
|
|
722
|
-
return data;
|
|
768
|
+
return data.data.launchedEscrows.map((escrow: any) => escrow.id);
|
|
723
769
|
} catch (e: any) {
|
|
724
770
|
return throwError(e);
|
|
725
771
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,17 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import EscrowClient from './escrow';
|
|
1
|
+
import { StakingClient } from './staking';
|
|
2
|
+
import { StorageClient } from './storage';
|
|
3
|
+
import { KVStoreClient } from './kvstore';
|
|
4
|
+
import { EscrowClient } from './escrow';
|
|
6
5
|
|
|
7
6
|
export * from './constants';
|
|
8
7
|
export * from './types';
|
|
9
8
|
export * from './enums';
|
|
9
|
+
export * from './interfaces';
|
|
10
10
|
|
|
11
|
-
export {
|
|
12
|
-
InitClient,
|
|
13
|
-
StakingClient,
|
|
14
|
-
StorageClient,
|
|
15
|
-
KVStoreClient,
|
|
16
|
-
EscrowClient,
|
|
17
|
-
};
|
|
11
|
+
export { StakingClient, StorageClient, KVStoreClient, EscrowClient };
|
package/src/interfaces.ts
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
import { BigNumber
|
|
2
|
-
import {
|
|
3
|
-
import { NetworkData } from './types';
|
|
4
|
-
|
|
5
|
-
export interface IClientParams {
|
|
6
|
-
signerOrProvider: Signer | Provider;
|
|
7
|
-
network: NetworkData;
|
|
8
|
-
}
|
|
1
|
+
import { BigNumber } from 'ethers';
|
|
2
|
+
import { EscrowStatus } from './types';
|
|
9
3
|
|
|
10
4
|
export interface IAllocation {
|
|
11
5
|
escrowAddress: string;
|
|
@@ -29,9 +23,9 @@ export interface IStaker {
|
|
|
29
23
|
}
|
|
30
24
|
|
|
31
25
|
export interface IEscrowsFilter {
|
|
32
|
-
address
|
|
26
|
+
address?: string;
|
|
33
27
|
role?: number;
|
|
34
|
-
status?:
|
|
28
|
+
status?: EscrowStatus;
|
|
35
29
|
from?: Date;
|
|
36
30
|
to?: Date;
|
|
37
31
|
}
|
|
@@ -42,9 +36,12 @@ export interface IEscrowConfig {
|
|
|
42
36
|
recordingOracleFee: BigNumber;
|
|
43
37
|
reputationOracleFee: BigNumber;
|
|
44
38
|
manifestUrl: string;
|
|
45
|
-
|
|
39
|
+
manifestHash: string;
|
|
46
40
|
}
|
|
47
41
|
|
|
48
|
-
export interface
|
|
49
|
-
|
|
42
|
+
export interface IKeyPair {
|
|
43
|
+
privateKey: string;
|
|
44
|
+
publicKey: string;
|
|
45
|
+
passphrase: string;
|
|
46
|
+
revocationCertificate?: string;
|
|
50
47
|
}
|
package/src/kvstore.ts
CHANGED
|
@@ -1,33 +1,69 @@
|
|
|
1
|
+
import { Provider } from '@ethersproject/abstract-provider';
|
|
2
|
+
import { Network } from '@ethersproject/networks';
|
|
1
3
|
import {
|
|
2
4
|
KVStore,
|
|
3
5
|
KVStore__factory,
|
|
4
6
|
} from '@human-protocol/core/typechain-types';
|
|
5
7
|
import { Signer, ethers } from 'ethers';
|
|
6
|
-
import {
|
|
8
|
+
import { NETWORKS } from './constants';
|
|
9
|
+
import { requiresSigner } from './decorators';
|
|
10
|
+
import { ChainId } from './enums';
|
|
7
11
|
import {
|
|
8
12
|
ErrorInvalidAddress,
|
|
9
13
|
ErrorKVStoreArrayLength,
|
|
10
14
|
ErrorKVStoreEmptyKey,
|
|
15
|
+
ErrorProviderDoesNotExist,
|
|
11
16
|
ErrorSigner,
|
|
17
|
+
ErrorUnsupportedChainID,
|
|
12
18
|
} from './error';
|
|
13
|
-
import {
|
|
14
|
-
import { requiresSigner } from './decorators';
|
|
19
|
+
import { NetworkData } from './types';
|
|
15
20
|
|
|
16
|
-
export
|
|
21
|
+
export class KVStoreClient {
|
|
17
22
|
private contract: KVStore;
|
|
18
23
|
private signerOrProvider: Signer | Provider;
|
|
19
24
|
|
|
20
25
|
/**
|
|
21
|
-
* **
|
|
26
|
+
* **KVStoreClient constructor**
|
|
22
27
|
*
|
|
23
|
-
*
|
|
28
|
+
* @param {Signer | Provider} signerOrProvider - The Signer or Provider object to interact with the Ethereum network
|
|
29
|
+
* @param {NetworkData} network - The network information required to connect to the KVStore contract
|
|
24
30
|
*/
|
|
25
|
-
constructor(
|
|
31
|
+
constructor(signerOrProvider: Signer | Provider, network: NetworkData) {
|
|
26
32
|
this.contract = KVStore__factory.connect(
|
|
27
|
-
|
|
28
|
-
|
|
33
|
+
network.factoryAddress,
|
|
34
|
+
signerOrProvider
|
|
29
35
|
);
|
|
30
|
-
this.signerOrProvider =
|
|
36
|
+
this.signerOrProvider = signerOrProvider;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Creates an instance of KVStoreClient from a Signer or Provider.
|
|
41
|
+
*
|
|
42
|
+
* @param {Signer | Provider} signerOrProvider - The Signer or Provider object to interact with the Ethereum network
|
|
43
|
+
* @returns {Promise<KVStoreClient>} - An instance of KVStoreClient
|
|
44
|
+
* @throws {ErrorProviderDoesNotExist} - Thrown if the provider does not exist for the provided Signer
|
|
45
|
+
* @throws {ErrorUnsupportedChainID} - Thrown if the network's chainId is not supported
|
|
46
|
+
*/
|
|
47
|
+
public static async build(signerOrProvider: Signer | Provider) {
|
|
48
|
+
let network: Network;
|
|
49
|
+
if (Signer.isSigner(signerOrProvider)) {
|
|
50
|
+
if (!signerOrProvider.provider) {
|
|
51
|
+
throw ErrorProviderDoesNotExist;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
network = await signerOrProvider.provider.getNetwork();
|
|
55
|
+
} else {
|
|
56
|
+
network = await signerOrProvider.getNetwork();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const chainId: ChainId = network.chainId;
|
|
60
|
+
const networkData = NETWORKS[chainId];
|
|
61
|
+
|
|
62
|
+
if (!networkData) {
|
|
63
|
+
throw ErrorUnsupportedChainID;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return new KVStoreClient(signerOrProvider, networkData);
|
|
31
67
|
}
|
|
32
68
|
|
|
33
69
|
/**
|
|
@@ -39,7 +75,7 @@ export default class KVStoreClient {
|
|
|
39
75
|
* @throws {Error} - An error object if an error occurred
|
|
40
76
|
*/
|
|
41
77
|
@requiresSigner
|
|
42
|
-
public async set(key: string, value: string) {
|
|
78
|
+
public async set(key: string, value: string): Promise<void> {
|
|
43
79
|
if (!Signer.isSigner(this.signerOrProvider)) throw ErrorSigner;
|
|
44
80
|
if (key === '') throw ErrorKVStoreEmptyKey;
|
|
45
81
|
try {
|
|
@@ -58,7 +94,7 @@ export default class KVStoreClient {
|
|
|
58
94
|
* @throws {Error} - An error object if an error occurred
|
|
59
95
|
*/
|
|
60
96
|
@requiresSigner
|
|
61
|
-
public async setBulk(keys: string[], values: string[]) {
|
|
97
|
+
public async setBulk(keys: string[], values: string[]): Promise<void> {
|
|
62
98
|
if (!Signer.isSigner(this.signerOrProvider)) throw ErrorSigner;
|
|
63
99
|
if (keys.length !== values.length) throw ErrorKVStoreArrayLength;
|
|
64
100
|
if (keys.includes('')) throw ErrorKVStoreEmptyKey;
|
|
@@ -76,10 +112,10 @@ export default class KVStoreClient {
|
|
|
76
112
|
*
|
|
77
113
|
* @param {string} address - The Ethereum address associated with the key-value pair
|
|
78
114
|
* @param {string} key - The key of the key-value pair to get
|
|
79
|
-
* @returns {string} - The value of the key-value pair if it exists
|
|
115
|
+
* @returns {Promise<string>} - The value of the key-value pair if it exists
|
|
80
116
|
* @throws {Error} - An error object if an error occurred
|
|
81
117
|
*/
|
|
82
|
-
public async get(address: string, key: string) {
|
|
118
|
+
public async get(address: string, key: string): Promise<string> {
|
|
83
119
|
if (key === '') throw ErrorKVStoreEmptyKey;
|
|
84
120
|
if (!ethers.utils.isAddress(address)) throw ErrorInvalidAddress;
|
|
85
121
|
|
|
@@ -88,6 +124,7 @@ export default class KVStoreClient {
|
|
|
88
124
|
return result;
|
|
89
125
|
} catch (e) {
|
|
90
126
|
if (e instanceof Error) throw Error(`Failed to get value: ${e.message}`);
|
|
127
|
+
return e;
|
|
91
128
|
}
|
|
92
129
|
}
|
|
93
130
|
}
|
package/src/queries.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { EscrowStatus } from './types';
|
|
2
|
+
|
|
1
3
|
export const RAW_REWARDS_QUERY = (slasherAddress: string) => `{
|
|
2
4
|
rewardAddedEvents(id: "${slasherAddress}") {
|
|
3
5
|
escrow,
|
|
@@ -5,14 +7,20 @@ export const RAW_REWARDS_QUERY = (slasherAddress: string) => `{
|
|
|
5
7
|
}
|
|
6
8
|
}`;
|
|
7
9
|
|
|
8
|
-
export const RAW_LAUNCHED_ESCROWS_QUERY = () => `{
|
|
9
|
-
launchedEscrows(where: { from: $
|
|
10
|
+
export const RAW_LAUNCHED_ESCROWS_QUERY = (requesterAddress: string) => `{
|
|
11
|
+
launchedEscrows(where: { from: ${requesterAddress} }) {
|
|
10
12
|
id
|
|
11
13
|
}
|
|
12
14
|
}`;
|
|
13
15
|
|
|
14
|
-
export const RAW_LAUNCHED_ESCROWS_FILTERED_QUERY = (
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
export const RAW_LAUNCHED_ESCROWS_FILTERED_QUERY = (
|
|
17
|
+
address?: string,
|
|
18
|
+
status?: EscrowStatus,
|
|
19
|
+
from?: Date,
|
|
20
|
+
to?: Date
|
|
21
|
+
) =>
|
|
22
|
+
`{ launchedEscrows(where: { ${address ? 'from: "' + address + '", ' : ''}${
|
|
23
|
+
status ? 'status: "' + EscrowStatus[status] + '", ' : ''
|
|
24
|
+
}${from ? 'timestamp_gte: "' + from?.valueOf() + '", ' : ''}${
|
|
25
|
+
to ? 'timestamp_lte: "' + to?.valueOf() + '"' : ''
|
|
26
|
+
}}) { id }}`;
|