dagger-contracts/contracts/Storage.sol

153 lines
3.7 KiB
Solidity
Raw Normal View History

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./Contracts.sol";
import "./Proofs.sol";
import "./Stakes.sol";
contract Storage is Contracts, Proofs, Stakes {
2022-02-09 13:17:23 +00:00
uint256 public stakeAmount;
uint256 public slashMisses;
uint256 public slashPercentage;
2022-02-09 13:17:23 +00:00
mapping(bytes32 => bool) private finished;
constructor(
IERC20 token,
2022-02-09 13:17:23 +00:00
uint256 _stakeAmount,
uint256 _slashMisses,
uint256 _slashPercentage
) Stakes(token) {
2021-11-02 11:45:09 +00:00
stakeAmount = _stakeAmount;
slashMisses = _slashMisses;
slashPercentage = _slashPercentage;
2021-11-02 11:45:09 +00:00
}
function newContract(
2022-02-09 13:17:23 +00:00
uint256 _duration,
uint256 _size,
bytes32 _contentHash,
2022-02-09 13:17:23 +00:00
uint256 _proofPeriod,
uint256 _proofTimeout,
bytes32 _nonce,
2022-02-09 13:17:23 +00:00
uint256 _price,
address _host,
2022-02-09 13:17:23 +00:00
uint256 _bidExpiry,
bytes memory requestSignature,
bytes memory bidSignature
2022-02-09 13:17:23 +00:00
) public {
2021-11-02 11:45:09 +00:00
require(_stake(_host) >= stakeAmount, "Insufficient stake");
_lockStake(_host);
_token().transferFrom(msg.sender, address(this), _price);
_newContract(
_duration,
_size,
_contentHash,
_proofPeriod,
_proofTimeout,
_nonce,
_price,
_host,
_bidExpiry,
requestSignature,
bidSignature
);
}
2021-11-04 08:53:01 +00:00
modifier onlyHost(bytes32 id) {
require(msg.sender == host(id), "Only host can call this function");
_;
}
function startContract(bytes32 id) public onlyHost(id) {
_expectProofs(id, proofPeriod(id), proofTimeout(id), duration(id));
}
2021-11-04 09:19:23 +00:00
function finishContract(bytes32 id) public {
require(block.number > proofEnd(id), "Contract has not ended yet");
require(!finished[id], "Contract already finished");
2021-11-04 10:55:47 +00:00
require(_token().transfer(host(id), price(id)), "Payment failed");
2021-11-04 09:19:23 +00:00
_unlockStake(host(id));
finished[id] = true;
2021-11-04 09:19:23 +00:00
}
2022-02-09 13:17:23 +00:00
function duration(bytes32 contractId) public view returns (uint256) {
return _duration(contractId);
}
2022-02-09 13:17:23 +00:00
function size(bytes32 contractId) public view returns (uint256) {
return _size(contractId);
}
function contentHash(bytes32 contractId) public view returns (bytes32) {
return _contentHash(contractId);
}
2022-02-09 13:17:23 +00:00
function price(bytes32 contractId) public view returns (uint256) {
return _price(contractId);
2021-10-18 13:29:58 +00:00
}
function host(bytes32 contractId) public view returns (address) {
return _host(contractId);
}
2022-02-09 13:17:23 +00:00
function proofPeriod(bytes32 contractId) public view returns (uint256) {
return _proofPeriod(contractId);
}
2022-02-09 13:17:23 +00:00
function proofTimeout(bytes32 contractId) public view returns (uint256) {
return _proofTimeout(contractId);
}
2022-02-09 13:17:23 +00:00
function proofEnd(bytes32 contractId) public view returns (uint256) {
return _end(contractId);
}
2022-02-09 13:17:23 +00:00
function missingProofs(bytes32 contractId) public view returns (uint256) {
return _missed(contractId);
}
2022-02-09 13:17:23 +00:00
function stake(address account) public view returns (uint256) {
return _stake(account);
}
2022-02-09 13:17:23 +00:00
function isProofRequired(bytes32 contractId, uint256 blocknumber)
public
view
returns (bool)
{
return _isProofRequired(contractId, blocknumber);
}
2021-10-18 12:55:59 +00:00
2022-02-09 13:17:23 +00:00
function isProofTimedOut(bytes32 contractId, uint256 blocknumber)
public
view
returns (bool)
{
return _isProofTimedOut(contractId, blocknumber);
2021-10-19 07:37:03 +00:00
}
function submitProof(
bytes32 contractId,
2022-02-09 13:17:23 +00:00
uint256 blocknumber,
bool proof
2022-02-09 13:17:23 +00:00
) public {
_submitProof(contractId, blocknumber, proof);
2021-10-18 12:55:59 +00:00
}
2021-10-19 07:37:03 +00:00
2022-02-09 13:17:23 +00:00
function markProofAsMissing(bytes32 contractId, uint256 blocknumber) public {
_markProofAsMissing(contractId, blocknumber);
if (_missed(contractId) % slashMisses == 0) {
_slash(host(contractId), slashPercentage);
}
2021-10-19 07:37:03 +00:00
}
2022-02-09 13:17:23 +00:00
function increaseStake(uint256 amount) public {
_increaseStake(amount);
}
function withdrawStake() public {
_withdrawStake();
}
}