Skip to content

Commit 9cc3768

Browse files
committed
Refactor escrow, staking, and transaction utilities to handle missing values
1 parent a7cbbc7 commit 9cc3768

File tree

5 files changed

+101
-95
lines changed

5 files changed

+101
-95
lines changed

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,15 @@ def get_escrows(
290290
[
291291
EscrowData(
292292
chain_id=chain_id,
293-
id=escrow.get("id", ""),
294-
address=escrow.get("address", ""),
293+
id=escrow.get("id") or "",
294+
address=escrow.get("address") or "",
295295
amount_paid=int(escrow.get("amountPaid") or 0),
296296
balance=int(escrow.get("balance") or 0),
297297
count=int(escrow.get("count") or 0),
298-
factory_address=escrow.get("factoryAddress", ""),
299-
launcher=escrow.get("launcher", ""),
300-
status=escrow.get("status", ""),
301-
token=escrow.get("token", ""),
298+
factory_address=escrow.get("factoryAddress") or "",
299+
launcher=escrow.get("launcher") or "",
300+
status=escrow.get("status") or "",
301+
token=escrow.get("token") or "",
302302
total_funded_amount=int(escrow.get("totalFundedAmount") or 0),
303303
created_at=datetime.fromtimestamp(
304304
int(escrow.get("createdAt") or 0)
@@ -316,17 +316,17 @@ def get_escrows(
316316
exchange_oracle=escrow.get("exchangeOracle", None),
317317
recording_oracle_fee=(
318318
int(escrow.get("recordingOracleFee"))
319-
if escrow.get("recordingOracleFee", None) not in (None, "")
319+
if escrow.get("recordingOracleFee", None) not in (None) or ""
320320
else None
321321
),
322322
reputation_oracle_fee=(
323323
int(escrow.get("reputationOracleFee"))
324-
if escrow.get("reputationOracleFee", None) not in (None, "")
324+
if escrow.get("reputationOracleFee", None) not in (None) or ""
325325
else None
326326
),
327327
exchange_oracle_fee=(
328328
int(escrow.get("exchangeOracleFee"))
329-
if escrow.get("exchangeOracleFee", None) not in (None, "")
329+
if escrow.get("exchangeOracleFee", None) not in (None) or ""
330330
else None
331331
),
332332
)
@@ -393,15 +393,15 @@ def get_escrow(
393393

394394
return EscrowData(
395395
chain_id=chain_id,
396-
id=escrow.get("id", ""),
397-
address=escrow.get("address", ""),
396+
id=escrow.get("id") or "",
397+
address=escrow.get("address") or "",
398398
amount_paid=int(escrow.get("amountPaid") or 0),
399399
balance=int(escrow.get("balance") or 0),
400400
count=int(escrow.get("count") or 0),
401-
factory_address=escrow.get("factoryAddress", ""),
402-
launcher=escrow.get("launcher", ""),
403-
status=escrow.get("status", ""),
404-
token=escrow.get("token", ""),
401+
factory_address=escrow.get("factoryAddress") or "",
402+
launcher=escrow.get("launcher") or "",
403+
status=escrow.get("status") or "",
404+
token=escrow.get("token") or "",
405405
total_funded_amount=int(escrow.get("totalFundedAmount") or 0),
406406
created_at=datetime.fromtimestamp(int(escrow.get("createdAt") or 0)),
407407
final_results_url=escrow.get("finalResultsUrl", None),
@@ -415,17 +415,17 @@ def get_escrow(
415415
exchange_oracle=escrow.get("exchangeOracle", None),
416416
recording_oracle_fee=(
417417
int(escrow.get("recordingOracleFee"))
418-
if escrow.get("recordingOracleFee", None) not in (None, "")
418+
if escrow.get("recordingOracleFee", None) not in (None) or ""
419419
else None
420420
),
421421
reputation_oracle_fee=(
422422
int(escrow.get("reputationOracleFee"))
423-
if escrow.get("reputationOracleFee", None) not in (None, "")
423+
if escrow.get("reputationOracleFee", None) not in (None) or ""
424424
else None
425425
),
426426
exchange_oracle_fee=(
427427
int(escrow.get("exchangeOracleFee"))
428-
if escrow.get("exchangeOracleFee", None) not in (None, "")
428+
if escrow.get("exchangeOracleFee", None) not in (None) or ""
429429
else None
430430
),
431431
)

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

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -244,20 +244,24 @@ def get_operators(filter: OperatorFilter) -> List[OperatorData]:
244244
operators.append(
245245
OperatorData(
246246
chain_id=filter.chain_id,
247-
id=operator.get("id", ""),
248-
address=operator.get("address", ""),
247+
id=operator.get("id") or "",
248+
address=operator.get("address") or "",
249249
staked_amount=int(staker.get("stakedAmount") or 0),
250250
locked_amount=int(staker.get("lockedAmount") or 0),
251251
locked_until_timestamp=int(staker.get("lockedUntilTimestamp") or 0),
252252
withdrawn_amount=int(staker.get("withdrawnAmount") or 0),
253253
slashed_amount=int(staker.get("slashedAmount") or 0),
254254
amount_jobs_processed=int(operator.get("amountJobsProcessed") or 0),
255-
role=operator.get("role", None),
256-
fee=int(operator.get("fee")) if operator.get("fee", None) else None,
257-
public_key=operator.get("publicKey", None),
258-
webhook_url=operator.get("webhookUrl", None),
259-
website=operator.get("website", None),
260-
url=operator.get("url", None),
255+
role=operator.get("role") or None,
256+
fee=(
257+
int(operator.get("fee"))
258+
if operator.get("fee") or None
259+
else None
260+
),
261+
public_key=operator.get("publicKey") or None,
262+
webhook_url=operator.get("webhookUrl") or None,
263+
website=operator.get("website") or None,
264+
url=operator.get("url") or None,
261265
job_types=(
262266
operator.get("jobTypes").split(",")
263267
if isinstance(operator.get("jobTypes"), str)
@@ -267,13 +271,13 @@ def get_operators(filter: OperatorFilter) -> List[OperatorData]:
267271
else []
268272
)
269273
),
270-
registration_needed=operator.get("registrationNeeded", None),
274+
registration_needed=operator.get("registrationNeeded") or None,
271275
registration_instructions=operator.get(
272-
"registrationInstructions", None
276+
"registrationInstructions" or None
273277
),
274278
reputation_networks=reputation_networks,
275-
name=operator.get("name", None),
276-
category=operator.get("category", None),
279+
name=operator.get("name") or None,
280+
category=operator.get("category") or None,
277281
)
278282
)
279283

@@ -341,20 +345,20 @@ def get_operator(
341345
staker = operator.get("staker") or {}
342346
return OperatorData(
343347
chain_id=chain_id,
344-
id=operator.get("id", ""),
345-
address=operator.get("address", ""),
348+
id=operator.get("id") or "",
349+
address=operator.get("address") or "",
346350
staked_amount=int(staker.get("stakedAmount") or 0),
347351
locked_amount=int(staker.get("lockedAmount") or 0),
348352
locked_until_timestamp=int(staker.get("lockedUntilTimestamp") or 0),
349353
withdrawn_amount=int(staker.get("withdrawnAmount") or 0),
350354
slashed_amount=int(staker.get("slashedAmount") or 0),
351355
amount_jobs_processed=int(operator.get("amountJobsProcessed") or 0),
352-
role=operator.get("role", None),
353-
fee=int(operator.get("fee")) if operator.get("fee", None) else None,
354-
public_key=operator.get("publicKey", None),
355-
webhook_url=operator.get("webhookUrl", None),
356-
website=operator.get("website", None),
357-
url=operator.get("url", None),
356+
role=operator.get("role") or None,
357+
fee=int(operator.get("fee")) if operator.get("fee") or None else None,
358+
public_key=operator.get("publicKey") or None,
359+
webhook_url=operator.get("webhookUrl") or None,
360+
website=operator.get("website") or None,
361+
url=operator.get("url") or None,
358362
job_types=(
359363
operator.get("jobTypes").split(",")
360364
if isinstance(operator.get("jobTypes"), str)
@@ -364,11 +368,11 @@ def get_operator(
364368
else []
365369
)
366370
),
367-
registration_needed=operator.get("registrationNeeded", None),
368-
registration_instructions=operator.get("registrationInstructions", None),
371+
registration_needed=operator.get("registrationNeeded") or None,
372+
registration_instructions=operator.get("registrationInstructions") or None,
369373
reputation_networks=reputation_networks,
370-
name=operator.get("name", None),
371-
category=operator.get("category", None),
374+
name=operator.get("name") or None,
375+
category=operator.get("category") or None,
372376
)
373377

374378
@staticmethod
@@ -426,8 +430,8 @@ def get_reputation_network_operators(
426430
return [
427431
OperatorData(
428432
chain_id=chain_id,
429-
id=operator.get("id", ""),
430-
address=operator.get("address", ""),
433+
id=operator.get("id") or "",
434+
address=operator.get("address") or "",
431435
staked_amount=int(
432436
(staker := operator.get("staker") or {}).get("stakedAmount") or 0
433437
),
@@ -436,12 +440,12 @@ def get_reputation_network_operators(
436440
withdrawn_amount=int(staker.get("withdrawnAmount") or 0),
437441
slashed_amount=int(staker.get("slashedAmount") or 0),
438442
amount_jobs_processed=int(operator.get("amountJobsProcessed") or 0),
439-
role=operator.get("role", None),
440-
fee=int(operator.get("fee")) if operator.get("fee", None) else None,
441-
public_key=operator.get("publicKey", None),
442-
webhook_url=operator.get("webhookUrl", None),
443-
website=operator.get("website", None),
444-
url=operator.get("url", None),
443+
role=operator.get("role") or None,
444+
fee=int(operator.get("fee")) if operator.get("fee") or None else None,
445+
public_key=operator.get("publicKey") or None,
446+
webhook_url=operator.get("webhookUrl") or None,
447+
website=operator.get("website") or None,
448+
url=operator.get("url") or None,
445449
job_types=(
446450
operator.get("jobTypes").split(",")
447451
if isinstance(operator.get("jobTypes"), str)
@@ -451,18 +455,18 @@ def get_reputation_network_operators(
451455
else []
452456
)
453457
),
454-
registration_needed=operator.get("registrationNeeded", None),
458+
registration_needed=operator.get("registrationNeeded") or None,
455459
registration_instructions=operator.get(
456-
"registrationInstructions", None
460+
"registrationInstructions" or None
457461
),
458462
reputation_networks=(
459463
[network["address"] for network in operator["reputationNetworks"]]
460464
if operator.get("reputationNetworks")
461465
and isinstance(operator.get("reputationNetworks"), list)
462466
else []
463467
),
464-
name=operator.get("name", None),
465-
category=operator.get("category", None),
468+
name=operator.get("name") or None,
469+
category=operator.get("category") or None,
466470
)
467471
for operator in operators
468472
]
@@ -515,7 +519,7 @@ def get_rewards_info(chain_id: ChainId, slasher: str) -> List[RewardData]:
515519

516520
return [
517521
RewardData(
518-
escrow_address=reward_added_event.get("escrowAddress", ""),
522+
escrow_address=reward_added_event.get("escrowAddress") or "",
519523
amount=int(reward_added_event.get("amount") or 0),
520524
)
521525
for reward_added_event in reward_added_events

packages/sdk/python/human-protocol-sdk/human_protocol_sdk/staking/staking_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ def get_staker(chain_id: ChainId, address: str) -> Optional[StakerData]:
8282

8383
staker = data["data"]["staker"]
8484
return StakerData(
85-
id=staker.get("id", ""),
86-
address=staker.get("address", ""),
85+
id=staker.get("id") or "",
86+
address=staker.get("address") or "",
8787
staked_amount=int(staker.get("stakedAmount") or 0),
8888
locked_amount=int(staker.get("lockedAmount") or 0),
8989
withdrawn_amount=int(staker.get("withdrawnAmount") or 0),
@@ -127,8 +127,8 @@ def get_stakers(filter: StakersFilter) -> List[StakerData]:
127127
stakers_raw = data["data"]["stakers"]
128128
return [
129129
StakerData(
130-
id=staker.get("id", ""),
131-
address=staker.get("address", ""),
130+
id=staker.get("id") or "",
131+
address=staker.get("address") or "",
132132
staked_amount=int(staker.get("stakedAmount") or 0),
133133
locked_amount=int(staker.get("lockedAmount") or 0),
134134
withdrawn_amount=int(staker.get("withdrawnAmount") or 0),

packages/sdk/python/human-protocol-sdk/human_protocol_sdk/transaction/transaction_utils.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -141,25 +141,25 @@ def get_transaction(chain_id: ChainId, hash: str) -> Optional[TransactionData]:
141141

142142
return TransactionData(
143143
chain_id=chain_id,
144-
block=int(transaction.get("block", 0) or 0),
145-
tx_hash=transaction.get("txHash", ""),
146-
from_address=transaction.get("from", ""),
147-
to_address=transaction.get("to", ""),
144+
block=int(transaction.get("block") or 0),
145+
tx_hash=transaction.get("txHash") or "",
146+
from_address=transaction.get("from") or "",
147+
to_address=transaction.get("to") or "",
148148
timestamp=int(transaction.get("timestamp") or 0),
149149
value=int(transaction.get("value") or 0),
150-
method=transaction.get("method", ""),
151-
receiver=transaction.get("receiver", ""),
152-
escrow=transaction.get("escrow", ""),
153-
token=transaction.get("token", ""),
150+
method=transaction.get("method") or "",
151+
receiver=transaction.get("receiver") or "",
152+
escrow=transaction.get("escrow") or "",
153+
token=transaction.get("token") or "",
154154
internal_transactions=[
155155
InternalTransaction(
156-
from_address=internal_tx.get("from", ""),
157-
to_address=internal_tx.get("to", ""),
156+
from_address=internal_tx.get("from") or "",
157+
to_address=internal_tx.get("to") or "",
158158
value=int(internal_tx.get("value") or 0),
159-
method=internal_tx.get("method", ""),
160-
receiver=internal_tx.get("receiver", ""),
161-
escrow=internal_tx.get("escrow", ""),
162-
token=internal_tx.get("token", ""),
159+
method=internal_tx.get("method") or "",
160+
receiver=internal_tx.get("receiver") or "",
161+
escrow=internal_tx.get("escrow") or "",
162+
token=internal_tx.get("token") or "",
163163
)
164164
for internal_tx in transaction.get("internalTransactions", [])
165165
],
@@ -239,25 +239,25 @@ def get_transactions(filter: TransactionFilter) -> List[TransactionData]:
239239
[
240240
TransactionData(
241241
chain_id=filter.chain_id,
242-
block=int(transaction.get("block", 0) or 0),
243-
tx_hash=transaction.get("txHash", ""),
244-
from_address=transaction.get("from", ""),
245-
to_address=transaction.get("to", ""),
242+
block=int(transaction.get("block") or 0),
243+
tx_hash=transaction.get("txHash") or "",
244+
from_address=transaction.get("from") or "",
245+
to_address=transaction.get("to") or "",
246246
timestamp=int(transaction.get("timestamp") or 0),
247247
value=int(transaction.get("value") or 0),
248-
method=transaction.get("method", ""),
249-
receiver=transaction.get("receiver", ""),
250-
escrow=transaction.get("escrow", ""),
251-
token=transaction.get("token", ""),
248+
method=transaction.get("method") or "",
249+
receiver=transaction.get("receiver") or "",
250+
escrow=transaction.get("escrow") or "",
251+
token=transaction.get("token") or "",
252252
internal_transactions=[
253253
InternalTransaction(
254-
from_address=internal_tx.get("from", ""),
255-
to_address=internal_tx.get("to", ""),
254+
from_address=internal_tx.get("from") or "",
255+
to_address=internal_tx.get("to") or "",
256256
value=int(internal_tx.get("value") or 0),
257-
method=internal_tx.get("method", ""),
258-
receiver=internal_tx.get("receiver", ""),
259-
escrow=internal_tx.get("escrow", ""),
260-
token=internal_tx.get("token", ""),
257+
method=internal_tx.get("method") or "",
258+
receiver=internal_tx.get("receiver") or "",
259+
escrow=internal_tx.get("escrow") or "",
260+
token=internal_tx.get("token") or "",
261261
)
262262
for internal_tx in transaction.get("internalTransactions", [])
263263
],

packages/sdk/python/human-protocol-sdk/human_protocol_sdk/worker/worker_utils.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,12 @@ def get_workers(filter: WorkerFilter) -> List[WorkerData]:
8787
for worker in workers_raw:
8888
workers.append(
8989
WorkerData(
90-
id=worker.get("id", ""),
91-
address=worker.get("address", ""),
92-
total_amount_received=int(worker.get("totalHMTAmountReceived", 0)),
93-
payout_count=int(worker.get("payoutCount", 0)),
90+
id=worker.get("id") or "",
91+
address=worker.get("address") or "",
92+
total_amount_received=int(
93+
worker.get("totalHMTAmountReceived") or 0
94+
),
95+
payout_count=int(worker.get("payoutCount") or 0),
9496
)
9597
)
9698

@@ -133,8 +135,8 @@ def get_worker(chain_id: ChainId, worker_address: str) -> Optional[WorkerData]:
133135
worker = worker_data["data"]["worker"]
134136

135137
return WorkerData(
136-
id=worker.get("id", ""),
137-
address=worker.get("address", ""),
138-
total_amount_received=int(worker.get("totalHMTAmountReceived", 0)),
139-
payout_count=int(worker.get("payoutCount", 0)),
138+
id=worker.get("id") or "",
139+
address=worker.get("address") or "",
140+
total_amount_received=int(worker.get("totalHMTAmountReceived") or 0),
141+
payout_count=int(worker.get("payoutCount") or 0),
140142
)

0 commit comments

Comments
 (0)