|
| 1 | +pragma solidity 0.7.6; |
| 2 | + |
| 3 | +// SPDX-License-Identifier: GPL-3.0-only |
| 4 | + |
| 5 | +import "@openzeppelin/contracts/math/SafeMath.sol"; |
| 6 | + |
| 7 | +import "../../RocketBase.sol"; |
| 8 | +import "../../../interface/dao/node/RocketDAONodeTrustedInterface.sol"; |
| 9 | +import "../../../interface/network/RocketNetworkBalancesInterface.sol"; |
| 10 | +import "../../../interface/dao/protocol/settings/RocketDAOProtocolSettingsNetworkInterface.sol"; |
| 11 | + |
| 12 | +// Network balances |
| 13 | + |
| 14 | +contract RocketNetworkBalancesOld is RocketBase, RocketNetworkBalancesInterface { |
| 15 | + |
| 16 | + // Libs |
| 17 | + using SafeMath for uint; |
| 18 | + |
| 19 | + // Events |
| 20 | + event BalancesSubmitted(address indexed from, uint256 block, uint256 totalEth, uint256 stakingEth, uint256 rethSupply, uint256 time); |
| 21 | + event BalancesUpdated(uint256 block, uint256 totalEth, uint256 stakingEth, uint256 rethSupply, uint256 time); |
| 22 | + |
| 23 | + // Construct |
| 24 | + constructor(RocketStorageInterface _rocketStorageAddress) RocketBase(_rocketStorageAddress) { |
| 25 | + version = 1; |
| 26 | + } |
| 27 | + |
| 28 | + // The block number which balances are current for |
| 29 | + function getBalancesBlock() override public view returns (uint256) { |
| 30 | + return getUint(keccak256("network.balances.updated.block")); |
| 31 | + } |
| 32 | + function setBalancesBlock(uint256 _value) private { |
| 33 | + setUint(keccak256("network.balances.updated.block"), _value); |
| 34 | + } |
| 35 | + |
| 36 | + // The current RP network total ETH balance |
| 37 | + function getTotalETHBalance() override public view returns (uint256) { |
| 38 | + return getUint(keccak256("network.balance.total")); |
| 39 | + } |
| 40 | + function setTotalETHBalance(uint256 _value) private { |
| 41 | + setUint(keccak256("network.balance.total"), _value); |
| 42 | + } |
| 43 | + |
| 44 | + // The current RP network staking ETH balance |
| 45 | + function getStakingETHBalance() override public view returns (uint256) { |
| 46 | + return getUint(keccak256("network.balance.staking")); |
| 47 | + } |
| 48 | + function setStakingETHBalance(uint256 _value) private { |
| 49 | + setUint(keccak256("network.balance.staking"), _value); |
| 50 | + } |
| 51 | + |
| 52 | + // The current RP network total rETH supply |
| 53 | + function getTotalRETHSupply() override external view returns (uint256) { |
| 54 | + return getUint(keccak256("network.balance.reth.supply")); |
| 55 | + } |
| 56 | + function setTotalRETHSupply(uint256 _value) private { |
| 57 | + setUint(keccak256("network.balance.reth.supply"), _value); |
| 58 | + } |
| 59 | + |
| 60 | + // Get the current RP network ETH utilization rate as a fraction of 1 ETH |
| 61 | + // Represents what % of the network's balance is actively earning rewards |
| 62 | + function getETHUtilizationRate() override external view returns (uint256) { |
| 63 | + uint256 totalEthBalance = getTotalETHBalance(); |
| 64 | + uint256 stakingEthBalance = getStakingETHBalance(); |
| 65 | + if (totalEthBalance == 0) { return calcBase; } |
| 66 | + return calcBase.mul(stakingEthBalance).div(totalEthBalance); |
| 67 | + } |
| 68 | + |
| 69 | + // Submit network balances for a block |
| 70 | + // Only accepts calls from trusted (oracle) nodes |
| 71 | + function submitBalances(uint256 _block, uint256 _totalEth, uint256 _stakingEth, uint256 _rethSupply) override external onlyLatestContract("rocketNetworkBalances", address(this)) onlyTrustedNode(msg.sender) { |
| 72 | + // Check settings |
| 73 | + RocketDAOProtocolSettingsNetworkInterface rocketDAOProtocolSettingsNetwork = RocketDAOProtocolSettingsNetworkInterface(getContractAddress("rocketDAOProtocolSettingsNetwork")); |
| 74 | + require(rocketDAOProtocolSettingsNetwork.getSubmitBalancesEnabled(), "Submitting balances is currently disabled"); |
| 75 | + // Check block |
| 76 | + require(_block < block.number, "Balances can not be submitted for a future block"); |
| 77 | + require(_block > getBalancesBlock(), "Network balances for an equal or higher block are set"); |
| 78 | + // Check balances |
| 79 | + require(_stakingEth <= _totalEth, "Invalid network balances"); |
| 80 | + // Get submission keys |
| 81 | + bytes32 nodeSubmissionKey = keccak256(abi.encodePacked("network.balances.submitted.node", msg.sender, _block, _totalEth, _stakingEth, _rethSupply)); |
| 82 | + bytes32 submissionCountKey = keccak256(abi.encodePacked("network.balances.submitted.count", _block, _totalEth, _stakingEth, _rethSupply)); |
| 83 | + // Check & update node submission status |
| 84 | + require(!getBool(nodeSubmissionKey), "Duplicate submission from node"); |
| 85 | + setBool(nodeSubmissionKey, true); |
| 86 | + setBool(keccak256(abi.encodePacked("network.balances.submitted.node", msg.sender, _block)), true); |
| 87 | + // Increment submission count |
| 88 | + uint256 submissionCount = getUint(submissionCountKey).add(1); |
| 89 | + setUint(submissionCountKey, submissionCount); |
| 90 | + // Emit balances submitted event |
| 91 | + emit BalancesSubmitted(msg.sender, _block, _totalEth, _stakingEth, _rethSupply, block.timestamp); |
| 92 | + // Check submission count & update network balances |
| 93 | + RocketDAONodeTrustedInterface rocketDAONodeTrusted = RocketDAONodeTrustedInterface(getContractAddress("rocketDAONodeTrusted")); |
| 94 | + if (calcBase.mul(submissionCount).div(rocketDAONodeTrusted.getMemberCount()) >= rocketDAOProtocolSettingsNetwork.getNodeConsensusThreshold()) { |
| 95 | + updateBalances(_block, _totalEth, _stakingEth, _rethSupply); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Executes updateBalances if consensus threshold is reached |
| 100 | + function executeUpdateBalances(uint256 _block, uint256 _totalEth, uint256 _stakingEth, uint256 _rethSupply) override external onlyLatestContract("rocketNetworkBalances", address(this)) { |
| 101 | + // Check settings |
| 102 | + RocketDAOProtocolSettingsNetworkInterface rocketDAOProtocolSettingsNetwork = RocketDAOProtocolSettingsNetworkInterface(getContractAddress("rocketDAOProtocolSettingsNetwork")); |
| 103 | + require(rocketDAOProtocolSettingsNetwork.getSubmitBalancesEnabled(), "Submitting balances is currently disabled"); |
| 104 | + // Check block |
| 105 | + require(_block < block.number, "Balances can not be submitted for a future block"); |
| 106 | + require(_block > getBalancesBlock(), "Network balances for an equal or higher block are set"); |
| 107 | + // Check balances |
| 108 | + require(_stakingEth <= _totalEth, "Invalid network balances"); |
| 109 | + // Get submission keys |
| 110 | + bytes32 submissionCountKey = keccak256(abi.encodePacked("network.balances.submitted.count", _block, _totalEth, _stakingEth, _rethSupply)); |
| 111 | + // Get submission count |
| 112 | + uint256 submissionCount = getUint(submissionCountKey); |
| 113 | + // Check submission count & update network balances |
| 114 | + RocketDAONodeTrustedInterface rocketDAONodeTrusted = RocketDAONodeTrustedInterface(getContractAddress("rocketDAONodeTrusted")); |
| 115 | + require(calcBase.mul(submissionCount).div(rocketDAONodeTrusted.getMemberCount()) >= rocketDAOProtocolSettingsNetwork.getNodeConsensusThreshold(), "Consensus has not been reached"); |
| 116 | + updateBalances(_block, _totalEth, _stakingEth, _rethSupply); |
| 117 | + } |
| 118 | + |
| 119 | + // Update network balances |
| 120 | + function updateBalances(uint256 _block, uint256 _totalEth, uint256 _stakingEth, uint256 _rethSupply) private { |
| 121 | + // Update balances |
| 122 | + setBalancesBlock(_block); |
| 123 | + setTotalETHBalance(_totalEth); |
| 124 | + setStakingETHBalance(_stakingEth); |
| 125 | + setTotalRETHSupply(_rethSupply); |
| 126 | + // Emit balances updated event |
| 127 | + emit BalancesUpdated(_block, _totalEth, _stakingEth, _rethSupply, block.timestamp); |
| 128 | + } |
| 129 | + |
| 130 | + // Returns the latest block number that oracles should be reporting balances for |
| 131 | + function getLatestReportableBlock() override external view returns (uint256) { |
| 132 | + // Load contracts |
| 133 | + RocketDAOProtocolSettingsNetworkInterface rocketDAOProtocolSettingsNetwork = RocketDAOProtocolSettingsNetworkInterface(getContractAddress("rocketDAOProtocolSettingsNetwork")); |
| 134 | + // Get the block balances were lasted updated and the update frequency |
| 135 | + uint256 updateFrequency = rocketDAOProtocolSettingsNetwork.getSubmitBalancesFrequency(); |
| 136 | + // Calculate the last reportable block based on update frequency |
| 137 | + return block.number.div(updateFrequency).mul(updateFrequency); |
| 138 | + } |
| 139 | +} |
0 commit comments