Skip to content

Commit

Permalink
add end of audit 1 snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
ethereumdegen committed Oct 31, 2024
1 parent fe9e864 commit 43bae3e
Show file tree
Hide file tree
Showing 17 changed files with 435 additions and 1,431 deletions.
59 changes: 5 additions & 54 deletions packages/contracts/contracts/CollateralManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";

// Libraries
import "@openzeppelin/contracts-upgradeable/utils/structs/EnumerableSetUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol";

// Interfaces
import "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC721/IERC721Upgradeable.sol";
Expand All @@ -16,8 +16,7 @@ import "@openzeppelin/contracts-upgradeable/token/ERC721/IERC721ReceiverUpgradea
import "./interfaces/ICollateralManager.sol";
import { Collateral, CollateralType, ICollateralEscrowV1 } from "./interfaces/escrow/ICollateralEscrowV1.sol";
import "./interfaces/ITellerV2.sol";
import "./interfaces/IProtocolPausingManager.sol";
import "./interfaces/IHasProtocolPausingManager.sol";

contract CollateralManager is OwnableUpgradeable, ICollateralManager {
/* Storage */
using EnumerableSetUpgradeable for EnumerableSetUpgradeable.AddressSet;
Expand Down Expand Up @@ -71,21 +70,6 @@ contract CollateralManager is OwnableUpgradeable, ICollateralManager {
_;
}

modifier onlyProtocolOwner() {

address protocolOwner = OwnableUpgradeable(address(tellerV2)).owner();

require(_msgSender() == protocolOwner, "Sender not authorized");
_;
}

modifier whenProtocolNotPaused() {
address pausingManager = IHasProtocolPausingManager( address(tellerV2) ).getProtocolPausingManager();

require( IProtocolPausingManager(address(pausingManager)).protocolPaused() == false , "Protocol is paused");
_;
}

/* External Functions */

/**
Expand Down Expand Up @@ -270,7 +254,7 @@ contract CollateralManager is OwnableUpgradeable, ICollateralManager {
* @notice Withdraws deposited collateral from the created escrow of a bid that has been successfully repaid.
* @param _bidId The id of the bid to withdraw collateral for.
*/
function withdraw(uint256 _bidId) external whenProtocolNotPaused {
function withdraw(uint256 _bidId) external {
BidState bidState = tellerV2.getBidState(_bidId);

require(bidState == BidState.PAID, "collateral cannot be withdrawn");
Expand All @@ -280,25 +264,11 @@ contract CollateralManager is OwnableUpgradeable, ICollateralManager {
emit CollateralClaimed(_bidId);
}

function withdrawDustTokens(
uint256 _bidId,
address _tokenAddress,
uint256 _amount,
address _recipientAddress
) external onlyProtocolOwner whenProtocolNotPaused {

ICollateralEscrowV1(_escrows[_bidId]).withdrawDustTokens(
_tokenAddress,
_amount,
_recipientAddress
);
}

/**
* @notice Withdraws deposited collateral from the created escrow of a bid that has been CLOSED after being defaulted.
* @param _bidId The id of the bid to withdraw collateral for.
*/
function lenderClaimCollateral(uint256 _bidId) external onlyTellerV2 whenProtocolNotPaused {
function lenderClaimCollateral(uint256 _bidId) external onlyTellerV2 {
if (isBidCollateralBacked(_bidId)) {
BidState bidState = tellerV2.getBidState(_bidId);

Expand All @@ -312,24 +282,6 @@ contract CollateralManager is OwnableUpgradeable, ICollateralManager {
}
}

/**
* @notice Withdraws deposited collateral from the created escrow of a bid that has been CLOSED after being defaulted.
* @param _bidId The id of the bid to withdraw collateral for.
*/
function lenderClaimCollateralWithRecipient(uint256 _bidId, address _collateralRecipient) external onlyTellerV2 whenProtocolNotPaused {
if (isBidCollateralBacked(_bidId)) {
BidState bidState = tellerV2.getBidState(_bidId);

require(
bidState == BidState.CLOSED,
"Loan has not been liquidated"
);

_withdraw(_bidId, _collateralRecipient);
emit CollateralClaimed(_bidId);
}
}

/**
* @notice Sends the deposited collateral to a liquidator of a bid.
* @notice Can only be called by the protocol.
Expand All @@ -339,7 +291,6 @@ contract CollateralManager is OwnableUpgradeable, ICollateralManager {
function liquidateCollateral(uint256 _bidId, address _liquidatorAddress)
external
onlyTellerV2
whenProtocolNotPaused
{
if (isBidCollateralBacked(_bidId)) {
BidState bidState = tellerV2.getBidState(_bidId);
Expand Down Expand Up @@ -635,4 +586,4 @@ contract CollateralManager is OwnableUpgradeable, ICollateralManager {
)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,13 @@
pragma solidity ^0.8.0;

import "../TellerV2MarketForwarder_G3.sol";
import "./extensions/ExtensionsContextUpgradeable.sol";

import "../interfaces/ILenderCommitmentForwarder.sol";
import "../interfaces/ISmartCommitmentForwarder.sol";
import "./LenderCommitmentForwarder_G1.sol";

import "../interfaces/IPausableTimestamp.sol";

import "../interfaces/IHasProtocolPausingManager.sol";

import "../interfaces/IProtocolPausingManager.sol";

import "../oracleprotection/OracleProtectionManager.sol";

import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol";

import "@openzeppelin/contracts-upgradeable/utils/math/MathUpgradeable.sol";


import "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol";

import { CommitmentCollateralType, ISmartCommitment } from "../interfaces/ISmartCommitment.sol";


contract SmartCommitmentForwarder is
ExtensionsContextUpgradeable, //this should always be first for upgradeability
TellerV2MarketForwarder_G3,
PausableUpgradeable, //this does add some storage but AFTER all other storage
ReentrancyGuardUpgradeable, //adds many storage slots so breaks upgradeability
OracleProtectionManager, //uses deterministic storage slots
ISmartCommitmentForwarder,
IPausableTimestamp
{

using MathUpgradeable for uint256;

contract SmartCommitmentForwarder is TellerV2MarketForwarder_G3 {
event ExercisedSmartCommitment(
address indexed smartCommitmentAddress,
address borrower,
Expand All @@ -47,44 +18,9 @@ contract SmartCommitmentForwarder is

error InsufficientBorrowerCollateral(uint256 required, uint256 actual);



modifier onlyProtocolPauser() {

address pausingManager = IHasProtocolPausingManager( _tellerV2 ).getProtocolPausingManager();
require( IProtocolPausingManager( pausingManager ).isPauser(_msgSender()) , "Sender not authorized");
_;
}


modifier onlyProtocolOwner() {
require( Ownable( _tellerV2 ).owner() == _msgSender() , "Sender not authorized");
_;
}

uint256 public liquidationProtocolFeePercent;
uint256 internal lastUnpausedAt;


constructor(address _protocolAddress, address _marketRegistry)
TellerV2MarketForwarder_G3(_protocolAddress, _marketRegistry)
{ }

function initialize() public initializer {
__Pausable_init();
}

function setLiquidationProtocolFeePercent(uint256 _percent)
public onlyProtocolOwner {
//max is 100%
require( _percent <= 10000 , "invalid fee percent" );
liquidationProtocolFeePercent = _percent;
}

function getLiquidationProtocolFeePercent()
public view returns (uint256){
return liquidationProtocolFeePercent ;
}
{}

/**
* @notice Accept the commitment to submitBid and acceptBid using the funds
Expand All @@ -99,7 +35,7 @@ contract SmartCommitmentForwarder is
* @param _loanDuration The overall duration for the loan. Must be longer than market payment cycle duration.
* @return bidId The ID of the loan that was created on TellerV2
*/
function acceptSmartCommitmentWithRecipient(
function acceptCommitmentWithRecipient(
address _smartCommitmentAddress,
uint256 _principalAmount,
uint256 _collateralAmount,
Expand All @@ -108,7 +44,7 @@ contract SmartCommitmentForwarder is
address _recipient,
uint16 _interestRate,
uint32 _loanDuration
) public whenNotPaused nonReentrant returns (uint256 bidId) {
) public returns (uint256 bidId) {
require(
ISmartCommitment(_smartCommitmentAddress)
.getCollateralTokenType() <=
Expand Down Expand Up @@ -217,58 +153,4 @@ contract SmartCommitmentForwarder is

revert("Unknown Collateral Type");
}



/**
* @notice Lets the DAO/owner of the protocol implement an emergency stop mechanism.
*/
function pause() public virtual onlyProtocolPauser whenNotPaused {
_pause();
}

/**
* @notice Lets the DAO/owner of the protocol undo a previously implemented emergency stop.
*/
function unpause() public virtual onlyProtocolPauser whenPaused {
setLastUnpausedAt();
_unpause();
}


function getLastUnpausedAt()
public view
returns (uint256) {


address pausingManager = IHasProtocolPausingManager( _tellerV2 ).getProtocolPausingManager();

return MathUpgradeable.max(
lastUnpausedAt,
IPausableTimestamp(pausingManager).getLastUnpausedAt()
)
;


}


function setLastUnpausedAt() internal {
lastUnpausedAt = block.timestamp;
}


// -----

//Overrides
function _msgSender()
internal
view
virtual
override(ContextUpgradeable, ExtensionsContextUpgradeable)
returns (address sender)
{
return ExtensionsContextUpgradeable._msgSender();
}

}
}
Loading

0 comments on commit 43bae3e

Please sign in to comment.