Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dec 21 '23 last changes before audit. #275

Merged
merged 6 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
1 change: 1 addition & 0 deletions contracts/main/flash-mint/FlashMintModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ contract FlashMintModule is CommonMath, PausableUpgradeable, IERC3156FlashLender
emit LogSetFeeRate(_data);
}

// To show old state from storage and the new state from call data, emits before changing state
function setDecentralizedStatesStatus(bool _status) external onlyOwner {
emit LogDecentralizedStateStatus(isDecentralizedState, _status);
isDecentralizedState = _status;
Expand Down
2 changes: 2 additions & 0 deletions contracts/main/interfaces/IDelayPriceFeed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ interface IDelayPriceFeed is IPriceFeed {

event LogPeekPriceFailed(address indexed _caller, string _reason);

event LogSetAccessControlConfig(address indexed _caller, address _accessControlConfig);

function setTimeDelay(uint256 _second) external;

function timeDelay() external view returns (uint256);
Expand Down
2 changes: 2 additions & 0 deletions contracts/main/interfaces/IFathomCentralizedOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
pragma solidity 0.8.17;

interface IFathomCentralizedOracle {
event LogSetAccessControlConfig(address indexed _caller, address _accessControlConfig);

function getPrice() external view returns (uint256 price, uint256 lastUpdate);
}
1 change: 1 addition & 0 deletions contracts/main/price-feeders/DelayPriceFeedBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ abstract contract DelayPriceFeedBase is PausableUpgradeable, IDelayPriceFeed {
"DelayPriceFeed/msgsender-not-owner"
);
accessControlConfig = IAccessControlConfig(_accessControlConfig);
emit LogSetAccessControlConfig(msg.sender, _accessControlConfig);
}

function setPriceLife(uint256 _second) external onlyOwner {
Expand Down
1 change: 1 addition & 0 deletions contracts/main/price-oracles/FathomPriceOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ contract FathomPriceOracle is Initializable, IFathomCentralizedOracle {
"FathomPriceOracle/msgsender-not-owner"
);
accessControlConfig = IAccessControlConfig(_accessControlConfig);
emit LogSetAccessControlConfig(msg.sender, _accessControlConfig);
}

function setOracle(address _oracle) external onlyOwner {
Expand Down
1 change: 1 addition & 0 deletions contracts/main/price-oracles/PluginPriceOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ contract PluginPriceOracle is Initializable, IFathomCentralizedOracle {
"PluginPriceOracle/msgsender-not-owner"
);
accessControlConfig = IAccessControlConfig(_accessControlConfig);
emit LogSetAccessControlConfig(msg.sender, _accessControlConfig);
}

function setOracle(address _oracle) external onlyOwner {
Expand Down
25 changes: 14 additions & 11 deletions contracts/main/stablecoin-core/BookKeeper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ contract BookKeeper is IBookKeeper, ICagable, IPausable, CommonMath, PausableUpg
event LogAddCollateral(address indexed _caller, address indexed _usr, int256 _amount);
event LogMoveCollateral(address indexed _caller, bytes32 indexed _collateralPoolId, address _src, address indexed _dst, uint256 _amount);
event LogMoveStablecoin(address indexed _caller, address _src, address indexed _dst, uint256 _amount);

event LogAddToWhitelist(address indexed _user);
event LogRemoveFromWhitelist(address indexed _user);
event StablecoinIssuedAmount(uint256 _totalStablecoinIssued, bytes32 indexed _collateralPoolId, uint256 _poolStablecoinIssued);

modifier onlyOwner() {
Expand Down Expand Up @@ -156,7 +157,6 @@ contract BookKeeper is IBookKeeper, ICagable, IPausable, CommonMath, PausableUpg
IAccessControlConfig(_accessControlConfig).hasRole(IAccessControlConfig(_accessControlConfig).OWNER_ROLE(), msg.sender),
"BookKeeper/msgsender-not-owner"
);

accessControlConfig = _accessControlConfig;
emit LogSetAccessControlConfig(msg.sender, _accessControlConfig);
}
Expand Down Expand Up @@ -197,6 +197,7 @@ contract BookKeeper is IBookKeeper, ICagable, IPausable, CommonMath, PausableUpg
/// @dev Emits no events.
function addToWhitelist(address _toBeWhitelistedAddress) external override whenNotPaused {
positionWhitelist[msg.sender][_toBeWhitelistedAddress] = 1;
emit LogAddToWhitelist(_toBeWhitelistedAddress);
}

/// @dev Revokes the allowance from the `toBeRemovedAddress` to adjust the position address of the caller.
Expand All @@ -205,6 +206,7 @@ contract BookKeeper is IBookKeeper, ICagable, IPausable, CommonMath, PausableUpg
/// @dev Emits no events.
function removeFromWhitelist(address _toBeRemovedAddress) external override whenNotPaused {
positionWhitelist[msg.sender][_toBeRemovedAddress] = 0;
emit LogRemoveFromWhitelist(_toBeRemovedAddress);
}

// --- Core Logic ---
Expand All @@ -224,7 +226,8 @@ contract BookKeeper is IBookKeeper, ICagable, IPausable, CommonMath, PausableUpg

/// @notice Moves collateral tokens from one address (often a position address) to another in a specified collateral pool.
/// @dev This function can only be called by an entity with the Collateral Manager role when the BookKeeper contract is not paused.
/// @dev It also requires that the entity making the call has the authority to adjust the position (or any address that holds collateralToken), as determined by `_requireAllowedPositionAdjustment`.
/// @dev It also requires that the entity making the call has the authority to adjust the position (or any address that holds collateralToken),
/// as determined by `_requireAllowedPositionAdjustment`.
/// @param _collateralPoolId The ID of the collateral pool from which the collateral tokens are being moved.
/// @param _src The address from which collateral tokens are being moved.
/// @param _dst The address to which collateral tokens are being moved.
Expand Down Expand Up @@ -416,14 +419,14 @@ contract BookKeeper is IBookKeeper, ICagable, IPausable, CommonMath, PausableUpg
totalUnbackedStablecoin = sub(totalUnbackedStablecoin, _debtValue);
}

/// @notice Settles the system's bad debt of the caller.
/// @dev This function can be called by the SystemDebtEngine, which incurs the system debt. The BookKeeper contract must not be paused.
/// @dev Even though the function has no modifier that restricts access exclusively to SystemDebtEngine,
/// the action of the function—reducing the systemBadDebt of msg.sender—effectively limits the function callers to SystemDebtEngine.
/// @dev To execute this function, the SystemDebtEngine must have enough stablecoin, which typically comes from the protocol's surplus.
/// @dev A successful execution of this function removes the bad debt from the system.
/// @param _value The amount of bad debt to be settled.
////
/// @notice Settles the system's bad debt of the caller.
/// @dev This function can be called by the SystemDebtEngine, which incurs the system debt. The BookKeeper contract must not be paused.
/// @dev Even though the function has no modifier that restricts access exclusively to SystemDebtEngine,
/// the action of the function—reducing the systemBadDebt of msg.sender—effectively limits the function callers to SystemDebtEngine.
/// @dev To execute this function, the SystemDebtEngine must have enough stablecoin, which typically comes from the protocol's surplus.
/// @dev A successful execution of this function removes the bad debt from the system.
/// @param _value The amount of bad debt to be settled.
////
function settleSystemBadDebt(uint256 _value) external override nonReentrant whenNotPaused {
systemBadDebt[msg.sender] -= _value;
stablecoin[msg.sender] -= _value;
Expand Down
8 changes: 6 additions & 2 deletions contracts/main/stablecoin-core/LiquidationEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ contract LiquidationEngine is PausableUpgradeable, ReentrancyGuardUpgradeable, I
// --- Math ---
uint256 internal constant WAD = 10 ** 18;

bytes32 internal deprecated;

IBookKeeper public bookKeeper; // CDP Engine
ISystemDebtEngine public systemDebtEngine; // Debt Engine
uint256 public override live; // Active Flag
mapping(address => bool) public liquidatorsWhitelist;

event LiquidationFail(bytes32 _collateralPoolIds, address _positionAddresses, address _liquidator, string reason);
event LogSetBookKeeper(address indexed _newAddress);
codinghistorian marked this conversation as resolved.
Show resolved Hide resolved
event LogAddToWhitelist(address indexed _user);
event LogRemoveFromWhitelist(address indexed _user);

modifier onlyOwnerOrShowStopper() {
IAccessControlConfig _accessControlConfig = IAccessControlConfig(bookKeeper.accessControlConfig());
Expand Down Expand Up @@ -105,13 +106,15 @@ contract LiquidationEngine is PausableUpgradeable, ReentrancyGuardUpgradeable, I
function addToWhitelist(address _toBeWhitelisted) external onlyOwnerOrGov {
require(_toBeWhitelisted != address(0), "LiquidationEngine/whitelist-invalidAddress");
liquidatorsWhitelist[_toBeWhitelisted] = true;
emit LogAddToWhitelist(_toBeWhitelisted);
}

/// @notice Remove a liquidator from the whitelist
/// @param _toBeRemoved The address to be removed from the whitelist
/// @dev Can only be called by the contract owner or the governance system
function removeFromWhitelist(address _toBeRemoved) external onlyOwnerOrGov {
liquidatorsWhitelist[_toBeRemoved] = false;
emit LogRemoveFromWhitelist(_toBeRemoved);
}

/// @notice Batch liquidate multiple positions
Expand Down Expand Up @@ -208,6 +211,7 @@ contract LiquidationEngine is PausableUpgradeable, ReentrancyGuardUpgradeable, I
function setBookKeeper(address _bookKeeper) external onlyOwner isLive {
require(IBookKeeper(_bookKeeper).totalStablecoinIssued() >= 0, "LiquidationEngine/invalid-bookKeeper"); // Sanity Check Call
bookKeeper = IBookKeeper(_bookKeeper);
emit LogSetBookKeeper(_bookKeeper);
}

// --- Cage ---
Expand Down
2 changes: 2 additions & 0 deletions contracts/main/stablecoin-core/PriceOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ contract PriceOracle is CommonMath, PausableUpgradeable, IPriceOracle, ICagable,
);

event LogSetStableCoinReferencePrice(address indexed _caller, uint256 _data);
event LogSetBookKeeper(address indexed _newAddress);
codinghistorian marked this conversation as resolved.
Show resolved Hide resolved

modifier onlyOwner() {
IAccessControlConfig _accessControlConfig = IAccessControlConfig(bookKeeper.accessControlConfig());
Expand Down Expand Up @@ -82,6 +83,7 @@ contract PriceOracle is CommonMath, PausableUpgradeable, IPriceOracle, ICagable,
function setBookKeeper(address _bookKeeper) external onlyOwner isLive {
require(IBookKeeper(_bookKeeper).totalStablecoinIssued() >= 0, "ShowStopper/invalid-bookKeeper"); // Sanity Check Call
bookKeeper = IBookKeeper(_bookKeeper);
emit LogSetBookKeeper(_bookKeeper);
}

function setStableCoinReferencePrice(uint256 _referencePrice) external onlyOwner isLive {
Expand Down
2 changes: 2 additions & 0 deletions contracts/main/stablecoin-core/StabilityFeeCollector.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ contract StabilityFeeCollector is CommonMath, PausableUpgradeable, ReentrancyGua
address public systemDebtEngine;

event LogSetSystemDebtEngine(address indexed _caller, address _data);
event LogNewDebtAccumulatedRate(uint256 _newDebtAccumulatedRate);

modifier onlyOwner() {
IAccessControlConfig _accessControlConfig = IAccessControlConfig(bookKeeper.accessControlConfig());
Expand Down Expand Up @@ -79,6 +80,7 @@ contract StabilityFeeCollector is CommonMath, PausableUpgradeable, ReentrancyGua
/// @return _debtAccumulatedRate Updated debtAccumulatedRate for the specified collateral pool.
function collect(bytes32 _collateralPool) external override whenNotPaused nonReentrant returns (uint256 _debtAccumulatedRate) {
_debtAccumulatedRate = _collect(_collateralPool);
emit LogNewDebtAccumulatedRate(_debtAccumulatedRate);
}

/// @dev Internal function to collect the stability fee of the specified collateral pool.
Expand Down
1 change: 1 addition & 0 deletions contracts/main/stablecoin-core/StableSwapModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ contract StableSwapModule is PausableUpgradeable, ReentrancyGuardUpgradeable, IS
emit LogSetFeeOut(msg.sender, _feeOut);
}

// To show old state from storage and the new state from call data, emits before changing state
function setDecentralizedStatesStatus(bool _status) external onlyOwner {
emit LogDecentralizedStateStatus(isDecentralizedState, _status);
isDecentralizedState = _status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,17 @@ contract CollateralTokenAdapter is CommonMath, ICollateralAdapter, PausableUpgra

IVault public vault;

/// @dev deprecated but needs to be kept to minimize storage layout confusion
bytes32 internal deprecated2;
IProxyRegistry public proxyWalletFactory;

/// @dev Total CollateralTokens that has been staked in WAD
uint256 public totalShare;

/// @dev deprecated but needs to be kept to minimize storage layout confusion
bytes32 internal deprecated;

mapping(address => bool) public whiteListed;

event LogDeposit(uint256 _val);
event LogWithdraw(uint256 _val);
event LogWhitelisted(address indexed _user, bool _isWhitelisted);
event LogAddToWhitelist(address indexed _user);
event LogRemoveFromWhitelist(address indexed _user);
event LogEmergencyWithdraw(address indexed _caller, address _to);

modifier onlyOwner() {
Expand Down Expand Up @@ -89,7 +85,7 @@ contract CollateralTokenAdapter is CommonMath, ICollateralAdapter, PausableUpgra
function addToWhitelist(address _toBeWhitelisted) external onlyOwnerOrGov {
require(_toBeWhitelisted != address(0), "CollateralTokenAdapter/whitelist-invalidAdds");
whiteListed[_toBeWhitelisted] = true;
emit LogWhitelisted(_toBeWhitelisted, true);
emit LogAddToWhitelist(_toBeWhitelisted);
}

/// @notice Removes an address from the whitelist
Expand All @@ -98,7 +94,7 @@ contract CollateralTokenAdapter is CommonMath, ICollateralAdapter, PausableUpgra
function removeFromWhitelist(address _toBeRemoved) external onlyOwnerOrGov {
require(_toBeRemoved != address(0), "CollateralTokenAdapter/removeFromWL-invalidAdds");
whiteListed[_toBeRemoved] = false;
emit LogWhitelisted(_toBeRemoved, false);
emit LogRemoveFromWhitelist(_toBeRemoved);
}

/// @dev The `cage` function permanently halts the `collateralTokenAdapter` contract.
Expand Down
29 changes: 15 additions & 14 deletions contracts/main/stablecoin-core/config/CollateralPoolConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,21 @@ contract CollateralPoolConfig is Initializable, ICollateralPoolConfig {
_collateralPools[_poolId].liquidationRatio = _liquidationRatio;
emit LogSetLiquidationRatio(msg.sender, _poolId, _liquidationRatio);
}
/// @dev Set the stability fee rate of the collateral pool.
/// The rate to be set here is the `r` in:
/// r^N = APR
/// Where:
/// r = stability fee rate
/// N = Accumulation frequency which is per-second in this case; the value will be 60*60*24*365 = 31536000 to signify the number of seconds within a year.
/// APR = the annual percentage rate
/// For example, to achieve 0.5% APR for stability fee rate:
/// r^31536000 = 1.005
/// Find the 31536000th root of 1.005 and we will get:
/// r = 1.000000000158153903837946258002097...
/// The rate is in [ray] format, so the actual value of `stabilityFeeRate` will be:
/// stabilityFeeRate = 1000000000158153903837946258
/// The above `stabilityFeeRate` will be the value we will use in this contract.

/// @dev Set the stability fee rate of the collateral pool.
/// The rate to be set here is the `r` in:
/// r^N = APR
/// Where:
/// r = stability fee rate
/// N = Accumulation frequency which is per-second in this case; the value will be 60*60*24*365 = 31536000 to signify the number of seconds within a year.
/// APR = the annual percentage rate
/// For example, to achieve 0.5% APR for stability fee rate:
/// r^31536000 = 1.005
/// Find the 31536000th root of 1.005 and we will get:
/// r = 1.000000000158153903837946258002097...
/// The rate is in [ray] format, so the actual value of `stabilityFeeRate` will be:
/// stabilityFeeRate = 1000000000158153903837946258
/// The above `stabilityFeeRate` will be the value we will use in this contract.
function setStabilityFeeRate(bytes32 _collateralPool, uint256 _stabilityFeeRate) external onlyOwner {
require(_stabilityFeeRate >= RAY, "CollateralPoolConfig/invalid-stability-fee-rate");
// Maximum stability fee rate is 50% yearly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ contract FixedSpreadLiquidationStrategy is CommonMath, PausableUpgradeable, Reen
uint256 _treasuryFees
);
event LogSetFlashLendingEnabled(address indexed _caller, uint256 _flashLendingEnabled);
event LogSetBookKeeper(address indexed _newAddress);
codinghistorian marked this conversation as resolved.
Show resolved Hide resolved

modifier onlyOwnerOrGov() {
IAccessControlConfig _accessControlConfig = IAccessControlConfig(bookKeeper.accessControlConfig());
Expand Down Expand Up @@ -256,6 +257,7 @@ contract FixedSpreadLiquidationStrategy is CommonMath, PausableUpgradeable, Reen
function setBookKeeper(address _bookKeeper) external onlyOwner {
require(IBookKeeper(_bookKeeper).totalStablecoinIssued() >= 0, "FixedSpreadLiquidationStrategy/invalid-bookKeeper"); // Sanity Check Call
bookKeeper = IBookKeeper(_bookKeeper);
emit LogSetBookKeeper(_bookKeeper);
}

function setLiquidationEngine(address _liquidationEngine) external onlyOwner {
Expand Down
7 changes: 4 additions & 3 deletions contracts/tests/mocks/MockCollateralTokenAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,13 @@ contract MockCollateralTokenAdapter is MockCollateralTokenAdapterMath, ICollater
/// @dev Total CollateralTokens that has been staked in WAD
uint256 public totalShare;

/// @dev deprecated but needs to be kept to minimize storage layout confusion
bytes32 deprecated;

mapping(address => bool) public whiteListed;

event LogDeposit(uint256 _val);
event LogWithdraw(uint256 _val);
event LogEmergencyWithdraw(address indexed _caller, address _to);
event LogAddToWhitelist(address indexed _user);
event LogRemoveFromWhitelist(address indexed _user);

modifier onlyOwner() {
IAccessControlConfig _accessControlConfig = IAccessControlConfig(bookKeeper.accessControlConfig());
Expand Down Expand Up @@ -135,11 +134,13 @@ contract MockCollateralTokenAdapter is MockCollateralTokenAdapterMath, ICollater
function addToWhitelist(address _toBeWhitelisted) external onlyOwnerOrGov {
require(_toBeWhitelisted != address(0), "AnkrColadapter/whitelist-invalidAdds");
whiteListed[_toBeWhitelisted] = true;
emit LogAddToWhitelist(_toBeWhitelisted);
}

function removeFromWhitelist(address _toBeRemoved) external onlyOwnerOrGov {
require(_toBeRemoved != address(0), "CollateralTokenAdapter/removeFromWL-invalidAdds");
whiteListed[_toBeRemoved] = false;
emit LogRemoveFromWhitelist(_toBeRemoved);
}

/// @dev Cage function halts MockCollateralTokenAdapter contract for good.
Expand Down
2 changes: 2 additions & 0 deletions contracts/tests/mocks/ParanoidDexOraclePriceFeed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ contract ParanoidDexOraclePriceFeed is PausableUpgradeable, IPriceFeed {
address[] public verificationFeeds;

event LogSetTimeDelay(address indexed _caller, uint256 _second);
event LogSetAccessControlConfig(address indexed _caller, address _accessControlConfig);

modifier onlyOwner() {
require(accessControlConfig.hasRole(accessControlConfig.OWNER_ROLE(), msg.sender), "!ownerRole");
Expand Down Expand Up @@ -83,6 +84,7 @@ contract ParanoidDexOraclePriceFeed is PausableUpgradeable, IPriceFeed {
"FathomOraclePriceFeed/msgsender-not-owner"
);
accessControlConfig = IAccessControlConfig(_accessControlConfig);
emit LogSetAccessControlConfig(msg.sender, _accessControlConfig);
}

function addVerificationFeed(address _priceFeed) external onlyOwner {
Expand Down
2 changes: 0 additions & 2 deletions scripts/tests/integration/CollateralTokenAdapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ describe("CollateralTokenAdapter", () => {

expect(await collateralTokenAdapter.totalShare()).to.be.eq(ethers.utils.parseEther("1"))

// expect(await bookKeeper.collateralToken(COLLATERAL_POOL_ID, AliceAddress).to.be.eq(ethers.utils.parseEther("1")))
expect(await bookKeeper.collateralToken(collateralPoolIdFromAdapter, AliceAddress)).to.be.eq(ethers.utils.parseEther("1"))


Expand All @@ -177,7 +176,6 @@ describe("CollateralTokenAdapter", () => {
expect(await collateralTokenAdapter.totalShare()).to.be.eq(ethers.utils.parseEther("5"))
expect(await bookKeeper.collateralToken(collateralPoolIdFromAdapter, AliceAddress)).to.be.eq(ethers.utils.parseEther("1"))

// expect(await bookKeeper.collateralToken(COLLATERAL_POOL_ID, BobAddress).to.be.eq(ethers.utils.parseEther("4")))
expect(await bookKeeper.collateralToken(collateralPoolIdFromAdapter, BobAddress)).to.be.eq(ethers.utils.parseEther("4"))


Expand Down
Loading
Loading