@human-protocol/sdk 2.1.2 → 3.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/constants.d.ts +1 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +68 -23
- package/dist/enums.d.ts +4 -2
- package/dist/enums.d.ts.map +1 -1
- package/dist/enums.js +3 -1
- package/dist/error.d.ts +9 -0
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +11 -2
- package/dist/escrow.d.ts +1 -2
- package/dist/escrow.d.ts.map +1 -1
- package/dist/escrow.js +4 -5
- package/dist/graphql/queries/kvstore.d.ts +2 -0
- package/dist/graphql/queries/kvstore.d.ts.map +1 -0
- package/dist/graphql/queries/kvstore.js +28 -0
- package/dist/graphql/queries/transaction.d.ts +4 -0
- package/dist/graphql/queries/transaction.d.ts.map +1 -0
- package/dist/graphql/queries/transaction.js +59 -0
- package/dist/graphql/types.d.ts +8 -0
- package/dist/graphql/types.d.ts.map +1 -1
- package/dist/interfaces.d.ts +33 -1
- package/dist/interfaces.d.ts.map +1 -1
- package/dist/kvstore.d.ts +84 -0
- package/dist/kvstore.d.ts.map +1 -1
- package/dist/kvstore.js +103 -1
- package/dist/operator.d.ts +5 -2
- package/dist/operator.d.ts.map +1 -1
- package/dist/operator.js +54 -17
- package/dist/statistics.d.ts +1 -0
- package/dist/statistics.d.ts.map +1 -1
- package/dist/statistics.js +8 -7
- package/dist/transaction.d.ts +69 -0
- package/dist/transaction.d.ts.map +1 -0
- package/dist/transaction.js +121 -0
- package/dist/types.d.ts +4 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +9 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +21 -1
- package/package.json +4 -4
- package/src/constants.ts +86 -22
- package/src/enums.ts +3 -1
- package/src/error.ts +15 -0
- package/src/escrow.ts +5 -6
- package/src/graphql/queries/kvstore.ts +23 -0
- package/src/graphql/queries/transaction.ts +59 -0
- package/src/graphql/types.ts +9 -0
- package/src/interfaces.ts +40 -1
- package/src/kvstore.ts +114 -1
- package/src/operator.ts +69 -24
- package/src/statistics.ts +10 -8
- package/src/transaction.ts +142 -0
- package/src/types.ts +4 -0
- package/src/utils.ts +25 -0
package/src/kvstore.ts
CHANGED
|
@@ -16,8 +16,12 @@ import {
|
|
|
16
16
|
ErrorProviderDoesNotExist,
|
|
17
17
|
ErrorUnsupportedChainID,
|
|
18
18
|
} from './error';
|
|
19
|
+
import gqlFetch from 'graphql-request';
|
|
19
20
|
import { NetworkData } from './types';
|
|
20
|
-
import { isValidUrl } from './utils';
|
|
21
|
+
import { getSubgraphUrl, isValidUrl } from './utils';
|
|
22
|
+
import { GET_KVSTORE_BY_ADDRESS_QUERY } from './graphql/queries/kvstore';
|
|
23
|
+
import { KVStoreData } from './graphql';
|
|
24
|
+
import { IKVStore } from './interfaces';
|
|
21
25
|
/**
|
|
22
26
|
* ## Introduction
|
|
23
27
|
*
|
|
@@ -410,3 +414,112 @@ export class KVStoreClient extends BaseEthersClient {
|
|
|
410
414
|
return publicKey;
|
|
411
415
|
}
|
|
412
416
|
}
|
|
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
|
+
* networks: [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
|
+
}
|
package/src/operator.ts
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
import gqlFetch from 'graphql-request';
|
|
3
3
|
import {
|
|
4
4
|
ILeader,
|
|
5
|
+
ILeaderSubgraph,
|
|
5
6
|
ILeadersFilter,
|
|
6
7
|
IOperator,
|
|
7
|
-
|
|
8
|
+
IReputationNetworkSubgraph,
|
|
8
9
|
IReward,
|
|
9
10
|
} from './interfaces';
|
|
10
11
|
import { GET_REWARD_ADDED_EVENTS_QUERY } from './graphql/queries/reward';
|
|
@@ -20,7 +21,7 @@ import {
|
|
|
20
21
|
ErrorInvalidStakerAddressProvided,
|
|
21
22
|
ErrorUnsupportedChainID,
|
|
22
23
|
} from './error';
|
|
23
|
-
import { throwError } from './utils';
|
|
24
|
+
import { getSubgraphUrl, throwError } from './utils';
|
|
24
25
|
import { ChainId } from './enums';
|
|
25
26
|
import { NETWORKS } from './constants';
|
|
26
27
|
|
|
@@ -55,12 +56,23 @@ export class OperatorUtils {
|
|
|
55
56
|
|
|
56
57
|
try {
|
|
57
58
|
const { leader } = await gqlFetch<{
|
|
58
|
-
leader:
|
|
59
|
-
}>(networkData
|
|
59
|
+
leader: ILeaderSubgraph;
|
|
60
|
+
}>(getSubgraphUrl(networkData), GET_LEADER_QUERY, {
|
|
60
61
|
address: address.toLowerCase(),
|
|
61
62
|
});
|
|
62
63
|
|
|
63
|
-
|
|
64
|
+
let jobTypes: string[] = [];
|
|
65
|
+
|
|
66
|
+
if (typeof leader.jobTypes === 'string') {
|
|
67
|
+
jobTypes = leader.jobTypes.split(',');
|
|
68
|
+
} else if (Array.isArray(leader.jobTypes)) {
|
|
69
|
+
jobTypes = leader.jobTypes;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
...leader,
|
|
74
|
+
jobTypes,
|
|
75
|
+
};
|
|
64
76
|
} catch (e) {
|
|
65
77
|
return throwError(e);
|
|
66
78
|
}
|
|
@@ -78,28 +90,48 @@ export class OperatorUtils {
|
|
|
78
90
|
* ```ts
|
|
79
91
|
* import { OperatorUtils } from '@human-protocol/sdk';
|
|
80
92
|
*
|
|
81
|
-
* const
|
|
93
|
+
* const filter: ILeadersFilter = {
|
|
94
|
+
* chainId: ChainId.POLYGON
|
|
95
|
+
* };
|
|
96
|
+
* const leaders = await OperatorUtils.getLeaders(filter);
|
|
82
97
|
* ```
|
|
83
98
|
*/
|
|
84
|
-
public static async getLeaders(
|
|
85
|
-
filter: ILeadersFilter = { networks: [ChainId.POLYGON_AMOY] }
|
|
86
|
-
): Promise<ILeader[]> {
|
|
99
|
+
public static async getLeaders(filter: ILeadersFilter): Promise<ILeader[]> {
|
|
87
100
|
try {
|
|
88
101
|
let leaders_data: ILeader[] = [];
|
|
89
|
-
for (const chainId of filter.networks) {
|
|
90
|
-
const networkData = NETWORKS[chainId];
|
|
91
102
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
103
|
+
const networkData = NETWORKS[filter.chainId];
|
|
104
|
+
|
|
105
|
+
if (!networkData) {
|
|
106
|
+
throw ErrorUnsupportedChainID;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const { leaders } = await gqlFetch<{
|
|
110
|
+
leaders: ILeaderSubgraph[];
|
|
111
|
+
}>(getSubgraphUrl(networkData), GET_LEADERS_QUERY(filter), {
|
|
112
|
+
role: filter?.role,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
if (!leaders) {
|
|
116
|
+
return [];
|
|
101
117
|
}
|
|
102
118
|
|
|
119
|
+
leaders_data = leaders_data.concat(
|
|
120
|
+
leaders.map((leader) => {
|
|
121
|
+
let jobTypes: string[] = [];
|
|
122
|
+
|
|
123
|
+
if (typeof leader.jobTypes === 'string') {
|
|
124
|
+
jobTypes = leader.jobTypes.split(',');
|
|
125
|
+
} else if (Array.isArray(leader.jobTypes)) {
|
|
126
|
+
jobTypes = leader.jobTypes;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
...leader,
|
|
131
|
+
jobTypes,
|
|
132
|
+
};
|
|
133
|
+
})
|
|
134
|
+
);
|
|
103
135
|
return leaders_data;
|
|
104
136
|
} catch (e) {
|
|
105
137
|
return throwError(e);
|
|
@@ -132,13 +164,26 @@ export class OperatorUtils {
|
|
|
132
164
|
}
|
|
133
165
|
try {
|
|
134
166
|
const { reputationNetwork } = await gqlFetch<{
|
|
135
|
-
reputationNetwork:
|
|
136
|
-
}>(networkData
|
|
167
|
+
reputationNetwork: IReputationNetworkSubgraph;
|
|
168
|
+
}>(getSubgraphUrl(networkData), GET_REPUTATION_NETWORK_QUERY(role), {
|
|
137
169
|
address: address.toLowerCase(),
|
|
138
170
|
role: role,
|
|
139
171
|
});
|
|
140
172
|
|
|
141
|
-
return reputationNetwork.operators
|
|
173
|
+
return reputationNetwork.operators.map((operator) => {
|
|
174
|
+
let jobTypes: string[] = [];
|
|
175
|
+
|
|
176
|
+
if (typeof operator.jobTypes === 'string') {
|
|
177
|
+
jobTypes = operator.jobTypes.split(',');
|
|
178
|
+
} else if (Array.isArray(operator.jobTypes)) {
|
|
179
|
+
jobTypes = operator.jobTypes;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return {
|
|
183
|
+
...operator,
|
|
184
|
+
jobTypes,
|
|
185
|
+
};
|
|
186
|
+
});
|
|
142
187
|
} catch (e) {
|
|
143
188
|
return throwError(e);
|
|
144
189
|
}
|
|
@@ -175,7 +220,7 @@ export class OperatorUtils {
|
|
|
175
220
|
try {
|
|
176
221
|
const { rewardAddedEvents } = await gqlFetch<{
|
|
177
222
|
rewardAddedEvents: RewardAddedEventData[];
|
|
178
|
-
}>(networkData
|
|
223
|
+
}>(getSubgraphUrl(networkData), GET_REWARD_ADDED_EVENTS_QUERY, {
|
|
179
224
|
slasherAddress: slasherAddress.toLowerCase(),
|
|
180
225
|
});
|
|
181
226
|
|
package/src/statistics.ts
CHANGED
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
} from './graphql';
|
|
19
19
|
import { IStatisticsParams } from './interfaces';
|
|
20
20
|
import { NetworkData } from './types';
|
|
21
|
-
import { throwError } from './utils';
|
|
21
|
+
import { getSubgraphUrl, throwError } from './utils';
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
24
|
* ## Introduction
|
|
@@ -59,6 +59,7 @@ import { throwError } from './utils';
|
|
|
59
59
|
*/
|
|
60
60
|
export class StatisticsClient {
|
|
61
61
|
public networkData: NetworkData;
|
|
62
|
+
public subgraphUrl: string;
|
|
62
63
|
|
|
63
64
|
/**
|
|
64
65
|
* **StatisticsClient constructor**
|
|
@@ -67,6 +68,7 @@ export class StatisticsClient {
|
|
|
67
68
|
*/
|
|
68
69
|
constructor(networkData: NetworkData) {
|
|
69
70
|
this.networkData = networkData;
|
|
71
|
+
this.subgraphUrl = getSubgraphUrl(networkData);
|
|
70
72
|
}
|
|
71
73
|
|
|
72
74
|
/**
|
|
@@ -124,11 +126,11 @@ export class StatisticsClient {
|
|
|
124
126
|
try {
|
|
125
127
|
const { escrowStatistics } = await gqlFetch<{
|
|
126
128
|
escrowStatistics: EscrowStatisticsData;
|
|
127
|
-
}>(this.
|
|
129
|
+
}>(this.subgraphUrl, GET_ESCROW_STATISTICS_QUERY);
|
|
128
130
|
|
|
129
131
|
const { eventDayDatas } = await gqlFetch<{
|
|
130
132
|
eventDayDatas: EventDayData[];
|
|
131
|
-
}>(this.
|
|
133
|
+
}>(this.subgraphUrl, GET_EVENT_DAY_DATA_QUERY(params), {
|
|
132
134
|
from: params.from ? params.from.getTime() / 1000 : undefined,
|
|
133
135
|
to: params.to ? params.to.getTime() / 1000 : undefined,
|
|
134
136
|
});
|
|
@@ -199,7 +201,7 @@ export class StatisticsClient {
|
|
|
199
201
|
try {
|
|
200
202
|
const { eventDayDatas } = await gqlFetch<{
|
|
201
203
|
eventDayDatas: EventDayData[];
|
|
202
|
-
}>(this.
|
|
204
|
+
}>(this.subgraphUrl, GET_EVENT_DAY_DATA_QUERY(params), {
|
|
203
205
|
from: params.from ? params.from.getTime() / 1000 : undefined,
|
|
204
206
|
to: params.to ? params.to.getTime() / 1000 : undefined,
|
|
205
207
|
});
|
|
@@ -288,7 +290,7 @@ export class StatisticsClient {
|
|
|
288
290
|
try {
|
|
289
291
|
const { eventDayDatas } = await gqlFetch<{
|
|
290
292
|
eventDayDatas: EventDayData[];
|
|
291
|
-
}>(this.
|
|
293
|
+
}>(this.subgraphUrl, GET_EVENT_DAY_DATA_QUERY(params), {
|
|
292
294
|
from: params.from ? params.from.getTime() / 1000 : undefined,
|
|
293
295
|
to: params.to ? params.to.getTime() / 1000 : undefined,
|
|
294
296
|
});
|
|
@@ -397,15 +399,15 @@ export class StatisticsClient {
|
|
|
397
399
|
try {
|
|
398
400
|
const { hmtokenStatistics } = await gqlFetch<{
|
|
399
401
|
hmtokenStatistics: HMTStatisticsData;
|
|
400
|
-
}>(this.
|
|
402
|
+
}>(this.subgraphUrl, GET_HMTOKEN_STATISTICS_QUERY);
|
|
401
403
|
|
|
402
404
|
const { holders } = await gqlFetch<{
|
|
403
405
|
holders: HMTHolderData[];
|
|
404
|
-
}>(this.
|
|
406
|
+
}>(this.subgraphUrl, GET_HOLDERS_QUERY);
|
|
405
407
|
|
|
406
408
|
const { eventDayDatas } = await gqlFetch<{
|
|
407
409
|
eventDayDatas: EventDayData[];
|
|
408
|
-
}>(this.
|
|
410
|
+
}>(this.subgraphUrl, GET_EVENT_DAY_DATA_QUERY(params), {
|
|
409
411
|
from: params.from ? params.from.getTime() / 1000 : undefined,
|
|
410
412
|
to: params.to ? params.to.getTime() / 1000 : undefined,
|
|
411
413
|
});
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import { ethers } from 'ethers';
|
|
3
|
+
import gqlFetch from 'graphql-request';
|
|
4
|
+
import { NETWORKS } from './constants';
|
|
5
|
+
import { ChainId } from './enums';
|
|
6
|
+
import {
|
|
7
|
+
ErrorCannotUseDateAndBlockSimultaneously,
|
|
8
|
+
ErrorInvalidHahsProvided,
|
|
9
|
+
ErrorUnsupportedChainID,
|
|
10
|
+
} from './error';
|
|
11
|
+
import {
|
|
12
|
+
GET_TRANSACTIONS_QUERY,
|
|
13
|
+
GET_TRANSACTION_QUERY,
|
|
14
|
+
} from './graphql/queries/transaction';
|
|
15
|
+
import { ITransaction, ITransactionsFilter } from './interfaces';
|
|
16
|
+
import { getSubgraphUrl } from './utils';
|
|
17
|
+
|
|
18
|
+
export class TransactionUtils {
|
|
19
|
+
/**
|
|
20
|
+
* This function returns the transaction data for the given hash.
|
|
21
|
+
*
|
|
22
|
+
* @param {ChainId} chainId The chain ID.
|
|
23
|
+
* @param {string} hash The transaction hash.
|
|
24
|
+
* @returns {Promise<ITransaction>} Returns the transaction details.
|
|
25
|
+
*
|
|
26
|
+
* **Code example**
|
|
27
|
+
*
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { TransactionUtils, ChainId } from '@human-protocol/sdk';
|
|
30
|
+
*
|
|
31
|
+
* const transaction = await TransactionUtils.getTransaction(ChainId.POLYGON, '0x62dD51230A30401C455c8398d06F85e4EaB6309f');
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
public static async getTransaction(
|
|
35
|
+
chainId: ChainId,
|
|
36
|
+
hash: string
|
|
37
|
+
): Promise<ITransaction> {
|
|
38
|
+
if (!ethers.isHexString(hash)) {
|
|
39
|
+
throw ErrorInvalidHahsProvided;
|
|
40
|
+
}
|
|
41
|
+
const networkData = NETWORKS[chainId];
|
|
42
|
+
|
|
43
|
+
if (!networkData) {
|
|
44
|
+
throw ErrorUnsupportedChainID;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const { transaction } = await gqlFetch<{
|
|
48
|
+
transaction: ITransaction;
|
|
49
|
+
}>(getSubgraphUrl(networkData), GET_TRANSACTION_QUERY, {
|
|
50
|
+
hash: hash.toLowerCase(),
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
return transaction;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* This function returns all transaction details based on the provided filter.
|
|
58
|
+
*
|
|
59
|
+
* > This uses Subgraph
|
|
60
|
+
*
|
|
61
|
+
* **Input parameters**
|
|
62
|
+
*
|
|
63
|
+
* ```ts
|
|
64
|
+
* interface ITransactionsFilter {
|
|
65
|
+
* networks: ChainId[]; // List of chain IDs to query.
|
|
66
|
+
* fromAddress?: string; // (Optional) The address from which transactions are sent.
|
|
67
|
+
* toAddress?: string; // (Optional) The address to which transactions are sent.
|
|
68
|
+
* startDate?: Date; // (Optional) The start date to filter transactions (inclusive).
|
|
69
|
+
* endDate?: Date; // (Optional) The end date to filter transactions (inclusive).
|
|
70
|
+
* startBlock?: number; // (Optional) The start block number to filter transactions (inclusive).
|
|
71
|
+
* endBlock?: number; // (Optional) The end block number to filter transactions (inclusive).
|
|
72
|
+
* }
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* ```ts
|
|
76
|
+
* type ITransaction = {
|
|
77
|
+
* block: number;
|
|
78
|
+
* txHash: string;
|
|
79
|
+
* from: string;
|
|
80
|
+
* to: string;
|
|
81
|
+
* timestamp: number;
|
|
82
|
+
* value: string;
|
|
83
|
+
* method: string;
|
|
84
|
+
* };
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* @param {ITransactionsFilter} filter Filter for the transactions.
|
|
88
|
+
* @returns {Promise<ITransaction[]>} Returns an array with all the transaction details.
|
|
89
|
+
*
|
|
90
|
+
* **Code example**
|
|
91
|
+
*
|
|
92
|
+
* ```ts
|
|
93
|
+
* import { TransactionUtils, ChainId } from '@human-protocol/sdk';
|
|
94
|
+
*
|
|
95
|
+
* const filter: ITransactionsFilter = {
|
|
96
|
+
* networks: [ChainId.POLYGON],
|
|
97
|
+
* startDate: new Date('2022-01-01'),
|
|
98
|
+
* endDate: new Date('2022-12-31')
|
|
99
|
+
* };
|
|
100
|
+
* const transactions = await TransactionUtils.getTransactions(filter);
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
public static async getTransactions(
|
|
104
|
+
filter: ITransactionsFilter
|
|
105
|
+
): Promise<ITransaction[]> {
|
|
106
|
+
if (
|
|
107
|
+
(!!filter.startDate || !!filter.endDate) &&
|
|
108
|
+
(!!filter.startBlock || !!filter.endBlock)
|
|
109
|
+
) {
|
|
110
|
+
throw ErrorCannotUseDateAndBlockSimultaneously;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const transactions_data: ITransaction[] = [];
|
|
114
|
+
for (const chainId of filter.networks) {
|
|
115
|
+
const networkData = NETWORKS[chainId];
|
|
116
|
+
if (!networkData) {
|
|
117
|
+
throw ErrorUnsupportedChainID;
|
|
118
|
+
}
|
|
119
|
+
const { transactions } = await gqlFetch<{
|
|
120
|
+
transactions: ITransaction[];
|
|
121
|
+
}>(getSubgraphUrl(networkData), GET_TRANSACTIONS_QUERY(filter), {
|
|
122
|
+
fromAddress: filter?.fromAddress,
|
|
123
|
+
toAddress: filter?.toAddress,
|
|
124
|
+
startDate: filter?.startDate
|
|
125
|
+
? Math.floor(filter?.startDate.getTime() / 1000)
|
|
126
|
+
: undefined,
|
|
127
|
+
endDate: filter.endDate
|
|
128
|
+
? Math.floor(filter.endDate.getTime() / 1000)
|
|
129
|
+
: undefined,
|
|
130
|
+
startBlock: filter.startBlock ? filter.startBlock : undefined,
|
|
131
|
+
endBlock: filter.endBlock ? filter.endBlock : undefined,
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
if (!transactions) {
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
transactions_data.push(...transactions);
|
|
139
|
+
}
|
|
140
|
+
return transactions_data;
|
|
141
|
+
}
|
|
142
|
+
}
|
package/src/types.ts
CHANGED
package/src/utils.ts
CHANGED
|
@@ -9,7 +9,10 @@ import {
|
|
|
9
9
|
NumericFault,
|
|
10
10
|
ReplacementUnderpriced,
|
|
11
11
|
TransactionReplaced,
|
|
12
|
+
WarnSubgraphApiKeyNotProvided,
|
|
12
13
|
} from './error';
|
|
14
|
+
import { NetworkData } from './types';
|
|
15
|
+
import { SUBGRAPH_API_KEY_PLACEHOLDER } from './constants';
|
|
13
16
|
|
|
14
17
|
/**
|
|
15
18
|
* **Handle and throw the error.*
|
|
@@ -49,3 +52,25 @@ export const isValidUrl = (url: string) => {
|
|
|
49
52
|
return false;
|
|
50
53
|
}
|
|
51
54
|
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* **Get the subgraph URL.*
|
|
58
|
+
*
|
|
59
|
+
* @param {NetworkData} networkData
|
|
60
|
+
* @param {string} apiKey
|
|
61
|
+
* @returns
|
|
62
|
+
*/
|
|
63
|
+
export const getSubgraphUrl = (networkData: NetworkData) => {
|
|
64
|
+
let subgraphUrl = networkData.subgraphUrl;
|
|
65
|
+
if (process.env.SUBGRAPH_API_KEY) {
|
|
66
|
+
subgraphUrl = networkData.subgraphUrlApiKey.replace(
|
|
67
|
+
SUBGRAPH_API_KEY_PLACEHOLDER,
|
|
68
|
+
process.env.SUBGRAPH_API_KEY
|
|
69
|
+
);
|
|
70
|
+
} else {
|
|
71
|
+
// eslint-disable-next-line no-console
|
|
72
|
+
console.warn(WarnSubgraphApiKeyNotProvided);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return subgraphUrl;
|
|
76
|
+
};
|