Skip to content

Commit ca405f6

Browse files
authored
[SDK] fix: statistics client for tasks and workers (#969)
* fix statistics client for tasks and workers * fix python sdk * fix im data api to return empty for non-polygon network
1 parent ee8aa1f commit ca405f6

File tree

15 files changed

+851
-200
lines changed

15 files changed

+851
-200
lines changed

packages/sdk/python/human-protocol-sdk/example.py

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,43 @@
22
import json
33
from web3 import Web3
44

5-
from human_protocol_sdk.escrow import EscrowFilter, EscrowUtils, Status
5+
from human_protocol_sdk.escrow import EscrowUtils, Status
6+
from human_protocol_sdk.filter import EscrowFilter
67
from human_protocol_sdk.staking import StakingClient, LeaderFilter
78
from human_protocol_sdk.statistics import StatisticsClient, StatisticsParam
89
from human_protocol_sdk.storage import StorageClient
910
from human_protocol_sdk.agreement import agreement
1011

11-
if __name__ == "__main__":
12-
alchemy_url = (
13-
"https://polygon-mumbai.g.alchemy.com/v2/lnog1fIT7pvL4_o3lkcosQ7PL08ed3nX"
14-
)
15-
w3 = Web3(Web3.HTTPProvider(alchemy_url))
12+
# Replace with your own Alchemy URL and IM API key
13+
ALCHEMY_URL = ""
14+
IM_API_KEY = ""
15+
1616

17+
def get_escrow_statistics(statistics_client: StatisticsClient):
18+
print(statistics_client.get_escrow_statistics())
1719
print(
18-
EscrowUtils.get_escrows(
19-
EscrowFilter(
20-
status=Status.Pending,
20+
statistics_client.get_escrow_statistics(
21+
StatisticsParam(
2122
date_from=datetime.datetime(2023, 5, 8),
2223
date_to=datetime.datetime(2023, 6, 8),
23-
networks=[80001],
2424
)
2525
)
2626
)
2727

28-
statistics_client = StatisticsClient(w3)
2928

30-
print(statistics_client.get_escrow_statistics())
29+
def get_task_statistics(statistics_client: StatisticsClient):
30+
print(statistics_client.get_task_statistics())
3131
print(
32-
statistics_client.get_escrow_statistics(
32+
statistics_client.get_task_statistics(
3333
StatisticsParam(
3434
date_from=datetime.datetime(2023, 5, 8),
3535
date_to=datetime.datetime(2023, 6, 8),
3636
)
3737
)
3838
)
3939

40+
41+
def get_worker_statistics(statistics_client: StatisticsClient):
4042
print(statistics_client.get_worker_statistics())
4143
print(
4244
statistics_client.get_worker_statistics(
@@ -47,6 +49,8 @@
4749
)
4850
)
4951

52+
53+
def get_payment_statistics(statistics_client: StatisticsClient):
5054
print(statistics_client.get_payment_statistics())
5155
print(
5256
statistics_client.get_payment_statistics(
@@ -56,6 +60,9 @@
5660
)
5761
)
5862
)
63+
64+
65+
def get_hmt_statistics(statistics_client: StatisticsClient):
5966
print(statistics_client.get_hmt_statistics())
6067
print(
6168
statistics_client.get_hmt_statistics(
@@ -66,6 +73,28 @@
6673
)
6774
)
6875

76+
77+
def get_escrows():
78+
print(
79+
EscrowUtils.get_escrows(
80+
EscrowFilter(
81+
status=Status.Pending,
82+
date_from=datetime.datetime(2023, 5, 8),
83+
date_to=datetime.datetime(2023, 6, 8),
84+
networks=[80001],
85+
)
86+
)
87+
)
88+
89+
90+
def get_leaders(staking_client: StakingClient):
91+
leaders = staking_client.get_leaders()
92+
print(leaders)
93+
print(staking_client.get_leader(leaders[0]["address"]))
94+
print(staking_client.get_leaders(LeaderFilter(role="Job Launcher")))
95+
96+
97+
def agreeement_example():
6998
# process annotation data and get quality estimates
7099
url = "https://raw.githubusercontent.com/humanprotocol/human-protocol/efa8d3789ac35915b42435011cd0a8d36507564c/packages/sdk/python/human-protocol-sdk/example_annotations.json"
71100
annotations = json.loads(StorageClient.download_file_from_url(url))
@@ -83,8 +112,25 @@
83112
print(report["results"])
84113
print(report["config"])
85114

86-
staking_client = StakingClient(w3)
87-
leaders = staking_client.get_leaders()
88-
print(leaders)
89-
print(staking_client.get_leader(leaders[0]["address"]))
90-
print(staking_client.get_leaders(LeaderFilter(role="Job Launcher")))
115+
116+
if __name__ == "__main__":
117+
alchemy_url = ALCHEMY_URL
118+
w3 = Web3(Web3.HTTPProvider(alchemy_url))
119+
120+
statistics_client = StatisticsClient(
121+
w3,
122+
IM_API_KEY,
123+
)
124+
125+
# Run single example while testing, and remove comments before commit
126+
127+
get_task_statistics(statistics_client)
128+
get_escrow_statistics(statistics_client)
129+
get_worker_statistics(statistics_client)
130+
get_payment_statistics(statistics_client)
131+
get_hmt_statistics(statistics_client)
132+
133+
agreeement_example()
134+
135+
get_escrows()
136+
get_leaders()

packages/sdk/python/human-protocol-sdk/human_protocol_sdk/escrow.py

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env python3
22

3-
import datetime
43
import logging
54
import os
65
from decimal import Decimal
76
from typing import List, Optional
87

98
from human_protocol_sdk.constants import NETWORKS, ChainId, Status
9+
from human_protocol_sdk.filter import EscrowFilter
1010
from human_protocol_sdk.utils import (
1111
get_data_from_subgraph,
1212
get_escrow_interface,
@@ -96,72 +96,6 @@ def __init__(
9696
self.hash = hash
9797

9898

99-
class EscrowFilter:
100-
"""
101-
A class used to filter escrow requests.
102-
"""
103-
104-
def __init__(
105-
self,
106-
networks: [List[ChainId]],
107-
launcher: Optional[str] = None,
108-
reputation_oracle: Optional[str] = None,
109-
recording_oracle: Optional[str] = None,
110-
exchange_oracle: Optional[str] = None,
111-
job_requester_id: Optional[str] = None,
112-
status: Optional[Status] = None,
113-
date_from: Optional[datetime.datetime] = None,
114-
date_to: Optional[datetime.datetime] = None,
115-
):
116-
"""
117-
Initializes a EscrowFilter instance.
118-
119-
Args:
120-
networks (List[ChainId]): Networks to request data
121-
launcher (Optional[str]): Launcher address
122-
reputation_oracle (Optional[str]): Reputation oracle address
123-
recording_oracle (Optional[str]): Recording oracle address
124-
exchange_oracle (Optional[str]): Exchange oracle address
125-
job_requester_id (Optional[str]): Job requester id
126-
status (Optional[Status]): Escrow status
127-
date_from (Optional[datetime.datetime]): Created from date
128-
date_to (Optional[datetime.datetime]): Created to date
129-
"""
130-
131-
if not networks or any(
132-
network not in set(chain_id.value for chain_id in ChainId)
133-
for network in networks
134-
):
135-
raise EscrowClientError(f"Invalid ChainId")
136-
137-
if launcher and not Web3.is_address(launcher):
138-
raise EscrowClientError(f"Invalid address: {launcher}")
139-
140-
if reputation_oracle and not Web3.is_address(reputation_oracle):
141-
raise EscrowClientError(f"Invalid address: {reputation_oracle}")
142-
143-
if recording_oracle and not Web3.is_address(recording_oracle):
144-
raise EscrowClientError(f"Invalid address: {recording_oracle}")
145-
146-
if exchange_oracle and not Web3.is_address(exchange_oracle):
147-
raise EscrowClientError(f"Invalid address: {exchange_oracle}")
148-
149-
if date_from and date_to and date_from > date_to:
150-
raise EscrowClientError(
151-
f"Invalid dates: {date_from} must be earlier than {date_to}"
152-
)
153-
154-
self.launcher = launcher
155-
self.reputation_oracle = reputation_oracle
156-
self.recording_oracle = recording_oracle
157-
self.exchange_oracle = exchange_oracle
158-
self.job_requester_id = job_requester_id
159-
self.status = status
160-
self.date_from = date_from
161-
self.date_to = date_to
162-
self.networks = networks
163-
164-
16599
class EscrowClient:
166100
"""
167101
A class used to manage escrow on the HUMAN network.
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#!/usr/bin/env python3
2+
3+
import datetime
4+
5+
from typing import List, Optional
6+
7+
from human_protocol_sdk.constants import NETWORKS, ChainId, Status
8+
9+
from web3 import Web3
10+
11+
12+
class FilterError(Exception):
13+
"""
14+
Raises when some error happens when building filter object.
15+
"""
16+
17+
pass
18+
19+
20+
class EscrowFilter:
21+
"""
22+
A class used to filter escrow requests.
23+
"""
24+
25+
def __init__(
26+
self,
27+
networks: [List[ChainId]],
28+
launcher: Optional[str] = None,
29+
reputation_oracle: Optional[str] = None,
30+
recording_oracle: Optional[str] = None,
31+
exchange_oracle: Optional[str] = None,
32+
job_requester_id: Optional[str] = None,
33+
status: Optional[Status] = None,
34+
date_from: Optional[datetime.datetime] = None,
35+
date_to: Optional[datetime.datetime] = None,
36+
):
37+
"""
38+
Initializes a EscrowFilter instance.
39+
40+
Args:
41+
networks (List[ChainId]): Networks to request data
42+
launcher (Optional[str]): Launcher address
43+
reputation_oracle (Optional[str]): Reputation oracle address
44+
recording_oracle (Optional[str]): Recording oracle address
45+
exchange_oracle (Optional[str]): Exchange oracle address
46+
job_requester_id (Optional[str]): Job requester id
47+
status (Optional[Status]): Escrow status
48+
date_from (Optional[datetime.datetime]): Created from date
49+
date_to (Optional[datetime.datetime]): Created to date
50+
"""
51+
52+
if not networks or any(
53+
network not in set(chain_id.value for chain_id in ChainId)
54+
for network in networks
55+
):
56+
raise FilterError(f"Invalid ChainId")
57+
58+
if launcher and not Web3.is_address(launcher):
59+
raise FilterError(f"Invalid address: {launcher}")
60+
61+
if reputation_oracle and not Web3.is_address(reputation_oracle):
62+
raise FilterError(f"Invalid address: {reputation_oracle}")
63+
64+
if recording_oracle and not Web3.is_address(recording_oracle):
65+
raise FilterError(f"Invalid address: {recording_oracle}")
66+
67+
if exchange_oracle and not Web3.is_address(exchange_oracle):
68+
raise FilterError(f"Invalid address: {exchange_oracle}")
69+
70+
if date_from and date_to and date_from > date_to:
71+
raise FilterError(
72+
f"Invalid dates: {date_from} must be earlier than {date_to}"
73+
)
74+
75+
self.launcher = launcher
76+
self.reputation_oracle = reputation_oracle
77+
self.recording_oracle = recording_oracle
78+
self.exchange_oracle = exchange_oracle
79+
self.job_requester_id = job_requester_id
80+
self.status = status
81+
self.date_from = date_from
82+
self.date_to = date_to
83+
self.networks = networks
84+
85+
86+
class PayoutFilter:
87+
"""
88+
A class used to filter payout requests.
89+
"""
90+
91+
def __init__(
92+
self,
93+
escrow_address: Optional[str] = None,
94+
recipient: Optional[str] = None,
95+
date_from: Optional[datetime.datetime] = None,
96+
date_to: Optional[datetime.datetime] = None,
97+
):
98+
"""
99+
Initializes a PayoutFilter instance.
100+
101+
Args:
102+
escrow_address (Optional[str]): Escrow address
103+
recipient (Optional[str]): Recipient address
104+
date_from (Optional[datetime.datetime]): Created from date
105+
date_to (Optional[datetime.datetime]): Created to date
106+
"""
107+
108+
if escrow_address and not Web3.is_address(escrow_address):
109+
raise FilterError(f"Invalid address: {escrow_address}")
110+
111+
if recipient and not Web3.is_address(recipient):
112+
raise FilterError(f"Invalid address: {recipient}")
113+
114+
if date_from and date_to and date_from > date_to:
115+
raise FilterError(
116+
f"Invalid dates: {date_from} must be earlier than {date_to}"
117+
)
118+
119+
self.escrow_address = escrow_address
120+
self.recipient = recipient
121+
self.date_from = date_from
122+
self.date_to = date_to

packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/escrow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from human_protocol_sdk.escrow import EscrowFilter
1+
from human_protocol_sdk.filter import EscrowFilter
22

33
escrow_fragment = """
44
fragment EscrowFields on Escrow {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from human_protocol_sdk.filter import PayoutFilter
2+
3+
payout_fragment = """
4+
fragment PayoutFields on Payout {
5+
id
6+
escrowAddress
7+
recipient
8+
amount
9+
createdAt
10+
}
11+
"""
12+
13+
14+
def get_payouts_query(filter: PayoutFilter):
15+
return """
16+
query GetPayouts(
17+
$escrowAddress: String
18+
$recipient: String
19+
$from: Int
20+
$to: Int
21+
) {{
22+
payouts(
23+
where: {{
24+
{escrow_address_clause}
25+
{recipient_clause}
26+
{from_clause}
27+
{to_clause}
28+
}}
29+
) {{
30+
...PayoutFields
31+
}}
32+
}}
33+
{payout_fragment}
34+
""".format(
35+
payout_fragment=payout_fragment,
36+
escrow_address_clause="escrowAddress: $escrowAddress"
37+
if filter.escrow_address
38+
else "",
39+
recipient_clause="recipient: $recipient" if filter.recipient else "",
40+
from_clause="createdAt_gte: $from" if filter.date_from else "",
41+
to_clause="createdAt_lte: $to" if filter.date_from else "",
42+
)

0 commit comments

Comments
 (0)