Skip to content

Commit

Permalink
Wallet lease
Browse files Browse the repository at this point in the history
  • Loading branch information
alidzm committed Jun 5, 2023
1 parent f6ca86d commit 18bc04a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
4 changes: 4 additions & 0 deletions packages/sdk/python/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Python
__pycache__
.pytest_cache
bin
lib
lib64
pyvenv.cfg

# pipenv
Pipfile.lock
Expand Down
51 changes: 31 additions & 20 deletions packages/sdk/python/human_protocol_sdk/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def __init__(
else:
raise ValueError("Job instantiation wrong, double-check arguments.")

def launch(self, pub_key: bytes) -> bool:
def launch(self, pub_key: bytes, **kwargs) -> bool:
"""Launches an escrow contract to the network, uploads the manifest
to S3 with the public key of the Reputation Oracle and stores
the S3 url to the escrow contract.
Expand Down Expand Up @@ -343,7 +343,7 @@ def launch(self, pub_key: bytes) -> bool:
# Use factory to deploy a new escrow contract.
trusted_handlers = [addr for addr, priv_key in self.multi_credentials]

txn = self._create_escrow(trusted_handlers)
txn = self._create_escrow(trusted_handlers, **kwargs)

if not txn["txn_succeeded"]:
raise Exception("Unable to create escrow")
Expand All @@ -359,7 +359,7 @@ def launch(self, pub_key: bytes) -> bool:
self.manifest_hash = hash_
return self.status() == Status.Launched and self.balance() == 0

def setup(self, sender: str = None) -> bool:
def setup(self, sender: str = None, **kwargs) -> bool:
"""Sets the escrow contract to be ready to receive answers from the Recording Oracle.
The contract needs to be deployed and funded first.
Expand Down Expand Up @@ -419,6 +419,8 @@ def setup(self, sender: str = None) -> bool:
"gas": self.gas,
"hmt_server_addr": self.hmt_server_addr,
}
txn_info.update(kwargs)

if sender:
txn_func = hmtoken_contract.functions.transferFrom
func_args = [sender, self.job_contract.address, hmt_amount]
Expand All @@ -442,7 +444,7 @@ def setup(self, sender: str = None) -> bool:
)
except Exception as e:
LOG.debug(
f"{txn_event} failed with main credentials: {self.gas_payer}, {self.gas_payer_priv} due to {e}. Using secondary ones..."
f"{txn_event} failed with main credentials: {txn_info['gas_payer']}, {txn_info['gas_payer_priv']} due to {e}. Using secondary ones..."
)

if not hmt_transferred:
Expand Down Expand Up @@ -492,7 +494,7 @@ def setup(self, sender: str = None) -> bool:

return str(self.status()) == str(Status.Pending) and tx_balance == hmt_amount

def add_trusted_handlers(self, handlers: List[str]) -> bool:
def add_trusted_handlers(self, handlers: List[str], **kwargs) -> bool:
"""Add trusted handlers that can freely transact with the contract and
perform aborts and cancels for example.
Expand Down Expand Up @@ -534,6 +536,7 @@ def add_trusted_handlers(self, handlers: List[str]) -> bool:
"gas": self.gas,
"hmt_server_addr": self.hmt_server_addr,
}
txn_info.update(kwargs)
func_args = [handlers]

try:
Expand Down Expand Up @@ -561,6 +564,7 @@ def bulk_payout(
pub_key: bytes,
encrypt_final_results: bool = True,
store_pub_final_results: bool = False,
**kwargs,
) -> bool:
"""Performs a payout to multiple ethereum addresses. When the payout happens,
final results are uploaded to IPFS and contract's state is updated to Partial or Paid
Expand Down Expand Up @@ -634,6 +638,7 @@ def bulk_payout(
"gas": self.gas,
"hmt_server_addr": self.hmt_server_addr,
}
txn_info.update(kwargs)

hash_, url = upload(
msg=results,
Expand Down Expand Up @@ -664,18 +669,18 @@ def bulk_payout(
bulk_paid = self._check_transfer_event(tx_receipt)

LOG.debug(
f"Bulk paid: {bulk_paid} with main credentials: {self.gas_payer} and transaction receipt: {tx_receipt}."
f"Bulk paid: {bulk_paid} with main credentials: {txn_info['gas_payer']} and transaction receipt: {tx_receipt}."
)
except Exception as e:
LOG.warning(
f"{txn_event} failed with main credentials: {self.gas_payer}, {self.gas_payer_priv} due to {e}. Using secondary ones..."
f"{txn_event} failed with main credentials: {txn_info['gas_payer']}, {txn_info['gas_payer_priv']} due to {e}. Using secondary ones..."
)

if bulk_paid:
return bulk_paid

LOG.warning(
f"{txn_event} failed with main credentials: {self.gas_payer}, {self.gas_payer_priv}. Using secondary ones..."
f"{txn_event} failed with main credentials: {txn_info['gas_payer']}, {txn_info['gas_payer_priv']}. Using secondary ones..."
)

raffle_txn_res = self._raffle_txn(
Expand All @@ -691,7 +696,7 @@ def bulk_payout(

return bulk_paid

def abort(self) -> bool:
def abort(self, **kwargs) -> bool:
"""Kills the contract and returns the HMT back to the gas payer.
The contract cannot be aborted if the contract is in Partial, Paid or Complete state.
Expand Down Expand Up @@ -773,13 +778,15 @@ def abort(self) -> bool:
"hmt_server_addr": self.hmt_server_addr,
}

txn_info.update(kwargs)

try:
handle_transaction_with_retry(txn_func, self.retry, *[], **txn_info)
# After abort the contract should be destroyed
return w3.eth.getCode(self.job_contract.address) == b""
except Exception as e:
LOG.info(
f"{txn_event} failed with main credentials: {self.gas_payer}, {self.gas_payer_priv} due to {e}. Using secondary ones..."
f"{txn_event} failed with main credentials: {txn_info['gas_payer']}, {txn_info['gas_payer_priv']} due to {e}. Using secondary ones..."
)

raffle_txn_res = self._raffle_txn(
Expand All @@ -792,7 +799,7 @@ def abort(self) -> bool:

return w3.eth.getCode(self.job_contract.address) == b""

def cancel(self) -> bool:
def cancel(self, **kwargs) -> bool:
"""Returns the HMT back to the gas payer. It's the softer version of abort as the contract is not destroyed.
>>> credentials = {
Expand Down Expand Up @@ -854,13 +861,14 @@ def cancel(self) -> bool:
"gas": self.gas,
"hmt_server_addr": self.hmt_server_addr,
}
txn_info.update(kwargs)

try:
handle_transaction_with_retry(txn_func, self.retry, *[], **txn_info)
return self.status() == Status.Cancelled
except Exception as e:
LOG.info(
f"{txn_event} failed with main credentials: {self.gas_payer}, {self.gas_payer_priv} due to {e}. Using secondary ones..."
f"{txn_event} failed with main credentials: {txn_info['gas_payer']}, {txn_info['gas_payer_priv']} due to {e}. Using secondary ones..."
)

raffle_txn_res = self._raffle_txn(
Expand All @@ -873,7 +881,9 @@ def cancel(self) -> bool:

return self.status() == Status.Cancelled

def store_intermediate_results(self, results: Dict, pub_key: bytes) -> bool:
def store_intermediate_results(
self, results: Dict, pub_key: bytes, **kwargs
) -> bool:
"""Recording Oracle stores intermediate results with Reputation Oracle's public key to S3
and updates the contract's state.
Expand Down Expand Up @@ -938,6 +948,7 @@ def store_intermediate_results(self, results: Dict, pub_key: bytes) -> bool:
"gas": self.gas,
"hmt_server_addr": self.hmt_server_addr,
}
txn_info.update(kwargs)
(hash_, url) = upload(results, pub_key)

self.intermediate_manifest_hash = hash_
Expand All @@ -950,7 +961,7 @@ def store_intermediate_results(self, results: Dict, pub_key: bytes) -> bool:
return True
except Exception as e:
LOG.info(
f"{txn_event} failed with main credentials: {self.gas_payer}, {self.gas_payer_priv} due to {e}. Using secondary ones..."
f"{txn_event} failed with main credentials: {txn_info['gas_payer']}, {txn_info['gas_payer_priv']} due to {e}. Using secondary ones..."
)

raffle_txn_res = self._raffle_txn(
Expand All @@ -965,9 +976,7 @@ def store_intermediate_results(self, results: Dict, pub_key: bytes) -> bool:

return results_stored

def complete(
self, blocking: bool = False, retries: int = 3, delay: int = 5, backoff: int = 2
) -> bool:
def complete(self, **kwargs) -> bool:
"""Completes the Job if it has been paid.
>>> from test.human_protocol_sdk.utils import manifest
Expand Down Expand Up @@ -1014,13 +1023,14 @@ def complete(
"gas": self.gas,
"hmt_server_addr": self.hmt_server_addr,
}
txn_info.update(kwargs)

try:
handle_transaction_with_retry(txn_func, self.retry, *[], **txn_info)
return self.status() == Status.Complete
except Exception as e:
LOG.info(
f"{txn_event} failed with main credentials: {self.gas_payer}, {self.gas_payer_priv} due to {e}. Using secondary ones..."
f"{txn_event} failed with main credentials: {txn_info['gas_payer']}, {txn_info['gas_payer_priv']} due to {e}. Using secondary ones..."
)

raffle_txn_res = self._raffle_txn(
Expand Down Expand Up @@ -1439,7 +1449,7 @@ def _bulk_paid(self) -> int:
{"from": self.gas_payer, "gas": Wei(self.gas)}
)

def _create_escrow(self, trusted_handlers=[]) -> RaffleTxn:
def _create_escrow(self, trusted_handlers=[], **kwargs) -> RaffleTxn:
"""Launches a new escrow contract to the ethereum network.
>>> from test.human_protocol_sdk.utils import manifest
Expand Down Expand Up @@ -1481,6 +1491,7 @@ def _create_escrow(self, trusted_handlers=[]) -> RaffleTxn:
"gas": self.gas,
"hmt_server_addr": self.hmt_server_addr,
}
txn_info.update(kwargs)
func_args = [trusted_handlers]

try:
Expand All @@ -1490,7 +1501,7 @@ def _create_escrow(self, trusted_handlers=[]) -> RaffleTxn:
return {"txn_succeeded": True, "tx_receipt": tx_receipt}
except Exception as e:
LOG.info(
f"{txn_event} failed with main credentials: {self.gas_payer}, {self.gas_payer_priv} due to {e}. Using secondary ones..."
f"{txn_event} failed with main credentials: {txn_info['gas_payer']}, {txn_info['gas_payer_priv']} due to {e}. Using secondary ones..."
)

raffle_txn_res = self._raffle_txn(
Expand Down

0 comments on commit 18bc04a

Please sign in to comment.