Skip to content

Commit

Permalink
Merge pull request #790 from ebtc-protocol/release-0.8
Browse files Browse the repository at this point in the history
Release 0.8
  • Loading branch information
wtj2021 authored Jun 18, 2024
2 parents 3406f0d + bbf26ed commit 5ba2b60
Show file tree
Hide file tree
Showing 26 changed files with 683 additions and 162 deletions.
10 changes: 10 additions & 0 deletions packages/contracts/contracts/CRLens.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ contract CRLens {
return icr;
}

function getRealStake(bytes32 cdpId) external returns (uint256) {
cdpManager.syncAccounting(cdpId);
uint256 collShares = cdpManager.getCdpCollShares(cdpId);
return
cdpManager.totalCollateralSnapshot() == 0
? collShares
: (collShares * cdpManager.totalStakesSnapshot()) /
cdpManager.totalCollateralSnapshot();
}

/// @dev Returns 1 if we're in RM
function getCheckRecoveryMode(bool revertValue) external returns (uint256) {
// Synch State
Expand Down
14 changes: 14 additions & 0 deletions packages/contracts/contracts/CdpManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,20 @@ contract CdpManager is CdpManagerStorage, ICdpManager, Proxy {
collateral.getPooledEthByShares(newColl) < MIN_NET_STETH_BALANCE ||
newDebt < MIN_CHANGE
) {
_updateStakeAndTotalStakes(_redeemColFromCdp.cdpId);

emit CdpUpdated(
_redeemColFromCdp.cdpId,
ISortedCdps(sortedCdps).getOwnerAddress(_redeemColFromCdp.cdpId),
msg.sender,
_oldDebtAndColl.debt,
_oldDebtAndColl.collShares,
_oldDebtAndColl.debt,
_oldDebtAndColl.collShares,
Cdps[_redeemColFromCdp.cdpId].stake,
CdpOperation.failedPartialRedemption
);

singleRedemption.cancelledPartial = true;
return singleRedemption;
}
Expand Down
14 changes: 6 additions & 8 deletions packages/contracts/contracts/ChainlinkAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,17 @@ contract ChainlinkAdapter is AggregatorV3Interface {
(ETH_USD_PRECISION * btcUsdPrice);
}

function latestRound() external view returns (uint80) {
return CURRENT_ROUND;
}

function _getRoundData(
AggregatorV3Interface _feed,
uint80 _roundId
) private view returns (int256 answer, uint256 updatedAt) {
uint80 latestRoundId = _feed.latestRound();
uint80 feedRoundId;
(feedRoundId, answer, , updatedAt, ) = _feed.getRoundData(
_roundId == CURRENT_ROUND ? latestRoundId : latestRoundId - 1
);
if (_roundId == CURRENT_ROUND) {
(feedRoundId, answer, , updatedAt, ) = _feed.latestRoundData();
} else {
(uint80 latestRoundId, , , , ) = _feed.latestRoundData();
(feedRoundId, answer, , updatedAt, ) = _feed.getRoundData(latestRoundId - 1);
}
require(feedRoundId > 0);
require(answer > 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ interface AggregatorV3Interface {

function version() external view returns (uint256);

function latestRound() external view returns (uint80);

// getRoundData and latestRoundData should both raise "No data present"
// if they do not have data to report, instead of returning unset values
// which could be misinterpreted as actual reported values.
Expand Down
4 changes: 0 additions & 4 deletions packages/contracts/contracts/FixedAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ contract FixedAdapter is AggregatorV3Interface {
return "stETH/ETH Fixed Adapter";
}

function latestRound() external view returns (uint80) {
return CURRENT_ROUND;
}

// getRoundData and latestRoundData should both raise "No data present"
// if they do not have data to report, instead of returning unset values
// which could be misinterpreted as actual reported values.
Expand Down
7 changes: 6 additions & 1 deletion packages/contracts/contracts/Interfaces/ICdpManagerData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ interface ICdpManagerData is IRecoveryModeGracePeriod {
liquidateInNormalMode,
liquidateInRecoveryMode,
redeemCollateral,
partiallyLiquidate
partiallyLiquidate,
failedPartialRedemption
}

enum Status {
Expand Down Expand Up @@ -265,4 +266,8 @@ interface ICdpManagerData is IRecoveryModeGracePeriod {
) external view returns (uint256 debt, uint256 collShares);

function canLiquidateRecoveryMode(uint256 icr, uint256 tcr) external view returns (bool);

function totalCollateralSnapshot() external view returns (uint256);

function totalStakesSnapshot() external view returns (uint256);
}
23 changes: 14 additions & 9 deletions packages/contracts/contracts/LiquidationLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -334,15 +334,20 @@ contract LiquidationLibrary is CdpManagerStorage {
_totalColToSend
);
if (_collSurplus > 0) {
collSurplusPool.increaseSurplusCollShares(
_recoveryState.cdpId,
_borrower,
_collSurplus,
0
);
_recoveryState.totalSurplusCollShares =
_recoveryState.totalSurplusCollShares +
_collSurplus;
if (_checkICRAgainstMCR(_recoveryState.ICR)) {
_cappedColPortion = _collSurplus + _cappedColPortion;
_collSurplus = 0;
} else {
collSurplusPool.increaseSurplusCollShares(
_recoveryState.cdpId,
_borrower,
_collSurplus,
0
);
_recoveryState.totalSurplusCollShares =
_recoveryState.totalSurplusCollShares +
_collSurplus;
}
}
if (_debtToRedistribute > 0) {
_totalDebtToBurn = _totalDebtToBurn - _debtToRedistribute;
Expand Down
4 changes: 0 additions & 4 deletions packages/contracts/contracts/TestContracts/MockAggregator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ contract MockAggregator is AggregatorV3Interface {

// --- Getters that adhere to the AggregatorV3 interface ---

function latestRound() external view returns (uint80) {
return latestRoundId;
}

function decimals() external view override returns (uint8) {
if (decimalsRevert) {
require(1 == 0, "decimals reverted");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ abstract contract BeforeAfter is BaseStorageVariables {
using Pretty for bool;

struct Vars {
uint256 userSurplusBefore;
uint256 userSurplusAfter;
uint256 valueInSystemBefore;
uint256 valueInSystemAfter;
uint256 nicrBefore;
Expand All @@ -34,6 +36,8 @@ abstract contract BeforeAfter is BaseStorageVariables {
uint256 cdpCollAfter;
uint256 cdpDebtBefore;
uint256 cdpDebtAfter;
uint256 cdpStakeBefore;
uint256 cdpStakeAfter;
uint256 liquidatorRewardSharesBefore;
uint256 liquidatorRewardSharesAfter;
uint256 sortedCdpsSizeBefore;
Expand Down Expand Up @@ -71,6 +75,12 @@ abstract contract BeforeAfter is BaseStorageVariables {
uint256 cumulativeCdpsAtTimeOfRebase;
uint256 prevStEthFeeIndex;
uint256 afterStEthFeeIndex;
uint256 totalStakesBefore;
uint256 totalStakesAfter;
uint256 totalStakesSnapshotBefore;
uint256 totalStakesSnapshotAfter;
uint256 totalCollateralSnapshotBefore;
uint256 totalCollateralSnapshotAfter;
}

Vars vars;
Expand All @@ -82,14 +92,18 @@ abstract contract BeforeAfter is BaseStorageVariables {
function _before(bytes32 _cdpId) internal {
vars.priceBefore = priceFeedMock.fetchPrice();

(uint256 debtBefore, ) = cdpManager.getSyncedDebtAndCollShares(_cdpId);
address ownerToCheck = sortedCdps.getOwnerAddress(_cdpId);
vars.userSurplusBefore = collSurplusPool.getSurplusCollShares(ownerToCheck);

(uint256 debtBefore, uint256 collBefore) = cdpManager.getSyncedDebtAndCollShares(_cdpId);

vars.nicrBefore = _cdpId != bytes32(0) ? crLens.quoteRealNICR(_cdpId) : 0;
vars.icrBefore = _cdpId != bytes32(0)
? cdpManager.getCachedICR(_cdpId, vars.priceBefore)
: 0;
vars.cdpCollBefore = _cdpId != bytes32(0) ? cdpManager.getCdpCollShares(_cdpId) : 0;
vars.cdpCollBefore = _cdpId != bytes32(0) ? collBefore : 0;
vars.cdpDebtBefore = _cdpId != bytes32(0) ? debtBefore : 0;
vars.cdpStakeBefore = _cdpId != bytes32(0) ? crLens.getRealStake(_cdpId) : 0;
vars.liquidatorRewardSharesBefore = _cdpId != bytes32(0)
? cdpManager.getCdpLiquidatorRewardShares(_cdpId)
: 0;
Expand Down Expand Up @@ -137,15 +151,25 @@ abstract contract BeforeAfter is BaseStorageVariables {
1e18 -
vars.activePoolDebtBefore;
vars.prevStEthFeeIndex = cdpManager.systemStEthFeePerUnitIndex();

vars.totalStakesBefore = cdpManager.totalStakes();
vars.totalStakesSnapshotBefore = cdpManager.totalStakesSnapshot();
vars.totalCollateralSnapshotBefore = cdpManager.totalCollateralSnapshot();
}

function _after(bytes32 _cdpId) internal {
address ownerToCheck = sortedCdps.getOwnerAddress(_cdpId);
vars.userSurplusAfter = collSurplusPool.getSurplusCollShares(ownerToCheck);

vars.priceAfter = priceFeedMock.fetchPrice();

(, uint256 collAfter) = cdpManager.getSyncedDebtAndCollShares(_cdpId);

vars.nicrAfter = _cdpId != bytes32(0) ? crLens.quoteRealNICR(_cdpId) : 0;
vars.icrAfter = _cdpId != bytes32(0) ? cdpManager.getCachedICR(_cdpId, vars.priceAfter) : 0;
vars.cdpCollAfter = _cdpId != bytes32(0) ? cdpManager.getCdpCollShares(_cdpId) : 0;
vars.cdpCollAfter = _cdpId != bytes32(0) ? collAfter : 0;
vars.cdpDebtAfter = _cdpId != bytes32(0) ? cdpManager.getCdpDebt(_cdpId) : 0;
vars.cdpStakeAfter = _cdpId != bytes32(0) ? crLens.getRealStake(_cdpId) : 0;
vars.liquidatorRewardSharesAfter = _cdpId != bytes32(0)
? cdpManager.getCdpLiquidatorRewardShares(_cdpId)
: 0;
Expand Down Expand Up @@ -200,6 +224,10 @@ abstract contract BeforeAfter is BaseStorageVariables {
if (vars.afterStEthFeeIndex > vars.prevStEthFeeIndex) {
vars.cumulativeCdpsAtTimeOfRebase += cdpManager.getActiveCdpsCount();
}

vars.totalStakesAfter = cdpManager.totalStakes();
vars.totalStakesSnapshotAfter = cdpManager.totalStakesSnapshot();
vars.totalCollateralSnapshotAfter = cdpManager.totalCollateralSnapshot();
}

function _diff() internal view returns (string memory log) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,35 @@ abstract contract Properties is BeforeAfter, PropertiesDescriptions, Asserts, Pr
isApproximateEq(vars.valueInSystemAfter, vars.valueInSystemBefore, 0.01e18);
}

function invariant_CDPM_10(CdpManager cdpManager) internal view returns (bool) {
if (vars.afterStEthFeeIndex > vars.prevStEthFeeIndex) {
return cdpManager.totalStakesSnapshot() == cdpManager.totalStakes();
}
return true;
}

function invariant_CDPM_11(CdpManager cdpManager) internal view returns (bool) {
if (vars.afterStEthFeeIndex > vars.prevStEthFeeIndex) {
return cdpManager.totalCollateralSnapshot() == cdpManager.getSystemCollShares();
}
return true;
}

function invariant_CDPM_12(
SortedCdps sortedCdps,
Vars memory vars
) internal view returns (bool) {
bytes32 currentCdp = sortedCdps.getFirst();

uint256 sumStakes;
while (currentCdp != bytes32(0)) {
sumStakes += cdpManager.getCdpStake(currentCdp);
currentCdp = sortedCdps.getNext(currentCdp);
}

return sumStakes == vars.totalStakesAfter;
}

function invariant_CSP_01(
ICollateralToken collateral,
CollSurplusPool collSurplusPool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ abstract contract PropertiesDescriptions {
string constant CDPM_04 = "CDPM-04: The total system value does not decrease during redemptions";
string constant CDPM_05 = "CDPM-05: Redemptions do not increase the total system debt";
string constant CDPM_06 = "CDPM-06: Redemptions do not increase the total system debt";
string constant CDPM_07 = "CDPM-07: Stake decreases when collShares decreases for a CDP";
string constant CDPM_08 = "CDPM-08: Stake increases when collShares increases for a CDP";
string constant CDPM_09 =
"CDPM-09: expectedStake = coll * totalStakesSnapshot / totalCollateralSnapshot after every operation involving a CDP";
string constant CDPM_10 =
"CDPM-10: totalStakesSnapshot matches totalStakes after an operation, if rebase index changed during the OP";
string constant CDPM_11 =
"CDPM-11: totalCollateralSnapshot matches activePool.systemCollShares after an operation, if rebase index changed during the OP";
string constant CDPM_12 =
"CDPM-12: Sum of all individual CDP stakes should equal to totalStakes";

///////////////////////////////////////////////////////
// Collateral Surplus Pool
Expand Down
Loading

0 comments on commit 5ba2b60

Please sign in to comment.