From ae0b1d173481c167e348548f9147b243b3d1c4dc Mon Sep 17 00:00:00 2001 From: "Kieran R. Prasch" Date: Thu, 2 Jan 2020 15:01:25 -0800 Subject: [PATCH] Adds new contract function agency for worklock: refunds, cancellation, tests to match. --- nucypher/blockchain/eth/agents.py | 20 +++++++++++- .../entities/agents/test_worklock_agent.py | 32 ++++++++++++------- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/nucypher/blockchain/eth/agents.py b/nucypher/blockchain/eth/agents.py index 0aceff3c4..1bcd5465a 100644 --- a/nucypher/blockchain/eth/agents.py +++ b/nucypher/blockchain/eth/agents.py @@ -970,7 +970,7 @@ class WorkLockAgent(EthereumContractAgent): def bid(self, eth_amount: int, sender_address: str) -> dict: """ - Bid for tokens with ETH. + Bid for NU tokens with ETH. """ contract_function = self.contract.functions.bid() receipt = self.blockchain.send_transaction(contract_function=contract_function, @@ -978,6 +978,15 @@ class WorkLockAgent(EthereumContractAgent): payload={'value': eth_amount}) return receipt + def cancel_bid(self, sender_address: str) -> dict: + """ + Cancel bid and refund deposited ETH. + """ + contract_function = self.contract.functions.cancelBid() + receipt = self.blockchain.send_transaction(contract_function=contract_function, + sender_address=sender_address) + return receipt + def claim(self, sender_address: str) -> dict: """ Claim tokens - will be deposited and locked as stake in the StakingEscrow contract. @@ -987,6 +996,15 @@ class WorkLockAgent(EthereumContractAgent): sender_address=sender_address) return receipt + def burn_unclaimed(self, sender_address: str) -> dict: + """ + Burn unclaimed tokens. + """ + contract_function = self.contract.functions.burnUnclaimed() + receipt = self.blockchain.send_transaction(contract_function=contract_function, + sender_address=sender_address) + return receipt + def refund(self, sender_address: str) -> dict: """ Refund ETH for completed work. diff --git a/tests/blockchain/eth/entities/agents/test_worklock_agent.py b/tests/blockchain/eth/entities/agents/test_worklock_agent.py index d3e5a4196..a5481cb1e 100644 --- a/tests/blockchain/eth/entities/agents/test_worklock_agent.py +++ b/tests/blockchain/eth/entities/agents/test_worklock_agent.py @@ -48,7 +48,6 @@ def test_bidding_post_funding(testerchain, agency, token_economics, test_registr maximum_deposit_eth = token_economics.maximum_allowed_locked // DEPOSIT_RATE minimum_deposit_eth = token_economics.minimum_allowed_locked // DEPOSIT_RATE - # testerchain.time_travel(seconds=3602) # Wait exactly 1 hour, until bid start time agent = ContractAgency.get_agent(WorkLockAgent, registry=test_registry) # Round 1 @@ -69,23 +68,34 @@ def test_bidding_post_funding(testerchain, agency, token_economics, test_registr assert receipt['status'] == 1 -def test_get_remaining_work(testerchain, agency, token_economics, test_registry): +def test_get_remaining_work_before_bidding_ends(testerchain, agency, token_economics, test_registry): agent = ContractAgency.get_agent(WorkLockAgent, registry=test_registry) bidder = testerchain.unassigned_accounts[-1] remaining = agent.get_remaining_work(target_address=bidder) - assert remaining + assert remaining == 0 -def test_claim(testerchain, agency, token_economics, test_registry): - testerchain.time_travel(seconds=(60*60)+1) # Wait exactly 1 hour + 1 second +def test_early_claim(testerchain, agency, token_economics, test_registry): + agent = ContractAgency.get_agent(WorkLockAgent, registry=test_registry) + bidder = testerchain.unassigned_accounts[-1] + with pytest.raises(TransactionFailed): + receipt = agent.claim(sender_address=bidder) + assert receipt + + +def test_refund_before_bidding_ends(testerchain, agency, token_economics, test_registry): + agent = ContractAgency.get_agent(WorkLockAgent, registry=test_registry) + bidder = testerchain.unassigned_accounts[-1] + with pytest.raises(TransactionFailed): + _receipt = agent.refund(sender_address=bidder) + + +def test_successful_claim(testerchain, agency, token_economics, test_registry): + # Wait exactly 1 hour + 1 second + testerchain.time_travel(seconds=(60*60)+1) + agent = ContractAgency.get_agent(WorkLockAgent, registry=test_registry) bidder = testerchain.unassigned_accounts[-1] receipt = agent.claim(sender_address=bidder) assert receipt - -def test_refund_rejection_without_work(testerchain, agency, token_economics, test_registry): - agent = ContractAgency.get_agent(WorkLockAgent, registry=test_registry) - bidder = testerchain.unassigned_accounts[-1] - with pytest.raises(TransactionFailed): - _receipt = agent.refund(sender_address=bidder)