Skip to content

Commit 6b58513

Browse files
committed
add retry configuration to subgraph requests across both SDKs
Also fixed unit tests
1 parent 0369e2b commit 6b58513

File tree

27 files changed

+601
-204
lines changed

27 files changed

+601
-204
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from human_protocol_sdk.operator import OperatorUtils, OperatorFilter
1919
from human_protocol_sdk.agreement import agreement
2020
from human_protocol_sdk.staking.staking_utils import StakingUtils
21+
from human_protocol_sdk.utils import SubgraphRetryConfig
2122

2223

2324
def get_escrow_statistics(statistics_client: StatisticsClient):
@@ -162,7 +163,8 @@ def get_escrows():
162163
status=Status.Pending,
163164
date_from=datetime.datetime(2023, 5, 8),
164165
date_to=datetime.datetime(2023, 6, 8),
165-
)
166+
),
167+
SubgraphRetryConfig(3, 1000),
166168
)
167169
)
168170

@@ -232,12 +234,13 @@ def get_stakers_example():
232234
chain_id=ChainId.POLYGON_AMOY,
233235
order_by="lastDepositTimestamp",
234236
order_direction=OrderDirection.ASC,
235-
)
237+
),
238+
SubgraphRetryConfig(3, 1000),
236239
)
237240
print("Filtered stakers:", len(stakers))
238241

239242
if stakers:
240-
staker = StakingUtils.get_staker(ChainId.LOCALHOST, stakers[0].address)
243+
staker = StakingUtils.get_staker(ChainId.POLYGON_AMOY, stakers[0].address)
241244
print("Staker info:", staker.address)
242245
else:
243246
print("No stakers found.")

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
PayoutFilter,
3939
)
4040
from human_protocol_sdk.utils import (
41+
SubgraphRetryConfig,
4142
get_data_from_subgraph,
4243
)
4344

@@ -219,10 +220,12 @@ class EscrowUtils:
219220
@staticmethod
220221
def get_escrows(
221222
filter: EscrowFilter,
223+
retry_config: Optional[SubgraphRetryConfig] = None,
222224
) -> List[EscrowData]:
223225
"""Get an array of escrow addresses based on the specified filter parameters.
224226
225227
:param filter: Object containing all the necessary parameters to filter
228+
:param retry_config: Optional retry behaviour for subgraph requests
226229
227230
:return: List of escrows
228231
@@ -283,6 +286,7 @@ def get_escrows(
283286
"skip": filter.skip,
284287
"orderDirection": filter.order_direction.value,
285288
},
289+
retry_config=retry_config,
286290
)
287291

288292
if (
@@ -334,11 +338,13 @@ def get_escrows(
334338
def get_escrow(
335339
chain_id: ChainId,
336340
escrow_address: str,
341+
retry_config: Optional[SubgraphRetryConfig] = None,
337342
) -> Optional[EscrowData]:
338343
"""Returns the escrow for a given address.
339344
340345
:param chain_id: Network in which the escrow has been deployed
341346
:param escrow_address: Address of the escrow
347+
:param retry_config: Optional retry behaviour for subgraph requests
342348
343349
:return: Escrow data
344350
@@ -373,6 +379,7 @@ def get_escrow(
373379
params={
374380
"escrowAddress": escrow_address.lower(),
375381
},
382+
retry_config=retry_config,
376383
)
377384

378385
if (
@@ -414,11 +421,15 @@ def get_escrow(
414421
)
415422

416423
@staticmethod
417-
def get_status_events(filter: StatusEventFilter) -> List[StatusEvent]:
424+
def get_status_events(
425+
filter: StatusEventFilter,
426+
retry_config: Optional[SubgraphRetryConfig] = None,
427+
) -> List[StatusEvent]:
418428
"""
419429
Retrieve status events for specified networks and statuses within a date range.
420430
421431
:param filter: Object containing all the necessary parameters to filter status events.
432+
:param retry_config: Optional retry behaviour for subgraph requests
422433
423434
:return List[StatusEvent]: List of status events matching the query parameters.
424435
@@ -447,6 +458,7 @@ def get_status_events(filter: StatusEventFilter) -> List[StatusEvent]:
447458
"skip": filter.skip,
448459
"orderDirection": filter.order_direction.value,
449460
},
461+
retry_config=retry_config,
450462
)
451463

452464
if (
@@ -472,11 +484,15 @@ def get_status_events(filter: StatusEventFilter) -> List[StatusEvent]:
472484
return events_with_chain_id
473485

474486
@staticmethod
475-
def get_payouts(filter: PayoutFilter) -> List[Payout]:
487+
def get_payouts(
488+
filter: PayoutFilter,
489+
retry_config: Optional[SubgraphRetryConfig] = None,
490+
) -> List[Payout]:
476491
"""
477492
Fetch payouts from the subgraph based on the provided filter.
478493
479494
:param filter: Object containing all the necessary parameters to filter payouts.
495+
:param retry_config: Optional retry behaviour for subgraph requests
480496
481497
:return List[Payout]: List of payouts matching the query parameters.
482498
@@ -508,6 +524,7 @@ def get_payouts(filter: PayoutFilter) -> List[Payout]:
508524
"skip": filter.skip,
509525
"orderDirection": filter.order_direction.value,
510526
},
527+
retry_config=retry_config,
511528
)
512529

513530
if (
@@ -536,11 +553,13 @@ def get_payouts(filter: PayoutFilter) -> List[Payout]:
536553
@staticmethod
537554
def get_cancellation_refunds(
538555
filter: CancellationRefundFilter,
556+
retry_config: Optional[SubgraphRetryConfig] = None,
539557
) -> List[CancellationRefund]:
540558
"""
541559
Fetch cancellation refunds from the subgraph based on the provided filter.
542560
543561
:param filter: Object containing all the necessary parameters to filter cancellation refunds.
562+
:param retry_config: Optional retry behaviour for subgraph requests
544563
545564
:return List[CancellationRefund]: List of cancellation refunds matching the query parameters.
546565
@@ -572,6 +591,7 @@ def get_cancellation_refunds(
572591
"skip": filter.skip,
573592
"orderDirection": filter.order_direction.value,
574593
},
594+
retry_config=retry_config,
575595
)
576596

577597
if (
@@ -601,13 +621,16 @@ def get_cancellation_refunds(
601621

602622
@staticmethod
603623
def get_cancellation_refund(
604-
chain_id: ChainId, escrow_address: str
624+
chain_id: ChainId,
625+
escrow_address: str,
626+
retry_config: Optional[SubgraphRetryConfig] = None,
605627
) -> CancellationRefund:
606628
"""
607629
Returns the cancellation refund for a given escrow address.
608630
609631
:param chain_id: Network in which the escrow has been deployed
610632
:param escrow_address: Address of the escrow
633+
:param retry_config: Optional retry behaviour for subgraph requests
611634
612635
:return: CancellationRefund data or None
613636
@@ -641,6 +664,7 @@ def get_cancellation_refund(
641664
{
642665
"escrowAddress": escrow_address.lower(),
643666
},
667+
retry_config=retry_config,
644668
)
645669

646670
if (

packages/sdk/python/human-protocol-sdk/human_protocol_sdk/kvstore/kvstore_utils.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import requests
3030

3131
from human_protocol_sdk.constants import NETWORKS, ChainId, KVStoreKeys
32-
from human_protocol_sdk.utils import get_data_from_subgraph
32+
from human_protocol_sdk.utils import SubgraphRetryConfig, get_data_from_subgraph
3333

3434
from human_protocol_sdk.kvstore.kvstore_client import KVStoreClientError
3535

@@ -57,11 +57,13 @@ class KVStoreUtils:
5757
def get_kvstore_data(
5858
chain_id: ChainId,
5959
address: str,
60+
retry_config: Optional[SubgraphRetryConfig] = None,
6061
) -> Optional[List[KVStoreData]]:
6162
"""Returns the KVStore data for a given address.
6263
6364
:param chain_id: Network in which the KVStore data has been deployed
6465
:param address: Address of the KVStore
66+
:param retry_config: Optional retry behaviour for subgraph requests
6567
6668
:return: List of KVStore data
6769
@@ -94,6 +96,7 @@ def get_kvstore_data(
9496
params={
9597
"address": address.lower(),
9698
},
99+
retry_config=retry_config,
97100
)
98101

99102
if (
@@ -111,12 +114,18 @@ def get_kvstore_data(
111114
]
112115

113116
@staticmethod
114-
def get(chain_id: ChainId, address: str, key: str) -> str:
117+
def get(
118+
chain_id: ChainId,
119+
address: str,
120+
key: str,
121+
retry_config: Optional[SubgraphRetryConfig] = None,
122+
) -> str:
115123
"""Gets the value of a key-value pair in the contract.
116124
117125
:param chain_id: Network in which the KVStore data has been deployed
118126
:param address: The Ethereum address associated with the key-value pair
119127
:param key: The key of the key-value pair to get
128+
:param retry_config: Optional retry behaviour for subgraph requests
120129
121130
:return: The value of the key-value pair if it exists
122131
@@ -149,6 +158,7 @@ def get(chain_id: ChainId, address: str, key: str) -> str:
149158
"address": address.lower(),
150159
"key": key,
151160
},
161+
retry_config=retry_config,
152162
)
153163

154164
if (
@@ -163,13 +173,17 @@ def get(chain_id: ChainId, address: str, key: str) -> str:
163173

164174
@staticmethod
165175
def get_file_url_and_verify_hash(
166-
chain_id: ChainId, address: str, key: Optional[str] = "url"
176+
chain_id: ChainId,
177+
address: str,
178+
key: Optional[str] = "url",
179+
retry_config: Optional[SubgraphRetryConfig] = None,
167180
) -> str:
168181
"""Gets the URL value of the given entity, and verify its hash.
169182
170183
:param chain_id: Network in which the KVStore data has been deployed
171184
:param address: Address from which to get the URL value.
172185
:param key: Configurable URL key. `url` by default.
186+
:param retry_config: Optional retry behaviour for subgraph requests
173187
174188
:return url: The URL value of the given address if exists, and the content is valid
175189
@@ -189,8 +203,10 @@ def get_file_url_and_verify_hash(
189203
if not Web3.is_address(address):
190204
raise KVStoreClientError(f"Invalid address: {address}")
191205

192-
url = KVStoreUtils.get(chain_id, address, key)
193-
hash = KVStoreUtils.get(chain_id, address, key + "_hash")
206+
url = KVStoreUtils.get(chain_id, address, key, retry_config=retry_config)
207+
hash = KVStoreUtils.get(
208+
chain_id, address, key + "_hash", retry_config=retry_config
209+
)
194210

195211
if len(url) == 0:
196212
return url

packages/sdk/python/human-protocol-sdk/human_protocol_sdk/operator/operator_utils.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
from human_protocol_sdk.constants import NETWORKS, ChainId, OrderDirection
2727
from human_protocol_sdk.gql.reward import get_reward_added_events_query
28-
from human_protocol_sdk.utils import get_data_from_subgraph
28+
from human_protocol_sdk.utils import SubgraphRetryConfig, get_data_from_subgraph
2929
from web3 import Web3
3030

3131
LOG = logging.getLogger("human_protocol_sdk.operator")
@@ -198,10 +198,14 @@ class OperatorUtils:
198198
"""
199199

200200
@staticmethod
201-
def get_operators(filter: OperatorFilter) -> List[OperatorData]:
201+
def get_operators(
202+
filter: OperatorFilter,
203+
retry_config: Optional[SubgraphRetryConfig] = None,
204+
) -> List[OperatorData]:
202205
"""Get operators data of the protocol.
203206
204207
:param filter: Operator filter
208+
:param retry_config: Optional retry behaviour for subgraph requests
205209
206210
:return: List of operators data
207211
@@ -237,6 +241,7 @@ def get_operators(filter: OperatorFilter) -> List[OperatorData]:
237241
"first": filter.first,
238242
"skip": filter.skip,
239243
},
244+
retry_config=retry_config,
240245
)
241246

242247
if (
@@ -283,11 +288,13 @@ def get_operators(filter: OperatorFilter) -> List[OperatorData]:
283288
def get_operator(
284289
chain_id: ChainId,
285290
operator_address: str,
291+
retry_config: Optional[SubgraphRetryConfig] = None,
286292
) -> Optional[OperatorData]:
287293
"""Gets the operator details.
288294
289295
:param chain_id: Network in which the operator exists
290296
:param operator_address: Address of the operator
297+
:param retry_config: Optional retry behaviour for subgraph requests
291298
292299
:return: Operator data if exists, otherwise None
293300
@@ -318,6 +325,7 @@ def get_operator(
318325
network,
319326
query=get_operator_query,
320327
params={"address": operator_address.lower()},
328+
retry_config=retry_config,
321329
)
322330

323331
if (
@@ -359,12 +367,14 @@ def get_reputation_network_operators(
359367
chain_id: ChainId,
360368
address: str,
361369
role: Optional[str] = None,
370+
retry_config: Optional[SubgraphRetryConfig] = None,
362371
) -> List[OperatorData]:
363372
"""Get the reputation network operators of the specified address.
364373
365374
:param chain_id: Network in which the reputation network exists
366375
:param address: Address of the reputation oracle
367376
:param role: (Optional) Role of the operator
377+
:param retry_config: Optional retry behaviour for subgraph requests
368378
369379
:return: Returns an array of operator details
370380
@@ -395,6 +405,7 @@ def get_reputation_network_operators(
395405
network,
396406
query=get_reputation_network_query(role),
397407
params={"address": address.lower(), "role": role},
408+
retry_config=retry_config,
398409
)
399410

400411
if (
@@ -438,11 +449,16 @@ def get_reputation_network_operators(
438449
return result
439450

440451
@staticmethod
441-
def get_rewards_info(chain_id: ChainId, slasher: str) -> List[RewardData]:
452+
def get_rewards_info(
453+
chain_id: ChainId,
454+
slasher: str,
455+
retry_config: Optional[SubgraphRetryConfig] = None,
456+
) -> List[RewardData]:
442457
"""Get rewards of the given slasher.
443458
444459
:param chain_id: Network in which the slasher exists
445460
:param slasher: Address of the slasher
461+
:param retry_config: Optional retry behaviour for subgraph requests
446462
447463
:return: List of rewards info
448464
@@ -471,6 +487,7 @@ def get_rewards_info(chain_id: ChainId, slasher: str) -> List[RewardData]:
471487
network,
472488
query=get_reward_added_events_query,
473489
params={"slasherAddress": slasher.lower()},
490+
retry_config=retry_config,
474491
)
475492

476493
if (

0 commit comments

Comments
 (0)