2022-02-16 09:50:00 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2024-01-23 09:24:02 +00:00
|
|
|
pragma solidity 0.8.23;
|
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-06-13 08:40:18 +00:00
|
|
|
import "./Proofs.sol";
|
2023-01-10 14:04:16 +00:00
|
|
|
import "./StateRetrieval.sol";
|
2024-01-22 15:43:03 +00:00
|
|
|
import "./Endian.sol";
|
2024-01-18 12:50:54 +00:00
|
|
|
import "./Groth16.sol";
|
2022-02-16 09:50:00 +00:00
|
|
|
|
2024-01-22 15:43:03 +00:00
|
|
|
contract Marketplace is Proofs, StateRetrieval, Endian {
|
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-03-08 11:02:34 +00:00
|
|
|
IERC20 public immutable token;
|
2023-01-17 12:55:58 +00:00
|
|
|
MarketplaceConfig public config;
|
2023-01-09 14:52:27 +00:00
|
|
|
|
2023-01-23 10:57:10 +00:00
|
|
|
mapping(RequestId => Request) private _requests;
|
|
|
|
mapping(RequestId => RequestContext) private _requestContexts;
|
2023-03-08 11:02:34 +00:00
|
|
|
mapping(SlotId => Slot) internal _slots;
|
2022-02-16 09:50:00 +00:00
|
|
|
|
2023-01-30 09:32:30 +00:00
|
|
|
MarketplaceTotals internal _marketplaceTotals;
|
|
|
|
|
2023-01-19 11:06:38 +00:00
|
|
|
struct RequestContext {
|
|
|
|
RequestState state;
|
|
|
|
uint256 slotsFilled;
|
2023-10-16 09:14:02 +00:00
|
|
|
/// @notice Tracks how much funds should be returned when Request expires to the Request creator
|
|
|
|
/// @dev The sum is deducted every time a host fills a Slot by precalculated amount that he should receive if the Request expires
|
|
|
|
uint256 expiryFundsWithdraw;
|
2023-01-19 11:06:38 +00:00
|
|
|
uint256 startedAt;
|
|
|
|
uint256 endsAt;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Slot {
|
|
|
|
SlotState state;
|
|
|
|
RequestId requestId;
|
2023-10-16 09:14:02 +00:00
|
|
|
/// @notice Timestamp that signals when slot was filled
|
|
|
|
/// @dev Used for partial payouts when Requests expires and Hosts are paid out only the time they host the content.
|
|
|
|
uint256 filledAt;
|
2023-04-13 23:28:39 +00:00
|
|
|
uint256 slotIndex;
|
2023-03-08 11:02:34 +00:00
|
|
|
/// @notice Tracks the current amount of host's collateral that is to be payed out at the end of Slot's lifespan.
|
|
|
|
/// @dev When Slot is filled, the collateral is collected in amount of request.ask.collateral
|
|
|
|
/// @dev When Host is slashed for missing a proof the slashed amount is reflected in this variable
|
|
|
|
uint256 currentCollateral;
|
2023-01-19 11:06:38 +00:00
|
|
|
address host;
|
|
|
|
}
|
|
|
|
|
2023-04-13 23:28:39 +00:00
|
|
|
struct ActiveSlot {
|
|
|
|
Request request;
|
|
|
|
uint256 slotIndex;
|
|
|
|
}
|
|
|
|
|
2022-06-13 08:40:18 +00:00
|
|
|
constructor(
|
2024-01-05 11:27:53 +00:00
|
|
|
MarketplaceConfig memory configuration,
|
2024-01-10 14:12:06 +00:00
|
|
|
IERC20 token_,
|
2024-01-23 12:28:53 +00:00
|
|
|
IGroth16Verifier verifier
|
2024-01-10 14:12:06 +00:00
|
|
|
) Proofs(configuration.proofs, verifier) {
|
2023-03-08 11:02:34 +00:00
|
|
|
token = token_;
|
2023-03-30 09:11:21 +00:00
|
|
|
|
2023-05-30 12:42:59 +00:00
|
|
|
require(
|
|
|
|
configuration.collateral.repairRewardPercentage <= 100,
|
|
|
|
"Must be less than 100"
|
|
|
|
);
|
|
|
|
require(
|
|
|
|
configuration.collateral.slashPercentage <= 100,
|
|
|
|
"Must be less than 100"
|
|
|
|
);
|
|
|
|
require(
|
|
|
|
configuration.collateral.maxNumberOfSlashes *
|
|
|
|
configuration.collateral.slashPercentage <=
|
|
|
|
100,
|
|
|
|
"Maximum slashing exceeds 100%"
|
|
|
|
);
|
2023-01-17 12:55:58 +00:00
|
|
|
config = configuration;
|
2022-02-16 09:50:00 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 09:32:30 +00:00
|
|
|
function requestStorage(Request calldata request) public {
|
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();
|
2023-01-23 10:57:10 +00:00
|
|
|
require(_requests[id].client == address(0), "Request already exists");
|
2022-02-21 11:55:00 +00:00
|
|
|
|
2023-01-23 10:57:10 +00:00
|
|
|
_requests[id] = request;
|
2024-01-10 14:18:31 +00:00
|
|
|
uint256 endsAt = block.timestamp + request.ask.duration;
|
|
|
|
require(endsAt > request.expiry, "Request end before expiry");
|
|
|
|
_requestContexts[id].endsAt = endsAt;
|
2022-09-29 10:07:55 +00:00
|
|
|
|
2023-01-19 15:47:29 +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();
|
2023-10-16 09:14:02 +00:00
|
|
|
_requestContexts[id].expiryFundsWithdraw = amount;
|
2023-01-30 09:32:30 +00:00
|
|
|
_marketplaceTotals.received += amount;
|
2023-01-19 15:47:29 +00:00
|
|
|
_transferFrom(msg.sender, amount);
|
2022-02-21 11:55:00 +00:00
|
|
|
|
2023-06-21 20:18:33 +00:00
|
|
|
emit StorageRequested(id, request.ask, request.expiry);
|
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,
|
2024-01-18 12:50:54 +00:00
|
|
|
Groth16Proof calldata proof
|
2023-01-17 07:57:31 +00:00
|
|
|
) public requestIsKnown(requestId) {
|
2023-01-23 10:57:10 +00:00
|
|
|
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);
|
2023-01-23 10:57:10 +00:00
|
|
|
Slot storage slot = _slots[slotId];
|
2023-01-16 15:31:04 +00:00
|
|
|
slot.requestId = requestId;
|
2023-04-13 23:28:39 +00:00
|
|
|
slot.slotIndex = slotIndex;
|
2023-01-16 15:31:04 +00:00
|
|
|
|
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-10 11:51:26 +00:00
|
|
|
_startRequiringProofs(slotId, request.ask.proofProbability);
|
2024-01-15 15:25:30 +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-10-16 09:14:02 +00:00
|
|
|
slot.filledAt = block.timestamp;
|
2023-01-23 10:57:10 +00:00
|
|
|
RequestContext storage context = _requestContexts[requestId];
|
2022-08-03 09:54:29 +00:00
|
|
|
context.slotsFilled += 1;
|
2024-01-10 14:12:18 +00:00
|
|
|
context.expiryFundsWithdraw -= _expiryPayoutAmount(
|
|
|
|
requestId,
|
|
|
|
block.timestamp
|
|
|
|
);
|
2022-11-23 11:25:47 +00:00
|
|
|
|
2023-03-08 11:02:34 +00:00
|
|
|
// Collect collateral
|
|
|
|
uint256 collateralAmount = request.ask.collateral;
|
|
|
|
_transferFrom(msg.sender, collateralAmount);
|
2023-01-30 09:32:30 +00:00
|
|
|
_marketplaceTotals.received += collateralAmount;
|
2023-03-08 11:02:34 +00:00
|
|
|
slot.currentCollateral = collateralAmount;
|
|
|
|
|
2023-01-19 15:47:29 +00:00
|
|
|
_addToMySlots(slot.host, slotId);
|
2022-11-23 11:25:47 +00:00
|
|
|
|
2023-06-21 20:18:33 +00:00
|
|
|
emit SlotFilled(requestId, slotIndex);
|
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) {
|
2023-01-23 10:57:10 +00:00
|
|
|
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");
|
2023-03-08 11:02:34 +00:00
|
|
|
|
2023-01-16 16:18:17 +00:00
|
|
|
if (state == SlotState.Finished) {
|
2023-01-19 15:47:29 +00:00
|
|
|
_payoutSlot(slot.requestId, slotId);
|
2023-10-16 09:14:02 +00:00
|
|
|
} else if (state == SlotState.Cancelled) {
|
|
|
|
_payoutCancelledSlot(slot.requestId, slotId);
|
2023-01-16 16:18:17 +00:00
|
|
|
} else if (state == SlotState.Failed) {
|
2023-01-19 15:47:29 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-22 15:43:03 +00:00
|
|
|
function _challengeToFieldElement(
|
|
|
|
bytes32 challenge
|
|
|
|
) internal pure returns (uint256) {
|
|
|
|
// use only 31 bytes of the challenge to ensure that it fits into the field
|
|
|
|
bytes32 truncated = bytes32(bytes31(challenge));
|
|
|
|
// convert from little endian to big endian
|
|
|
|
bytes32 bigEndian = _byteSwap(truncated);
|
|
|
|
// convert bytes to integer
|
|
|
|
return uint256(bigEndian);
|
|
|
|
}
|
|
|
|
|
|
|
|
function _merkleRootToFieldElement(
|
|
|
|
bytes32 merkleRoot
|
|
|
|
) internal pure returns (uint256) {
|
|
|
|
// convert from little endian to big endian
|
|
|
|
bytes32 bigEndian = _byteSwap(merkleRoot);
|
|
|
|
// convert bytes to integer
|
|
|
|
return uint256(bigEndian);
|
|
|
|
}
|
|
|
|
|
|
|
|
function submitProof(
|
|
|
|
SlotId id,
|
|
|
|
Groth16Proof calldata proof
|
|
|
|
) public requestIsKnown(_slots[id].requestId) {
|
|
|
|
Slot storage slot = _slots[id];
|
|
|
|
Request storage request = _requests[slot.requestId];
|
2024-01-23 11:50:14 +00:00
|
|
|
uint256[] memory pubSignals = new uint256[](3);
|
|
|
|
pubSignals[0] = _challengeToFieldElement(getChallenge(id));
|
|
|
|
pubSignals[1] = _merkleRootToFieldElement(request.content.merkleRoot);
|
|
|
|
pubSignals[2] = slot.slotIndex;
|
|
|
|
_proofReceived(id, proof, pubSignals);
|
2024-01-18 13:09:36 +00:00
|
|
|
}
|
|
|
|
|
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-03-08 11:02:34 +00:00
|
|
|
Slot storage slot = _slots[slotId];
|
2023-03-30 09:11:21 +00:00
|
|
|
Request storage request = _requests[slot.requestId];
|
2023-03-08 11:02:34 +00:00
|
|
|
|
2023-01-17 12:55:58 +00:00
|
|
|
if (missingProofs(slotId) % config.collateral.slashCriterion == 0) {
|
2023-05-30 12:42:59 +00:00
|
|
|
uint256 slashedAmount = (request.ask.collateral *
|
|
|
|
config.collateral.slashPercentage) / 100;
|
2023-03-08 11:02:34 +00:00
|
|
|
slot.currentCollateral -= slashedAmount;
|
2023-05-30 12:42:59 +00:00
|
|
|
if (
|
|
|
|
missingProofs(slotId) / config.collateral.slashCriterion >=
|
|
|
|
config.collateral.maxNumberOfSlashes
|
|
|
|
) {
|
2023-03-30 09:11:21 +00:00
|
|
|
// When the number of slashings is at or above the allowed amount,
|
|
|
|
// free the slot.
|
2023-01-09 14:52:27 +00:00
|
|
|
_forciblyFreeSlot(slotId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 09:32:30 +00:00
|
|
|
function _forciblyFreeSlot(SlotId slotId) internal {
|
2023-01-23 10:57:10 +00:00
|
|
|
Slot storage slot = _slots[slotId];
|
2022-09-29 10:18:02 +00:00
|
|
|
RequestId requestId = slot.requestId;
|
2023-01-23 10:57:10 +00:00
|
|
|
RequestContext storage context = _requestContexts[requestId];
|
2022-09-16 05:00:54 +00:00
|
|
|
|
2023-01-19 15:47:29 +00:00
|
|
|
_removeFromMySlots(slot.host, slotId);
|
2022-11-23 11:25:47 +00:00
|
|
|
|
2023-06-21 20:18:33 +00:00
|
|
|
uint256 slotIndex = slot.slotIndex;
|
2023-03-08 11:02:34 +00:00
|
|
|
delete _slots[slotId];
|
2022-09-16 05:00:54 +00:00
|
|
|
context.slotsFilled -= 1;
|
2023-06-21 20:18:33 +00:00
|
|
|
emit SlotFreed(requestId, slotIndex);
|
2023-05-30 12:42:59 +00:00
|
|
|
_resetMissingProofs(slotId);
|
2022-09-16 05:00:54 +00:00
|
|
|
|
2023-01-23 10:57:10 +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: send client remaining funds
|
|
|
|
}
|
|
|
|
}
|
2022-10-25 02:04:16 +00:00
|
|
|
|
2023-01-19 15:47:29 +00:00
|
|
|
function _payoutSlot(
|
2023-01-10 10:04:04 +00:00
|
|
|
RequestId requestId,
|
|
|
|
SlotId slotId
|
2023-01-30 09:32:30 +00:00
|
|
|
) private requestIsKnown(requestId) {
|
2023-01-23 10:57:10 +00:00
|
|
|
RequestContext storage context = _requestContexts[requestId];
|
|
|
|
Request storage request = _requests[requestId];
|
2022-09-21 09:57:26 +00:00
|
|
|
context.state = RequestState.Finished;
|
2023-01-19 15:47:29 +00:00
|
|
|
_removeFromMyRequests(request.client, requestId);
|
2023-01-23 10:57:10 +00:00
|
|
|
Slot storage slot = _slots[slotId];
|
2022-11-23 11:25:47 +00:00
|
|
|
|
2023-01-19 15:47:29 +00:00
|
|
|
_removeFromMySlots(slot.host, slotId);
|
2022-11-23 11:25:47 +00:00
|
|
|
|
2023-05-30 12:42:59 +00:00
|
|
|
uint256 amount = _requests[requestId].pricePerSlot() +
|
|
|
|
slot.currentCollateral;
|
2023-01-30 09:32:30 +00:00
|
|
|
_marketplaceTotals.sent += amount;
|
2023-01-16 16:18:17 +00:00
|
|
|
slot.state = SlotState.Paid;
|
2023-05-30 12:46:56 +00:00
|
|
|
assert(token.transfer(slot.host, amount));
|
2022-07-19 15:09:35 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 09:14:02 +00:00
|
|
|
function _payoutCancelledSlot(
|
|
|
|
RequestId requestId,
|
|
|
|
SlotId slotId
|
|
|
|
) private requestIsKnown(requestId) {
|
|
|
|
Slot storage slot = _slots[slotId];
|
|
|
|
_removeFromMySlots(slot.host, slotId);
|
|
|
|
|
2024-01-10 14:12:18 +00:00
|
|
|
uint256 amount = _expiryPayoutAmount(requestId, slot.filledAt) +
|
|
|
|
slot.currentCollateral;
|
2023-10-16 09:14:02 +00:00
|
|
|
_marketplaceTotals.sent += amount;
|
|
|
|
slot.state = SlotState.Paid;
|
|
|
|
assert(token.transfer(slot.host, amount));
|
|
|
|
}
|
|
|
|
|
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
|
2023-01-30 09:32:30 +00:00
|
|
|
function withdrawFunds(RequestId requestId) public {
|
2023-01-23 10:57:10 +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-23 10:57:10 +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-19 15:47:29 +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
|
|
|
|
2023-10-16 09:14:02 +00:00
|
|
|
uint256 amount = context.expiryFundsWithdraw;
|
2023-01-30 09:32:30 +00:00
|
|
|
_marketplaceTotals.sent += amount;
|
2023-05-30 12:46:56 +00:00
|
|
|
assert(token.transfer(msg.sender, amount));
|
2022-08-04 02:14:36 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 12:42:59 +00:00
|
|
|
function getActiveSlot(
|
|
|
|
SlotId slotId
|
|
|
|
) public view slotIsNotFree(slotId) returns (ActiveSlot memory) {
|
2023-01-24 04:59:56 +00:00
|
|
|
Slot storage slot = _slots[slotId];
|
2023-04-13 23:28:39 +00:00
|
|
|
ActiveSlot memory activeSlot;
|
|
|
|
activeSlot.request = _requests[slot.requestId];
|
|
|
|
activeSlot.slotIndex = slot.slotIndex;
|
|
|
|
return activeSlot;
|
2023-01-24 04:59:56 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 07:57:31 +00:00
|
|
|
modifier requestIsKnown(RequestId requestId) {
|
2023-01-23 10:57:10 +00:00
|
|
|
require(_requests[requestId].client != address(0), "Unknown request");
|
2023-01-17 07:57:31 +00:00
|
|
|
_;
|
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) {
|
2023-01-23 10:57:10 +00:00
|
|
|
return _requests[requestId];
|
2023-01-09 12:38:12 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 08:05:44 +00:00
|
|
|
modifier slotIsNotFree(SlotId slotId) {
|
2023-01-23 10:57:10 +00:00
|
|
|
require(_slots[slotId].state != SlotState.Free, "Slot is free");
|
2023-01-17 08:05:44 +00:00
|
|
|
_;
|
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-23 10:57:10 +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-10-16 09:14:02 +00:00
|
|
|
/// @notice Calculates the amount that should be payed out to a host if a request expires based on when the host fills the slot
|
2024-01-10 14:12:18 +00:00
|
|
|
function _expiryPayoutAmount(
|
|
|
|
RequestId requestId,
|
|
|
|
uint256 startingTimestamp
|
|
|
|
) private view returns (uint256) {
|
2023-10-16 09:14:02 +00:00
|
|
|
Request storage request = _requests[requestId];
|
|
|
|
require(startingTimestamp < request.expiry, "Start not before expiry");
|
|
|
|
|
|
|
|
return (request.expiry - startingTimestamp) * request.ask.reward;
|
|
|
|
}
|
|
|
|
|
2023-03-08 11:02:34 +00:00
|
|
|
function getHost(SlotId slotId) public view returns (address) {
|
|
|
|
return _slots[slotId].host;
|
|
|
|
}
|
|
|
|
|
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-23 10:57:10 +00:00
|
|
|
RequestContext storage context = _requestContexts[requestId];
|
2023-01-16 13:14:15 +00:00
|
|
|
if (
|
|
|
|
context.state == RequestState.New &&
|
2023-01-23 10:57:10 +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-23 10:57:10 +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-10-16 09:14:02 +00:00
|
|
|
return SlotState.Cancelled;
|
2023-01-16 15:31:04 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2023-03-08 11:02:34 +00:00
|
|
|
function _transferFrom(address sender, uint256 amount) internal {
|
|
|
|
address receiver = address(this);
|
|
|
|
require(token.transferFrom(sender, receiver, amount), "Transfer failed");
|
|
|
|
}
|
|
|
|
|
2023-06-21 20:18:33 +00:00
|
|
|
event StorageRequested(RequestId requestId, Ask ask, uint256 expiry);
|
2022-09-29 10:18:02 +00:00
|
|
|
event RequestFulfilled(RequestId indexed requestId);
|
|
|
|
event RequestFailed(RequestId indexed requestId);
|
2024-01-10 14:12:18 +00:00
|
|
|
event SlotFilled(RequestId indexed requestId, uint256 slotIndex);
|
|
|
|
event SlotFreed(RequestId indexed requestId, uint256 slotIndex);
|
2022-09-29 10:18:02 +00:00
|
|
|
event RequestCancelled(RequestId indexed requestId);
|
2022-02-16 09:50:00 +00:00
|
|
|
|
2023-01-30 09:32:30 +00:00
|
|
|
struct MarketplaceTotals {
|
2022-02-16 09:50:00 +00:00
|
|
|
uint256 received;
|
|
|
|
uint256 sent;
|
|
|
|
}
|
|
|
|
}
|