Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK] [Python] Updated cancel method #960

Merged
merged 7 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions docs/sdk/python/human_protocol_sdk.escrow.escrow_client.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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
Expand All @@ -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"
)
```
Expand Down
2 changes: 2 additions & 0 deletions docs/sdk/python/human_protocol_sdk.escrow.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/sdk/python/human_protocol_sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,27 +185,23 @@ 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


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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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: {
Expand All @@ -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",
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -714,22 +733,49 @@ 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"
)
"""

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(),
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.
Expand Down
Loading