Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
9fdd223
DeFi Protocol Solvency Proof Mechanism
SeanLuis Feb 25, 2025
6ffe4fe
Merge branch 'master' into feat/erc-draft
SeanLuis Feb 25, 2025
c69b262
Update eip-draft.md
SeanLuis Feb 26, 2025
a496511
Merge branch 'master' into feat/erc-draft
SeanLuis Feb 26, 2025
e7448be
Merge branch 'master' into feat/erc-draft
SeanLuis Feb 26, 2025
7761e8c
Merge branch 'master' into feat/erc-draft
SeanLuis Feb 27, 2025
70975b6
Merge branch 'master' into feat/erc-draft
SeanLuis Mar 1, 2025
4c1987f
Update and rename eip-draft.md to eip-7893.md
SamWilsn Mar 3, 2025
1d6b2bc
Update and rename eip-7893.md to erc-7893.md
SamWilsn Mar 3, 2025
a989efa
Merge branch 'master' into feat/erc-draft
SeanLuis Mar 3, 2025
9be9260
Merge branch 'master' into feat/erc-draft
SeanLuis Mar 15, 2025
b636174
Merge branch 'master' into feat/erc-draft
SeanLuis Mar 24, 2025
69e1230
Merge branch 'master' into feat/erc-draft
SeanLuis Mar 25, 2025
09c5721
Update ERC-7893: Add discussions link and refine description
SeanLuis Apr 8, 2025
08fc322
Update ERC-7893: Move discussions link to the correct position in the…
SeanLuis Apr 8, 2025
34da3a5
Merge branch 'master' into feat/erc-draft
SeanLuis Apr 8, 2025
dd61673
Update ERC-7893: Change discussions link to point to the correct forum
SeanLuis Apr 8, 2025
5120cea
Merge branch 'feat/erc-draft' of https://github.com/SeanLuis/ercs int…
SeanLuis Apr 8, 2025
c541162
Merge branch 'master' into feat/erc-draft
SeanLuis Apr 17, 2025
61f1fa2
refactor: restructure ERC-7893 to align with EIP standards and improv…
SeanLuis Apr 28, 2025
049acda
fix: update description for clarity and consistency in ERC-7893
SeanLuis Apr 28, 2025
88ad68a
Merge branch 'master' into feat/erc-draft
SeanLuis Apr 28, 2025
8a07771
Merge branch 'master' into feat/erc-draft
SeanLuis Apr 30, 2025
9f2b8d1
Merge branch 'master' into feat/erc-draft
SeanLuis May 8, 2025
cba368b
Merge branch 'master' into feat/erc-draft
SeanLuis May 12, 2025
9710a63
Merge branch 'master' into feat/erc-draft
SeanLuis May 20, 2025
24a56bf
Merge branch 'master' into feat/erc-draft
SeanLuis May 28, 2025
a8ec7ca
Update erc-7893.md
SamWilsn Jun 16, 2025
fa4f921
Update ERC-7893: add prose interface explanation, move optional oracl…
SeanLuis Jun 17, 2025
200bbaf
Merge branch 'master' into feat/erc-draft
SeanLuis Jun 30, 2025
4adfcc6
Update erc-7893.md
SamWilsn Jul 15, 2025
e03560c
Remove unused PNG assets for ERC-7893
SeanLuis Jul 21, 2025
d03e123
Merge branch 'master' into feat/erc-draft
SeanLuis Jul 21, 2025
6ff0cbe
Merge branch 'master' into feat/erc-draft
SeanLuis Jul 28, 2025
2548967
Merge branch 'master' into feat/erc-draft
SeanLuis Aug 22, 2025
66f74bb
Merge branch 'master' into feat/erc-draft
SeanLuis Aug 28, 2025
f14c6e9
Merge branch 'master' into feat/erc-draft
SeanLuis Sep 8, 2025
85951ef
Update erc-7893.md
SamWilsn Sep 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
572 changes: 572 additions & 0 deletions ERCS/erc-7893.md

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions assets/erc-7893/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# MIT License

Copyright (c) 2025 Sean Luis

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
248 changes: 248 additions & 0 deletions assets/erc-7893/SolvencyProof.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "./ISolvencyProof.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";

/**
* @title SolvencyProof
* @author Sean Luis (@SeanLuis) <[email protected]>
* @notice Implementation of DeFi Protocol Solvency Proof Standard (EIP-DRAFT)
* @dev This contract implements ISolvencyProof interface for tracking and verifying protocol solvency
* It includes asset/liability tracking, solvency ratio calculations, and historical metrics
*/
contract SolvencyProof is ISolvencyProof, Ownable, ReentrancyGuard {
// === Constants ===
/// @notice Base multiplier for ratio calculations (100% = 10000)
uint256 private constant RATIO_DECIMALS = 10000;

/// @notice Minimum solvency ratio required (105%)
uint256 private constant MIN_SOLVENCY_RATIO = 10500;

/// @notice Critical threshold for emergency measures (102%)
uint256 private constant CRITICAL_RATIO = 10200;

// === State Variables ===
/// @notice Current state of protocol assets
ProtocolAssets private currentAssets;

/// @notice Current state of protocol liabilities
ProtocolLiabilities private currentLiabilities;

/// @notice Mapping of authorized price oracles
/// @dev address => isAuthorized
mapping(address => bool) public assetOracles;

/**
* @notice Structure for storing historical solvency metrics
* @dev Used to track protocol's financial health over time
* @param timestamp Time when metrics were recorded
* @param solvencyRatio Calculated solvency ratio at that time
* @param assets Snapshot of protocol assets
* @param liabilities Snapshot of protocol liabilities
*/
struct HistoricalMetric {
uint256 timestamp;
uint256 solvencyRatio;
ProtocolAssets assets;
ProtocolLiabilities liabilities;
}

/// @notice Array storing historical solvency metrics
HistoricalMetric[] private metricsHistory;

// === Events ===
/// @notice Emitted when an oracle's authorization status changes
/// @param oracle Address of the oracle
/// @param authorized New authorization status
event OracleUpdated(address indexed oracle, bool authorized);

/**
* @notice Contract constructor
* @dev Initializes Ownable with msg.sender as owner
*/
constructor() Ownable(msg.sender) {}

/**
* @notice Restricts function access to authorized oracles
* @dev Throws if called by non-authorized address
*/
modifier onlyOracle() {
require(assetOracles[msg.sender], "Not authorized oracle");
_;
}

// === External Functions ===
/// @inheritdoc ISolvencyProof
function getProtocolAssets() external view returns (ProtocolAssets memory) {
return currentAssets;
}

/// @inheritdoc ISolvencyProof
function getProtocolLiabilities() external view returns (ProtocolLiabilities memory) {
return currentLiabilities;
}

/// @inheritdoc ISolvencyProof
function getSolvencyRatio() external view returns (uint256) {
return _calculateSolvencyRatio();
}

/// @inheritdoc ISolvencyProof
function verifySolvency() external view returns (bool isSolvent, uint256 healthFactor) {
uint256 ratio = _calculateSolvencyRatio();
return (ratio >= MIN_SOLVENCY_RATIO, ratio);
}

/// @inheritdoc ISolvencyProof
function getSolvencyHistory(uint256 startTime, uint256 endTime)
external
view
returns (
uint256[] memory timestamps,
uint256[] memory ratios,
ProtocolAssets[] memory assets,
ProtocolLiabilities[] memory liabilities
)
{
uint256 count = 0;
for (uint256 i = 0; i < metricsHistory.length; i++) {
if (metricsHistory[i].timestamp >= startTime &&
metricsHistory[i].timestamp <= endTime) {
count++;
}
}

timestamps = new uint256[](count);
ratios = new uint256[](count);
assets = new ProtocolAssets[](count);
liabilities = new ProtocolLiabilities[](count);
uint256 index = 0;

for (uint256 i = 0; i < metricsHistory.length && index < count; i++) {
if (metricsHistory[i].timestamp >= startTime &&
metricsHistory[i].timestamp <= endTime) {
timestamps[index] = metricsHistory[i].timestamp;
ratios[index] = metricsHistory[i].solvencyRatio;
assets[index] = metricsHistory[i].assets;
liabilities[index] = metricsHistory[i].liabilities;
index++;
}
}

return (timestamps, ratios, assets, liabilities);
}

/// @inheritdoc ISolvencyProof
function updateAssets(
address[] calldata tokens,
uint256[] calldata amounts,
uint256[] calldata values
) external onlyOracle nonReentrant {
require(tokens.length == amounts.length && amounts.length == values.length,
"Array lengths mismatch");

currentAssets = ProtocolAssets({
tokens: tokens,
amounts: amounts,
values: values,
timestamp: block.timestamp
});

_updateMetrics();
}

/// @inheritdoc ISolvencyProof
function updateLiabilities(
address[] calldata tokens,
uint256[] calldata amounts,
uint256[] calldata values
) external onlyOracle nonReentrant {
require(tokens.length == amounts.length && amounts.length == values.length,
"Array lengths mismatch");

currentLiabilities = ProtocolLiabilities({
tokens: tokens,
amounts: amounts,
values: values,
timestamp: block.timestamp
});

_updateMetrics();
}

/**
* @notice Updates oracle authorization status
* @dev Only callable by contract owner
* @param oracle Address of the oracle to update
* @param authorized New authorization status
*/
function setOracle(address oracle, bool authorized) external onlyOwner {
require(oracle != address(0), "Invalid oracle address");
assetOracles[oracle] = authorized;
emit OracleUpdated(oracle, authorized);
}

// === Internal Functions ===
/**
* @notice Calculates current solvency ratio
* @dev Ratio = (Total Assets / Total Liabilities) × RATIO_DECIMALS
* @return Current solvency ratio with RATIO_DECIMALS precision
*/
function _calculateSolvencyRatio() internal view returns (uint256) {
uint256 totalAssets = _sumArray(currentAssets.values);
uint256 totalLiabilities = _sumArray(currentLiabilities.values);

if (totalLiabilities == 0) {
return totalAssets > 0 ? RATIO_DECIMALS * 2 : RATIO_DECIMALS;
}

return (totalAssets * RATIO_DECIMALS) / totalLiabilities;
}

/**
* @notice Updates protocol metrics and emits relevant events
* @dev Called after asset or liability updates
*/
function _updateMetrics() internal {
uint256 totalAssets = _sumArray(currentAssets.values);
uint256 totalLiabilities = _sumArray(currentLiabilities.values);
uint256 ratio = _calculateSolvencyRatio();

// Debug log
emit SolvencyMetricsUpdated(
totalAssets,
totalLiabilities,
ratio,
block.timestamp
);

metricsHistory.push(HistoricalMetric({
timestamp: block.timestamp,
solvencyRatio: ratio,
assets: currentAssets,
liabilities: currentLiabilities
}));

// Update alerts based on actual ratio
if (ratio < CRITICAL_RATIO) {
emit RiskAlert("CRITICAL", ratio, totalAssets, totalLiabilities);
} else if (ratio < MIN_SOLVENCY_RATIO) {
emit RiskAlert("HIGH_RISK", ratio, totalAssets, totalLiabilities);
}
}

/**
* @notice Sums all values in an array
* @param array Array of uint256 values to sum
* @return sum Total sum of array values
*/
function _sumArray(uint256[] memory array) internal pure returns (uint256) {
uint256 sum = 0;
for (uint256 i = 0; i < array.length; i++) {
sum += array[i];
}
return sum;
}
}
Loading
Loading