2022-02-16 09:50:00 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
2022-02-16 13:38:19 +00:00
|
|
|
import "./Collateral.sol";
|
2022-02-16 09:50:00 +00:00
|
|
|
|
2022-02-16 13:38:19 +00:00
|
|
|
contract Marketplace is Collateral {
|
|
|
|
uint256 public immutable collateral;
|
2022-02-16 13:15:43 +00:00
|
|
|
MarketplaceFunds private funds;
|
2022-02-16 09:50:00 +00:00
|
|
|
mapping(bytes32 => Request) private requests;
|
2022-02-16 13:38:19 +00:00
|
|
|
mapping(bytes32 => Offer) private offers;
|
2022-02-16 09:50:00 +00:00
|
|
|
|
2022-02-16 13:38:19 +00:00
|
|
|
constructor(IERC20 _token, uint256 _collateral)
|
|
|
|
Collateral(_token)
|
|
|
|
marketplaceInvariant
|
|
|
|
{
|
|
|
|
collateral = _collateral;
|
2022-02-16 09:50:00 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 13:15:43 +00:00
|
|
|
function requestStorage(Request calldata request)
|
|
|
|
public
|
|
|
|
marketplaceInvariant
|
|
|
|
{
|
2022-02-16 09:50:00 +00:00
|
|
|
bytes32 id = keccak256(abi.encode(request));
|
2022-02-17 10:00:18 +00:00
|
|
|
require(request.client == msg.sender, "Invalid client address");
|
2022-02-16 09:50:00 +00:00
|
|
|
require(request.size > 0, "Invalid size");
|
|
|
|
require(requests[id].size == 0, "Request already exists");
|
|
|
|
requests[id] = request;
|
|
|
|
transferFrom(msg.sender, request.maxPrice);
|
2022-02-16 13:15:43 +00:00
|
|
|
funds.received += request.maxPrice;
|
|
|
|
funds.balance += request.maxPrice;
|
2022-02-16 09:50:00 +00:00
|
|
|
emit StorageRequested(id, request);
|
|
|
|
}
|
|
|
|
|
2022-02-16 13:38:19 +00:00
|
|
|
function offerStorage(Offer calldata offer) public marketplaceInvariant {
|
|
|
|
bytes32 id = keccak256(abi.encode(offer));
|
|
|
|
Request storage request = requests[offer.requestId];
|
|
|
|
require(balanceOf(msg.sender) >= collateral, "Insufficient collateral");
|
|
|
|
require(request.size != 0, "Unknown request");
|
|
|
|
require(offers[id].expiry == 0, "Offer already exists");
|
|
|
|
// solhint-disable-next-line not-rely-on-time
|
|
|
|
require(offer.expiry > block.timestamp, "Offer expired");
|
|
|
|
require(offer.price <= request.maxPrice, "Price too high");
|
|
|
|
offers[id] = offer;
|
|
|
|
emit StorageOffered(id, offer);
|
|
|
|
}
|
|
|
|
|
2022-02-16 09:50:00 +00:00
|
|
|
struct Request {
|
2022-02-17 10:00:18 +00:00
|
|
|
address client;
|
2022-02-16 09:50:00 +00:00
|
|
|
uint256 duration;
|
|
|
|
uint256 size;
|
|
|
|
bytes32 contentHash;
|
|
|
|
uint256 proofPeriod;
|
|
|
|
uint256 proofTimeout;
|
|
|
|
uint256 maxPrice;
|
|
|
|
bytes32 nonce;
|
|
|
|
}
|
|
|
|
|
2022-02-16 13:38:19 +00:00
|
|
|
struct Offer {
|
|
|
|
bytes32 requestId;
|
|
|
|
uint256 price;
|
|
|
|
uint256 expiry;
|
|
|
|
}
|
|
|
|
|
2022-02-16 09:50:00 +00:00
|
|
|
event StorageRequested(bytes32 id, Request request);
|
2022-02-16 13:38:19 +00:00
|
|
|
event StorageOffered(bytes32 id, Offer offer);
|
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;
|
|
|
|
}
|
|
|
|
}
|