Skip to content

Commit

Permalink
remove create_and_setup_escrow function
Browse files Browse the repository at this point in the history
  • Loading branch information
leric7 committed Aug 29, 2024
1 parent 13cca30 commit 0c7b261
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 418 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ jest.mock('minio', () => {
jest.mock('@human-protocol/sdk', () => ({
...jest.requireActual('@human-protocol/sdk'),
EscrowClient: {
build: jest.fn().mockImplementation(() => ({
createAndSetupEscrow: jest.fn().mockResolvedValue(MOCK_ADDRESS),
})),
build: jest.fn().mockImplementation(() => ({})),
},
StorageClient: jest.fn().mockImplementation(() => ({
downloadFileFromUrl: jest.fn().mockResolvedValue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Test } from '@nestjs/testing';
import { RoutingProtocolService } from './routing-protocol.service';
import { NETWORKS } from '@human-protocol/sdk';
import {
MOCK_ADDRESS,
MOCK_FILE_HASH,
MOCK_FILE_KEY,
MOCK_FILE_URL,
Expand All @@ -12,9 +11,7 @@ import {
jest.mock('@human-protocol/sdk', () => ({
...jest.requireActual('@human-protocol/sdk'),
EscrowClient: {
build: jest.fn().mockImplementation(() => ({
createAndSetupEscrow: jest.fn().mockResolvedValue(MOCK_ADDRESS),
})),
build: jest.fn().mockImplementation(() => ({})),
},
StorageClient: jest.fn().mockImplementation(() => ({
uploadFiles: jest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
bulk_payout,
create_escrow,
fund_escrow,
setup_escrow,
get_intermediate_results_url,
)

Expand All @@ -51,6 +52,7 @@ def setUp(self):

self.escrow_address = create_escrow(self.w3)
fund_escrow(self.w3, self.escrow_address)
setup_escrow(self.w3, self.escrow_address)

def escrow(self, status: str = "Pending", balance: float = amount):
mock_escrow = MagicMock()
Expand Down Expand Up @@ -79,6 +81,7 @@ def test_validate_escrow_without_funds(self):
def test_validate_escrow_invalid_status(self):
escrow_address = create_escrow(self.w3)
fund_escrow(self.w3, escrow_address)
setup_escrow(self.w3, self.escrow_address)
bulk_payout(
self.w3,
escrow_address,
Expand Down Expand Up @@ -132,6 +135,8 @@ def test_get_encrypted_escrow_manifest(self):

def test_store_results(self):
escrow_address = create_escrow(self.w3)
fund_escrow(self.w3, escrow_address)
setup_escrow(self.w3, self.escrow_address)
with patch("src.chain.escrow.get_web3") as mock_function:
mock_function.return_value = self.w3
results = store_results(
Expand All @@ -143,13 +148,17 @@ def test_store_results(self):

def test_store_results_invalid_url(self):
escrow_address = create_escrow(self.w3)
fund_escrow(self.w3, escrow_address)
setup_escrow(self.w3, self.escrow_address)
with patch("src.chain.escrow.get_web3") as mock_function:
mock_function.return_value = self.w3
with pytest.raises(EscrowClientError, match="Invalid URL: invalid_url"):
store_results(self.w3.eth.chain_id, escrow_address, "invalid_url", DEFAULT_HASH)

def test_store_results_invalid_hash(self):
escrow_address = create_escrow(self.w3)
fund_escrow(self.w3, escrow_address)
setup_escrow(self.w3, self.escrow_address)
with patch("src.chain.escrow.get_web3") as mock_function:
mock_function.return_value = self.w3
with pytest.raises(EscrowClientError, match="Invalid empty hash"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from src.services.webhook import OracleWebhookDirectionTags

from tests.utils.constants import DEFAULT_GAS_PAYER_PRIV, SIGNATURE
from tests.utils.setup_escrow import create_escrow, fund_escrow
from tests.utils.setup_escrow import create_escrow, fund_escrow, setup_escrow


class ServiceIntegrationTest(unittest.TestCase):
Expand Down Expand Up @@ -52,6 +52,7 @@ def make_webhook(self, escrow_address):
def test_process_exchange_oracle_webhook(self):
escrow_address = create_escrow(self.w3)
fund_escrow(self.w3, escrow_address)
setup_escrow(self.w3, self.escrow_address)

webhook = self.make_webhook(escrow_address)
self.session.add(webhook)
Expand Down Expand Up @@ -111,6 +112,7 @@ def test_process_job_launcher_webhooks_invalid_manifest_url(self, mock_manifest_
mock_manifest_url.return_value = "invalid_url"
escrow_address = create_escrow(self.w3)
fund_escrow(self.w3, escrow_address)
setup_escrow(self.w3, self.escrow_address)

webhook = self.make_webhook(escrow_address)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@ def create_escrow(web3: Web3):
escrow_client = EscrowClient(web3)
staking_client.approve_stake(amount)
staking_client.stake(amount)
return escrow_client.create_and_setup_escrow(
return escrow_client.create(
token_address=NETWORKS[ChainId.LOCALHOST]["hmt_address"],
trusted_handlers=[web3.eth.default_account],
job_requester_id=JOB_REQUESTER_ID,
)


def setup_escrow(web3: Web3, escrow_address: str):
escrow_client = EscrowClient(web3)
escrow_client.setup(
escrow_address=escrow_address,
escrow_config=EscrowConfig(
exchange_oracle_address=EXCHANGE_ORACLE_ADDRESS,
exchange_oracle_fee=EXCHANGE_ORACLE_FEE,
Expand Down
19 changes: 16 additions & 3 deletions packages/sdk/python/human-protocol-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,17 @@ escrow_config = EscrowConfig(
)
```

Calling `create_and_setup_escrow`, the escrow can be created and set up as follows:
Calling `create`, the escrow can be created:

```python
from human_protocol_sdk import EscrowClient

escrow_client = EscrowClient(w3)
escrow_address = escrow_client.create_and_setup_escrow(
job_requester_id = 'job-requester'
escrow_address = escrow_client.create(
token_address = "0x5FbDB2315678afecb367f032d93F642f64180aa3",
trusted_handlers = ["0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"],
escrow_config = escrow_config
job_requester_id = job_requester_id
)
```

Expand All @@ -159,6 +160,18 @@ escrow_client.fund(
)
```

Calling `setup`, the escrow can be setup:

```python
from human_protocol_sdk import EscrowClient

escrow_client = EscrowClient(w3)
escrow_client.setup(
escrow_address = escrow_address,
escrow_config = escrow_config
)
```

While no payouts have been performed, aborting and canceling a job is still possible.

```python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,83 +356,6 @@ def get_w3_with_priv_key(priv_key: str):
tx_options,
)

def create_and_setup_escrow(
self,
token_address: str,
trusted_handlers: List[str],
job_requester_id: str,
escrow_config: EscrowConfig,
amount: Decimal,
) -> 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",
)
amount = Web3.to_wei(5, 'ether') # convert from ETH to WEI
escrow_address = escrow_client.create_and_setup_escrow(
token_address,
trusted_handlers,
job_requester_id,
escrow_config,
amount
)
"""

escrow_address = self.create_escrow(
token_address, trusted_handlers, job_requester_id
)
self.fund(escrow_address, amount)
self.setup(escrow_address, escrow_config)

return escrow_address

def fund(
self,
escrow_address: str,
Expand Down
Loading

0 comments on commit 0c7b261

Please sign in to comment.