-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathescrow_client.py
1294 lines (991 loc) · 46.3 KB
/
escrow_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
This client enables to perform actions on Escrow contracts and
obtain information from both the contracts and subgraph.
Internally, the SDK will use one network or another according to the network ID of the web3.
To use this client, you need to create Web3 instance, and configure default account,
as well as some middlewares.
Code Example
------------
* With Signer
.. code-block:: python
from eth_typing import URI
from web3 import Web3
from web3.middleware import construct_sign_and_send_raw_middleware
from web3.providers.auto import load_provider_from_uri
from human_protocol_sdk.escrow import EscrowClient
def get_w3_with_priv_key(priv_key: str):
w3 = Web3(load_provider_from_uri(URI("http://localhost:8545")))
gas_payer = w3.eth.account.from_key(priv_key)
w3.eth.default_account = gas_payer.address
w3.middleware_onion.add(
construct_sign_and_send_raw_middleware(gas_payer),
"construct_sign_and_send_raw_middleware",
)
return (w3, gas_payer)
(w3, gas_payer) = get_w3_with_priv_key('YOUR_PRIVATE_KEY')
escrow_client = EscrowClient(w3)
* Without Signer (For read operations only)
.. code-block:: python
from eth_typing import URI
from web3 import Web3
from web3.providers.auto import load_provider_from_uri
from human_protocol_sdk.escrow import EscrowClient
w3 = Web3(load_provider_from_uri(URI("http://localhost:8545")))
escrow_client = EscrowClient(w3)
Module
------
"""
import logging
import os
from decimal import Decimal
from typing import List, Optional
from human_protocol_sdk.constants import NETWORKS, ChainId, Status
from human_protocol_sdk.utils import (
get_escrow_interface,
get_factory_interface,
get_erc20_interface,
handle_transaction,
)
from web3 import Web3, contract
from web3 import eth
from web3.middleware import geth_poa_middleware
from web3.types import TxParams
from eth_utils import abi
from human_protocol_sdk.utils import validate_url
LOG = logging.getLogger("human_protocol_sdk.escrow")
class EscrowCancel:
def __init__(self, tx_hash: str, amount_refunded: any):
"""
Represents the result of an escrow cancellation transaction.
Args:
tx_hash (str): The hash of the transaction that cancelled the escrow.
amount_refunded (Any): The amount refunded during the escrow cancellation.
"""
self.txHash = tx_hash
self.amountRefunded = amount_refunded
class EscrowClientError(Exception):
"""
Raises when some error happens when interacting with escrow.
"""
pass
class EscrowConfig:
"""
A class used to manage escrow parameters.
"""
def __init__(
self,
recording_oracle_address: str,
reputation_oracle_address: str,
exchange_oracle_address: str,
recording_oracle_fee: Decimal,
reputation_oracle_fee: Decimal,
exchange_oracle_fee: Decimal,
manifest_url: str,
hash: str,
):
"""
Initializes a Escrow instance.
:param recording_oracle_address: Address of the Recording Oracle
:param reputation_oracle_address: Address of the Reputation Oracle
:param recording_oracle_fee: Fee percentage of the Recording Oracle
:param reputation_oracle_fee: Fee percentage of the Reputation Oracle
:param manifest_url: Manifest file url
:param hash: Manifest file hash
"""
if not Web3.is_address(recording_oracle_address):
raise EscrowClientError(
f"Invalid recording oracle address: {recording_oracle_address}"
)
if not Web3.is_address(reputation_oracle_address):
raise EscrowClientError(
f"Invalid reputation oracle address: {reputation_oracle_address}"
)
if not Web3.is_address(exchange_oracle_address):
raise EscrowClientError(
f"Invalid exchange oracle address: {exchange_oracle_address}"
)
if (
not (0 <= recording_oracle_fee <= 100)
or not (0 <= reputation_oracle_fee <= 100)
or not (0 <= exchange_oracle_fee <= 100)
):
raise EscrowClientError("Fee must be between 0 and 100")
if recording_oracle_fee + reputation_oracle_fee + exchange_oracle_fee > 100:
raise EscrowClientError("Total fee must be less than 100")
if not validate_url(manifest_url):
raise EscrowClientError(f"Invalid manifest URL: {manifest_url}")
if not hash:
raise EscrowClientError("Invalid empty manifest hash")
self.recording_oracle_address = recording_oracle_address
self.reputation_oracle_address = reputation_oracle_address
self.exchange_oracle_address = exchange_oracle_address
self.recording_oracle_fee = recording_oracle_fee
self.reputation_oracle_fee = reputation_oracle_fee
self.exchange_oracle_fee = exchange_oracle_fee
self.manifest_url = manifest_url
self.hash = hash
class EscrowClient:
"""
A class used to manage escrow on the HUMAN network.
"""
def __init__(self, web3: Web3):
"""
Initializes a Escrow instance.
:param web3: The Web3 object
"""
# Initialize web3 instance
self.w3 = web3
if not self.w3.middleware_onion.get("geth_poa"):
self.w3.middleware_onion.inject(geth_poa_middleware, "geth_poa", layer=0)
chain_id = None
# Load network configuration based on chain_id
try:
chain_id = self.w3.eth.chain_id
self.network = NETWORKS[ChainId(chain_id)]
except:
if chain_id is not None:
raise EscrowClientError(f"Invalid ChainId: {chain_id}")
else:
raise EscrowClientError(f"Invalid Web3 Instance")
# Initialize contract instances
factory_interface = get_factory_interface()
self.factory_contract = self.w3.eth.contract(
address=self.network["factory_address"], abi=factory_interface["abi"]
)
def create_escrow(
self,
token_address: str,
trusted_handlers: List[str],
job_requester_id: str,
tx_options: Optional[TxParams] = None,
) -> str:
"""
Creates an escrow contract that uses the token passed to pay oracle fees and reward workers.
:param tokenAddress: The address of the token to use for payouts
:param trusted_handlers: Array of addresses that can perform actions on the contract
:param job_requester_id: The id of the job requester
:param tx_options: (Optional) Additional transaction parameters
:return: The address of the escrow created
:raise EscrowClientError: If an error occurs while checking the parameters
:example:
.. code-block:: python
from eth_typing import URI
from web3 import Web3
from web3.middleware import construct_sign_and_send_raw_middleware
from web3.providers.auto import load_provider_from_uri
from human_protocol_sdk.escrow import EscrowClient
def get_w3_with_priv_key(priv_key: str):
w3 = Web3(load_provider_from_uri(URI("http://localhost:8545")))
gas_payer = w3.eth.account.from_key(priv_key)
w3.eth.default_account = gas_payer.address
w3.middleware_onion.add(
construct_sign_and_send_raw_middleware(gas_payer),
"construct_sign_and_send_raw_middleware",
)
return (w3, gas_payer)
(w3, gas_payer) = get_w3_with_priv_key('YOUR_PRIVATE_KEY')
escrow_client = EscrowClient(w3)
token_address = '0x0376D26246Eb35FF4F9924cF13E6C05fd0bD7Fb4'
trusted_handlers = [
'0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
'0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266'
]
job_requester_id = 'job-requester'
escrow_address = escrow_client.create_escrow(
token_address,
trusted_handlers,
job_requester_id
)
"""
if not Web3.is_address(token_address):
raise EscrowClientError(f"Invalid token address: {token_address}")
for handler in trusted_handlers:
if not Web3.is_address(handler):
raise EscrowClientError(f"Invalid handler address: {handler}")
transaction_receipt = handle_transaction(
self.w3,
"Create Escrow",
self.factory_contract.functions.createEscrow(
token_address, trusted_handlers, job_requester_id
),
EscrowClientError,
tx_options,
)
return next(
(
self.factory_contract.events.LaunchedV2().process_log(log)
for log in transaction_receipt["logs"]
if log["address"] == self.network["factory_address"]
),
None,
).args.escrow
def setup(
self,
escrow_address: str,
escrow_config: EscrowConfig,
tx_options: Optional[TxParams] = None,
) -> None:
"""
Sets up the parameters of the escrow.
:param escrow_address: Address of the escrow to setup
:param escrow_config: Object containing all the necessary information to setup an escrow
:param tx_options: (Optional) Additional transaction parameters
:return: None
:raise EscrowClientError: If an error occurs while checking the parameters
:example:
.. code-block:: python
from eth_typing import URI
from web3 import Web3
from web3.middleware import construct_sign_and_send_raw_middleware
from web3.providers.auto import load_provider_from_uri
from human_protocol_sdk.escrow import EscrowClient
def get_w3_with_priv_key(priv_key: str):
w3 = Web3(load_provider_from_uri(URI("http://localhost:8545")))
gas_payer = w3.eth.account.from_key(priv_key)
w3.eth.default_account = gas_payer.address
w3.middleware_onion.add(
construct_sign_and_send_raw_middleware(gas_payer),
"construct_sign_and_send_raw_middleware",
)
return (w3, gas_payer)
(w3, gas_payer) = get_w3_with_priv_key('YOUR_PRIVATE_KEY')
escrow_client = EscrowClient(w3)
escrow_address = "0x62dD51230A30401C455c8398d06F85e4EaB6309f"
escrow_config = EscrowConfig(
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
10,
10,
10,
"htttp://localhost/manifest.json",
"b5dad76bf6772c0f07fd5e048f6e75a5f86ee079",
)
escrow_client.setup(escrow_address, escrow_config)
"""
if not Web3.is_address(escrow_address):
raise EscrowClientError(f"Invalid escrow address: {escrow_address}")
handle_transaction(
self.w3,
"Setup",
self._get_escrow_contract(escrow_address).functions.setup(
escrow_config.reputation_oracle_address,
escrow_config.recording_oracle_address,
escrow_config.exchange_oracle_address,
escrow_config.reputation_oracle_fee,
escrow_config.recording_oracle_fee,
escrow_config.exchange_oracle_fee,
escrow_config.manifest_url,
escrow_config.hash,
),
EscrowClientError,
tx_options,
)
def create_and_setup_escrow(
self,
token_address: str,
trusted_handlers: List[str],
job_requester_id: str,
escrow_config: EscrowConfig,
) -> str:
"""
Creates and sets up an escrow.
:param token_address: Token to use for pay outs
:param trusted_handlers: Array of addresses that can perform actions on the contract
:param job_requester_id: The id of the job requester
:param escrow_config: Object containing all the necessary information to setup an escrow
:return: The address of the escrow created
:raise EscrowClientError: If an error occurs while checking the parameters
:example:
.. code-block:: python
from eth_typing import URI
from web3 import Web3
from web3.middleware import construct_sign_and_send_raw_middleware
from web3.providers.auto import load_provider_from_uri
from human_protocol_sdk.escrow import EscrowClient
def get_w3_with_priv_key(priv_key: str):
w3 = Web3(load_provider_from_uri(URI("http://localhost:8545")))
gas_payer = w3.eth.account.from_key(priv_key)
w3.eth.default_account = gas_payer.address
w3.middleware_onion.add(
construct_sign_and_send_raw_middleware(gas_payer),
"construct_sign_and_send_raw_middleware",
)
return (w3, gas_payer)
(w3, gas_payer) = get_w3_with_priv_key('YOUR_PRIVATE_KEY')
escrow_client = EscrowClient(w3)
token_address = '0x0376D26246Eb35FF4F9924cF13E6C05fd0bD7Fb4'
trusted_handlers = [
'0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
'0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266'
]
job_requester_id = 'job-requester'
escrow_config = EscrowConfig(
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
10,
10,
10,
"htttp://localhost/manifest.json",
"b5dad76bf6772c0f07fd5e048f6e75a5f86ee079",
)
escrow_address = escrow_client.create_and_setup_escrow(
token_address,
trusted_handlers,
job_requester_id,
escrow_config
)
"""
escrow_address = self.create_escrow(
token_address, trusted_handlers, job_requester_id
)
self.setup(escrow_address, escrow_config)
return escrow_address
def fund(
self,
escrow_address: str,
amount: Decimal,
tx_options: Optional[TxParams] = None,
) -> None:
"""
Adds funds to the escrow.
:param escrow_address: Address of the escrow to setup
:param amount: Amount to be added as funds
:param tx_options: (Optional) Additional transaction parameters
:return: None
:raise EscrowClientError: If an error occurs while checking the parameters
:example:
.. code-block:: python
from eth_typing import URI
from web3 import Web3
from web3.middleware import construct_sign_and_send_raw_middleware
from web3.providers.auto import load_provider_from_uri
from human_protocol_sdk.escrow import EscrowClient
def get_w3_with_priv_key(priv_key: str):
w3 = Web3(load_provider_from_uri(URI("http://localhost:8545")))
gas_payer = w3.eth.account.from_key(priv_key)
w3.eth.default_account = gas_payer.address
w3.middleware_onion.add(
construct_sign_and_send_raw_middleware(gas_payer),
"construct_sign_and_send_raw_middleware",
)
return (w3, gas_payer)
(w3, gas_payer) = get_w3_with_priv_key('YOUR_PRIVATE_KEY')
escrow_client = EscrowClient(w3)
amount = Web3.to_wei(5, 'ether') # convert from ETH to WEI
escrow_client.fund("0x62dD51230A30401C455c8398d06F85e4EaB6309f", amount)
"""
if not Web3.is_address(escrow_address):
raise EscrowClientError(f"Invalid escrow address: {escrow_address}")
if 0 >= amount:
raise EscrowClientError("Amount must be positive")
token_address = self.get_token_address(escrow_address)
erc20_interface = get_erc20_interface()
token_contract = self.w3.eth.contract(token_address, abi=erc20_interface["abi"])
handle_transaction(
self.w3,
"Fund",
token_contract.functions.transfer(escrow_address, amount),
EscrowClientError,
tx_options,
)
def store_results(
self,
escrow_address: str,
url: str,
hash: str,
tx_options: Optional[TxParams] = None,
) -> None:
"""Stores the results url.
:param escrow_address: Address of the escrow
:param url: Results file url
:param hash: Results file hash
:param tx_options: (Optional) Additional transaction parameters
:return: None
:raise EscrowClientError: If an error occurs while checking the parameters
:example:
.. code-block:: python
from eth_typing import URI
from web3 import Web3
from web3.middleware import construct_sign_and_send_raw_middleware
from web3.providers.auto import load_provider_from_uri
from human_protocol_sdk.escrow import EscrowClient
def get_w3_with_priv_key(priv_key: str):
w3 = Web3(load_provider_from_uri(URI("http://localhost:8545")))
gas_payer = w3.eth.account.from_key(priv_key)
w3.eth.default_account = gas_payer.address
w3.middleware_onion.add(
construct_sign_and_send_raw_middleware(gas_payer),
"construct_sign_and_send_raw_middleware",
)
return (w3, gas_payer)
(w3, gas_payer) = get_w3_with_priv_key('YOUR_PRIVATE_KEY')
escrow_client = EscrowClient(w3)
escrow_client.store_results(
"0x62dD51230A30401C455c8398d06F85e4EaB6309f",
"http://localhost/results.json",
"b5dad76bf6772c0f07fd5e048f6e75a5f86ee079"
)
"""
if not Web3.is_address(escrow_address):
raise EscrowClientError(f"Invalid escrow address: {escrow_address}")
if not hash:
raise EscrowClientError("Invalid empty hash")
if not validate_url(url):
raise EscrowClientError(f"Invalid URL: {url}")
if not self.w3.eth.default_account:
raise EscrowClientError("You must add an account to Web3 instance")
handle_transaction(
self.w3,
"Store Results",
self._get_escrow_contract(escrow_address).functions.storeResults(url, hash),
EscrowClientError,
tx_options,
)
def complete(
self, escrow_address: str, tx_options: Optional[TxParams] = None
) -> None:
"""Sets the status of an escrow to completed.
:param escrow_address: Address of the escrow to complete
:param tx_options: (Optional) Additional transaction parameters
:return: None
:raise EscrowClientError: If an error occurs while checking the parameters
:example:
.. code-block:: python
from eth_typing import URI
from web3 import Web3
from web3.middleware import construct_sign_and_send_raw_middleware
from web3.providers.auto import load_provider_from_uri
from human_protocol_sdk.escrow import EscrowClient
def get_w3_with_priv_key(priv_key: str):
w3 = Web3(load_provider_from_uri(URI("http://localhost:8545")))
gas_payer = w3.eth.account.from_key(priv_key)
w3.eth.default_account = gas_payer.address
w3.middleware_onion.add(
construct_sign_and_send_raw_middleware(gas_payer),
"construct_sign_and_send_raw_middleware",
)
return (w3, gas_payer)
(w3, gas_payer) = get_w3_with_priv_key('YOUR_PRIVATE_KEY')
escrow_client = EscrowClient(w3)
escrow_client.complete("0x62dD51230A30401C455c8398d06F85e4EaB6309f")
"""
if not Web3.is_address(escrow_address):
raise EscrowClientError(f"Invalid escrow address: {escrow_address}")
handle_transaction(
self.w3,
"Complete",
self._get_escrow_contract(escrow_address).functions.complete(),
EscrowClientError,
tx_options,
)
def bulk_payout(
self,
escrow_address: str,
recipients: List[str],
amounts: List[Decimal],
final_results_url: str,
final_results_hash: str,
txId: Decimal,
tx_options: Optional[TxParams] = None,
) -> None:
"""Pays out the amounts specified to the workers and sets the URL of the final results file.
:param escrow_address: Address of the escrow
:param recipients: Array of recipient addresses
:param amounts: Array of amounts the recipients will receive
:param final_results_url: Final results file url
:param final_results_hash: Final results file hash
:param txId: Serial number of the bulks
:param tx_options: (Optional) Additional transaction parameters
:return: None
:raise EscrowClientError: If an error occurs while checking the parameters
:example:
.. code-block:: python
from eth_typing import URI
from web3 import Web3
from web3.middleware import construct_sign_and_send_raw_middleware
from web3.providers.auto import load_provider_from_uri
from human_protocol_sdk.escrow import EscrowClient
def get_w3_with_priv_key(priv_key: str):
w3 = Web3(load_provider_from_uri(URI("http://localhost:8545")))
gas_payer = w3.eth.account.from_key(priv_key)
w3.eth.default_account = gas_payer.address
w3.middleware_onion.add(
construct_sign_and_send_raw_middleware(gas_payer),
"construct_sign_and_send_raw_middleware",
)
return (w3, gas_payer)
(w3, gas_payer) = get_w3_with_priv_key('YOUR_PRIVATE_KEY')
escrow_client = EscrowClient(w3)
recipients = [
'0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
'0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92267'
]
amounts = [
Web3.to_wei(5, 'ether'),
Web3.to_wei(10, 'ether')
]
results_url = 'http://localhost/results.json'
results_hash = 'b5dad76bf6772c0f07fd5e048f6e75a5f86ee079'
escrow_client.bulk_payout(
"0x62dD51230A30401C455c8398d06F85e4EaB6309f",
recipients,
amounts,
results_url,
results_hash,
1
)
"""
if not Web3.is_address(escrow_address):
raise EscrowClientError(f"Invalid escrow address: {escrow_address}")
for recipient in recipients:
if not Web3.is_address(recipient):
raise EscrowClientError(f"Invalid recipient address: {recipient}")
if len(recipients) == 0:
raise EscrowClientError("Arrays must have any value")
if len(recipients) != len(amounts):
raise EscrowClientError("Arrays must have same length")
if 0 in amounts:
raise EscrowClientError("Amounts cannot be empty")
if any(amount < 0 for amount in amounts):
raise EscrowClientError("Amounts cannot be negative")
balance = self.get_balance(escrow_address)
total_amount = sum(amounts)
if total_amount > balance:
raise EscrowClientError(
f"Escrow does not have enough balance. Current balance: {balance}. Amounts: {total_amount}"
)
if not validate_url(final_results_url):
raise EscrowClientError(f"Invalid final results URL: {final_results_url}")
if not final_results_hash:
raise EscrowClientError("Invalid empty final results hash")
handle_transaction(
self.w3,
"Bulk Payout",
self._get_escrow_contract(escrow_address).functions.bulkPayOut(
recipients, amounts, final_results_url, final_results_hash, txId
),
EscrowClientError,
tx_options,
)
def cancel(
self, escrow_address: str, tx_options: Optional[TxParams] = None
) -> EscrowCancel:
"""Cancels the specified escrow and sends the balance to the canceler.
:param escrow_address: Address of the escrow to cancel
:param tx_options: (Optional) Additional transaction parameters
:return: EscrowCancel:
An instance of the EscrowCancel class containing details of the cancellation transaction,
including the transaction hash and the amount refunded.
:raise EscrowClientError: If an error occurs while checking the parameters
:raise EscrowClientError: If the transfer event associated with the cancellation
is not found in the transaction logs
:example:
.. code-block:: python
from eth_typing import URI
from web3 import Web3
from web3.middleware import construct_sign_and_send_raw_middleware
from web3.providers.auto import load_provider_from_uri
from human_protocol_sdk.escrow import EscrowClient
def get_w3_with_priv_key(priv_key: str):
w3 = Web3(load_provider_from_uri(URI("http://localhost:8545")))
gas_payer = w3.eth.account.from_key(priv_key)
w3.eth.default_account = gas_payer.address
w3.middleware_onion.add(
construct_sign_and_send_raw_middleware(gas_payer),
"construct_sign_and_send_raw_middleware",
)
return (w3, gas_payer)
(w3, gas_payer) = get_w3_with_priv_key('YOUR_PRIVATE_KEY')
escrow_client = EscrowClient(w3)
escrow_cancel_data = escrow_client.cancel(
"0x62dD51230A30401C455c8398d06F85e4EaB6309f"
)
"""
if not Web3.is_address(escrow_address):
raise EscrowClientError(f"Invalid escrow address: {escrow_address}")
transaction_receipt = handle_transaction(
self.w3,
"Cancel",
self._get_escrow_contract(escrow_address).functions.cancel(),
EscrowClientError,
tx_options,
)
amount_transferred = None
token_address = self.get_token_address(escrow_address)
erc20_interface = get_erc20_interface()
token_contract = self.w3.eth.contract(token_address, abi=erc20_interface["abi"])
for log in transaction_receipt["logs"]:
if log["address"] == token_address:
processed_log = token_contract.events.Transfer().process_log(log)
if (
processed_log["event"] == "Transfer"
and processed_log["args"]["from"] == escrow_address
):
amount_transferred = processed_log["args"]["value"]
break
if amount_transferred is None:
raise EscrowClientError("Transfer Event Not Found in Transaction Logs")
escrow_cancel_data = EscrowCancel(
tx_hash=transaction_receipt["transactionHash"].hex(),
amount_refunded=amount_transferred,
)
return escrow_cancel_data
def abort(self, escrow_address: str, tx_options: Optional[TxParams] = None) -> None:
"""Cancels the specified escrow,
sends the balance to the canceler and selfdestructs the escrow contract.
:param escrow_address: Address of the escrow to abort
:param tx_options: (Optional) Additional transaction parameters
:return: None
:raise EscrowClientError: If an error occurs while checking the parameters
:example:
.. code-block:: python
from eth_typing import URI
from web3 import Web3
from web3.middleware import construct_sign_and_send_raw_middleware
from web3.providers.auto import load_provider_from_uri
from human_protocol_sdk.escrow import EscrowClient
def get_w3_with_priv_key(priv_key: str):
w3 = Web3(load_provider_from_uri(URI("http://localhost:8545")))
gas_payer = w3.eth.account.from_key(priv_key)
w3.eth.default_account = gas_payer.address
w3.middleware_onion.add(
construct_sign_and_send_raw_middleware(gas_payer),
"construct_sign_and_send_raw_middleware",
)
return (w3, gas_payer)
(w3, gas_payer) = get_w3_with_priv_key('YOUR_PRIVATE_KEY')
escrow_client = EscrowClient(w3)
escrow_client.abort("0x62dD51230A30401C455c8398d06F85e4EaB6309f")
"""
if not Web3.is_address(escrow_address):
raise EscrowClientError(f"Invalid escrow address: {escrow_address}")
handle_transaction(
self.w3,
"Abort",
self._get_escrow_contract(escrow_address).functions.abort(),
EscrowClientError,
tx_options,
)
def add_trusted_handlers(
self,
escrow_address: str,
handlers: List[str],
tx_options: Optional[TxParams] = None,
) -> None:
"""Adds an array of addresses to the trusted handlers list.
:param escrow_address: Address of the escrow
:param handlers: Array of trusted handler addresses
:param tx_options: (Optional) Additional transaction parameters
:return: None
:raise EscrowClientError: If an error occurs while checking the parameters
:example:
.. code-block:: python
from eth_typing import URI
from web3 import Web3
from web3.middleware import construct_sign_and_send_raw_middleware
from web3.providers.auto import load_provider_from_uri
from human_protocol_sdk.escrow import EscrowClient
def get_w3_with_priv_key(priv_key: str):
w3 = Web3(load_provider_from_uri(URI("http://localhost:8545")))
gas_payer = w3.eth.account.from_key(priv_key)
w3.eth.default_account = gas_payer.address
w3.middleware_onion.add(
construct_sign_and_send_raw_middleware(gas_payer),
"construct_sign_and_send_raw_middleware",
)
return (w3, gas_payer)
(w3, gas_payer) = get_w3_with_priv_key('YOUR_PRIVATE_KEY')
escrow_client = EscrowClient(w3)
trusted_handlers = [
'0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
'0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266'
]
escrow_client.add_trusted_handlers(
"0x62dD51230A30401C455c8398d06F85e4EaB6309f",
trusted_handlers
)
"""
if not Web3.is_address(escrow_address):
raise EscrowClientError(f"Invalid escrow address: {escrow_address}")
for handler in handlers:
if not Web3.is_address(handler):
raise EscrowClientError(f"Invalid handler address: {handler}")
handle_transaction(
self.w3,
"Add Trusted Handlers",
self._get_escrow_contract(escrow_address).functions.addTrustedHandlers(
handlers
),
EscrowClientError,
tx_options,
)
def get_balance(self, escrow_address: str) -> Decimal:
"""Gets the balance for a specified escrow address.
:param escrow_address: Address of the escrow
:return: Value of the balance
:raise EscrowClientError: If an error occurs while checking the parameters
:example:
.. code-block:: python
from eth_typing import URI
from web3 import Web3
from web3.providers.auto import load_provider_from_uri
from human_protocol_sdk.escrow import EscrowClient
w3 = Web3(load_provider_from_uri(URI("http://localhost:8545")))
escrow_client = EscrowClient(w3)
balance = escrow_client.get_balance(
"0x62dD51230A30401C455c8398d06F85e4EaB6309f"
)
"""
if not Web3.is_address(escrow_address):
raise EscrowClientError(f"Invalid escrow address: {escrow_address}")
return self._get_escrow_contract(escrow_address).functions.getBalance().call()
def get_manifest_hash(self, escrow_address: str) -> str:
"""Gets the manifest file hash.
:param escrow_address: Address of the escrow
:return: Manifest file hash
:raise EscrowClientError: If an error occurs while checking the parameters
:example:
.. code-block:: python
from eth_typing import URI
from web3 import Web3
from web3.providers.auto import load_provider_from_uri
from human_protocol_sdk.escrow import EscrowClient
w3 = Web3(load_provider_from_uri(URI("http://localhost:8545")))
escrow_client = EscrowClient(w3)
manifest_hash = escrow_client.get_manifest_hash(
"0x62dD51230A30401C455c8398d06F85e4EaB6309f"
)
"""
if not Web3.is_address(escrow_address):
raise EscrowClientError(f"Invalid escrow address: {escrow_address}")
return self._get_escrow_contract(escrow_address).functions.manifestHash().call()
def get_manifest_url(self, escrow_address: str) -> str:
"""Gets the manifest file URL.
:param escrow_address: Address of the escrow
:return str: Manifest file url
:raise EscrowClientError: If an error occurs while checking the parameters
:example:
.. code-block:: python
from eth_typing import URI
from web3 import Web3
from web3.providers.auto import load_provider_from_uri
from human_protocol_sdk.escrow import EscrowClient
w3 = Web3(load_provider_from_uri(URI("http://localhost:8545")))
escrow_client = EscrowClient(w3)
url = escrow_client.get_manifest_url(
"0x62dD51230A30401C455c8398d06F85e4EaB6309f"
)
"""
if not Web3.is_address(escrow_address):
raise EscrowClientError(f"Invalid escrow address: {escrow_address}")
return self._get_escrow_contract(escrow_address).functions.manifestUrl().call()
def get_results_url(self, escrow_address: str) -> str:
"""Gets the results file URL.
:param escrow_address: Address of the escrow
:return: Results file url
:raise EscrowClientError: If an error occurs while checking the parameters
:example:
.. code-block:: python
from eth_typing import URI
from web3 import Web3
from web3.providers.auto import load_provider_from_uri
from human_protocol_sdk.escrow import EscrowClient