Skip to content

Commit

Permalink
chore: rc-2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
wei3erHase authored Aug 21, 2023
2 parents 89a3e8a + 50df3bf commit d1c26b6
Show file tree
Hide file tree
Showing 70 changed files with 3,367 additions and 5,593 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"postinstall": "forge install",
"lint:check": "yarn lint:sol && forge fmt check",
"lint:fix": "sort-package-json && forge fmt && yarn lint:sol --fix",
"lint:sol": "cross-env solhint 'src/contracts/**/*.sol' 'src/interfaces/**/*.sol' 'src/test/**/*.sol'",
"lint:sol": "cross-env solhint 'src/contracts/**/*.sol' 'src/libraries/**/*.sol' 'src/script/**/*.sol' 'src/interfaces/**/*.sol' 'src/test/**/*.sol'",
"prepare": "husky install",
"script:goerli": "bash -c 'source .env && forge script GoerliScript --with-gas-price 2000000000 -vvvvv --rpc-url $OP_GOERLI_RPC --broadcast --private-key $OP_GOERLI_GOVERNOR_PK --verify --etherscan-api-key $OP_ETHERSCAN_API_KEY'",
"script:goerli:delegate": "bash -c 'source .env && forge script GoerliDelegate --with-gas-price 2000000000 -vvvvv --rpc-url $OP_GOERLI_RPC --broadcast --private-key $OP_GOERLI_GOVERNOR_PK --verify --etherscan-api-key $OP_ETHERSCAN_API_KEY'",
Expand Down
667 changes: 147 additions & 520 deletions src/contracts/CollateralAuctionHouse.sol

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/contracts/DebtAuctionHouse.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ contract DebtAuctionHouse is Authorizable, Modifiable, Disableable, IDebtAuction

emit StartAuction({
_id: _id,
_auctioneer: msg.sender,
_blockTimestamp: block.timestamp,
_amountToSell: _amountToSell,
_amountToRaise: _initialBid,
Expand Down Expand Up @@ -209,7 +210,7 @@ contract DebtAuctionHouse is Authorizable, Modifiable, Disableable, IDebtAuction

// --- Administration ---

function _modifyParameters(bytes32 _param, bytes memory _data) internal override whenEnabled {
function _modifyParameters(bytes32 _param, bytes memory _data) internal override {
address _address = _data.toAddress();
uint256 _uint256 = _data.toUint256();

Expand Down
78 changes: 44 additions & 34 deletions src/contracts/LiquidationEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,19 @@ contract LiquidationEngine is Authorizable, Modifiable, Disableable, ReentrancyG
ISAFEEngine.SAFEEngineCollateralData memory _safeEngCData = safeEngine.cData(_cType);
ISAFEEngine.SAFE memory _safeData = safeEngine.safes(_cType, _safe);

if (
_safeEngCData.liquidationPrice == 0
|| _safeData.lockedCollateral * _safeEngCData.liquidationPrice
>= _safeData.generatedDebt * _safeEngCData.accumulatedRate
) revert LiqEng_SAFENotUnsafe();
if (
currentOnAuctionSystemCoins >= _params.onAuctionSystemCoinLimit
|| _params.onAuctionSystemCoinLimit - currentOnAuctionSystemCoins < _debtFloor
) revert LiqEng_LiquidationLimitHit();
// --- Safety checks ---
{
if (
_safeEngCData.liquidationPrice == 0
|| _safeData.lockedCollateral * _safeEngCData.liquidationPrice
>= _safeData.generatedDebt * _safeEngCData.accumulatedRate
) revert LiqEng_SAFENotUnsafe();

if (
currentOnAuctionSystemCoins >= _params.onAuctionSystemCoinLimit
|| _params.onAuctionSystemCoinLimit - currentOnAuctionSystemCoins < _debtFloor
) revert LiqEng_LiquidationLimitHit();
}

if (chosenSAFESaviour[_cType][_safe] != address(0) && safeSaviours[chosenSAFESaviour[_cType][_safe]] == 1) {
try ISAFESaviour(chosenSAFESaviour[_cType][_safe]).saveSAFE(msg.sender, _cType, _safe) returns (
Expand All @@ -131,10 +135,15 @@ contract LiquidationEngine is Authorizable, Modifiable, Disableable, ReentrancyG
if (_ok && _collateralAddedOrDebtRepaid > 0) {
// Checks that the saviour didn't take collateral or add more debt to the SAFE
ISAFEEngine.SAFE memory _newSafeData = safeEngine.safes(_cType, _safe);
if (
_newSafeData.lockedCollateral < _safeData.lockedCollateral
|| _newSafeData.generatedDebt > _safeData.generatedDebt
) revert LiqEng_InvalidSAFESaviourOperation();

// --- Safety checks ---
{
if (
_newSafeData.lockedCollateral < _safeData.lockedCollateral
|| _newSafeData.generatedDebt > _safeData.generatedDebt
) revert LiqEng_InvalidSAFESaviourOperation();
}

_safeEngCData = safeEngine.cData(_cType);
_safeData = _newSafeData;
emit SaveSAFE(_cType, _safe, _collateralAddedOrDebtRepaid);
Expand All @@ -145,30 +154,33 @@ contract LiquidationEngine is Authorizable, Modifiable, Disableable, ReentrancyG
}

if (
(_safeEngCData.liquidationPrice > 0)
&& (
_safeData.lockedCollateral * _safeEngCData.liquidationPrice
< _safeData.generatedDebt * _safeEngCData.accumulatedRate
)
_safeData.lockedCollateral * _safeEngCData.liquidationPrice
< _safeData.generatedDebt * _safeEngCData.accumulatedRate
) {
LiquidationEngineCollateralParams memory __cParams = _cParams[_cType];

uint256 _limitAdjustedDebt = Math.min(
_safeData.generatedDebt,
Math.min(__cParams.liquidationQuantity, _params.onAuctionSystemCoinLimit - currentOnAuctionSystemCoins) * WAD
/ _safeEngCData.accumulatedRate / __cParams.liquidationPenalty
Math.min(__cParams.liquidationQuantity, _params.onAuctionSystemCoinLimit - currentOnAuctionSystemCoins).wdiv(
_safeEngCData.accumulatedRate
) / __cParams.liquidationPenalty
);

if (_limitAdjustedDebt == 0) revert LiqEng_NullAuction();
if (
_limitAdjustedDebt != _safeData.generatedDebt
&& (_safeData.generatedDebt - _limitAdjustedDebt) * _safeEngCData.accumulatedRate < _debtFloor
) revert LiqEng_DustySAFE();

uint256 _collateralToSell =
Math.min(_safeData.lockedCollateral, _safeData.lockedCollateral * _limitAdjustedDebt / _safeData.generatedDebt);
uint256 _amountToRaise = (_limitAdjustedDebt * _safeEngCData.accumulatedRate).wmul(__cParams.liquidationPenalty);

if (_collateralToSell == 0) revert LiqEng_NullCollateralToSell();
// --- Safety checks ---
{
if (_limitAdjustedDebt == 0) revert LiqEng_NullAuction();

if (_collateralToSell == 0) revert LiqEng_NullCollateralToSell();

if (
_limitAdjustedDebt != _safeData.generatedDebt
&& (_safeData.generatedDebt - _limitAdjustedDebt) * _safeEngCData.accumulatedRate < _debtFloor
) revert LiqEng_DustySAFE();
}

safeEngine.confiscateSAFECollateralAndDebt({
_cType: _cType,
Expand All @@ -181,9 +193,6 @@ contract LiquidationEngine is Authorizable, Modifiable, Disableable, ReentrancyG

accountingEngine.pushDebtToQueue(_limitAdjustedDebt * _safeEngCData.accumulatedRate);

// This calcuation will overflow if multiply(limitAdjustedDebt, accumulatedRate) exceeds ~10^14,
// i.e. the maximum amountToRaise is roughly 100 trillion system coins.
uint256 _amountToRaise = _limitAdjustedDebt * _safeEngCData.accumulatedRate * __cParams.liquidationPenalty / WAD;
currentOnAuctionSystemCoins += _amountToRaise;

_auctionId = ICollateralAuctionHouse(__cParams.collateralAuctionHouse).startAuction({
Expand Down Expand Up @@ -214,7 +223,7 @@ contract LiquidationEngine is Authorizable, Modifiable, Disableable, ReentrancyG
function initializeCollateralType(
bytes32 _cType,
LiquidationEngineCollateralParams memory _liqEngineCParams
) external isAuthorized validCParams(_cType) {
) external isAuthorized whenEnabled validCParams(_cType) {
if (!_collateralList.add(_cType)) revert LiqEng_CollateralTypeAlreadyInitialized();
_setCollateralAuctionHouse(_cType, _liqEngineCParams.collateralAuctionHouse);
_cParams[_cType] = _liqEngineCParams;
Expand Down Expand Up @@ -245,8 +254,9 @@ contract LiquidationEngine is Authorizable, Modifiable, Disableable, ReentrancyG

return Math.min(
_generatedDebt,
Math.min(__cParams.liquidationQuantity, _params.onAuctionSystemCoinLimit - currentOnAuctionSystemCoins) * WAD
/ _accumulatedRate / __cParams.liquidationPenalty
Math.min(__cParams.liquidationQuantity, _params.onAuctionSystemCoinLimit - currentOnAuctionSystemCoins).wdiv(
_accumulatedRate
) / __cParams.liquidationPenalty
);
}

Expand All @@ -262,7 +272,7 @@ contract LiquidationEngine is Authorizable, Modifiable, Disableable, ReentrancyG
else revert UnrecognizedParam();
}

function _modifyParameters(bytes32 _cType, bytes32 _param, bytes memory _data) internal override {
function _modifyParameters(bytes32 _cType, bytes32 _param, bytes memory _data) internal override whenEnabled {
uint256 _uint256 = _data.toUint256();

if (!_collateralList.contains(_cType)) revert UnrecognizedCType();
Expand Down
18 changes: 8 additions & 10 deletions src/contracts/OracleRelayer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ contract OracleRelayer is Authorizable, Modifiable, Disableable, IOracleRelayer
}

/**
* @notice Fetch the last recorded redemptionPrice
* @dev To be used when having the absolute latest redemptionPrice is irrelevant
* @notice Calculate the current redemption price
* @dev To be used when requiring a view, not to be used in transactions use `redemptionPrice` instead
*/
function lastRedemptionPrice() external view returns (uint256 _lastRedemptionPrice) {
return _redemptionPrice;
function calcRedemptionPrice() external view returns (uint256 _virtualRedemptionPrice) {
return redemptionRate.rpow(block.timestamp - redemptionPriceUpdateTime).rmul(_redemptionPrice);
}

// --- Redemption Price Update ---
Expand Down Expand Up @@ -113,13 +113,11 @@ contract OracleRelayer is Authorizable, Modifiable, Disableable, IOracleRelayer
(uint256 _priceFeedValue, bool _hasValidValue) = _cParams[_cType].oracle.getResultWithValidity();
uint256 _updatedRedemptionPrice = _getRedemptionPrice();

uint256 _safetyPrice = _hasValidValue
? (uint256(_priceFeedValue) * uint256(10 ** 9)).rdiv(_updatedRedemptionPrice).rdiv(_cParams[_cType].safetyCRatio)
: 0;
uint256 _safetyPrice =
_hasValidValue ? (_priceFeedValue * 1e9).rdiv(_updatedRedemptionPrice).rdiv(_cParams[_cType].safetyCRatio) : 0;

uint256 _liquidationPrice = _hasValidValue
? (uint256(_priceFeedValue) * uint256(10 ** 9)).rdiv(_updatedRedemptionPrice).rdiv(
_cParams[_cType].liquidationCRatio
)
? (_priceFeedValue * 1e9).rdiv(_updatedRedemptionPrice).rdiv(_cParams[_cType].liquidationCRatio)
: 0;

safeEngine.updateCollateralPrice(_cType, _safetyPrice, _liquidationPrice);
Expand Down
6 changes: 3 additions & 3 deletions src/contracts/SAFEEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ contract SAFEEngine is Authorizable, Modifiable, Disableable, ISAFEEngine {
function initializeCollateralType(
bytes32 _cType,
SAFEEngineCollateralParams memory _safeEngineCParams
) external isAuthorized {
) external isAuthorized whenEnabled {
if (!_collateralList.add(_cType)) revert SAFEEng_CollateralTypeAlreadyExists();
_cData[_cType].accumulatedRate = RAY;
_cParams[_cType] = _safeEngineCParams;
Expand Down Expand Up @@ -437,15 +437,15 @@ contract SAFEEngine is Authorizable, Modifiable, Disableable, ISAFEEngine {
}

// --- Administration ---
function _modifyParameters(bytes32 _param, bytes memory _data) internal override whenEnabled {
function _modifyParameters(bytes32 _param, bytes memory _data) internal override {
uint256 _uint256 = _data.toUint256();

if (_param == 'globalDebtCeiling') _params.globalDebtCeiling = _uint256;
else if (_param == 'safeDebtCeiling') _params.safeDebtCeiling = _uint256;
else revert UnrecognizedParam();
}

function _modifyParameters(bytes32 _cType, bytes32 _param, bytes memory _data) internal override whenEnabled {
function _modifyParameters(bytes32 _cType, bytes32 _param, bytes memory _data) internal override {
uint256 _uint256 = _data.toUint256();

if (!_collateralList.contains(_cType)) revert UnrecognizedCType();
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/StabilityFeeTreasury.sol
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ contract StabilityFeeTreasury is Authorizable, Modifiable, Disableable, IStabili

// --- Administration ---

function _modifyParameters(bytes32 _param, bytes memory _data) internal override whenEnabled {
function _modifyParameters(bytes32 _param, bytes memory _data) internal override {
uint256 _uint256 = _data.toUint256();

if (_param == 'extraSurplusReceiver') extraSurplusReceiver = _data.toAddress();
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/SurplusAuctionHouse.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ contract SurplusAuctionHouse is Authorizable, Modifiable, Disableable, ISurplusA
using SafeERC20 for IProtocolToken;

bytes32 public constant AUCTION_HOUSE_TYPE = bytes32('SURPLUS');
bytes32 public constant SURPLUS_AUCTION_TYPE = bytes32('MIXED-STRAT');

// --- Data ---
// Data for each separate auction
Expand Down Expand Up @@ -97,6 +96,7 @@ contract SurplusAuctionHouse is Authorizable, Modifiable, Disableable, ISurplusA

emit StartAuction({
_id: _id,
_auctioneer: msg.sender,
_blockTimestamp: block.timestamp,
_amountToSell: _amountToSell,
_amountToRaise: _initialBid,
Expand Down
57 changes: 23 additions & 34 deletions src/contracts/factories/CollateralAuctionHouseChild.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {IOracleRelayer} from '@interfaces/IOracleRelayer.sol';
import {CollateralAuctionHouse, ICollateralAuctionHouse} from '@contracts/CollateralAuctionHouse.sol';

import {AuthorizableChild, Authorizable} from '@contracts/factories/AuthorizableChild.sol';
import {DisableableChild, Disableable} from '@contracts/factories/DisableableChild.sol';

import {Math, RAY, WAD} from '@libraries/Math.sol';
import {EnumerableSet} from '@openzeppelin/utils/structs/EnumerableSet.sol';
Expand All @@ -17,49 +18,32 @@ import {EnumerableSet} from '@openzeppelin/utils/structs/EnumerableSet.sol';
* @title CollateralAuctionHouseChild
* @notice This contract inherits all the functionality of `CollateralAuctionHouse.sol` to be factory deployed
*/
contract CollateralAuctionHouseChild is AuthorizableChild, CollateralAuctionHouse, ICollateralAuctionHouseChild {
contract CollateralAuctionHouseChild is
DisableableChild,
AuthorizableChild,
CollateralAuctionHouse,
ICollateralAuctionHouseChild
{
using EnumerableSet for EnumerableSet.AddressSet;
using Math for uint256;

// --- Init ---
constructor(
address _safeEngine,
address _oracleRelayer,
address _liquidationEngine,
address _oracleRelayer,
bytes32 _cType,
CollateralAuctionHouseSystemCoinParams memory _cahParams,
CollateralAuctionHouseParams memory _cahCParams
CollateralAuctionHouseParams memory _cahParams
)
CollateralAuctionHouse(
_safeEngine,
_oracleRelayer, // empty
_liquidationEngine, // empty
_oracleRelayer, // empty
_cType,
_cahParams, // empty
_cahCParams
_cahParams
)
{}

// NOTE: child implementation reads params from factory
function params()
public
view
override(CollateralAuctionHouse, ICollateralAuctionHouse)
returns (CollateralAuctionHouseSystemCoinParams memory _cahParams)
{
return ICollateralAuctionHouseFactory(factory).params();
}

// solhint-disable-next-line private-vars-leading-underscore
function _params()
public
view
override(CollateralAuctionHouse, ICollateralAuctionHouse)
returns (uint256 _minSystemCoinDeviation, uint256 _lowerSystemCoinDeviation, uint256 _upperSystemCoinDeviation)
{
return ICollateralAuctionHouseFactory(factory)._params();
}

// NOTE: child implementation reads liquidationEngine from factory
function liquidationEngine()
public
Expand All @@ -70,9 +54,6 @@ contract CollateralAuctionHouseChild is AuthorizableChild, CollateralAuctionHous
return ILiquidationEngine(ICollateralAuctionHouseFactory(factory).liquidationEngine());
}

// NOTE: avoids adding authorization to address(0) on constructor
function _setLiquidationEngine(address _newLiquidationEngine) internal override {}

// NOTE: child implementation reads oracleRelayer from factory
function oracleRelayer()
public
Expand All @@ -83,10 +64,10 @@ contract CollateralAuctionHouseChild is AuthorizableChild, CollateralAuctionHous
return IOracleRelayer(ICollateralAuctionHouseFactory(factory).oracleRelayer());
}

// NOTE: global parameters are stored/modified in the factory
function _modifyParameters(bytes32, bytes memory) internal pure override {
revert UnrecognizedParam();
}
// NOTE: ignores modifying liquidationEngine's address (read from factory)
function _setLiquidationEngine(address _newLiquidationEngine) internal override {}
// NOTE: ignores modifying oracleRelayer's address (read from factory)
function _setOracleRelayer(address _newLiquidationEngine) internal override {}

function _isAuthorized(address _account)
internal
Expand All @@ -96,4 +77,12 @@ contract CollateralAuctionHouseChild is AuthorizableChild, CollateralAuctionHous
{
return super._isAuthorized(_account);
}

function _isEnabled() internal view override(DisableableChild, Disableable) returns (bool _enabled) {
return super._isEnabled();
}

function _onContractDisable() internal override(DisableableChild, Disableable) {
super._onContractDisable();
}
}
Loading

0 comments on commit d1c26b6

Please sign in to comment.