From 232802dfb15b1382dbf778c8e74fcb3a6a23c409 Mon Sep 17 00:00:00 2001 From: vzotova Date: Wed, 25 Mar 2020 11:59:51 +0300 Subject: [PATCH] WorkLock: forbid refund before claiming tokens --- nucypher/blockchain/eth/sol/source/contracts/WorkLock.sol | 3 ++- .../blockchain/eth/contracts/main/worklock/test_worklock.py | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/nucypher/blockchain/eth/sol/source/contracts/WorkLock.sol b/nucypher/blockchain/eth/sol/source/contracts/WorkLock.sol index 653c6db01..941df51ed 100644 --- a/nucypher/blockchain/eth/sol/source/contracts/WorkLock.sol +++ b/nucypher/blockchain/eth/sol/source/contracts/WorkLock.sol @@ -523,10 +523,11 @@ contract WorkLock { * @notice Refund ETH for the completed work */ function refund() external returns (uint256 refundETH) { + WorkInfo storage info = workInfo[msg.sender]; + require(info.claimed, "Tokens must be claimed before refund"); refundETH = getAvailableRefund(msg.sender); require(refundETH > 0, "Nothing to refund: there is no ETH to refund or no completed work"); - WorkInfo storage info = workInfo[msg.sender]; if (refundETH == info.depositedETH) { escrow.setWorkMeasurement(msg.sender, false); } diff --git a/tests/blockchain/eth/contracts/main/worklock/test_worklock.py b/tests/blockchain/eth/contracts/main/worklock/test_worklock.py index eb4eb8202..88a2f4cfe 100644 --- a/tests/blockchain/eth/contracts/main/worklock/test_worklock.py +++ b/tests/blockchain/eth/contracts/main/worklock/test_worklock.py @@ -590,6 +590,12 @@ def test_worklock(testerchain, token_economics, deploy_contract, token, escrow, tx = escrow.functions.setCompletedWork(staker2, staker2_remaining_work // 2).transact() testerchain.wait_for_receipt(tx) assert not worklock.functions.workInfo(staker2).call()[2] + + # Can't refund before claim even with completed work + with pytest.raises((TransactionFailed, ValueError)): + tx = worklock.functions.refund().transact({'from': staker2, 'gas_price': 0}) + testerchain.wait_for_receipt(tx) + tx = worklock.functions.claim().transact({'from': staker2, 'gas_price': 0}) testerchain.wait_for_receipt(tx) assert worklock.functions.getAvailableRefund(staker2).call() == 0