@human-protocol/sdk 1.1.18 → 2.0.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.
- package/dist/base.d.ts +4 -13
- package/dist/base.d.ts.map +1 -1
- package/dist/base.js +3 -18
- package/dist/constants.d.ts +7 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +18 -11
- package/dist/decorators.d.ts.map +1 -1
- package/dist/decorators.js +4 -2
- package/dist/encryption.d.ts +31 -0
- package/dist/encryption.d.ts.map +1 -1
- package/dist/encryption.js +37 -0
- package/dist/error.d.ts +0 -10
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +2 -18
- package/dist/escrow.d.ts +39 -33
- package/dist/escrow.d.ts.map +1 -1
- package/dist/escrow.js +121 -137
- package/dist/graphql/queries/{staking.d.ts → operator.d.ts} +2 -1
- package/dist/graphql/queries/operator.d.ts.map +1 -0
- package/dist/graphql/queries/{staking.js → operator.js} +24 -1
- package/dist/graphql/types.d.ts +5 -6
- package/dist/graphql/types.d.ts.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/interfaces.d.ts +28 -18
- package/dist/interfaces.d.ts.map +1 -1
- package/dist/kvstore.d.ts +14 -14
- package/dist/kvstore.d.ts.map +1 -1
- package/dist/kvstore.js +30 -48
- package/dist/operator.d.ts +68 -0
- package/dist/operator.d.ts.map +1 -0
- package/dist/operator.js +153 -0
- package/dist/staking.d.ts +35 -95
- package/dist/staking.d.ts.map +1 -1
- package/dist/staking.js +73 -201
- package/dist/statistics.d.ts.map +1 -1
- package/dist/statistics.js +7 -6
- package/dist/types.d.ts +1 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +0 -15
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +9 -49
- package/package.json +4 -4
- package/src/base.ts +5 -30
- package/src/constants.ts +18 -10
- package/src/decorators.ts +3 -2
- package/src/encryption.ts +40 -0
- package/src/error.ts +0 -17
- package/src/escrow.ts +169 -178
- package/src/graphql/queries/{staking.ts → operator.ts} +24 -0
- package/src/graphql/types.ts +5 -7
- package/src/index.ts +2 -0
- package/src/interfaces.ts +30 -18
- package/src/kvstore.ts +47 -59
- package/src/operator.ts +192 -0
- package/src/staking.ts +101 -213
- package/src/statistics.ts +8 -9
- package/src/types.ts +1 -3
- package/src/utils.ts +8 -58
- package/dist/graphql/queries/staking.d.ts.map +0 -1
|
@@ -47,6 +47,30 @@ export const GET_LEADERS_QUERY = (filter: ILeadersFilter) => {
|
|
|
47
47
|
`;
|
|
48
48
|
};
|
|
49
49
|
|
|
50
|
+
export const GET_REPUTATION_NETWORK_QUERY = (role?: string) => {
|
|
51
|
+
const WHERE_CLAUSE = `
|
|
52
|
+
where: {
|
|
53
|
+
${role ? `role: $role` : ''}
|
|
54
|
+
}
|
|
55
|
+
`;
|
|
56
|
+
|
|
57
|
+
return gql`
|
|
58
|
+
query getReputationNetwork(
|
|
59
|
+
$address: String,
|
|
60
|
+
$role: String
|
|
61
|
+
) {
|
|
62
|
+
reputationNetwork(id: $address) {
|
|
63
|
+
operators(
|
|
64
|
+
${WHERE_CLAUSE}
|
|
65
|
+
) {
|
|
66
|
+
address,
|
|
67
|
+
role
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
`;
|
|
72
|
+
};
|
|
73
|
+
|
|
50
74
|
export const GET_LEADER_QUERY = gql`
|
|
51
75
|
query getLeader($address: String!) {
|
|
52
76
|
leader(id: $address) {
|
package/src/graphql/types.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { BigNumber } from 'ethers';
|
|
2
|
-
|
|
3
1
|
export type EscrowData = {
|
|
4
2
|
id: string;
|
|
5
3
|
address: string;
|
|
@@ -108,9 +106,9 @@ export type WorkerStatistics = {
|
|
|
108
106
|
|
|
109
107
|
export type DailyPaymentData = {
|
|
110
108
|
timestamp: Date;
|
|
111
|
-
totalAmountPaid:
|
|
109
|
+
totalAmountPaid: bigint;
|
|
112
110
|
totalCount: number;
|
|
113
|
-
averageAmountPerWorker:
|
|
111
|
+
averageAmountPerWorker: bigint;
|
|
114
112
|
};
|
|
115
113
|
|
|
116
114
|
export type PaymentStatistics = {
|
|
@@ -124,17 +122,17 @@ export type HMTHolderData = {
|
|
|
124
122
|
|
|
125
123
|
export type HMTHolder = {
|
|
126
124
|
address: string;
|
|
127
|
-
balance:
|
|
125
|
+
balance: bigint;
|
|
128
126
|
};
|
|
129
127
|
|
|
130
128
|
export type DailyHMTData = {
|
|
131
129
|
timestamp: Date;
|
|
132
|
-
totalTransactionAmount:
|
|
130
|
+
totalTransactionAmount: bigint;
|
|
133
131
|
totalTransactionCount: number;
|
|
134
132
|
};
|
|
135
133
|
|
|
136
134
|
export type HMTStatistics = {
|
|
137
|
-
totalTransferAmount:
|
|
135
|
+
totalTransferAmount: bigint;
|
|
138
136
|
totalTransferCount: number;
|
|
139
137
|
totalHolders: number;
|
|
140
138
|
holders: HMTHolder[];
|
package/src/index.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { KVStoreClient } from './kvstore';
|
|
|
4
4
|
import { EscrowClient, EscrowUtils } from './escrow';
|
|
5
5
|
import { StatisticsClient } from './statistics';
|
|
6
6
|
import { Encryption, EncryptionUtils } from './encryption';
|
|
7
|
+
import { OperatorUtils } from './operator';
|
|
7
8
|
|
|
8
9
|
export * from './constants';
|
|
9
10
|
export * from './types';
|
|
@@ -19,4 +20,5 @@ export {
|
|
|
19
20
|
StatisticsClient,
|
|
20
21
|
Encryption,
|
|
21
22
|
EncryptionUtils,
|
|
23
|
+
OperatorUtils,
|
|
22
24
|
};
|
package/src/interfaces.ts
CHANGED
|
@@ -1,40 +1,52 @@
|
|
|
1
|
-
import { BigNumber } from 'ethers';
|
|
2
1
|
import { EscrowStatus } from './types';
|
|
3
2
|
import { ChainId } from './enums';
|
|
4
3
|
|
|
5
4
|
export interface IAllocation {
|
|
6
5
|
escrowAddress: string;
|
|
7
6
|
staker: string;
|
|
8
|
-
tokens:
|
|
9
|
-
createdAt:
|
|
10
|
-
closedAt:
|
|
7
|
+
tokens: bigint;
|
|
8
|
+
createdAt: bigint;
|
|
9
|
+
closedAt: bigint;
|
|
11
10
|
}
|
|
12
11
|
|
|
13
12
|
export interface IReward {
|
|
14
13
|
escrowAddress: string;
|
|
15
|
-
amount:
|
|
14
|
+
amount: bigint;
|
|
16
15
|
}
|
|
17
16
|
|
|
18
17
|
export interface ILeader {
|
|
19
18
|
id: string;
|
|
19
|
+
chainId: ChainId;
|
|
20
20
|
address: string;
|
|
21
|
-
amountStaked:
|
|
22
|
-
amountAllocated:
|
|
23
|
-
amountLocked:
|
|
24
|
-
lockedUntilTimestamp:
|
|
25
|
-
amountWithdrawn:
|
|
26
|
-
amountSlashed:
|
|
27
|
-
reputation:
|
|
28
|
-
reward:
|
|
29
|
-
amountJobsLaunched:
|
|
21
|
+
amountStaked: bigint;
|
|
22
|
+
amountAllocated: bigint;
|
|
23
|
+
amountLocked: bigint;
|
|
24
|
+
lockedUntilTimestamp: bigint;
|
|
25
|
+
amountWithdrawn: bigint;
|
|
26
|
+
amountSlashed: bigint;
|
|
27
|
+
reputation: bigint;
|
|
28
|
+
reward: bigint;
|
|
29
|
+
amountJobsLaunched: bigint;
|
|
30
30
|
role?: string;
|
|
31
|
-
fee?:
|
|
31
|
+
fee?: bigint;
|
|
32
32
|
publicKey?: string;
|
|
33
33
|
webhookUrl?: string;
|
|
34
34
|
url?: string;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
export interface ILeadersFilter {
|
|
38
|
+
networks: ChainId[];
|
|
39
|
+
role?: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface IReputationNetwork {
|
|
43
|
+
id: string;
|
|
44
|
+
address: string;
|
|
45
|
+
operators: IOperator[];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface IOperator {
|
|
49
|
+
address: string;
|
|
38
50
|
role?: string;
|
|
39
51
|
}
|
|
40
52
|
|
|
@@ -54,9 +66,9 @@ export interface IEscrowConfig {
|
|
|
54
66
|
recordingOracle: string;
|
|
55
67
|
reputationOracle: string;
|
|
56
68
|
exchangeOracle: string;
|
|
57
|
-
recordingOracleFee:
|
|
58
|
-
reputationOracleFee:
|
|
59
|
-
exchangeOracleFee:
|
|
69
|
+
recordingOracleFee: bigint;
|
|
70
|
+
reputationOracleFee: bigint;
|
|
71
|
+
exchangeOracleFee: bigint;
|
|
60
72
|
manifestUrl: string;
|
|
61
73
|
manifestHash: string;
|
|
62
74
|
}
|
package/src/kvstore.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import { Provider } from '@ethersproject/abstract-provider';
|
|
2
|
-
import { Network } from '@ethersproject/networks';
|
|
3
1
|
import {
|
|
4
2
|
KVStore,
|
|
5
3
|
KVStore__factory,
|
|
6
4
|
} from '@human-protocol/core/typechain-types';
|
|
7
|
-
import {
|
|
5
|
+
import { ContractRunner, Overrides, ethers } from 'ethers';
|
|
8
6
|
import { BaseEthersClient } from './base';
|
|
9
7
|
import { NETWORKS } from './constants';
|
|
10
8
|
import { requiresSigner } from './decorators';
|
|
@@ -16,7 +14,6 @@ import {
|
|
|
16
14
|
ErrorKVStoreArrayLength,
|
|
17
15
|
ErrorKVStoreEmptyKey,
|
|
18
16
|
ErrorProviderDoesNotExist,
|
|
19
|
-
ErrorSigner,
|
|
20
17
|
ErrorUnsupportedChainID,
|
|
21
18
|
} from './error';
|
|
22
19
|
import { NetworkData } from './types';
|
|
@@ -27,11 +24,11 @@ import { isValidUrl } from './utils';
|
|
|
27
24
|
*
|
|
28
25
|
* This client enables to perform actions on KVStore contract and obtain information from both the contracts and subgraph.
|
|
29
26
|
*
|
|
30
|
-
* Internally, the SDK will use one network or another according to the network ID of the `
|
|
27
|
+
* Internally, the SDK will use one network or another according to the network ID of the `runner`.
|
|
31
28
|
* To use this client, it is recommended to initialize it using the static `build` method.
|
|
32
29
|
*
|
|
33
30
|
* ```ts
|
|
34
|
-
* static async build(
|
|
31
|
+
* static async build(runner: ContractRunner);
|
|
35
32
|
* ```
|
|
36
33
|
*
|
|
37
34
|
* A `Signer` or a `Provider` should be passed depending on the use case of this module:
|
|
@@ -97,56 +94,42 @@ export class KVStoreClient extends BaseEthersClient {
|
|
|
97
94
|
/**
|
|
98
95
|
* **KVStoreClient constructor**
|
|
99
96
|
*
|
|
100
|
-
* @param {
|
|
97
|
+
* @param {ContractRunner} runner - The Runner object to interact with the Ethereum network
|
|
101
98
|
* @param {NetworkData} network - The network information required to connect to the KVStore contract
|
|
102
|
-
* @param {number | undefined} gasPriceMultiplier - The multiplier to apply to the gas price
|
|
103
99
|
*/
|
|
104
|
-
constructor(
|
|
105
|
-
|
|
106
|
-
networkData: NetworkData,
|
|
107
|
-
gasPriceMultiplier?: number
|
|
108
|
-
) {
|
|
109
|
-
super(signerOrProvider, networkData, gasPriceMultiplier);
|
|
100
|
+
constructor(runner: ContractRunner, networkData: NetworkData) {
|
|
101
|
+
super(runner, networkData);
|
|
110
102
|
|
|
111
103
|
this.contract = KVStore__factory.connect(
|
|
112
104
|
networkData.kvstoreAddress,
|
|
113
|
-
|
|
105
|
+
runner
|
|
114
106
|
);
|
|
115
107
|
}
|
|
116
108
|
|
|
117
109
|
/**
|
|
118
|
-
* Creates an instance of KVStoreClient from a
|
|
110
|
+
* Creates an instance of KVStoreClient from a runner.
|
|
119
111
|
*
|
|
120
|
-
* @param {
|
|
121
|
-
* @param {number | undefined} gasPriceMultiplier - The multiplier to apply to the gas price
|
|
112
|
+
* @param {ContractRunner} runner - The Runner object to interact with the Ethereum network
|
|
122
113
|
*
|
|
123
114
|
* @returns {Promise<KVStoreClient>} - An instance of KVStoreClient
|
|
124
115
|
* @throws {ErrorProviderDoesNotExist} - Thrown if the provider does not exist for the provided Signer
|
|
125
116
|
* @throws {ErrorUnsupportedChainID} - Thrown if the network's chainId is not supported
|
|
126
117
|
*/
|
|
127
|
-
public static async build(
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
) {
|
|
131
|
-
let network: Network;
|
|
132
|
-
if (Signer.isSigner(signerOrProvider)) {
|
|
133
|
-
if (!signerOrProvider.provider) {
|
|
134
|
-
throw ErrorProviderDoesNotExist;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
network = await signerOrProvider.provider.getNetwork();
|
|
138
|
-
} else {
|
|
139
|
-
network = await signerOrProvider.getNetwork();
|
|
118
|
+
public static async build(runner: ContractRunner) {
|
|
119
|
+
if (!runner.provider) {
|
|
120
|
+
throw ErrorProviderDoesNotExist;
|
|
140
121
|
}
|
|
141
122
|
|
|
142
|
-
const
|
|
123
|
+
const network = await runner.provider?.getNetwork();
|
|
124
|
+
|
|
125
|
+
const chainId: ChainId = Number(network?.chainId);
|
|
143
126
|
const networkData = NETWORKS[chainId];
|
|
144
127
|
|
|
145
128
|
if (!networkData) {
|
|
146
129
|
throw ErrorUnsupportedChainID;
|
|
147
130
|
}
|
|
148
131
|
|
|
149
|
-
return new KVStoreClient(
|
|
132
|
+
return new KVStoreClient(runner, networkData);
|
|
150
133
|
}
|
|
151
134
|
|
|
152
135
|
/**
|
|
@@ -154,6 +137,7 @@ export class KVStoreClient extends BaseEthersClient {
|
|
|
154
137
|
*
|
|
155
138
|
* @param {string} key Key of the key-value pair
|
|
156
139
|
* @param {string} value Value of the key-value pair
|
|
140
|
+
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
157
141
|
* @returns Returns void if successful. Throws error if any.
|
|
158
142
|
*
|
|
159
143
|
*
|
|
@@ -176,13 +160,14 @@ export class KVStoreClient extends BaseEthersClient {
|
|
|
176
160
|
* ```
|
|
177
161
|
*/
|
|
178
162
|
@requiresSigner
|
|
179
|
-
public async set(
|
|
180
|
-
|
|
163
|
+
public async set(
|
|
164
|
+
key: string,
|
|
165
|
+
value: string,
|
|
166
|
+
txOptions: Overrides = {}
|
|
167
|
+
): Promise<void> {
|
|
181
168
|
if (key === '') throw ErrorKVStoreEmptyKey;
|
|
182
169
|
try {
|
|
183
|
-
await this.contract
|
|
184
|
-
...(await this.gasPriceOptions()),
|
|
185
|
-
});
|
|
170
|
+
await (await this.contract.set(key, value, txOptions)).wait();
|
|
186
171
|
} catch (e) {
|
|
187
172
|
if (e instanceof Error) throw Error(`Failed to set value: ${e.message}`);
|
|
188
173
|
}
|
|
@@ -193,6 +178,7 @@ export class KVStoreClient extends BaseEthersClient {
|
|
|
193
178
|
*
|
|
194
179
|
* @param {string[]} keys Array of keys (keys and value must have the same order)
|
|
195
180
|
* @param {string[]} values Array of values
|
|
181
|
+
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
196
182
|
* @returns Returns void if successful. Throws error if any.
|
|
197
183
|
*
|
|
198
184
|
*
|
|
@@ -217,15 +203,16 @@ export class KVStoreClient extends BaseEthersClient {
|
|
|
217
203
|
* ```
|
|
218
204
|
*/
|
|
219
205
|
@requiresSigner
|
|
220
|
-
public async setBulk(
|
|
221
|
-
|
|
206
|
+
public async setBulk(
|
|
207
|
+
keys: string[],
|
|
208
|
+
values: string[],
|
|
209
|
+
txOptions: Overrides = {}
|
|
210
|
+
): Promise<void> {
|
|
222
211
|
if (keys.length !== values.length) throw ErrorKVStoreArrayLength;
|
|
223
212
|
if (keys.includes('')) throw ErrorKVStoreEmptyKey;
|
|
224
213
|
|
|
225
214
|
try {
|
|
226
|
-
await this.contract
|
|
227
|
-
...(await this.gasPriceOptions()),
|
|
228
|
-
});
|
|
215
|
+
await (await this.contract.setBulk(keys, values, txOptions)).wait();
|
|
229
216
|
} catch (e) {
|
|
230
217
|
if (e instanceof Error)
|
|
231
218
|
throw Error(`Failed to set bulk values: ${e.message}`);
|
|
@@ -237,6 +224,7 @@ export class KVStoreClient extends BaseEthersClient {
|
|
|
237
224
|
*
|
|
238
225
|
* @param {string} url URL to set
|
|
239
226
|
* @param {string | undefined} urlKey Configurable URL key. `url` by default.
|
|
227
|
+
* @param {Overrides} [txOptions] - Additional transaction parameters (optional, defaults to an empty object).
|
|
240
228
|
* @returns Returns void if successful. Throws error if any.
|
|
241
229
|
*
|
|
242
230
|
*
|
|
@@ -258,26 +246,28 @@ export class KVStoreClient extends BaseEthersClient {
|
|
|
258
246
|
* ```
|
|
259
247
|
*/
|
|
260
248
|
@requiresSigner
|
|
261
|
-
public async setURL(
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
}
|
|
265
|
-
|
|
249
|
+
public async setURL(
|
|
250
|
+
url: string,
|
|
251
|
+
urlKey = 'url',
|
|
252
|
+
txOptions: Overrides = {}
|
|
253
|
+
): Promise<void> {
|
|
266
254
|
if (!isValidUrl(url)) {
|
|
267
255
|
throw ErrorInvalidUrl;
|
|
268
256
|
}
|
|
269
257
|
|
|
270
258
|
const content = await fetch(url).then((res) => res.text());
|
|
271
|
-
const contentHash = ethers.
|
|
272
|
-
ethers.utils.toUtf8Bytes(content)
|
|
273
|
-
);
|
|
259
|
+
const contentHash = ethers.keccak256(ethers.toUtf8Bytes(content));
|
|
274
260
|
|
|
275
261
|
const hashKey = urlKey + 'Hash';
|
|
276
262
|
|
|
277
263
|
try {
|
|
278
|
-
await
|
|
279
|
-
|
|
280
|
-
|
|
264
|
+
await (
|
|
265
|
+
await this.contract.setBulk(
|
|
266
|
+
[urlKey, hashKey],
|
|
267
|
+
[url, contentHash],
|
|
268
|
+
txOptions
|
|
269
|
+
)
|
|
270
|
+
).wait();
|
|
281
271
|
} catch (e) {
|
|
282
272
|
if (e instanceof Error)
|
|
283
273
|
throw Error(`Failed to set URL and hash: ${e.message}`);
|
|
@@ -310,7 +300,7 @@ export class KVStoreClient extends BaseEthersClient {
|
|
|
310
300
|
*/
|
|
311
301
|
public async get(address: string, key: string): Promise<string> {
|
|
312
302
|
if (key === '') throw ErrorKVStoreEmptyKey;
|
|
313
|
-
if (!ethers.
|
|
303
|
+
if (!ethers.isAddress(address)) throw ErrorInvalidAddress;
|
|
314
304
|
|
|
315
305
|
try {
|
|
316
306
|
const result = await this.contract?.get(address, key);
|
|
@@ -348,7 +338,7 @@ export class KVStoreClient extends BaseEthersClient {
|
|
|
348
338
|
* ```
|
|
349
339
|
*/
|
|
350
340
|
public async getURL(address: string, urlKey = 'url'): Promise<string> {
|
|
351
|
-
if (!ethers.
|
|
341
|
+
if (!ethers.isAddress(address)) throw ErrorInvalidAddress;
|
|
352
342
|
const hashKey = urlKey + 'Hash';
|
|
353
343
|
|
|
354
344
|
let url = '',
|
|
@@ -372,9 +362,7 @@ export class KVStoreClient extends BaseEthersClient {
|
|
|
372
362
|
}
|
|
373
363
|
|
|
374
364
|
const content = await fetch(url).then((res) => res.text());
|
|
375
|
-
const contentHash = ethers.
|
|
376
|
-
ethers.utils.toUtf8Bytes(content)
|
|
377
|
-
);
|
|
365
|
+
const contentHash = ethers.keccak256(ethers.toUtf8Bytes(content));
|
|
378
366
|
|
|
379
367
|
if (hash !== contentHash) {
|
|
380
368
|
throw ErrorInvalidHash;
|
package/src/operator.ts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import gqlFetch from 'graphql-request';
|
|
3
|
+
import {
|
|
4
|
+
ILeader,
|
|
5
|
+
ILeadersFilter,
|
|
6
|
+
IOperator,
|
|
7
|
+
IReputationNetwork,
|
|
8
|
+
IReward,
|
|
9
|
+
} from './interfaces';
|
|
10
|
+
import { GET_REWARD_ADDED_EVENTS_QUERY } from './graphql/queries/reward';
|
|
11
|
+
import { RewardAddedEventData } from './graphql';
|
|
12
|
+
import {
|
|
13
|
+
GET_LEADER_QUERY,
|
|
14
|
+
GET_LEADERS_QUERY,
|
|
15
|
+
GET_REPUTATION_NETWORK_QUERY,
|
|
16
|
+
} from './graphql/queries/operator';
|
|
17
|
+
import { ethers } from 'ethers';
|
|
18
|
+
import {
|
|
19
|
+
ErrorInvalidSlasherAddressProvided,
|
|
20
|
+
ErrorInvalidStakerAddressProvided,
|
|
21
|
+
ErrorUnsupportedChainID,
|
|
22
|
+
} from './error';
|
|
23
|
+
import { throwError } from './utils';
|
|
24
|
+
import { ChainId } from './enums';
|
|
25
|
+
import { NETWORKS } from './constants';
|
|
26
|
+
|
|
27
|
+
export class OperatorUtils {
|
|
28
|
+
/**
|
|
29
|
+
* This function returns the leader data for the given address.
|
|
30
|
+
*
|
|
31
|
+
* @param {string} address Leader address.
|
|
32
|
+
* @returns {ILeader} Returns the leader details.
|
|
33
|
+
*
|
|
34
|
+
*
|
|
35
|
+
* **Code example**
|
|
36
|
+
*
|
|
37
|
+
* ```ts
|
|
38
|
+
* import { OperatorUtils, ChainId } from '@human-protocol/sdk';
|
|
39
|
+
*
|
|
40
|
+
* const leader = await OperatorUtils.getLeader(ChainId.POLYGON_MUMBAI, '0x62dD51230A30401C455c8398d06F85e4EaB6309f');
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
public static async getLeader(
|
|
44
|
+
chainId: ChainId,
|
|
45
|
+
address: string
|
|
46
|
+
): Promise<ILeader> {
|
|
47
|
+
if (!ethers.isAddress(address)) {
|
|
48
|
+
throw ErrorInvalidStakerAddressProvided;
|
|
49
|
+
}
|
|
50
|
+
const networkData = NETWORKS[chainId];
|
|
51
|
+
|
|
52
|
+
if (!networkData) {
|
|
53
|
+
throw ErrorUnsupportedChainID;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
try {
|
|
57
|
+
const { leader } = await gqlFetch<{
|
|
58
|
+
leader: ILeader;
|
|
59
|
+
}>(networkData.subgraphUrl, GET_LEADER_QUERY, {
|
|
60
|
+
address: address.toLowerCase(),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
return leader;
|
|
64
|
+
} catch (e) {
|
|
65
|
+
return throwError(e);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* This function returns all the leader details of the protocol.
|
|
71
|
+
*
|
|
72
|
+
* @param {ILeadersFilter} filter Filter for the leaders.
|
|
73
|
+
* @returns {ILeader[]} Returns an array with all the leader details.
|
|
74
|
+
*
|
|
75
|
+
*
|
|
76
|
+
* **Code example**
|
|
77
|
+
*
|
|
78
|
+
* ```ts
|
|
79
|
+
* import { OperatorUtils } from '@human-protocol/sdk';
|
|
80
|
+
*
|
|
81
|
+
* const leaders = await OperatorUtils.getLeaders();
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
public static async getLeaders(
|
|
85
|
+
filter: ILeadersFilter = { networks: [ChainId.POLYGON_MUMBAI] }
|
|
86
|
+
): Promise<ILeader[]> {
|
|
87
|
+
try {
|
|
88
|
+
let leaders_data: ILeader[] = [];
|
|
89
|
+
for (const chainId of filter.networks) {
|
|
90
|
+
const networkData = NETWORKS[chainId];
|
|
91
|
+
|
|
92
|
+
if (!networkData) {
|
|
93
|
+
throw ErrorUnsupportedChainID;
|
|
94
|
+
}
|
|
95
|
+
const { leaders } = await gqlFetch<{
|
|
96
|
+
leaders: ILeader[];
|
|
97
|
+
}>(networkData.subgraphUrl, GET_LEADERS_QUERY(filter), {
|
|
98
|
+
role: filter.role,
|
|
99
|
+
});
|
|
100
|
+
leaders_data = leaders_data.concat(leaders);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return leaders_data;
|
|
104
|
+
} catch (e) {
|
|
105
|
+
return throwError(e);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Retrieves the reputation network operators of the specified address.
|
|
111
|
+
*
|
|
112
|
+
* @param {string} address - Address of the reputation oracle.
|
|
113
|
+
* @param {string} [role] - (Optional) Role of the operator.
|
|
114
|
+
* @returns {Promise<IOperator[]>} - Returns an array of operator details.
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* import { OperatorUtils, ChainId } from '@human-protocol/sdk';
|
|
119
|
+
*
|
|
120
|
+
* const operators = await OperatorUtils.getReputationNetworkOperators(ChainId.POLYGON_MUMBAI, '0x62dD51230A30401C455c8398d06F85e4EaB6309f');
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
public static async getReputationNetworkOperators(
|
|
124
|
+
chainId: ChainId,
|
|
125
|
+
address: string,
|
|
126
|
+
role?: string
|
|
127
|
+
): Promise<IOperator[]> {
|
|
128
|
+
const networkData = NETWORKS[chainId];
|
|
129
|
+
|
|
130
|
+
if (!networkData) {
|
|
131
|
+
throw ErrorUnsupportedChainID;
|
|
132
|
+
}
|
|
133
|
+
try {
|
|
134
|
+
const { reputationNetwork } = await gqlFetch<{
|
|
135
|
+
reputationNetwork: IReputationNetwork;
|
|
136
|
+
}>(networkData.subgraphUrl, GET_REPUTATION_NETWORK_QUERY(role), {
|
|
137
|
+
address: address,
|
|
138
|
+
role: role,
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
return reputationNetwork.operators;
|
|
142
|
+
} catch (e) {
|
|
143
|
+
return throwError(e);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* This function returns information about the rewards for a given slasher address.
|
|
149
|
+
*
|
|
150
|
+
* @param {string} slasherAddress Slasher address.
|
|
151
|
+
* @returns {IReward[]} Returns an array of Reward objects that contain the rewards earned by the user through slashing other users.
|
|
152
|
+
*
|
|
153
|
+
*
|
|
154
|
+
* **Code example**
|
|
155
|
+
*
|
|
156
|
+
* ```ts
|
|
157
|
+
* import { OperatorUtils, ChainId } from '@human-protocol/sdk';
|
|
158
|
+
*
|
|
159
|
+
* const rewards = await OperatorUtils.getRewards(ChainId.POLYGON_MUMBAI, '0x62dD51230A30401C455c8398d06F85e4EaB6309f');
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
public static async getRewards(
|
|
163
|
+
chainId: ChainId,
|
|
164
|
+
slasherAddress: string
|
|
165
|
+
): Promise<IReward[]> {
|
|
166
|
+
if (!ethers.isAddress(slasherAddress)) {
|
|
167
|
+
throw ErrorInvalidSlasherAddressProvided;
|
|
168
|
+
}
|
|
169
|
+
const networkData = NETWORKS[chainId];
|
|
170
|
+
|
|
171
|
+
if (!networkData) {
|
|
172
|
+
throw ErrorUnsupportedChainID;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
try {
|
|
176
|
+
const { rewardAddedEvents } = await gqlFetch<{
|
|
177
|
+
rewardAddedEvents: RewardAddedEventData[];
|
|
178
|
+
}>(networkData.subgraphUrl, GET_REWARD_ADDED_EVENTS_QUERY, {
|
|
179
|
+
slasherAddress: slasherAddress.toLowerCase(),
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
return rewardAddedEvents.map((reward: any) => {
|
|
183
|
+
return {
|
|
184
|
+
escrowAddress: reward.escrow,
|
|
185
|
+
amount: reward.amount,
|
|
186
|
+
};
|
|
187
|
+
});
|
|
188
|
+
} catch (e) {
|
|
189
|
+
return throwError(e);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|