@human-protocol/sdk 1.1.2 → 1.1.5

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 (66) hide show
  1. package/README.md +1 -1
  2. package/dist/constants.d.ts +46 -0
  3. package/dist/constants.d.ts.map +1 -0
  4. package/dist/constants.js +203 -0
  5. package/dist/decorators.d.ts +2 -0
  6. package/dist/decorators.d.ts.map +1 -0
  7. package/dist/decorators.js +17 -0
  8. package/dist/enums.d.ts +17 -0
  9. package/dist/enums.d.ts.map +1 -0
  10. package/dist/enums.js +20 -0
  11. package/dist/error.d.ts +196 -0
  12. package/dist/error.d.ts.map +1 -0
  13. package/dist/error.js +229 -0
  14. package/dist/escrow.d.ts +184 -0
  15. package/dist/escrow.d.ts.map +1 -0
  16. package/dist/escrow.js +614 -0
  17. package/dist/index.d.ts +10 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +33 -0
  20. package/dist/init.d.ts +13 -0
  21. package/dist/init.d.ts.map +1 -0
  22. package/dist/init.js +35 -0
  23. package/dist/interfaces.d.ts +44 -0
  24. package/dist/interfaces.d.ts.map +1 -0
  25. package/dist/interfaces.js +2 -0
  26. package/dist/kvstore.d.ts +40 -0
  27. package/dist/kvstore.d.ts.map +1 -0
  28. package/dist/kvstore.js +106 -0
  29. package/dist/queries.d.ts +4 -0
  30. package/dist/queries.d.ts.map +1 -0
  31. package/dist/queries.js +22 -0
  32. package/dist/staking.d.ts +121 -0
  33. package/dist/staking.d.ts.map +1 -0
  34. package/dist/staking.js +381 -0
  35. package/dist/storage.d.ts +48 -0
  36. package/dist/storage.d.ts.map +1 -0
  37. package/dist/storage.js +165 -0
  38. package/dist/types.d.ts +123 -0
  39. package/dist/types.d.ts.map +1 -0
  40. package/dist/types.js +35 -0
  41. package/dist/utils.d.ts +32 -0
  42. package/dist/utils.d.ts.map +1 -0
  43. package/dist/utils.js +99 -0
  44. package/package.json +7 -7
  45. package/src/constants.ts +221 -4
  46. package/src/decorators.ts +21 -0
  47. package/src/enums.ts +16 -0
  48. package/src/error.ts +295 -18
  49. package/src/escrow.ts +785 -0
  50. package/src/index.ts +14 -1
  51. package/src/init.ts +45 -0
  52. package/src/interfaces.ts +50 -0
  53. package/src/kvstore.ts +93 -0
  54. package/src/queries.ts +18 -0
  55. package/src/staking.ts +422 -0
  56. package/src/storage.ts +160 -131
  57. package/src/types.ts +36 -586
  58. package/src/utils.ts +80 -143
  59. package/example/simple-existing-job.ts +0 -86
  60. package/example/simple-new-job-public.ts +0 -74
  61. package/example/simple-new-job.ts +0 -72
  62. package/src/job.ts +0 -1067
  63. package/src/logger.ts +0 -29
  64. package/test/job.test.ts +0 -817
  65. package/test/utils/constants.ts +0 -30
  66. package/test/utils/manifest.ts +0 -33
package/src/index.ts CHANGED
@@ -1,4 +1,17 @@
1
- export { Job } from './job';
1
+ import InitClient from './init';
2
+ import StakingClient from './staking';
3
+ import StorageClient from './storage';
4
+ import KVStoreClient from './kvstore';
5
+ import EscrowClient from './escrow';
2
6
 
3
7
  export * from './constants';
4
8
  export * from './types';
9
+ export * from './enums';
10
+
11
+ export {
12
+ InitClient,
13
+ StakingClient,
14
+ StorageClient,
15
+ KVStoreClient,
16
+ EscrowClient,
17
+ };
package/src/init.ts ADDED
@@ -0,0 +1,45 @@
1
+ import { Provider } from '@ethersproject/abstract-provider';
2
+ import { Network } from '@ethersproject/networks';
3
+ import { Signer } from 'ethers';
4
+ import { NETWORKS } from './constants';
5
+ import { IClientParams } from './interfaces';
6
+ import {
7
+ ErrorInitProviderDoesNotExist,
8
+ ErrorInitUnsupportedChainID,
9
+ } from './error';
10
+ import { ChainId } from './enums';
11
+
12
+ export default class InitClient {
13
+ /**
14
+ * **Get init client parameters**
15
+ *
16
+ * @param {Signer | Provider} providerOrSigner - Ethereum signer or provider
17
+ * @returns {Promise<IClientParams>} - Init client parameters
18
+ */
19
+ static async getParams(
20
+ signerOrProvider: Signer | Provider
21
+ ): Promise<IClientParams> {
22
+ let network: Network;
23
+ if (signerOrProvider instanceof Signer) {
24
+ if (!signerOrProvider.provider) {
25
+ throw ErrorInitProviderDoesNotExist;
26
+ }
27
+
28
+ network = await signerOrProvider.provider.getNetwork();
29
+ } else {
30
+ network = await signerOrProvider.getNetwork();
31
+ }
32
+
33
+ const chainId: ChainId = network.chainId;
34
+ const networkData = NETWORKS[chainId];
35
+
36
+ if (!networkData) {
37
+ throw ErrorInitUnsupportedChainID;
38
+ }
39
+
40
+ return {
41
+ signerOrProvider: signerOrProvider,
42
+ network: networkData,
43
+ };
44
+ }
45
+ }
@@ -0,0 +1,50 @@
1
+ import { BigNumber, Signer } from 'ethers';
2
+ import { Provider } from '@ethersproject/abstract-provider';
3
+ import { NetworkData } from './types';
4
+
5
+ export interface IClientParams {
6
+ signerOrProvider: Signer | Provider;
7
+ network: NetworkData;
8
+ }
9
+
10
+ export interface IAllocation {
11
+ escrowAddress: string;
12
+ staker: string;
13
+ tokens: BigNumber;
14
+ createdAt: BigNumber;
15
+ closedAt: BigNumber;
16
+ }
17
+
18
+ export interface IReward {
19
+ escrowAddress: string;
20
+ amount: BigNumber;
21
+ }
22
+
23
+ export interface IStaker {
24
+ tokensStaked: BigNumber;
25
+ tokensAllocated: BigNumber;
26
+ tokensLocked: BigNumber;
27
+ tokensLockedUntil: BigNumber;
28
+ tokensAvailable: BigNumber;
29
+ }
30
+
31
+ export interface IEscrowsFilter {
32
+ address: string;
33
+ role?: number;
34
+ status?: number;
35
+ from?: Date;
36
+ to?: Date;
37
+ }
38
+
39
+ export interface IEscrowConfig {
40
+ recordingOracle: string;
41
+ reputationOracle: string;
42
+ recordingOracleFee: BigNumber;
43
+ reputationOracleFee: BigNumber;
44
+ manifestUrl: string;
45
+ hash: string;
46
+ }
47
+
48
+ export interface ILauncherEscrowsResult {
49
+ id: string;
50
+ }
package/src/kvstore.ts ADDED
@@ -0,0 +1,93 @@
1
+ import {
2
+ KVStore,
3
+ KVStore__factory,
4
+ } from '@human-protocol/core/typechain-types';
5
+ import { Signer, ethers } from 'ethers';
6
+ import { Provider } from '@ethersproject/abstract-provider';
7
+ import {
8
+ ErrorInvalidAddress,
9
+ ErrorKVStoreArrayLength,
10
+ ErrorKVStoreEmptyKey,
11
+ ErrorSigner,
12
+ } from './error';
13
+ import { IClientParams } from './interfaces';
14
+ import { requiresSigner } from './decorators';
15
+
16
+ export default class KVStoreClient {
17
+ private contract: KVStore;
18
+ private signerOrProvider: Signer | Provider;
19
+
20
+ /**
21
+ * **KVStore constructor**
22
+ *
23
+ * * @param {IClientParams} clientParams - Init client parameters
24
+ */
25
+ constructor(readonly clientParams: IClientParams) {
26
+ this.contract = KVStore__factory.connect(
27
+ clientParams.network.kvstoreAddress,
28
+ clientParams.signerOrProvider
29
+ );
30
+ this.signerOrProvider = clientParams.signerOrProvider;
31
+ }
32
+
33
+ /**
34
+ * Sets a key-value pair in the contract
35
+ *
36
+ * @param {string} key - The key of the key-value pair to set
37
+ * @param {string} value - The value of the key-value pair to set
38
+ * @returns {Promise<void>}
39
+ * @throws {Error} - An error object if an error occurred
40
+ */
41
+ @requiresSigner
42
+ public async set(key: string, value: string) {
43
+ if (!Signer.isSigner(this.signerOrProvider)) throw ErrorSigner;
44
+ if (key === '') throw ErrorKVStoreEmptyKey;
45
+ try {
46
+ await this.contract?.set(key, value);
47
+ } catch (e) {
48
+ if (e instanceof Error) throw Error(`Failed to set value: ${e.message}`);
49
+ }
50
+ }
51
+
52
+ /**
53
+ * Sets multiple key-value pairs in the contract
54
+ *
55
+ * @param {string[]} keys - An array of keys to set
56
+ * @param {string[]} values - An array of values to set
57
+ * @returns {Promise<void>}
58
+ * @throws {Error} - An error object if an error occurred
59
+ */
60
+ @requiresSigner
61
+ public async setBulk(keys: string[], values: string[]) {
62
+ if (!Signer.isSigner(this.signerOrProvider)) throw ErrorSigner;
63
+ if (keys.length !== values.length) throw ErrorKVStoreArrayLength;
64
+ if (keys.includes('')) throw ErrorKVStoreEmptyKey;
65
+
66
+ try {
67
+ await this.contract?.setBulk(keys, values);
68
+ } catch (e) {
69
+ if (e instanceof Error)
70
+ throw Error(`Failed to set bulk values: ${e.message}`);
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Gets the value of a key-value pair in the contract
76
+ *
77
+ * @param {string} address - The Ethereum address associated with the key-value pair
78
+ * @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
80
+ * @throws {Error} - An error object if an error occurred
81
+ */
82
+ public async get(address: string, key: string) {
83
+ if (key === '') throw ErrorKVStoreEmptyKey;
84
+ if (!ethers.utils.isAddress(address)) throw ErrorInvalidAddress;
85
+
86
+ try {
87
+ const result = await this.contract?.get(address, key);
88
+ return result;
89
+ } catch (e) {
90
+ if (e instanceof Error) throw Error(`Failed to get value: ${e.message}`);
91
+ }
92
+ }
93
+ }
package/src/queries.ts ADDED
@@ -0,0 +1,18 @@
1
+ export const RAW_REWARDS_QUERY = (slasherAddress: string) => `{
2
+ rewardAddedEvents(id: "${slasherAddress}") {
3
+ escrow,
4
+ amount
5
+ }
6
+ }`;
7
+
8
+ export const RAW_LAUNCHED_ESCROWS_QUERY = () => `{
9
+ launchedEscrows(where: { from: $address }) {
10
+ id
11
+ }
12
+ }`;
13
+
14
+ export const RAW_LAUNCHED_ESCROWS_FILTERED_QUERY = () => `{
15
+ launchedEscrows(where: { from: $address, status: $status, timestamp_gte: $from, timestamp_lte: $to }) {
16
+ id
17
+ }
18
+ }`;
package/src/staking.ts ADDED
@@ -0,0 +1,422 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import { Provider } from '@ethersproject/abstract-provider';
3
+ import {
4
+ Staking__factory,
5
+ HMToken__factory,
6
+ HMToken,
7
+ Staking,
8
+ RewardPool__factory,
9
+ RewardPool,
10
+ EscrowFactory,
11
+ EscrowFactory__factory,
12
+ } from '@human-protocol/core/typechain-types';
13
+ import { BigNumber, ethers, Signer } from 'ethers';
14
+ import { NetworkData } from './types';
15
+ import { IAllocation, IClientParams, IReward, IStaker } from './interfaces';
16
+ import {
17
+ ErrorEscrowAddressIsNotProvidedByFactory,
18
+ ErrorInvalidEscrowAddressProvided,
19
+ ErrorInvalidSlasherAddressProvided,
20
+ ErrorInvalidStakerAddressProvided,
21
+ ErrorInvalidStakingValueSign,
22
+ ErrorInvalidStakingValueType,
23
+ ErrorStakingGetStakers,
24
+ } from './error';
25
+ import { gqlFetch, throwError } from './utils';
26
+ import { RAW_REWARDS_QUERY } from './queries';
27
+ import { requiresSigner } from './decorators';
28
+
29
+ export default class StakingClient {
30
+ public signerOrProvider: Signer | Provider;
31
+ public network: NetworkData;
32
+ public tokenContract: HMToken;
33
+ public stakingContract: Staking;
34
+ public escrowFactoryContract: EscrowFactory;
35
+
36
+ /**
37
+ * **Staking constructor**
38
+ *
39
+ * @param {IClientParams} clientParams - Init client parameters
40
+ */
41
+ constructor(readonly clientParams: IClientParams) {
42
+ this.signerOrProvider = clientParams.signerOrProvider;
43
+ this.network = clientParams.network;
44
+
45
+ this.stakingContract = Staking__factory.connect(
46
+ this.network.stakingAddress,
47
+ this.signerOrProvider
48
+ );
49
+
50
+ this.escrowFactoryContract = EscrowFactory__factory.connect(
51
+ this.network.factoryAddress,
52
+ this.signerOrProvider
53
+ );
54
+
55
+ this.tokenContract = HMToken__factory.connect(
56
+ this.network.hmtAddress,
57
+ this.signerOrProvider
58
+ );
59
+ }
60
+
61
+ /**
62
+ * **Approves the staking contract to transfer a specified amount of tokens when the user stakes.
63
+ * **It increases the allowance for the staking contract.*
64
+ *
65
+ * @param {BigNumber} amount - Amount of tokens to approve for stake
66
+ * @returns {Promise<void>}
67
+ * @throws {Error} - An error object if an error occurred, void otherwise
68
+ */
69
+ @requiresSigner
70
+ public async approveStake(amount: BigNumber): Promise<void> {
71
+ if (!BigNumber.isBigNumber(amount)) {
72
+ throw ErrorInvalidStakingValueType;
73
+ }
74
+
75
+ if (amount.isNegative()) {
76
+ throw ErrorInvalidStakingValueSign;
77
+ }
78
+
79
+ try {
80
+ await this.tokenContract.approve(this.stakingContract.address, amount);
81
+ return;
82
+ } catch (e) {
83
+ return throwError(e);
84
+ }
85
+ }
86
+
87
+ /**
88
+ * **Stakes a specified amount of tokens on a specific network.*
89
+ *
90
+ * @param {BigNumber} amount - Amount of tokens to stake
91
+ * @returns {Promise<void>}
92
+ * @throws {Error} - An error object if an error occurred, void otherwise
93
+ */
94
+ @requiresSigner
95
+ public async stake(amount: BigNumber): Promise<void> {
96
+ if (!BigNumber.isBigNumber(amount)) {
97
+ throw ErrorInvalidStakingValueType;
98
+ }
99
+
100
+ if (amount.isNegative()) {
101
+ throw ErrorInvalidStakingValueSign;
102
+ }
103
+
104
+ try {
105
+ await this.stakingContract.stake(amount);
106
+ return;
107
+ } catch (e) {
108
+ return throwError(e);
109
+ }
110
+ }
111
+
112
+ /**
113
+ * **Unstakes tokens from staking contract.
114
+ * **The unstaked tokens stay locked for a period of time.*
115
+ *
116
+ * @param {BigNumber} amount - Amount of tokens to unstake
117
+ * @returns {Promise<void>}
118
+ * @throws {Error} - An error object if an error occurred, void otherwise
119
+ */
120
+ @requiresSigner
121
+ public async unstake(amount: BigNumber): Promise<void> {
122
+ if (!BigNumber.isBigNumber(amount)) {
123
+ throw ErrorInvalidStakingValueType;
124
+ }
125
+
126
+ if (amount.isNegative()) {
127
+ throw ErrorInvalidStakingValueSign;
128
+ }
129
+
130
+ try {
131
+ await this.stakingContract.unstake(amount);
132
+ return;
133
+ } catch (e) {
134
+ return throwError(e);
135
+ }
136
+ }
137
+
138
+ /**
139
+ * **Withdraws unstaked and non locked tokens form staking contract to the user wallet.*
140
+ *
141
+ * @returns {Promise<void>}
142
+ * @throws {Error} - An error object if an error occurred, void otherwise
143
+ */
144
+ @requiresSigner
145
+ public async withdraw(): Promise<void> {
146
+ try {
147
+ await this.stakingContract.withdraw();
148
+ return;
149
+ } catch (e) {
150
+ return throwError(e);
151
+ }
152
+ }
153
+
154
+ /**
155
+ * **Slash the allocated amount by an staker in an escrow and transfers those tokens to the reward pool.
156
+ * **This allows the slasher to claim them later.*
157
+ *
158
+ * @param {string} slasher - Wallet address from who requested the slash
159
+ * @param {string} staker - Wallet address from who is going to be slashed
160
+ * @param {string} escrowAddress - Address of the escrow which allocation will be slashed
161
+ * @param {BigNumber} amount - Amount of tokens to slash
162
+ * @returns {Promise<void>}
163
+ * @throws {Error} - An error object if an error occurred, void otherwise
164
+ */
165
+ @requiresSigner
166
+ public async slash(
167
+ slasher: string,
168
+ staker: string,
169
+ escrowAddress: string,
170
+ amount: BigNumber
171
+ ): Promise<void> {
172
+ if (!BigNumber.isBigNumber(amount)) {
173
+ throw ErrorInvalidStakingValueType;
174
+ }
175
+
176
+ if (amount.isNegative()) {
177
+ throw ErrorInvalidStakingValueSign;
178
+ }
179
+
180
+ if (!ethers.utils.isAddress(slasher)) {
181
+ throw ErrorInvalidSlasherAddressProvided;
182
+ }
183
+
184
+ if (!ethers.utils.isAddress(staker)) {
185
+ throw ErrorInvalidStakerAddressProvided;
186
+ }
187
+
188
+ if (!ethers.utils.isAddress(escrowAddress)) {
189
+ throw ErrorInvalidEscrowAddressProvided;
190
+ }
191
+
192
+ if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
193
+ throw ErrorEscrowAddressIsNotProvidedByFactory;
194
+ }
195
+
196
+ try {
197
+ await this.stakingContract.slash(slasher, staker, escrowAddress, amount);
198
+
199
+ return;
200
+ } catch (e) {
201
+ return throwError(e);
202
+ }
203
+ }
204
+
205
+ /**
206
+ * **Allocates a portion of the staked tokens to a specific escrow.*
207
+ *
208
+ * @param {string} escrowAddress - Address of the escrow contract
209
+ * @param {BigNumber} amount - Amount of tokens to allocate
210
+ * @returns {Promise<void>}
211
+ * @throws {Error} - An error object if an error occurred, void otherwise
212
+ */
213
+ @requiresSigner
214
+ public async allocate(
215
+ escrowAddress: string,
216
+ amount: BigNumber
217
+ ): Promise<void> {
218
+ if (!BigNumber.isBigNumber(amount)) {
219
+ throw ErrorInvalidStakingValueType;
220
+ }
221
+
222
+ if (amount.isNegative()) {
223
+ throw ErrorInvalidStakingValueSign;
224
+ }
225
+
226
+ if (!ethers.utils.isAddress(escrowAddress)) {
227
+ throw ErrorInvalidEscrowAddressProvided;
228
+ }
229
+
230
+ if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
231
+ throw ErrorEscrowAddressIsNotProvidedByFactory;
232
+ }
233
+
234
+ try {
235
+ await this.stakingContract.allocate(escrowAddress, amount);
236
+ return;
237
+ } catch (e) {
238
+ return throwError(e);
239
+ }
240
+ }
241
+
242
+ /**
243
+ * **Drops the allocation from a specific escrow.*
244
+ *
245
+ * @param {string} escrowAddress - Address of the escrow contract.
246
+ * @returns {Promise<void>}
247
+ * @throws {Error} - An error object if an error occurred, void otherwise
248
+ */
249
+ @requiresSigner
250
+ public async closeAllocation(escrowAddress: string): Promise<void> {
251
+ if (!ethers.utils.isAddress(escrowAddress)) {
252
+ throw ErrorInvalidEscrowAddressProvided;
253
+ }
254
+
255
+ if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
256
+ throw ErrorEscrowAddressIsNotProvidedByFactory;
257
+ }
258
+
259
+ try {
260
+ await this.stakingContract.closeAllocation(escrowAddress);
261
+ return;
262
+ } catch (e) {
263
+ return throwError(e);
264
+ }
265
+ }
266
+
267
+ /**
268
+ * **Pays out rewards to the slashers for the specified escrow address.*
269
+ *
270
+ * @param {string} escrowAddress - Escrow address from which rewards are distributed.
271
+ * @returns {Promise<void>}
272
+ * @throws {Error} - An error object if an error occurred, void otherwise
273
+ */
274
+ @requiresSigner
275
+ public async distributeRewards(escrowAddress: string): Promise<void> {
276
+ if (!ethers.utils.isAddress(escrowAddress)) {
277
+ throw ErrorInvalidEscrowAddressProvided;
278
+ }
279
+
280
+ if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
281
+ throw ErrorEscrowAddressIsNotProvidedByFactory;
282
+ }
283
+
284
+ try {
285
+ const rewardPoolContract: RewardPool = RewardPool__factory.connect(
286
+ await this.stakingContract.rewardPool(),
287
+ this.signerOrProvider
288
+ );
289
+
290
+ await rewardPoolContract.distributeReward(escrowAddress);
291
+ return;
292
+ } catch (e) {
293
+ return throwError(e);
294
+ }
295
+ }
296
+
297
+ /**
298
+ * **Returns the staking information about an staker address.*
299
+ *
300
+ * @param {string} staker - Address of the staker
301
+ * @returns {Promise<IStaker>} - Return staking information of the specified address
302
+ * @throws {Error} - An error object if an error occurred, result otherwise
303
+ */
304
+ public async getStaker(staker: string): Promise<IStaker> {
305
+ if (!ethers.utils.isAddress(staker)) {
306
+ throw ErrorInvalidStakerAddressProvided;
307
+ }
308
+
309
+ try {
310
+ const result = await this.stakingContract.getStaker(staker);
311
+
312
+ const tokensStaked = BigNumber.from(result.tokensStaked),
313
+ tokensAllocated = BigNumber.from(result.tokensAllocated),
314
+ tokensLocked = BigNumber.from(result.tokensLocked),
315
+ tokensLockedUntil = BigNumber.from(result.tokensLockedUntil);
316
+
317
+ const tokensAvailable = tokensStaked
318
+ .sub(tokensAllocated)
319
+ .sub(tokensLocked);
320
+
321
+ return {
322
+ tokensStaked,
323
+ tokensAllocated,
324
+ tokensLocked,
325
+ tokensLockedUntil,
326
+ tokensAvailable,
327
+ };
328
+ } catch (e) {
329
+ return throwError(e);
330
+ }
331
+ }
332
+
333
+ /**
334
+ * **Returns the staking information about all stakers of the protocol.*
335
+ *
336
+ * @returns {Promise<IStakerInfo>} - Return an array with all stakers information
337
+ * @throws {Error} - An error object if an error occurred, results otherwise
338
+ */
339
+ public async getAllStakers(): Promise<IStaker[]> {
340
+ try {
341
+ const result = await this.stakingContract.getListOfStakers();
342
+
343
+ if (result[1].length === 0) {
344
+ throw ErrorStakingGetStakers;
345
+ }
346
+
347
+ return result[1].map((staker: any) => {
348
+ const tokensStaked = BigNumber.from(staker.tokensStaked),
349
+ tokensAllocated = BigNumber.from(staker.tokensAllocated),
350
+ tokensLocked = BigNumber.from(staker.tokensLocked),
351
+ tokensLockedUntil = BigNumber.from(staker.tokensLockedUntil);
352
+
353
+ const tokensAvailable = tokensStaked
354
+ .sub(tokensAllocated)
355
+ .sub(tokensLocked);
356
+
357
+ return {
358
+ tokensStaked,
359
+ tokensAllocated,
360
+ tokensLocked,
361
+ tokensLockedUntil,
362
+ tokensAvailable,
363
+ };
364
+ });
365
+ } catch (e) {
366
+ return throwError(e);
367
+ }
368
+ }
369
+
370
+ /**
371
+ * **Returns information about the allocation of the specified escrow.*
372
+ *
373
+ * @param {string} escrowAddress - The escrow address for the received allocation data
374
+ * @returns {Promise<IAllocation>} - Returns allocation info if exists
375
+ * @throws {Error} - An error object if an error occurred, result otherwise
376
+ */
377
+ public async getAllocation(escrowAddress: string): Promise<IAllocation> {
378
+ if (!ethers.utils.isAddress(escrowAddress)) {
379
+ throw ErrorInvalidEscrowAddressProvided;
380
+ }
381
+
382
+ if (!(await this.escrowFactoryContract.hasEscrow(escrowAddress))) {
383
+ throw ErrorEscrowAddressIsNotProvidedByFactory;
384
+ }
385
+
386
+ try {
387
+ const result = await this.stakingContract.getAllocation(escrowAddress);
388
+ return result;
389
+ } catch (e) {
390
+ return throwError(e);
391
+ }
392
+ }
393
+
394
+ /**
395
+ * **Returns information about the rewards for a given escrow address.*
396
+ *
397
+ * @param {string} slasherAddress - Address of the slasher
398
+ * @returns {Promise<IReward[]>} - Returns rewards info if exists
399
+ * @throws {Error} - An error object if an error occurred, results otherwise
400
+ */
401
+ public async getRewards(slasherAddress: string): Promise<IReward[]> {
402
+ if (!ethers.utils.isAddress(slasherAddress)) {
403
+ throw ErrorInvalidSlasherAddressProvided;
404
+ }
405
+
406
+ try {
407
+ const { data } = await gqlFetch(
408
+ this.network.subgraphUrl,
409
+ RAW_REWARDS_QUERY(slasherAddress)
410
+ );
411
+
412
+ return data.rewardAddedEvents.map((reward: any) => {
413
+ return {
414
+ escrowAddress: reward.escrow,
415
+ amount: reward.amount,
416
+ };
417
+ });
418
+ } catch (e) {
419
+ return throwError(e);
420
+ }
421
+ }
422
+ }