[marketplace] Unlock collateral when list of slots is empty

This commit is contained in:
Mark Spanbroek 2022-11-23 16:07:01 +01:00 committed by markspanbroek
parent c46c268880
commit a7be363f04
4 changed files with 18 additions and 4 deletions

View File

@ -4,7 +4,7 @@ pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "./AccountLocks.sol"; import "./AccountLocks.sol";
contract Collateral is AccountLocks { abstract contract Collateral is AccountLocks {
IERC20 public immutable token; IERC20 public immutable token;
CollateralFunds private funds; CollateralFunds private funds;
@ -39,8 +39,14 @@ contract Collateral is AccountLocks {
add(msg.sender, amount); add(msg.sender, amount);
} }
function withdraw() public collateralInvariant { // TODO: remove AccountLocks
function isWithdrawAllowed() internal virtual returns (bool) {
_unlockAccount(); _unlockAccount();
return true;
}
function withdraw() public collateralInvariant {
require(isWithdrawAllowed(), "Account locked");
uint256 amount = balanceOf(msg.sender); uint256 amount = balanceOf(msg.sender);
funds.withdrawn += amount; funds.withdrawn += amount;
subtract(msg.sender, amount); subtract(msg.sender, amount);

View File

@ -50,6 +50,10 @@ contract Marketplace is Collateral, Proofs {
return _toSlotIds(slotsPerHost[msg.sender].filter(isActive)); return _toSlotIds(slotsPerHost[msg.sender].filter(isActive));
} }
function isWithdrawAllowed() internal view override returns (bool) {
return slotsPerHost[msg.sender].length() == 0;
}
function _equals(RequestId a, RequestId b) internal pure returns (bool) { function _equals(RequestId a, RequestId b) internal pure returns (bool) {
return RequestId.unwrap(a) == RequestId.unwrap(b); return RequestId.unwrap(a) == RequestId.unwrap(b);
} }
@ -178,7 +182,10 @@ contract Marketplace is Collateral, Proofs {
private private
marketplaceInvariant marketplaceInvariant
{ {
require(_isFinished(requestId), "Contract not ended"); require(
_isFinished(requestId) || _isCancelled(requestId),
"Contract not ended"
);
RequestContext storage context = _context(requestId); RequestContext storage context = _context(requestId);
Request storage request = _request(requestId); Request storage request = _request(requestId);
context.state = RequestState.Finished; context.state = RequestState.Finished;

View File

@ -5,7 +5,7 @@ import "./Marketplace.sol";
import "./Proofs.sol"; import "./Proofs.sol";
import "./Collateral.sol"; import "./Collateral.sol";
contract Storage is Collateral, Marketplace { contract Storage is Marketplace {
uint256 public collateralAmount; uint256 public collateralAmount;
uint256 public slashMisses; uint256 public slashMisses;
uint256 public slashPercentage; uint256 public slashPercentage;

View File

@ -77,6 +77,7 @@ describe("Storage", function () {
it("unlocks the host collateral", async function () { it("unlocks the host collateral", async function () {
await storage.fillSlot(slot.request, slot.index, proof) await storage.fillSlot(slot.request, slot.index, proof)
await waitUntilFinished(storage, slot.request) await waitUntilFinished(storage, slot.request)
await storage.freeSlot(slotId(slot))
await expect(storage.withdraw()).not.to.be.reverted await expect(storage.withdraw()).not.to.be.reverted
}) })
}) })