2022-02-16 09:50:00 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-09-29 10:18:02 +00:00
|
|
|
pragma solidity ^0.8.8;
|
2022-02-16 09:50:00 +00:00
|
|
|
|
|
|
|
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
2022-09-23 02:33:39 +00:00
|
|
|
import "@openzeppelin/contracts/utils/math/Math.sol";
|
2022-10-25 02:06:52 +00:00
|
|
|
import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
|
2023-01-17 12:55:58 +00:00
|
|
|
import "./Configuration.sol";
|
2023-01-09 12:56:29 +00:00
|
|
|
import "./Requests.sol";
|
2022-02-16 13:38:19 +00:00
|
|
|
import "./Collateral.sol";
|
2022-06-13 08:40:18 +00:00
|
|
|
import "./Proofs.sol";
|
2023-01-10 14:04:16 +00:00
|
|
|
import "./StateRetrieval.sol";
|
2022-02-16 09:50:00 +00:00
|
|
|
|
2023-01-10 14:04:16 +00:00
|
|
|
contract Marketplace is Collateral, Proofs, StateRetrieval {
|
2022-10-25 02:06:52 +00:00
|
|
|
using EnumerableSet for EnumerableSet.Bytes32Set;
|
2023-01-10 14:04:16 +00:00
|
|
|
using Requests for Request;
|
2022-10-25 02:06:52 +00:00
|
|
|
|
2023-01-17 12:55:58 +00:00
|
|
|
MarketplaceConfig public config;
|
2023-01-09 14:52:27 +00:00
|
|
|
|
2022-02-16 13:15:43 +00:00
|
|
|
MarketplaceFunds private funds;
|
2022-09-29 10:18:02 +00:00
|
|
|
mapping(RequestId => Request) private requests;
|
|
|
|
mapping(RequestId => RequestContext) private requestContexts;
|
|
|
|
mapping(SlotId => Slot) private slots;
|
2022-02-16 09:50:00 +00:00
|
|
|
|
2023-01-19 11:06:38 +00:00
|
|
|
struct RequestContext {
|
|
|
|
RequestState state;
|
|
|
|
uint256 slotsFilled;
|
|
|
|
uint256 startedAt;
|
|
|
|
uint256 endsAt;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Slot {
|
|
|
|
SlotState state;
|
|
|
|
RequestId requestId;
|
|
|
|
address host;
|
|
|
|
}
|
|
|
|
|
2022-06-13 08:40:18 +00:00
|
|
|
constructor(
|
2023-01-17 12:55:58 +00:00
|
|
|
IERC20 token,
|
|
|
|
MarketplaceConfig memory configuration
|
|
|
|
) Collateral(token) Proofs(configuration.proofs) marketplaceInvariant {
|
|
|
|
config = configuration;
|
2022-02-16 09:50:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 15:07:01 +00:00
|
|
|
function isWithdrawAllowed() internal view override returns (bool) {
|
2023-01-10 14:04:16 +00:00
|
|
|
return !hasSlots(msg.sender);
|
2022-11-23 15:07:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 10:04:04 +00:00
|
|
|
function requestStorage(
|
|
|
|
Request calldata request
|
|
|
|
) public marketplaceInvariant {
|
2022-02-17 10:00:18 +00:00
|
|
|
require(request.client == msg.sender, "Invalid client address");
|
2022-02-21 11:55:00 +00:00
|
|
|
|
2023-01-10 14:04:16 +00:00
|
|
|
RequestId id = request.id();
|
2022-02-17 10:09:35 +00:00
|
|
|
require(requests[id].client == address(0), "Request already exists");
|
2022-02-21 11:55:00 +00:00
|
|
|
|
2022-02-16 09:50:00 +00:00
|
|
|
requests[id] = request;
|
2023-01-16 16:42:57 +00:00
|
|
|
requestContexts[id].endsAt = block.timestamp + request.ask.duration;
|
2022-09-29 10:07:55 +00:00
|
|
|
|
2023-01-10 14:04:16 +00:00
|
|
|
addToMyRequests(request.client, id);
|
2022-02-21 11:55:00 +00:00
|
|
|
|
2023-01-17 08:19:51 +00:00
|
|
|
uint256 amount = request.price();
|
2022-07-20 09:07:20 +00:00
|
|
|
funds.received += amount;
|
|
|
|
funds.balance += amount;
|
|
|
|
transferFrom(msg.sender, amount);
|
2022-02-21 11:55:00 +00:00
|
|
|
|
2022-04-06 12:26:56 +00:00
|
|
|
emit StorageRequested(id, request.ask);
|
2022-02-16 09:50:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-19 09:33:54 +00:00
|
|
|
function fillSlot(
|
2022-09-29 10:18:02 +00:00
|
|
|
RequestId requestId,
|
2022-07-19 09:33:54 +00:00
|
|
|
uint256 slotIndex,
|
|
|
|
bytes calldata proof
|
2023-01-17 07:57:31 +00:00
|
|
|
) public requestIsKnown(requestId) {
|
|
|
|
Request storage request = requests[requestId];
|
2022-07-20 08:44:22 +00:00
|
|
|
require(slotIndex < request.ask.slots, "Invalid slot");
|
2022-07-19 09:33:54 +00:00
|
|
|
|
2023-01-11 09:52:08 +00:00
|
|
|
SlotId slotId = Requests.slotId(requestId, slotIndex);
|
2022-07-19 09:33:54 +00:00
|
|
|
Slot storage slot = slots[slotId];
|
2023-01-16 15:31:04 +00:00
|
|
|
slot.requestId = requestId;
|
|
|
|
|
2023-01-16 16:26:11 +00:00
|
|
|
require(slotState(slotId) == SlotState.Free, "Slot is not free");
|
2022-07-19 09:33:54 +00:00
|
|
|
|
2023-01-17 12:55:58 +00:00
|
|
|
require(
|
|
|
|
balanceOf(msg.sender) >= config.collateral.initialAmount,
|
|
|
|
"Insufficient collateral"
|
|
|
|
);
|
2022-07-19 09:33:54 +00:00
|
|
|
|
2023-01-10 11:51:26 +00:00
|
|
|
_startRequiringProofs(slotId, request.ask.proofProbability);
|
2023-01-09 14:19:00 +00:00
|
|
|
submitProof(slotId, proof);
|
2022-07-19 09:33:54 +00:00
|
|
|
|
|
|
|
slot.host = msg.sender;
|
2023-01-16 15:31:04 +00:00
|
|
|
slot.state = SlotState.Filled;
|
2023-01-16 16:42:57 +00:00
|
|
|
RequestContext storage context = requestContexts[requestId];
|
2022-08-03 09:54:29 +00:00
|
|
|
context.slotsFilled += 1;
|
2022-11-23 11:25:47 +00:00
|
|
|
|
2023-01-10 14:04:16 +00:00
|
|
|
addToMySlots(slot.host, slotId);
|
2022-11-23 11:25:47 +00:00
|
|
|
|
2022-07-19 09:33:54 +00:00
|
|
|
emit SlotFilled(requestId, slotIndex, slotId);
|
2022-08-03 09:54:29 +00:00
|
|
|
if (context.slotsFilled == request.ask.slots) {
|
|
|
|
context.state = RequestState.Started;
|
2022-09-21 09:38:42 +00:00
|
|
|
context.startedAt = block.timestamp;
|
2022-07-20 08:29:24 +00:00
|
|
|
emit RequestFulfilled(requestId);
|
|
|
|
}
|
2022-07-19 09:33:54 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 08:05:44 +00:00
|
|
|
function freeSlot(SlotId slotId) public slotIsNotFree(slotId) {
|
|
|
|
Slot storage slot = slots[slotId];
|
2022-11-23 14:29:58 +00:00
|
|
|
require(slot.host == msg.sender, "Slot filled by other host");
|
2023-01-16 16:18:17 +00:00
|
|
|
SlotState state = slotState(slotId);
|
|
|
|
require(state != SlotState.Paid, "Already paid");
|
|
|
|
if (state == SlotState.Finished) {
|
2022-11-23 14:29:58 +00:00
|
|
|
payoutSlot(slot.requestId, slotId);
|
2023-01-16 16:18:17 +00:00
|
|
|
} else if (state == SlotState.Failed) {
|
2023-01-10 14:04:16 +00:00
|
|
|
removeFromMySlots(msg.sender, slotId);
|
2023-01-16 16:35:20 +00:00
|
|
|
} else if (state == SlotState.Filled) {
|
2022-11-23 14:29:58 +00:00
|
|
|
_forciblyFreeSlot(slotId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 16:35:20 +00:00
|
|
|
function markProofAsMissing(SlotId slotId, Period period) public {
|
|
|
|
require(slotState(slotId) == SlotState.Filled, "Slot not accepting proofs");
|
2023-01-09 14:52:27 +00:00
|
|
|
_markProofAsMissing(slotId, period);
|
2023-01-10 10:33:18 +00:00
|
|
|
address host = getHost(slotId);
|
2023-01-17 12:55:58 +00:00
|
|
|
if (missingProofs(slotId) % config.collateral.slashCriterion == 0) {
|
|
|
|
_slash(host, config.collateral.slashPercentage);
|
2023-01-09 14:52:27 +00:00
|
|
|
|
2023-01-17 12:55:58 +00:00
|
|
|
if (balanceOf(host) < config.collateral.minimumAmount) {
|
2023-01-09 14:52:27 +00:00
|
|
|
// When the collateral drops below the minimum threshold, the slot
|
|
|
|
// needs to be freed so that there is enough remaining collateral to be
|
|
|
|
// distributed for repairs and rewards (with any leftover to be burnt).
|
|
|
|
_forciblyFreeSlot(slotId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 16:35:20 +00:00
|
|
|
function _forciblyFreeSlot(SlotId slotId) internal marketplaceInvariant {
|
2023-01-17 08:05:44 +00:00
|
|
|
Slot storage slot = slots[slotId];
|
2022-09-29 10:18:02 +00:00
|
|
|
RequestId requestId = slot.requestId;
|
2022-09-16 05:00:54 +00:00
|
|
|
RequestContext storage context = requestContexts[requestId];
|
|
|
|
|
|
|
|
// TODO: burn host's slot collateral except for repair costs + mark proof
|
|
|
|
// missing reward
|
|
|
|
// Slot collateral is not yet implemented as the design decision was
|
|
|
|
// not finalised.
|
|
|
|
|
2023-01-10 14:04:16 +00:00
|
|
|
removeFromMySlots(slot.host, slotId);
|
2022-11-23 11:25:47 +00:00
|
|
|
|
2023-01-16 15:31:04 +00:00
|
|
|
slot.state = SlotState.Free;
|
2022-09-16 05:00:54 +00:00
|
|
|
slot.host = address(0);
|
2022-09-29 10:18:02 +00:00
|
|
|
slot.requestId = RequestId.wrap(0);
|
2022-09-16 05:00:54 +00:00
|
|
|
context.slotsFilled -= 1;
|
|
|
|
emit SlotFreed(requestId, slotId);
|
|
|
|
|
2023-01-17 07:57:31 +00:00
|
|
|
Request storage request = requests[requestId];
|
2022-09-16 05:00:54 +00:00
|
|
|
uint256 slotsLost = request.ask.slots - context.slotsFilled;
|
2022-10-26 08:58:14 +00:00
|
|
|
if (
|
|
|
|
slotsLost > request.ask.maxSlotLoss &&
|
|
|
|
context.state == RequestState.Started
|
|
|
|
) {
|
2022-09-16 05:00:54 +00:00
|
|
|
context.state = RequestState.Failed;
|
2022-09-23 02:33:39 +00:00
|
|
|
context.endsAt = block.timestamp - 1;
|
2022-09-16 05:00:54 +00:00
|
|
|
emit RequestFailed(requestId);
|
|
|
|
|
|
|
|
// TODO: burn all remaining slot collateral (note: slot collateral not
|
|
|
|
// yet implemented)
|
|
|
|
// TODO: send client remaining funds
|
|
|
|
}
|
|
|
|
}
|
2022-10-25 02:04:16 +00:00
|
|
|
|
2023-01-10 10:04:04 +00:00
|
|
|
function payoutSlot(
|
|
|
|
RequestId requestId,
|
|
|
|
SlotId slotId
|
2023-01-17 07:57:31 +00:00
|
|
|
) private requestIsKnown(requestId) marketplaceInvariant {
|
2023-01-16 16:42:57 +00:00
|
|
|
RequestContext storage context = requestContexts[requestId];
|
2023-01-17 07:57:31 +00:00
|
|
|
Request storage request = requests[requestId];
|
2022-09-21 09:57:26 +00:00
|
|
|
context.state = RequestState.Finished;
|
2023-01-10 14:04:16 +00:00
|
|
|
removeFromMyRequests(request.client, requestId);
|
2023-01-17 08:05:44 +00:00
|
|
|
Slot storage slot = slots[slotId];
|
2022-11-23 11:25:47 +00:00
|
|
|
|
2023-01-10 14:04:16 +00:00
|
|
|
removeFromMySlots(slot.host, slotId);
|
2022-11-23 11:25:47 +00:00
|
|
|
|
2023-01-17 08:19:51 +00:00
|
|
|
uint256 amount = requests[requestId].pricePerSlot();
|
2022-07-19 15:09:35 +00:00
|
|
|
funds.sent += amount;
|
|
|
|
funds.balance -= amount;
|
2023-01-16 16:18:17 +00:00
|
|
|
slot.state = SlotState.Paid;
|
2022-07-19 15:09:35 +00:00
|
|
|
require(token.transfer(slot.host, amount), "Payment failed");
|
|
|
|
}
|
|
|
|
|
2022-08-17 05:26:04 +00:00
|
|
|
/// @notice Withdraws storage request funds back to the client that deposited them.
|
|
|
|
/// @dev Request must be expired, must be in RequestState.New, and the transaction must originate from the depositer address.
|
|
|
|
/// @param requestId the id of the request
|
2022-09-29 10:18:02 +00:00
|
|
|
function withdrawFunds(RequestId requestId) public marketplaceInvariant {
|
2022-09-07 05:25:01 +00:00
|
|
|
Request storage request = requests[requestId];
|
2022-08-04 02:14:36 +00:00
|
|
|
require(block.timestamp > request.expiry, "Request not yet timed out");
|
|
|
|
require(request.client == msg.sender, "Invalid client address");
|
2023-01-16 16:42:57 +00:00
|
|
|
RequestContext storage context = requestContexts[requestId];
|
2022-08-04 02:14:36 +00:00
|
|
|
require(context.state == RequestState.New, "Invalid state");
|
|
|
|
|
|
|
|
// Update request state to Cancelled. Handle in the withdraw transaction
|
|
|
|
// as there needs to be someone to pay for the gas to update the state
|
|
|
|
context.state = RequestState.Cancelled;
|
2023-01-10 14:04:16 +00:00
|
|
|
removeFromMyRequests(request.client, requestId);
|
2022-11-23 11:25:47 +00:00
|
|
|
|
2022-08-04 02:14:36 +00:00
|
|
|
emit RequestCancelled(requestId);
|
2022-09-07 05:25:01 +00:00
|
|
|
|
|
|
|
// TODO: To be changed once we start paying out hosts for the time they
|
|
|
|
// fill a slot. The amount that we paid to hosts will then have to be
|
|
|
|
// deducted from the price.
|
2023-01-17 08:19:51 +00:00
|
|
|
uint256 amount = request.price();
|
2022-09-07 05:25:01 +00:00
|
|
|
funds.sent += amount;
|
|
|
|
funds.balance -= amount;
|
|
|
|
require(token.transfer(msg.sender, amount), "Withdraw failed");
|
2022-08-04 02:14:36 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 12:38:12 +00:00
|
|
|
function getHost(SlotId slotId) public view returns (address) {
|
2023-01-10 10:33:18 +00:00
|
|
|
return slots[slotId].host;
|
2023-01-09 12:38:12 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 07:57:31 +00:00
|
|
|
modifier requestIsKnown(RequestId requestId) {
|
|
|
|
require(requests[requestId].client != address(0), "Unknown request");
|
|
|
|
_;
|
2022-02-22 08:25:42 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 10:04:04 +00:00
|
|
|
function getRequest(
|
|
|
|
RequestId requestId
|
2023-01-17 07:57:31 +00:00
|
|
|
) public view requestIsKnown(requestId) returns (Request memory) {
|
|
|
|
return requests[requestId];
|
2023-01-09 12:38:12 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 08:05:44 +00:00
|
|
|
modifier slotIsNotFree(SlotId slotId) {
|
|
|
|
require(slots[slotId].state != SlotState.Free, "Slot is free");
|
|
|
|
_;
|
2022-08-22 06:16:45 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 12:49:37 +00:00
|
|
|
function requestEnd(RequestId requestId) public view returns (uint256) {
|
2023-01-16 16:42:57 +00:00
|
|
|
uint256 end = requestContexts[requestId].endsAt;
|
2023-01-16 16:39:27 +00:00
|
|
|
RequestState state = requestState(requestId);
|
|
|
|
if (state == RequestState.New || state == RequestState.Started) {
|
2022-10-26 02:15:42 +00:00
|
|
|
return end;
|
|
|
|
} else {
|
|
|
|
return Math.min(end, block.timestamp - 1);
|
|
|
|
}
|
2022-10-25 12:49:37 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 13:28:42 +00:00
|
|
|
function requestState(
|
|
|
|
RequestId requestId
|
2023-01-17 07:57:31 +00:00
|
|
|
) public view requestIsKnown(requestId) returns (RequestState) {
|
2023-01-16 16:42:57 +00:00
|
|
|
RequestContext storage context = requestContexts[requestId];
|
2023-01-16 13:14:15 +00:00
|
|
|
if (
|
|
|
|
context.state == RequestState.New &&
|
2023-01-17 07:57:31 +00:00
|
|
|
block.timestamp > requests[requestId].expiry
|
2023-01-16 13:14:15 +00:00
|
|
|
) {
|
2022-09-08 07:56:01 +00:00
|
|
|
return RequestState.Cancelled;
|
2023-01-16 13:14:15 +00:00
|
|
|
} else if (
|
|
|
|
context.state == RequestState.Started && block.timestamp > context.endsAt
|
|
|
|
) {
|
2022-09-21 09:57:26 +00:00
|
|
|
return RequestState.Finished;
|
2022-09-08 07:56:01 +00:00
|
|
|
} else {
|
|
|
|
return context.state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 14:26:21 +00:00
|
|
|
function slotState(SlotId slotId) public view override returns (SlotState) {
|
2023-01-16 15:31:04 +00:00
|
|
|
Slot storage slot = slots[slotId];
|
2023-01-18 14:26:21 +00:00
|
|
|
if (RequestId.unwrap(slot.requestId) == 0) {
|
|
|
|
return SlotState.Free;
|
|
|
|
}
|
2023-01-16 15:31:04 +00:00
|
|
|
RequestState reqState = requestState(slot.requestId);
|
2023-01-16 16:18:17 +00:00
|
|
|
if (slot.state == SlotState.Paid) {
|
|
|
|
return SlotState.Paid;
|
2023-01-18 14:26:21 +00:00
|
|
|
}
|
|
|
|
if (reqState == RequestState.Cancelled) {
|
2023-01-16 15:31:04 +00:00
|
|
|
return SlotState.Finished;
|
|
|
|
}
|
2023-01-18 14:26:21 +00:00
|
|
|
if (reqState == RequestState.Finished) {
|
|
|
|
return SlotState.Finished;
|
|
|
|
}
|
|
|
|
if (reqState == RequestState.Failed) {
|
|
|
|
return SlotState.Failed;
|
|
|
|
}
|
|
|
|
return slot.state;
|
2022-09-29 10:18:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
event StorageRequested(RequestId requestId, Ask ask);
|
|
|
|
event RequestFulfilled(RequestId indexed requestId);
|
|
|
|
event RequestFailed(RequestId indexed requestId);
|
2022-07-19 09:33:54 +00:00
|
|
|
event SlotFilled(
|
2022-09-29 10:18:02 +00:00
|
|
|
RequestId indexed requestId,
|
2022-07-19 09:33:54 +00:00
|
|
|
uint256 indexed slotIndex,
|
2022-09-29 10:18:02 +00:00
|
|
|
SlotId slotId
|
2022-07-19 09:33:54 +00:00
|
|
|
);
|
2022-09-29 10:18:02 +00:00
|
|
|
event SlotFreed(RequestId indexed requestId, SlotId slotId);
|
|
|
|
event RequestCancelled(RequestId indexed requestId);
|
2022-02-16 09:50:00 +00:00
|
|
|
|
2022-02-16 13:15:43 +00:00
|
|
|
modifier marketplaceInvariant() {
|
|
|
|
MarketplaceFunds memory oldFunds = funds;
|
2022-02-16 09:50:00 +00:00
|
|
|
_;
|
2022-02-16 13:15:43 +00:00
|
|
|
assert(funds.received >= oldFunds.received);
|
|
|
|
assert(funds.sent >= oldFunds.sent);
|
|
|
|
assert(funds.received == funds.balance + funds.sent);
|
2022-02-16 09:50:00 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 13:15:43 +00:00
|
|
|
struct MarketplaceFunds {
|
2022-02-16 09:50:00 +00:00
|
|
|
uint256 balance;
|
|
|
|
uint256 received;
|
|
|
|
uint256 sent;
|
|
|
|
}
|
|
|
|
}
|