Verified contractExactMIT
QairosInsurance
Address0x2d90c8adb951f3f9286ac239ff7e3d641d057902
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.28
- Same code
Only this contract
Details
- Compiler
- solc 0.8.28+commit.7893614a
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- cancun
- Via IR
- yes
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,162,618
- Creation transaction
- 0x8d1e6b3bf9…b34741eaf4
3 files
- src/QairosInsurance.solmain5.14 KB
- src/QairosMarket.sol4.28 KB
- src/interfaces/IQairosOracle.sol744 B
src/QairosInsurance.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {IQairosOracle} from "./interfaces/IQairosOracle.sol";
import {QairosMarket} from "./QairosMarket.sol";
/// @title QairosInsurance
/// @notice Exposure cover against the Qairos Moment. Underwriters pool ETH and earn premiums. A policy pays its full coverage
/// if the first armed QairosCanary break happens during the policy term. Price follows the QairosMarket crowd:
/// rate = BASE_BPS + impliedYes * SLOPE_BPS / 10000, per year.
contract QairosInsurance {
struct Policy {
address holder;
uint128 coverage;
uint64 start;
uint64 expiry;
bool closed;
}
uint256 public constant BASE_BPS = 40;
uint256 public constant SLOPE_BPS = 500;
uint256 public constant MAX_YEARS = 5;
IQairosOracle public immutable oracle;
QairosMarket public immutable market;
uint256 public immutable marketId;
uint256 public totalShares;
mapping(address => uint256) public shares;
uint256 public lockedCoverage;
Policy[] public policies;
uint256 private _lock = 1;
event Underwritten(address indexed account, uint256 amount, uint256 shares);
event Withdrawn(address indexed account, uint256 amount, uint256 shares);
event PolicyBought(uint256 indexed id, address indexed holder, uint256 coverage, uint256 premium, uint64 expiry);
event PolicyPaid(uint256 indexed id, uint256 coverage);
event PolicyExpired(uint256 indexed id);
error BadTerm();
error WrongPremium(uint256 expected);
error NoCapacity();
error NotHolder();
error NotCovered();
error StillActive();
error PolicyClosed();
error Locked();
error Reentrancy();
error TransferFailed();
modifier nonReentrant() {
if (_lock != 1) revert Reentrancy();
_lock = 2;
_;
_lock = 1;
}
constructor(IQairosOracle oracle_, QairosMarket market_, uint256 marketId_) {
oracle = oracle_;
market = market_;
marketId = marketId_;
}
function rateBps() public view returns (uint256) {
return BASE_BPS + market.impliedYesBps(marketId) * SLOPE_BPS / 10_000;
}
function quote(uint256 coverage, uint256 years_) public view returns (uint256) {
return coverage * rateBps() * years_ / 10_000;
}
function freeCapital() public view returns (uint256) {
uint256 bal = address(this).balance;
return bal > lockedCoverage ? bal - lockedCoverage : 0;
}
function underwrite() external payable {
uint256 assetsBefore = address(this).balance - msg.value;
uint256 s = (totalShares == 0 || assetsBefore == 0) ? msg.value : msg.value * totalShares / assetsBefore;
shares[msg.sender] += s;
totalShares += s;
emit Underwritten(msg.sender, msg.value, s);
}
function withdraw(uint256 s) external nonReentrant {
uint256 amount = s * address(this).balance / totalShares;
if (amount > freeCapital()) revert Locked();
shares[msg.sender] -= s;
totalShares -= s;
_send(msg.sender, amount);
emit Withdrawn(msg.sender, amount, s);
}
function buyPolicy(uint256 coverage, uint256 years_) external payable returns (uint256 id) {
if (years_ == 0 || years_ > MAX_YEARS) revert BadTerm();
if (oracle.firstBreakTime() != 0) revert NotCovered();
uint256 premium = quote(coverage, years_);
if (msg.value != premium) revert WrongPremium(premium);
if (freeCapital() < coverage) revert NoCapacity();
lockedCoverage += coverage;
uint64 expiry = uint64(block.timestamp + years_ * 365 days);
id = policies.length;
policies.push(Policy(msg.sender, uint128(coverage), uint64(block.timestamp), expiry, false));
emit PolicyBought(id, msg.sender, coverage, premium, expiry);
}
function claim(uint256 id) external nonReentrant {
Policy storage p = policies[id];
if (p.closed) revert PolicyClosed();
if (msg.sender != p.holder) revert NotHolder();
uint64 q = oracle.firstBreakTime();
if (q == 0 || q < p.start || q > p.expiry) revert NotCovered();
p.closed = true;
lockedCoverage -= p.coverage;
_send(p.holder, p.coverage);
emit PolicyPaid(id, p.coverage);
}
/// @notice Releases capital of a policy whose term ended without a covered event. Callable by anyone.
function expire(uint256 id) external {
Policy storage p = policies[id];
if (p.closed) revert PolicyClosed();
if (block.timestamp <= p.expiry) revert StillActive();
uint64 q = oracle.firstBreakTime();
if (q != 0 && q >= p.start && q <= p.expiry) revert StillActive();
p.closed = true;
lockedCoverage -= p.coverage;
emit PolicyExpired(id);
}
function policyCount() external view returns (uint256) {
return policies.length;
}
function _send(address to, uint256 amount) private {
(bool ok,) = to.call{value: amount}("");
if (!ok) revert TransferFailed();
}
}
src/QairosMarket.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {IQairosOracle} from "./interfaces/IQairosOracle.sol";
/// @title QairosMarket
/// @notice Parimutuel prediction markets on "will rung X be broken before deadline?". Settlement reads QairosOracle
/// directly: YES as soon as the rung is broken, NO once the deadline passes. No votes, no disputes.
contract QairosMarket {
struct Market {
uint16 bits;
uint64 deadline;
bool settled;
bool yesWon;
uint256 yesPool;
uint256 noPool;
string question;
}
IQairosOracle public immutable oracle;
address public immutable creator;
Market[] private _markets;
mapping(uint256 => mapping(address => uint256)) public yesStake;
mapping(uint256 => mapping(address => uint256)) public noStake;
uint256 private _lock = 1;
event MarketCreated(uint256 indexed id, uint16 bits, uint64 deadline, string question);
event Bought(uint256 indexed id, address indexed buyer, bool yes, uint256 amount);
event Settled(uint256 indexed id, bool yesWon);
event Claimed(uint256 indexed id, address indexed account, uint256 payout);
error NotCreator();
error Closed();
error NotSettleable();
error NotSettled();
error NothingToClaim();
error Reentrancy();
error TransferFailed();
modifier nonReentrant() {
if (_lock != 1) revert Reentrancy();
_lock = 2;
_;
_lock = 1;
}
constructor(IQairosOracle oracle_) {
oracle = oracle_;
creator = msg.sender;
}
function createMarket(uint16 bits, uint64 deadline, string calldata question) external returns (uint256 id) {
if (msg.sender != creator) revert NotCreator();
id = _markets.length;
_markets.push(Market(bits, deadline, false, false, 0, 0, question));
emit MarketCreated(id, bits, deadline, question);
}
function buy(uint256 id, bool yes) external payable {
Market storage m = _markets[id];
if (m.settled || block.timestamp >= m.deadline || oracle.rungBroken(m.bits)) revert Closed();
if (yes) {
m.yesPool += msg.value;
yesStake[id][msg.sender] += msg.value;
} else {
m.noPool += msg.value;
noStake[id][msg.sender] += msg.value;
}
emit Bought(id, msg.sender, yes, msg.value);
}
function settle(uint256 id) public {
Market storage m = _markets[id];
if (m.settled) return;
if (oracle.rungBroken(m.bits)) {
m.yesWon = true;
} else if (block.timestamp >= m.deadline) {
m.yesWon = false;
} else {
revert NotSettleable();
}
m.settled = true;
emit Settled(id, m.yesWon);
}
/// @notice Winners split the whole pot pro rata. If nobody backed the winning side, everyone is refunded.
function claim(uint256 id) external nonReentrant {
Market storage m = _markets[id];
if (!m.settled) settle(id);
uint256 y = yesStake[id][msg.sender];
uint256 n = noStake[id][msg.sender];
uint256 winPool = m.yesWon ? m.yesPool : m.noPool;
uint256 total = m.yesPool + m.noPool;
uint256 payout;
if (winPool == 0) {
payout = y + n;
} else {
uint256 stake = m.yesWon ? y : n;
payout = stake * total / winPool;
}
if (payout == 0) revert NothingToClaim();
yesStake[id][msg.sender] = 0;
noStake[id][msg.sender] = 0;
(bool ok,) = msg.sender.call{value: payout}("");
if (!ok) revert TransferFailed();
emit Claimed(id, msg.sender, payout);
}
/// @notice Implied probability of YES in basis points (0-10000). 5000 when the market is empty.
function impliedYesBps(uint256 id) public view returns (uint256) {
Market storage m = _markets[id];
uint256 total = m.yesPool + m.noPool;
if (total == 0) return 5000;
return m.yesPool * 10_000 / total;
}
function market(uint256 id) external view returns (Market memory) {
return _markets[id];
}
function marketCount() external view returns (uint256) {
return _markets.length;
}
}
src/interfaces/IQairosOracle.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
interface IQairosOracle {
event ThreatRaised(uint8 level, uint16 bits, address solver);
/// @notice Called by QairosCanary when a rung is broken.
function report(uint16 bits, bool armed, address solver) external;
/// @notice 0 = nominal. +1 per broken armed rung (128-bit and up). Max 5.
function threatLevel() external view returns (uint8);
function rungBroken(uint16 bits) external view returns (bool);
function lastBreak() external view returns (uint16 bits, uint64 ts);
/// @notice Timestamp of the first armed break, or 0 if none. Ark commitments before this time are pre-Moment.
function firstBreakTime() external view returns (uint64);
}
Compiler settingsShow
{
"viaIR": true,
"metadata": {
"bytecodeHash": "ipfs"
},
"libraries": {},
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "cancun",
"remappings": [
"forge-std/=lib/forge-std/src/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.