File size: 3,544 Bytes
60050be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

/// @title InsurancePool
/// @notice Composable underwriting-yield pool. Liquidity providers stake native token and
/// earn a share of every confidential slashing penalty collected by CipherTrust when an
/// autonomous agent breaches its SLA. This is a deliberately plain, fully transparent DeFi
/// primitive that composes with CipherTrust's confidential risk engine without ever needing
/// to see any agent's encrypted telemetry or trust score -- a concrete demonstration of
/// Season 3's "Composable Privacy" theme.
contract InsurancePool {
    address public admin;
    address public cipherTrust;

    uint256 public totalShares;
    uint256 public totalAssets;
    mapping(address => uint256) public sharesOf;

    event Staked(address indexed provider, uint256 amount, uint256 shares);
    event Withdrawn(address indexed provider, uint256 amount, uint256 shares);
    event PenaltyReceived(uint256 indexed agentId, uint256 amount);
    event CipherTrustSet(address indexed cipherTrust);
    event CreditDelegated(uint256 indexed agentId, uint256 amount);
    event CreditRepaid(uint256 indexed agentId, uint256 amount);

    modifier onlyAdmin() {
        require(msg.sender == admin, "InsurancePool: not admin");
        _;
    }

    modifier onlyCipherTrust() {
        require(msg.sender == cipherTrust, "InsurancePool: not CipherTrust");
        _;
    }

    constructor() {
        admin = msg.sender;
    }

    function setCipherTrust(address cipherTrust_) external onlyAdmin {
        require(cipherTrust == address(0), "InsurancePool: already set");
        cipherTrust = cipherTrust_;
        emit CipherTrustSet(cipherTrust_);
    }

    function stake() external payable returns (uint256 shares) {
        require(msg.value > 0, "InsurancePool: zero stake");
        shares = totalShares == 0 ? msg.value : (msg.value * totalShares) / totalAssets;
        totalShares += shares;
        totalAssets += msg.value;
        sharesOf[msg.sender] += shares;
        emit Staked(msg.sender, msg.value, shares);
    }

    function withdraw(uint256 shares) external {
        require(shares > 0 && shares <= sharesOf[msg.sender], "InsurancePool: bad shares");
        uint256 amount = (shares * totalAssets) / totalShares;
        sharesOf[msg.sender] -= shares;
        totalShares -= shares;
        totalAssets -= amount;
        payable(msg.sender).transfer(amount);
        emit Withdrawn(msg.sender, amount, shares);
    }

    /// @notice Called by CipherTrust when a confidential SLA breach results in a slashing
    /// penalty. The pool grows in value for existing stakers without minting new shares --
    /// this is the pool's yield.
    function receivePenalty(uint256 agentId) external payable onlyCipherTrust {
        totalAssets += msg.value;
        emit PenaltyReceived(agentId, msg.value);
    }

    function pricePerShare() external view returns (uint256) {
        if (totalShares == 0) return 1e18;
        return (totalAssets * 1e18) / totalShares;
    }

    function delegateCredit(uint256 agentId, uint256 amount) external onlyCipherTrust {
        require(totalAssets >= amount, "InsurancePool: insufficient assets for credit delegation");
        totalAssets -= amount;
        payable(cipherTrust).transfer(amount);
        emit CreditDelegated(agentId, amount);
    }

    function repayCredit(uint256 agentId) external payable onlyCipherTrust {
        totalAssets += msg.value;
        emit CreditRepaid(agentId, msg.value);
    }
}