diff --git a/docs/sdk/python/human_protocol_sdk.escrow.escrow_client.md b/docs/sdk/python/human_protocol_sdk.escrow.escrow_client.md index 76879c9f8a..c876b9468b 100644 --- a/docs/sdk/python/human_protocol_sdk.escrow.escrow_client.md +++ b/docs/sdk/python/human_protocol_sdk.escrow.escrow_client.md @@ -48,6 +48,18 @@ escrow_client = EscrowClient(w3) ## Module +### *class* human_protocol_sdk.escrow.escrow_client.EscrowCancel(tx_hash, amount_refunded) + +Bases: `object` + +#### \_\_init_\_(tx_hash, amount_refunded) + +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. + ### *class* human_protocol_sdk.escrow.escrow_client.EscrowClient(web3) Bases: `object` @@ -215,11 +227,15 @@ Cancels the specified escrow and sends the balance to the canceler. * **escrow_address** (`str`) – Address of the escrow to cancel * **tx_options** (`Optional`[`TxParams`]) – (Optional) Additional transaction parameters * **Return type:** - `None` + [`EscrowCancel`](#human_protocol_sdk.escrow.escrow_client.EscrowCancel) * **Returns:** - None + EscrowCancel: + An instance of the EscrowCancel class containing details of the cancellation transaction, + including the transaction hash and the amount refunded. * **Raises:** - [**EscrowClientError**](#human_protocol_sdk.escrow.escrow_client.EscrowClientError) – If an error occurs while checking the parameters + * [**EscrowClientError**](#human_protocol_sdk.escrow.escrow_client.EscrowClientError) – If an error occurs while checking the parameters + * [**EscrowClientError**](#human_protocol_sdk.escrow.escrow_client.EscrowClientError) – If the transfer event associated with the cancellation + is not found in the transaction logs * **Example:** ```python from eth_typing import URI @@ -242,7 +258,7 @@ Cancels the specified escrow and sends the balance to the canceler. (w3, gas_payer) = get_w3_with_priv_key('YOUR_PRIVATE_KEY') escrow_client = EscrowClient(w3) - transaction_hash = escrow_client.cancel( + escrow_cancel_data = escrow_client.cancel( "0x62dD51230A30401C455c8398d06F85e4EaB6309f" ) ``` diff --git a/docs/sdk/python/human_protocol_sdk.escrow.md b/docs/sdk/python/human_protocol_sdk.escrow.md index fd20e4b026..d5f4da28c0 100644 --- a/docs/sdk/python/human_protocol_sdk.escrow.md +++ b/docs/sdk/python/human_protocol_sdk.escrow.md @@ -8,6 +8,8 @@ obtain information from both the contracts and subgraph. * [human_protocol_sdk.escrow.escrow_client module](human_protocol_sdk.escrow.escrow_client.md) * [Code Example](human_protocol_sdk.escrow.escrow_client.md#code-example) * [Module](human_protocol_sdk.escrow.escrow_client.md#module) + * [`EscrowCancel`](human_protocol_sdk.escrow.escrow_client.md#human_protocol_sdk.escrow.escrow_client.EscrowCancel) + * [`EscrowCancel.__init__()`](human_protocol_sdk.escrow.escrow_client.md#human_protocol_sdk.escrow.escrow_client.EscrowCancel.__init__) * [`EscrowClient`](human_protocol_sdk.escrow.escrow_client.md#human_protocol_sdk.escrow.escrow_client.EscrowClient) * [`EscrowClient.__init__()`](human_protocol_sdk.escrow.escrow_client.md#human_protocol_sdk.escrow.escrow_client.EscrowClient.__init__) * [`EscrowClient.abort()`](human_protocol_sdk.escrow.escrow_client.md#human_protocol_sdk.escrow.escrow_client.EscrowClient.abort) diff --git a/docs/sdk/python/human_protocol_sdk.md b/docs/sdk/python/human_protocol_sdk.md index b42a16d2be..c7ee11f32c 100644 --- a/docs/sdk/python/human_protocol_sdk.md +++ b/docs/sdk/python/human_protocol_sdk.md @@ -36,6 +36,7 @@ * [human_protocol_sdk.escrow.escrow_client module](human_protocol_sdk.escrow.escrow_client.md) * [Code Example](human_protocol_sdk.escrow.escrow_client.md#code-example) * [Module](human_protocol_sdk.escrow.escrow_client.md#module) + * [`EscrowCancel`](human_protocol_sdk.escrow.escrow_client.md#human_protocol_sdk.escrow.escrow_client.EscrowCancel) * [`EscrowClient`](human_protocol_sdk.escrow.escrow_client.md#human_protocol_sdk.escrow.escrow_client.EscrowClient) * [`EscrowClientError`](human_protocol_sdk.escrow.escrow_client.md#human_protocol_sdk.escrow.escrow_client.EscrowClientError) * [`EscrowConfig`](human_protocol_sdk.escrow.escrow_client.md#human_protocol_sdk.escrow.escrow_client.EscrowConfig) diff --git a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/agreement/measures.py b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/agreement/measures.py index b0ccf3a7a0..a4b938ca89 100644 --- a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/agreement/measures.py +++ b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/agreement/measures.py @@ -185,13 +185,11 @@ def _percentage_from_label_counts(label_counts): max_item_agreements = (n_raters * (n_raters - 1)).sum() if max_item_agreements == 0: - warn( - """ + warn(""" All annotations were made by a single annotator, check your data to ensure this is not an error. Returning 1.0 - """ - ) + """) return 1.0 return item_agreements / max_item_agreements @@ -199,13 +197,11 @@ def _percentage_from_label_counts(label_counts): def _kappa(agreement_observed, agreement_expected): if agreement_expected == 1.0: - warn( - """ + warn(""" Annotations contained only a single value, check your data to ensure this is not an error. Returning 1.0. - """ - ) + """) return 1.0 return (agreement_observed - agreement_expected) / (1 - agreement_expected) diff --git a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/constants.py b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/constants.py index 2c2e799f71..2be1266488 100644 --- a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/constants.py +++ b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/constants.py @@ -26,7 +26,9 @@ class ChainId(Enum): ChainId.MAINNET: { "title": "Ethereum", "scan_url": "https://etherscan.io", - "subgraph_url": "https://api.thegraph.com/subgraphs/name/humanprotocol/mainnet-v2", + "subgraph_url": ( + "https://api.thegraph.com/subgraphs/name/humanprotocol/mainnet-v2" + ), "hmt_address": "0xd1ba9BAC957322D6e8c07a160a3A8dA11A0d2867", "factory_address": "0xD9c75a1Aa4237BB72a41E5E26bd8384f10c1f55a", "staking_address": "0x05398211bA2046E296fBc9a9D3EB49e3F15C3123", @@ -38,13 +40,17 @@ class ChainId(Enum): ChainId.GOERLI: { "title": "Ethereum Goerli", "scan_url": "https://goerli.etherscan.io", - "subgraph_url": "https://api.thegraph.com/subgraphs/name/humanprotocol/goerli-v2", + "subgraph_url": ( + "https://api.thegraph.com/subgraphs/name/humanprotocol/goerli-v2" + ), "hmt_address": "0xd3A31D57FDD790725d0F6B78095F62E8CD4ab317", "factory_address": "0x87469B4f2Fcf37cBd34E54244c0BD4Fa0603664c", "staking_address": "0xf46B45Df3d956369726d8Bd93Ba33963Ab692920", "reward_pool_address": "0x0376D26246Eb35FF4F9924cF13E6C05fd0bD7Fb4", "kvstore_address": "0x19Fc3e859C1813ac9427a7a78BeB9ae102CE96d3", - "old_subgraph_url": "https://api.thegraph.com/subgraphs/name/humanprotocol/goerli", + "old_subgraph_url": ( + "https://api.thegraph.com/subgraphs/name/humanprotocol/goerli" + ), "old_factory_address": "0xaAe6a2646C1F88763E62e0cD08aD050Ea66AC46F", }, ChainId.BSC_MAINNET: { @@ -62,55 +68,73 @@ class ChainId(Enum): ChainId.BSC_TESTNET: { "title": "Binance Smart Chain (Testnet)", "scan_url": "https://testnet.bscscan.com", - "subgraph_url": "https://api.thegraph.com/subgraphs/name/humanprotocol/bsctest-v2", + "subgraph_url": ( + "https://api.thegraph.com/subgraphs/name/humanprotocol/bsctest-v2" + ), "hmt_address": "0xE3D74BBFa45B4bCa69FF28891fBE392f4B4d4e4d", "factory_address": "0x2bfA592DBDaF434DDcbb893B1916120d181DAD18", "staking_address": "0x5517fE916Fe9F8dB15B0DDc76ebDf0BdDCd4ed18", "reward_pool_address": "0xB0A0500103eCEc431b73F6BAd923F0a2774E6e29", "kvstore_address": "0x32e27177BA6Ea91cf28dfd91a0Da9822A4b74EcF", - "old_subgraph_url": "https://api.thegraph.com/subgraphs/name/humanprotocol/bsctest", + "old_subgraph_url": ( + "https://api.thegraph.com/subgraphs/name/humanprotocol/bsctest" + ), "old_factory_address": "0xaae6a2646c1f88763e62e0cd08ad050ea66ac46f", }, ChainId.POLYGON: { "title": "Polygon", "scan_url": "https://polygonscan.com", - "subgraph_url": "https://api.thegraph.com/subgraphs/name/humanprotocol/polygon-v2", + "subgraph_url": ( + "https://api.thegraph.com/subgraphs/name/humanprotocol/polygon-v2" + ), "hmt_address": "0xc748B2A084F8eFc47E086ccdDD9b7e67aEb571BF", "factory_address": "0xBDBfD2cC708199C5640C6ECdf3B0F4A4C67AdfcB", "staking_address": "0xcbAd56bE3f504E98bd70875823d3CC0242B7bB29", "reward_pool_address": "0xa8e32d777a3839440cc7c24D591A64B9481753B3", "kvstore_address": "0xbcB28672F826a50B03EE91B28145EAbddA73B2eD", - "old_subgraph_url": "https://api.thegraph.com/subgraphs/name/humanprotocol/polygon", + "old_subgraph_url": ( + "https://api.thegraph.com/subgraphs/name/humanprotocol/polygon" + ), "old_factory_address": "0x45eBc3eAE6DA485097054ae10BA1A0f8e8c7f794", }, ChainId.POLYGON_MUMBAI: { "title": "Polygon Mumbai", "scan_url": "https://mumbai.polygonscan.com", - "subgraph_url": "https://api.thegraph.com/subgraphs/name/humanprotocol/mumbai-v2", + "subgraph_url": ( + "https://api.thegraph.com/subgraphs/name/humanprotocol/mumbai-v2" + ), "hmt_address": "0x0376D26246Eb35FF4F9924cF13E6C05fd0bD7Fb4", "factory_address": "0xA8D927C4DA17A6b71675d2D49dFda4E9eBE58f2d", "staking_address": "0x7Fd3dF914E7b6Bd96B4c744Df32183b51368Bfac", "reward_pool_address": "0xf0145eD99AC3c4f877aDa7dA4D1E059ec9116BAE", "kvstore_address": "0xD96158c7267Ea658a4688F4aEf1c85659851625d", - "old_subgraph_url": "https://api.thegraph.com/subgraphs/name/humanprotocol/mumbai", + "old_subgraph_url": ( + "https://api.thegraph.com/subgraphs/name/humanprotocol/mumbai" + ), "old_factory_address": "0x558cd800f9F0B02f3B149667bDe003284c867E94", }, ChainId.MOONBEAM: { "title": "Moonbeam", "scan_url": "https://moonbeam.moonscan.io", - "subgraph_url": "https://api.thegraph.com/subgraphs/name/humanprotocol/moonbeam-v2", + "subgraph_url": ( + "https://api.thegraph.com/subgraphs/name/humanprotocol/moonbeam-v2" + ), "hmt_address": "0x3b25BC1dC591D24d60560d0135D6750A561D4764", "factory_address": "0xD9c75a1Aa4237BB72a41E5E26bd8384f10c1f55a", "staking_address": "0x05398211bA2046E296fBc9a9D3EB49e3F15C3123", "reward_pool_address": "0x4A5963Dd6792692e9147EdC7659936b96251917a", "kvstore_address": "0x2B95bEcb6EBC4589f64CB000dFCF716b4aeF8aA6", - "old_subgraph_url": "https://api.thegraph.com/subgraphs/name/humanprotocol/moonbeam", + "old_subgraph_url": ( + "https://api.thegraph.com/subgraphs/name/humanprotocol/moonbeam" + ), "old_factory_address": "0x98108c28B7767a52BE38B4860832dd4e11A7ecad", }, ChainId.MOONBASE_ALPHA: { "title": "Moonbase Alpha", "scan_url": "https://moonbase.moonscan.io/", - "subgraph_url": "https://api.thegraph.com/subgraphs/name/humanprotocol/moonbase-alpha-v2", + "subgraph_url": ( + "https://api.thegraph.com/subgraphs/name/humanprotocol/moonbase-alpha-v2" + ), "hmt_address": "0x2dd72db2bBA65cE663e476bA8b84A1aAF802A8e3", "factory_address": "0x5e622FF522D81aa426f082bDD95210BC25fCA7Ed", "staking_address": "0xBFC7009F3371F93F3B54DdC8caCd02914a37495c", @@ -122,13 +146,17 @@ class ChainId(Enum): ChainId.AVALANCHE: { "title": "Avalanche C-Chain Mainnet", "scan_url": "https://snowtrace.io", - "subgraph_url": "https://api.thegraph.com/subgraphs/name/humanprotocol/avalanche-v2", + "subgraph_url": ( + "https://api.thegraph.com/subgraphs/name/humanprotocol/avalanche-v2" + ), "hmt_address": "0x12365293cb6477d4fc2686e46BB97E3Fb64f1550", "factory_address": "0xD9c75a1Aa4237BB72a41E5E26bd8384f10c1f55a", "staking_address": "0x05398211bA2046E296fBc9a9D3EB49e3F15C3123", "reward_pool_address": "0x4A5963Dd6792692e9147EdC7659936b96251917a", "kvstore_address": "0x9Bc7bff35B2Be2413708d48c3B0aEF5c43646728", - "old_subgraph_url": "https://api.thegraph.com/subgraphs/name/humanprotocol/avalanche", + "old_subgraph_url": ( + "https://api.thegraph.com/subgraphs/name/humanprotocol/avalanche" + ), "old_factory_address": "0x9767a578ba7a5FA1563c8229943cB01cd8446BB4", }, ChainId.AVALANCHE_TESTNET: { @@ -140,7 +168,9 @@ class ChainId(Enum): "staking_address": "0x9890473B0b93E24d6D1a8Dfb739D577C6f25FFd3", "reward_pool_address": "0x5517fE916Fe9F8dB15B0DDc76ebDf0BdDCd4ed18", "kvstore_address": "0x3aD4B091E054f192a822D1406f4535eAd38580e4", - "old_subgraph_url": "https://api.thegraph.com/subgraphs/name/humanprotocol/fuji", + "old_subgraph_url": ( + "https://api.thegraph.com/subgraphs/name/humanprotocol/fuji" + ), "old_factory_address": "0xfb4469201951C3B9a7F1996c477cb7BDBEcE0A88", }, ChainId.CELO: { @@ -158,7 +188,9 @@ class ChainId(Enum): ChainId.CELO_ALFAJORES: { "title": "Celo", "scan_url": "https://alfajores.celoscan.io/", - "subgraph_url": "https://api.thegraph.com/subgraphs/name/humanprotocol/celo-alfajores", + "subgraph_url": ( + "https://api.thegraph.com/subgraphs/name/humanprotocol/celo-alfajores" + ), "hmt_address": "0x2736B33455A872dC478E1E004106D04c35472468", "factory_address": "0x86Af9f6Cd34B69Db1B202223C6d6D109f2491569", "staking_address": "0x003548Df34be8836cF0F9673403a1E40ba449a0F", @@ -170,7 +202,9 @@ class ChainId(Enum): ChainId.SKALE: { "title": "SKALE Human Protocol Chain", "scan_url": "https://wan-red-ain.explorer.mainnet.skalenodes.com/", - "subgraph_url": "https://graph-skale.humanprotocol.org/subgraphs/name/skale-human", + "subgraph_url": ( + "https://graph-skale.humanprotocol.org/subgraphs/name/skale-human" + ), "hmt_address": "0x6E5FF61Ea88270F6142E0E0eC8cbe9d67476CbCd", "factory_address": "0x319070b49C8d1cC015915D1E7Eb5fd8e22833885", "staking_address": "0x79F37FB9C210910733c16228AC4D14a8e32C11BD", diff --git a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/escrow/escrow_client.py b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/escrow/escrow_client.py index 9f4a848552..0cbef4fa60 100644 --- a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/escrow/escrow_client.py +++ b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/escrow/escrow_client.py @@ -63,14 +63,28 @@ def get_w3_with_priv_key(priv_key: str): 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. @@ -681,15 +695,20 @@ def get_w3_with_priv_key(priv_key: str): def cancel( self, escrow_address: str, tx_options: Optional[TxParams] = None - ) -> 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: None + :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 @@ -714,7 +733,7 @@ def get_w3_with_priv_key(priv_key: str): (w3, gas_payer) = get_w3_with_priv_key('YOUR_PRIVATE_KEY') escrow_client = EscrowClient(w3) - transaction_hash = escrow_client.cancel( + escrow_cancel_data = escrow_client.cancel( "0x62dD51230A30401C455c8398d06F85e4EaB6309f" ) """ @@ -722,7 +741,7 @@ def get_w3_with_priv_key(priv_key: str): if not Web3.is_address(escrow_address): raise EscrowClientError(f"Invalid escrow address: {escrow_address}") - handle_transaction( + transaction_receipt = handle_transaction( self.w3, "Cancel", self._get_escrow_contract(escrow_address).functions.cancel(), @@ -730,6 +749,33 @@ def get_w3_with_priv_key(priv_key: str): 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. diff --git a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/escrow/escrow_utils.py b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/escrow/escrow_utils.py index 40548d5c6e..1157508a12 100644 --- a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/escrow/escrow_utils.py +++ b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/escrow/escrow_utils.py @@ -165,20 +165,26 @@ def get_escrows( query=get_escrows_query(filter), params={ "launcher": filter.launcher.lower() if filter.launcher else None, - "reputationOracle": filter.reputation_oracle.lower() - if filter.reputation_oracle - else None, - "recordingOracle": filter.recording_oracle.lower() - if filter.recording_oracle - else None, - "exchangeOracle": filter.exchange_oracle.lower() - if filter.exchange_oracle - else None, + "reputationOracle": ( + filter.reputation_oracle.lower() + if filter.reputation_oracle + else None + ), + "recordingOracle": ( + filter.recording_oracle.lower() + if filter.recording_oracle + else None + ), + "exchangeOracle": ( + filter.exchange_oracle.lower() + if filter.exchange_oracle + else None + ), "jobRequesterId": filter.job_requester_id, "status": filter.status.name if filter.status else None, - "from": int(filter.date_from.timestamp()) - if filter.date_from - else None, + "from": ( + int(filter.date_from.timestamp()) if filter.date_from else None + ), "to": int(filter.date_to.timestamp()) if filter.date_to else None, }, ) @@ -208,17 +214,23 @@ def get_escrows( manifest_hash=escrow.get("manifestHash", None), manifest_url=escrow.get("manifestUrl", None), recording_oracle=escrow.get("recordingOracle", None), - recording_oracle_fee=int(escrow.get("recordingOracleFee")) - if escrow.get("recordingOracleFee", None) - else None, + recording_oracle_fee=( + int(escrow.get("recordingOracleFee")) + if escrow.get("recordingOracleFee", None) + else None + ), reputation_oracle=escrow.get("reputationOracle", None), - reputation_oracle_fee=int(escrow.get("reputationOracleFee")) - if escrow.get("reputationOracleFee", None) - else None, + reputation_oracle_fee=( + int(escrow.get("reputationOracleFee")) + if escrow.get("reputationOracleFee", None) + else None + ), exchange_oracle=escrow.get("exchangeOracle", None), - exchange_oracle_fee=int(escrow.get("exchangeOracleFee")) - if escrow.get("exchangeOracleFee", None) - else None, + exchange_oracle_fee=( + int(escrow.get("exchangeOracleFee")) + if escrow.get("exchangeOracleFee", None) + else None + ), ) for escrow in escrows_raw ] @@ -294,15 +306,21 @@ def get_escrow( manifest_hash=escrow.get("manifestHash", None), manifest_url=escrow.get("manifestUrl", None), recording_oracle=escrow.get("recordingOracle", None), - recording_oracle_fee=int(escrow.get("recordingOracleFee")) - if escrow.get("recordingOracleFee", None) - else None, + recording_oracle_fee=( + int(escrow.get("recordingOracleFee")) + if escrow.get("recordingOracleFee", None) + else None + ), reputation_oracle=escrow.get("reputationOracle", None), - reputation_oracle_fee=int(escrow.get("reputationOracleFee")) - if escrow.get("reputationOracleFee", None) - else None, + reputation_oracle_fee=( + int(escrow.get("reputationOracleFee")) + if escrow.get("reputationOracleFee", None) + else None + ), exchange_oracle=escrow.get("exchangeOracle", None), - exchange_oracle_fee=int(escrow.get("exchangeOracleFee")) - if escrow.get("exchangeOracleFee", None) - else None, + exchange_oracle_fee=( + int(escrow.get("exchangeOracleFee")) + if escrow.get("exchangeOracleFee", None) + else None + ), ) diff --git a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/escrow.py b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/escrow.py index 3d8abdaebc..168c0dbca9 100644 --- a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/escrow.py +++ b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/escrow.py @@ -59,18 +59,18 @@ def get_escrows_query(filter: EscrowFilter): """.format( escrow_fragment=escrow_fragment, launcher_clause="launcher: $launcher" if filter.launcher else "", - reputation_oracle_clause="reputationOracle: $reputationOracle" - if filter.reputation_oracle - else "", - recording_oracle_clause="recordingOracle: $recordingOracle" - if filter.recording_oracle - else "", - exchange_oracle_clause="exchangeOracle: $exchangeOracle" - if filter.exchange_oracle - else "", - job_requester_clause="jobRequesterId: $jobRequesterId" - if filter.job_requester_id - else "", + reputation_oracle_clause=( + "reputationOracle: $reputationOracle" if filter.reputation_oracle else "" + ), + recording_oracle_clause=( + "recordingOracle: $recordingOracle" if filter.recording_oracle else "" + ), + exchange_oracle_clause=( + "exchangeOracle: $exchangeOracle" if filter.exchange_oracle else "" + ), + job_requester_clause=( + "jobRequesterId: $jobRequesterId" if filter.job_requester_id else "" + ), status_clause="status: $status" if filter.status else "", from_clause="createdAt_gte: $from" if filter.date_from else "", to_clause="createdAt_lte: $to" if filter.date_from else "", @@ -87,6 +87,4 @@ def get_escrow_query(): }} }} {escrow_fragment} -""".format( - escrow_fragment=escrow_fragment - ) +""".format(escrow_fragment=escrow_fragment) diff --git a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/hmtoken.py b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/hmtoken.py index a9bd32c28e..04f2096ad7 100644 --- a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/hmtoken.py +++ b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/hmtoken.py @@ -12,6 +12,4 @@ }} }} {holder_fragment} -""".format( - holder_fragment=holder_fragment -) +""".format(holder_fragment=holder_fragment) diff --git a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/payout.py b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/payout.py index 17bcf3c8a4..59db6df906 100644 --- a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/payout.py +++ b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/payout.py @@ -33,9 +33,9 @@ def get_payouts_query(filter: PayoutFilter): {payout_fragment} """.format( payout_fragment=payout_fragment, - escrow_address_clause="escrowAddress: $escrowAddress" - if filter.escrow_address - else "", + escrow_address_clause=( + "escrowAddress: $escrowAddress" if filter.escrow_address else "" + ), recipient_clause="recipient: $recipient" if filter.recipient else "", from_clause="createdAt_gte: $from" if filter.date_from else "", to_clause="createdAt_lte: $to" if filter.date_from else "", diff --git a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/reward.py b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/reward.py index 3f43bbff9e..09c06278ae 100644 --- a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/reward.py +++ b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/reward.py @@ -14,6 +14,4 @@ }} }} {reward_added_event_fragment} -""".format( - reward_added_event_fragment=reward_added_event_fragment -) +""".format(reward_added_event_fragment=reward_added_event_fragment) diff --git a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/statistics.py b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/statistics.py index 74da8d6815..4e787baa9a 100644 --- a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/statistics.py +++ b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/gql/statistics.py @@ -56,9 +56,7 @@ }} }} {hmtoken_statistics_fragment} -""".format( - hmtoken_statistics_fragment=hmtoken_statistics_fragment -) +""".format(hmtoken_statistics_fragment=hmtoken_statistics_fragment) get_escrow_statistics_query = """ query GetEscrowStatistics {{ @@ -67,9 +65,7 @@ }} }} {escrow_statistics_fragment} -""".format( - escrow_statistics_fragment=escrow_statistics_fragment -) +""".format(escrow_statistics_fragment=escrow_statistics_fragment) def get_event_day_data_query(param: StatisticsParam): diff --git a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/statistics/statistics_client.py b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/statistics/statistics_client.py index eb59b0a01f..0321154d95 100644 --- a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/statistics/statistics_client.py +++ b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/statistics/statistics_client.py @@ -470,12 +470,12 @@ def get_payment_statistics( ), total_amount_paid=int(event_day_data.get("dailyPayoutAmount", 0)), total_count=int(event_day_data.get("dailyPayoutCount", 0)), - average_amount_per_worker=int( - event_day_data.get("dailyPayoutAmount", 0) - ) - / int(event_day_data.get("dailyWorkerCount")) - if event_day_data.get("dailyWorkerCount", "0") != "0" - else 0, + average_amount_per_worker=( + int(event_day_data.get("dailyPayoutAmount", 0)) + / int(event_day_data.get("dailyWorkerCount")) + if event_day_data.get("dailyWorkerCount", "0") != "0" + else 0 + ), ) for event_day_data in event_day_datas ], diff --git a/packages/sdk/python/human-protocol-sdk/test/human_protocol_sdk/escrow/test_escrow_client.py b/packages/sdk/python/human-protocol-sdk/test/human_protocol_sdk/escrow/test_escrow_client.py index 34b302e8c3..14d3be5587 100644 --- a/packages/sdk/python/human-protocol-sdk/test/human_protocol_sdk/escrow/test_escrow_client.py +++ b/packages/sdk/python/human-protocol-sdk/test/human_protocol_sdk/escrow/test_escrow_client.py @@ -8,6 +8,7 @@ from human_protocol_sdk.escrow import EscrowClient, EscrowClientError, EscrowConfig from human_protocol_sdk.filter import EscrowFilter, FilterError from web3 import Web3 +from web3.constants import ADDRESS_ZERO from web3.middleware import construct_sign_and_send_raw_middleware from web3.providers.rpc import HTTPProvider @@ -1478,12 +1479,49 @@ def test_cancel(self): mock_contract = MagicMock() mock_contract.functions.cancel = MagicMock() self.escrow._get_escrow_contract = MagicMock(return_value=mock_contract) - escrow_address = "0x1234567890123456789012345678901234567890" + escrow_address = "0xa76507AbFE3B67cB25F16DbC75a883D4190B7e46" + token_address = "0x0376D26246Eb35FF4F9924cF13E6C05fd0bD7Fb4" + + self.escrow.get_token_address = MagicMock(return_value=token_address) with patch( "human_protocol_sdk.escrow.escrow_client.handle_transaction" ) as mock_function: - self.escrow.cancel(escrow_address) + tx_hash = bytes.fromhex( + "01682095d5abb0270d11a31139b9a1f410b363c84add467004e728ec831bd529" + ) + amount_refunded = 187744067287473730 + mock_function.return_value = { + "transactionHash": tx_hash, + "logs": [ + { + "logIndex": 0, + "transactionIndex": 0, + "transactionHash": tx_hash, + "blockHash": bytes.fromhex( + "92abf9325a3959a911a2581e9ea36cba3060d8b293b50e5738ff959feb95258a" + ), + "blockNumber": 5, + "address": token_address, + "data": bytes.fromhex( + "000000000000000000000000000000000000000000000000029b003c075b5e42" + ), + "topics": [ + bytes.fromhex( + "ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" + ), + bytes.fromhex( + "000000000000000000000000a76507abfe3b67cb25f16dbc75a883d4190b7e46" + ), + bytes.fromhex( + "0000000000000000000000005607acf0828e238099aa1784541a5abd7f975c76" + ), + ], + } + ], + } + + escrow_cancel_data = self.escrow.cancel(escrow_address) self.escrow._get_escrow_contract.assert_called_once_with(escrow_address) mock_contract.functions.cancel.assert_called_once_with() @@ -1495,6 +1533,9 @@ def test_cancel(self): None, ) + self.assertEqual(escrow_cancel_data.txHash, tx_hash.hex()) + self.assertEqual(escrow_cancel_data.amountRefunded, amount_refunded) + def test_cancel_invalid_address(self): escrow_address = "invalid_address" @@ -1560,13 +1601,51 @@ def test_cancel_with_tx_options(self): mock_contract = MagicMock() mock_contract.functions.cancel = MagicMock() self.escrow._get_escrow_contract = MagicMock(return_value=mock_contract) - escrow_address = "0x1234567890123456789012345678901234567890" tx_options = {"gas": 50000} + escrow_address = "0xa76507AbFE3B67cB25F16DbC75a883D4190B7e46" + token_address = "0x0376D26246Eb35FF4F9924cF13E6C05fd0bD7Fb4" + + self.escrow.get_token_address = MagicMock(return_value=token_address) + with patch( "human_protocol_sdk.escrow.escrow_client.handle_transaction" ) as mock_function: - self.escrow.cancel(escrow_address, tx_options) + tx_hash = bytes.fromhex( + "01682095d5abb0270d11a31139b9a1f410b363c84add467004e728ec831bd529" + ) + amount_refunded = 187744067287473730 + mock_function.return_value = { + "transactionHash": tx_hash, + "logs": [ + { + "logIndex": 0, + "transactionIndex": 0, + "transactionHash": tx_hash, + "blockHash": bytes.fromhex( + "92abf9325a3959a911a2581e9ea36cba3060d8b293b50e5738ff959feb95258a" + ), + "blockNumber": 5, + "address": token_address, + "data": bytes.fromhex( + "000000000000000000000000000000000000000000000000029b003c075b5e42" + ), + "topics": [ + bytes.fromhex( + "ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" + ), + bytes.fromhex( + "000000000000000000000000a76507abfe3b67cb25f16dbc75a883d4190b7e46" + ), + bytes.fromhex( + "0000000000000000000000005607acf0828e238099aa1784541a5abd7f975c76" + ), + ], + } + ], + } + + escrow_cancel_data = self.escrow.cancel(escrow_address, tx_options) self.escrow._get_escrow_contract.assert_called_once_with(escrow_address) mock_contract.functions.cancel.assert_called_once_with() @@ -1578,6 +1657,9 @@ def test_cancel_with_tx_options(self): tx_options, ) + self.assertEqual(escrow_cancel_data.txHash, tx_hash.hex()) + self.assertEqual(escrow_cancel_data.amountRefunded, amount_refunded) + def test_abort(self): mock_contract = MagicMock() mock_contract.functions.abort = MagicMock() diff --git a/packages/sdk/python/human-protocol-sdk/test/human_protocol_sdk/kvstore/test_kvstore_client.py b/packages/sdk/python/human-protocol-sdk/test/human_protocol_sdk/kvstore/test_kvstore_client.py index e5238ea181..7937a84700 100644 --- a/packages/sdk/python/human-protocol-sdk/test/human_protocol_sdk/kvstore/test_kvstore_client.py +++ b/packages/sdk/python/human-protocol-sdk/test/human_protocol_sdk/kvstore/test_kvstore_client.py @@ -241,9 +241,12 @@ def test_set_url_with_key(self): content = "example" content_hash = self.w3.keccak(text=content).hex() - with patch( - "human_protocol_sdk.kvstore.kvstore_client.handle_transaction" - ) as mock_handle_transaction, patch("requests.get") as mock_get: + with ( + patch( + "human_protocol_sdk.kvstore.kvstore_client.handle_transaction" + ) as mock_handle_transaction, + patch("requests.get") as mock_get, + ): mock_response = mock_get.return_value mock_response.text = content