@human-protocol/sdk 1.1.9 → 1.1.11

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 (41) hide show
  1. package/dist/constants.js +8 -8
  2. package/dist/escrow.d.ts +12 -12
  3. package/dist/escrow.d.ts.map +1 -1
  4. package/dist/escrow.js +42 -30
  5. package/dist/graphql/queries/escrow.d.ts +2 -2
  6. package/dist/graphql/queries/escrow.d.ts.map +1 -1
  7. package/dist/graphql/queries/escrow.js +30 -26
  8. package/dist/graphql/queries/hmtoken.d.ts +2 -0
  9. package/dist/graphql/queries/hmtoken.d.ts.map +1 -0
  10. package/dist/graphql/queries/hmtoken.js +21 -0
  11. package/dist/graphql/queries/index.d.ts +2 -0
  12. package/dist/graphql/queries/index.d.ts.map +1 -1
  13. package/dist/graphql/queries/index.js +2 -0
  14. package/dist/graphql/queries/statistics.d.ts +5 -0
  15. package/dist/graphql/queries/statistics.d.ts.map +1 -0
  16. package/dist/graphql/queries/statistics.js +87 -0
  17. package/dist/graphql/types.d.ts +91 -0
  18. package/dist/graphql/types.d.ts.map +1 -1
  19. package/dist/index.d.ts +2 -1
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +3 -1
  22. package/dist/interfaces.d.ts +9 -3
  23. package/dist/interfaces.d.ts.map +1 -1
  24. package/dist/statistics.d.ts +45 -0
  25. package/dist/statistics.d.ts.map +1 -0
  26. package/dist/statistics.js +144 -0
  27. package/dist/storage.d.ts +2 -2
  28. package/dist/storage.d.ts.map +1 -1
  29. package/dist/storage.js +4 -4
  30. package/package.json +1 -1
  31. package/src/constants.ts +8 -8
  32. package/src/escrow.ts +51 -36
  33. package/src/graphql/queries/escrow.ts +31 -25
  34. package/src/graphql/queries/hmtoken.ts +17 -0
  35. package/src/graphql/queries/index.ts +2 -0
  36. package/src/graphql/queries/statistics.ts +88 -0
  37. package/src/graphql/types.ts +105 -0
  38. package/src/index.ts +8 -1
  39. package/src/interfaces.ts +16 -3
  40. package/src/statistics.ts +196 -0
  41. package/src/storage.ts +4 -4
@@ -0,0 +1,88 @@
1
+ import gql from 'graphql-tag';
2
+ import { IStatisticsParams } from '../../interfaces';
3
+
4
+ const HMTOKEN_STATISTICS_FRAGMENT = gql`
5
+ fragment HMTokenStatisticsFields on HMTokenStatistics {
6
+ totalTransferEventCount
7
+ totalBulkTransferEventCount
8
+ totalApprovalEventCount
9
+ totalBulkApprovalEventCount
10
+ totalValueTransfered
11
+ holders
12
+ }
13
+ `;
14
+
15
+ const ESCROW_STATISTICS_FRAGMENT = gql`
16
+ fragment EscrowStatisticsFields on EscrowStatistics {
17
+ fundEventCount
18
+ setupEventCount
19
+ storeResultsEventCount
20
+ bulkPayoutEventCount
21
+ pendingStatusEventCount
22
+ cancelledStatusEventCount
23
+ partialStatusEventCount
24
+ paidStatusEventCount
25
+ completedStatusEventCount
26
+ totalEventCount
27
+ totalEscrowCount
28
+ }
29
+ `;
30
+
31
+ const EVENT_DAY_DATA_FRAGMENT = gql`
32
+ fragment EventDayDataFields on EventDayData {
33
+ timestamp
34
+ dailyFundEventCount
35
+ dailySetupEventCount
36
+ dailyStoreResultsEventCount
37
+ dailyBulkPayoutEventCount
38
+ dailyPendingStatusEventCount
39
+ dailyCancelledStatusEventCount
40
+ dailyPartialStatusEventCount
41
+ dailyPaidStatusEventCount
42
+ dailyCompletedStatusEventCount
43
+ dailyTotalEventCount
44
+ dailyEscrowCount
45
+ dailyWorkerCount
46
+ dailyPayoutCount
47
+ dailyPayoutAmount
48
+ dailyHMTTransferCount
49
+ dailyHMTTransferAmount
50
+ }
51
+ `;
52
+
53
+ export const GET_HMTOKEN_STATISTICS_QUERY = gql`
54
+ query GetHMTokenStatistics {
55
+ hmtokenStatistics(id: "hmt-statistics-id") {
56
+ ...HMTokenStatisticsFields
57
+ }
58
+ }
59
+ ${HMTOKEN_STATISTICS_FRAGMENT}
60
+ `;
61
+
62
+ export const GET_ESCROW_STATISTICS_QUERY = gql`
63
+ query GetEscrowStatistics {
64
+ escrowStatistics(id: "escrow-statistics-id") {
65
+ ...EscrowStatisticsFields
66
+ }
67
+ }
68
+ ${ESCROW_STATISTICS_FRAGMENT}
69
+ `;
70
+
71
+ export const GET_EVENT_DAY_DATA_QUERY = (params: IStatisticsParams) => {
72
+ const { from, to } = params;
73
+ const WHERE_CLAUSE = `
74
+ where: {
75
+ ${from !== undefined ? `timestamp_gte: $from` : ''}
76
+ ${to !== undefined ? `timestamp_lte: $to` : ''}
77
+ }
78
+ `;
79
+
80
+ return gql`
81
+ query GetEscrowDayData($from: Int, $to: Int) {
82
+ eventDayDatas(${WHERE_CLAUSE}) {
83
+ ...EventDayDataFields
84
+ }
85
+ }
86
+ ${EVENT_DAY_DATA_FRAGMENT}
87
+ `;
88
+ };
@@ -1,3 +1,5 @@
1
+ import { BigNumber } from 'ethers';
2
+
1
3
  export type EscrowData = {
2
4
  id: string;
3
5
  address: string;
@@ -17,6 +19,50 @@ export type EscrowData = {
17
19
  status: string;
18
20
  token: string;
19
21
  totalFundedAmount: string;
22
+ createdAt: string;
23
+ };
24
+
25
+ export type HMTStatisticsData = {
26
+ totalTransferEventCount: string;
27
+ totalBulkTransferEventCount: string;
28
+ totalApprovalEventCount: string;
29
+ totalBulkApprovalEventCount: string;
30
+ totalValueTransfered: string;
31
+ holders: string;
32
+ };
33
+
34
+ export type EscrowStatisticsData = {
35
+ fundEventCount: string;
36
+ setupEventCount: string;
37
+ storeResultsEventCount: string;
38
+ bulkPayoutEventCount: string;
39
+ pendingStatusEventCount: string;
40
+ cancelledStatusEventCount: string;
41
+ partialStatusEventCount: string;
42
+ paidStatusEventCount: string;
43
+ completedStatusEventCount: string;
44
+ totalEventCount: string;
45
+ totalEscrowCount: string;
46
+ };
47
+
48
+ export type EventDayData = {
49
+ timestamp: string;
50
+ dailyFundEventCount: string;
51
+ dailySetupEventCount: string;
52
+ dailyStoreResultsEventCount: string;
53
+ dailyBulkPayoutEventCount: string;
54
+ dailyPendingStatusEventCount: string;
55
+ dailyCancelledStatusEventCount: string;
56
+ dailyPartialStatusEventCount: string;
57
+ dailyPaidStatusEventCount: string;
58
+ dailyCompletedStatusEventCount: string;
59
+ dailyTotalEventCount: string;
60
+ dailyEscrowCount: string;
61
+ dailyWorkerCount: string;
62
+ dailyPayoutCount: string;
63
+ dailyPayoutAmount: string;
64
+ dailyHMTTransferCount: string;
65
+ dailyHMTTransferAmount: string;
20
66
  };
21
67
 
22
68
  export type RewardAddedEventData = {
@@ -25,3 +71,62 @@ export type RewardAddedEventData = {
25
71
  slasher: string;
26
72
  amount: string;
27
73
  };
74
+
75
+ export type DailyEscrowData = {
76
+ timestamp: Date;
77
+ escrowsTotal: number;
78
+ escrowsPending: number;
79
+ escrowsSolved: number;
80
+ escrowsPaid: number;
81
+ escrowsCancelled: number;
82
+ };
83
+
84
+ export type EscrowStatistics = {
85
+ totalEscrows: number;
86
+ dailyEscrowsData: DailyEscrowData[];
87
+ };
88
+
89
+ export type DailyWorkerData = {
90
+ timestamp: Date;
91
+ activeWorkers: number;
92
+ averageJobsSolved: number;
93
+ };
94
+
95
+ export type WorkerStatistics = {
96
+ dailyWorkersData: DailyWorkerData[];
97
+ };
98
+
99
+ export type DailyPaymentData = {
100
+ timestamp: Date;
101
+ totalAmountPaid: BigNumber;
102
+ totalCount: number;
103
+ averageAmountPerJob: BigNumber;
104
+ averageAmountPerWorker: BigNumber;
105
+ };
106
+
107
+ export type PaymentStatistics = {
108
+ dailyPaymentsData: DailyPaymentData[];
109
+ };
110
+
111
+ export type HMTHolderData = {
112
+ address: string;
113
+ balance: string;
114
+ };
115
+
116
+ export type HMTHolder = {
117
+ address: string;
118
+ balance: BigNumber;
119
+ };
120
+
121
+ export type DailyHMTData = {
122
+ timestamp: Date;
123
+ totalTransactionAmount: BigNumber;
124
+ totalTransactionCount: number;
125
+ };
126
+
127
+ export type HMTStatistics = {
128
+ totalTransferAmount: BigNumber;
129
+ totalHolders: number;
130
+ holders: HMTHolder[];
131
+ dailyHMTData: DailyHMTData[];
132
+ };
package/src/index.ts CHANGED
@@ -2,10 +2,17 @@ import { StakingClient } from './staking';
2
2
  import { StorageClient } from './storage';
3
3
  import { KVStoreClient } from './kvstore';
4
4
  import { EscrowClient } from './escrow';
5
+ import { StatisticsClient } from './statistics';
5
6
 
6
7
  export * from './constants';
7
8
  export * from './types';
8
9
  export * from './enums';
9
10
  export * from './interfaces';
10
11
 
11
- export { StakingClient, StorageClient, KVStoreClient, EscrowClient };
12
+ export {
13
+ StakingClient,
14
+ StorageClient,
15
+ KVStoreClient,
16
+ EscrowClient,
17
+ StatisticsClient,
18
+ };
package/src/interfaces.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { BigNumber } from 'ethers';
2
- import { EscrowStatus } from './types';
3
2
 
4
3
  export interface IAllocation {
5
4
  escrowAddress: string;
@@ -23,9 +22,18 @@ export interface IStaker {
23
22
  tokensAvailable: BigNumber;
24
23
  }
25
24
 
25
+ type EscrowStatus =
26
+ | 'Launched'
27
+ | 'Pending'
28
+ | 'Partial'
29
+ | 'Paid'
30
+ | 'Complete'
31
+ | 'Cancelled';
32
+
26
33
  export interface IEscrowsFilter {
27
- launcherAddress?: string;
28
- role?: number;
34
+ launcher?: string;
35
+ reputationOracle?: string;
36
+ recordingOracle?: string;
29
37
  status?: EscrowStatus;
30
38
  from?: Date;
31
39
  to?: Date;
@@ -46,3 +54,8 @@ export interface IKeyPair {
46
54
  passphrase: string;
47
55
  revocationCertificate?: string;
48
56
  }
57
+
58
+ export interface IStatisticsParams {
59
+ from?: Date;
60
+ to?: Date;
61
+ }
@@ -0,0 +1,196 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import gqlFetch from 'graphql-request';
3
+
4
+ import {
5
+ GET_ESCROW_STATISTICS_QUERY,
6
+ GET_EVENT_DAY_DATA_QUERY,
7
+ GET_HOLDERS_QUERY,
8
+ GET_HMTOKEN_STATISTICS_QUERY,
9
+ EscrowStatistics,
10
+ EscrowStatisticsData,
11
+ EventDayData,
12
+ HMTStatistics,
13
+ HMTStatisticsData,
14
+ PaymentStatistics,
15
+ WorkerStatistics,
16
+ HMTHolderData,
17
+ } from './graphql';
18
+ import { IStatisticsParams } from './interfaces';
19
+ import { NetworkData } from './types';
20
+ import { throwError } from './utils';
21
+ import { BigNumber } from 'ethers';
22
+
23
+ export class StatisticsClient {
24
+ public network: NetworkData;
25
+
26
+ /**
27
+ * **StatisticsClient constructor**
28
+ *
29
+ * @param {NetworkData} network - The network information required to connect to the Statistics contract
30
+ */
31
+ constructor(network: NetworkData) {
32
+ this.network = network;
33
+ }
34
+
35
+ /**
36
+ * Returns the escrow statistics data for the given date range
37
+ *
38
+ * @param {IStatisticsParams} params - Filter parameters.
39
+ * @returns {Promise<EscrowStatistics>}
40
+ * @throws {Error} - An error object if an error occurred.
41
+ */
42
+ async getEscrowStatistics(
43
+ params: IStatisticsParams = {}
44
+ ): Promise<EscrowStatistics> {
45
+ try {
46
+ const { escrowStatistics } = await gqlFetch<{
47
+ escrowStatistics: EscrowStatisticsData;
48
+ }>(this.network.subgraphUrl, GET_ESCROW_STATISTICS_QUERY);
49
+
50
+ const { eventDayDatas } = await gqlFetch<{
51
+ eventDayDatas: EventDayData[];
52
+ }>(this.network.subgraphUrl, GET_EVENT_DAY_DATA_QUERY(params), {
53
+ from: params.from ? params.from.getTime() / 1000 : undefined,
54
+ to: params.to ? params.to.getTime() / 1000 : undefined,
55
+ });
56
+
57
+ return {
58
+ totalEscrows: +escrowStatistics.totalEscrowCount,
59
+ dailyEscrowsData: eventDayDatas.map((eventDayData) => ({
60
+ timestamp: new Date(+eventDayData.timestamp * 1000),
61
+ escrowsTotal: +eventDayData.dailyEscrowCount,
62
+ escrowsPending: +eventDayData.dailyPendingStatusEventCount,
63
+ escrowsSolved: +eventDayData.dailyCompletedStatusEventCount,
64
+ escrowsPaid: +eventDayData.dailyPaidStatusEventCount,
65
+ escrowsCancelled: +eventDayData.dailyCancelledStatusEventCount,
66
+ })),
67
+ };
68
+ } catch (e: any) {
69
+ return throwError(e);
70
+ }
71
+ }
72
+
73
+ /**
74
+ * Returns the worker statistics data for the given date range
75
+ *
76
+ * @param {IStatisticsParams} params - Filter parameters.
77
+ * @returns {Promise<WorkerStatistics>}
78
+ * @throws {Error} - An error object if an error occurred.
79
+ */
80
+ async getWorkerStatistics(
81
+ params: IStatisticsParams = {}
82
+ ): Promise<WorkerStatistics> {
83
+ try {
84
+ const { eventDayDatas } = await gqlFetch<{
85
+ eventDayDatas: EventDayData[];
86
+ }>(this.network.subgraphUrl, GET_EVENT_DAY_DATA_QUERY(params), {
87
+ from: params.from ? params.from.getTime() / 1000 : undefined,
88
+ to: params.to ? params.to.getTime() / 1000 : undefined,
89
+ });
90
+
91
+ return {
92
+ dailyWorkersData: eventDayDatas.map((eventDayData) => ({
93
+ timestamp: new Date(+eventDayData.timestamp * 1000),
94
+ activeWorkers: +eventDayData.dailyWorkerCount,
95
+ averageJobsSolved:
96
+ eventDayData.dailyWorkerCount === '0'
97
+ ? 0
98
+ : +eventDayData.dailyBulkPayoutEventCount /
99
+ +eventDayData.dailyWorkerCount,
100
+ })),
101
+ };
102
+ } catch (e: any) {
103
+ return throwError(e);
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Returns the payment statistics data for the given date range
109
+ *
110
+ * @param {IStatisticsParams} params - Filter parameters.
111
+ * @returns {Promise<PaymentStatistics>}
112
+ * @throws {Error} - An error object if an error occurred.
113
+ */
114
+ async getPaymentStatistics(
115
+ params: IStatisticsParams = {}
116
+ ): Promise<PaymentStatistics> {
117
+ try {
118
+ const { eventDayDatas } = await gqlFetch<{
119
+ eventDayDatas: EventDayData[];
120
+ }>(this.network.subgraphUrl, GET_EVENT_DAY_DATA_QUERY(params), {
121
+ from: params.from ? params.from.getTime() / 1000 : undefined,
122
+ to: params.to ? params.to.getTime() / 1000 : undefined,
123
+ });
124
+
125
+ return {
126
+ dailyPaymentsData: eventDayDatas.map((eventDayData) => ({
127
+ timestamp: new Date(+eventDayData.timestamp * 1000),
128
+ totalAmountPaid: BigNumber.from(eventDayData.dailyPayoutAmount),
129
+ totalCount: +eventDayData.dailyPayoutCount,
130
+ averageAmountPerJob:
131
+ eventDayData.dailyBulkPayoutEventCount === '0'
132
+ ? BigNumber.from(0)
133
+ : BigNumber.from(eventDayData.dailyPayoutAmount).div(
134
+ eventDayData.dailyBulkPayoutEventCount
135
+ ),
136
+ averageAmountPerWorker:
137
+ eventDayData.dailyWorkerCount === '0'
138
+ ? BigNumber.from(0)
139
+ : BigNumber.from(eventDayData.dailyPayoutAmount).div(
140
+ eventDayData.dailyWorkerCount
141
+ ),
142
+ })),
143
+ };
144
+ } catch (e: any) {
145
+ return throwError(e);
146
+ }
147
+ }
148
+
149
+ /**
150
+ * Returns the HMToken statistics data for the given date range
151
+ *
152
+ * @param {IStatisticsParams} params - Filter parameters.
153
+ * @returns {Promise<HMTStatistics>}
154
+ * @throws {Error} - An error object if an error occurred.
155
+ */
156
+ async getHMTStatistics(
157
+ params: IStatisticsParams = {}
158
+ ): Promise<HMTStatistics> {
159
+ try {
160
+ const { hmtokenStatistics } = await gqlFetch<{
161
+ hmtokenStatistics: HMTStatisticsData;
162
+ }>(this.network.subgraphUrl, GET_HMTOKEN_STATISTICS_QUERY);
163
+
164
+ const { holders } = await gqlFetch<{
165
+ holders: HMTHolderData[];
166
+ }>(this.network.subgraphUrl, GET_HOLDERS_QUERY);
167
+
168
+ const { eventDayDatas } = await gqlFetch<{
169
+ eventDayDatas: EventDayData[];
170
+ }>(this.network.subgraphUrl, GET_EVENT_DAY_DATA_QUERY(params), {
171
+ from: params.from ? params.from.getTime() / 1000 : undefined,
172
+ to: params.to ? params.to.getTime() / 1000 : undefined,
173
+ });
174
+
175
+ return {
176
+ totalTransferAmount: BigNumber.from(
177
+ hmtokenStatistics.totalValueTransfered
178
+ ),
179
+ totalHolders: +hmtokenStatistics.holders,
180
+ holders: holders.map((holder) => ({
181
+ address: holder.address,
182
+ balance: BigNumber.from(holder.balance),
183
+ })),
184
+ dailyHMTData: eventDayDatas.map((eventDayData) => ({
185
+ timestamp: new Date(+eventDayData.timestamp * 1000),
186
+ totalTransactionAmount: BigNumber.from(
187
+ eventDayData.dailyHMTTransferAmount
188
+ ),
189
+ totalTransactionCount: +eventDayData.dailyHMTTransferCount,
190
+ })),
191
+ };
192
+ } catch (e: any) {
193
+ return throwError(e);
194
+ }
195
+ }
196
+ }
package/src/storage.ts CHANGED
@@ -20,17 +20,17 @@ export class StorageClient {
20
20
  /**
21
21
  * **Storage client constructor**
22
22
  *
23
- * @param {StorageCredentials} credentials - Cloud storage access data
24
23
  * @param {StorageParams} params - Cloud storage params
24
+ * @param {StorageCredentials} credentials - Optional. Cloud storage access data. If credentials is not provided - use an anonymous access to the bucket
25
25
  */
26
- constructor(credentials: StorageCredentials, params: StorageParams) {
26
+ constructor(params: StorageParams, credentials?: StorageCredentials) {
27
27
  try {
28
28
  this.clientParams = params;
29
29
 
30
30
  this.client = new Minio.Client({
31
31
  ...params,
32
- accessKey: credentials.accessKey,
33
- secretKey: credentials.secretKey,
32
+ accessKey: credentials?.accessKey ?? '',
33
+ secretKey: credentials?.secretKey ?? '',
34
34
  });
35
35
  } catch (e) {
36
36
  throw ErrorStorageClientNotInitialized;