Skip to content

Commit

Permalink
contracts at snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
ethereumdegen committed Nov 8, 2024
1 parent 43bae3e commit fd9ba4c
Show file tree
Hide file tree
Showing 51 changed files with 659 additions and 2,212 deletions.
54 changes: 50 additions & 4 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 Down Expand Up @@ -70,6 +70,19 @@ contract CollateralManager is OwnableUpgradeable, ICollateralManager {
_;
}

modifier onlyProtocolOwner() {

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

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

modifier whenProtocolNotPaused() {
require( PausableUpgradeable(address(tellerV2)).paused() == false , "Protocol is paused");
_;
}

/* External Functions */

/**
Expand Down Expand Up @@ -254,7 +267,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 {
function withdraw(uint256 _bidId) external whenProtocolNotPaused {
BidState bidState = tellerV2.getBidState(_bidId);

require(bidState == BidState.PAID, "collateral cannot be withdrawn");
Expand All @@ -264,11 +277,25 @@ 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 {
function lenderClaimCollateral(uint256 _bidId) external onlyTellerV2 whenProtocolNotPaused {
if (isBidCollateralBacked(_bidId)) {
BidState bidState = tellerV2.getBidState(_bidId);

Expand All @@ -282,6 +309,24 @@ 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 @@ -291,6 +336,7 @@ 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 @@ -586,4 +632,4 @@ contract CollateralManager is OwnableUpgradeable, ICollateralManager {
)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ contract LenderCommitmentForwarder_G2 is
);

require(
commitmentPrincipalAccepted[bidId] <= commitment.maxPrincipal,
commitmentPrincipalAccepted[_commitmentId] <= commitment.maxPrincipal,
"Invalid loan max principal"
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ import "./extensions/ExtensionsContextUpgradeable.sol";

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";



/*
Only do decimal expansion if it is an ERC20 not anything else !!
*/

contract LenderCommitmentForwarder_U1 is
ExtensionsContextUpgradeable, //this should always be first for upgradeability
TellerV2MarketForwarder_G2,
Expand Down Expand Up @@ -194,23 +186,17 @@ contract LenderCommitmentForwarder_U1 is
uint16 _poolOracleLtvRatio //generally always between 0 and 100 % , 0 to 10000
) public returns (uint256 commitmentId_) {
commitmentId_ = commitmentCount++;

require(
_commitment.lender == _msgSender(),
"unauthorized commitment creator"
);

commitments[commitmentId_] = _commitment;

require(_poolRoutes.length == 0 || _commitment.collateralTokenType == CommitmentCollateralType.ERC20 , "can only use pool routes with ERC20 collateral");


//routes length of 0 means ignore price oracle limits
require(_poolRoutes.length <= 2, "invalid pool routes length");




for (uint256 i = 0; i < _poolRoutes.length; i++) {
commitmentUniswapPoolRoutes[commitmentId_].push(_poolRoutes[i]);
}
Expand Down Expand Up @@ -535,7 +521,7 @@ contract LenderCommitmentForwarder_U1 is
);

require(
commitmentPrincipalAccepted[bidId] <= commitment.maxPrincipal,
commitmentPrincipalAccepted[_commitmentId] <= commitment.maxPrincipal,
"Invalid loan max principal"
);

Expand Down Expand Up @@ -662,26 +648,13 @@ contract LenderCommitmentForwarder_U1 is
return 0;
}

if (_collateralTokenType == CommitmentCollateralType.ERC20) {
return
return
MathUpgradeable.mulDiv(
_principalAmount,
STANDARD_EXPANSION_FACTOR,
_maxPrincipalPerCollateralAmount,
MathUpgradeable.Rounding.Up
);
}

//for NFTs, do not use the uniswap expansion factor
return
MathUpgradeable.mulDiv(
_principalAmount,
1,
_maxPrincipalPerCollateralAmount,
MathUpgradeable.Rounding.Up
);


}

/**
Expand Down Expand Up @@ -814,8 +787,8 @@ contract LenderCommitmentForwarder_U1 is
(sqrtPriceX96, , , , , , ) = IUniswapV3Pool(uniswapV3Pool).slot0();
} else {
uint32[] memory secondsAgos = new uint32[](2);
secondsAgos[0] = twapInterval + 1; // from (before)
secondsAgos[1] = 1; // one block prior
secondsAgos[0] = twapInterval; // from (before)
secondsAgos[1] = 0; // to (now)

(int56[] memory tickCumulatives, ) = IUniswapV3Pool(uniswapV3Pool)
.observe(secondsAgos);
Expand Down Expand Up @@ -915,24 +888,6 @@ contract LenderCommitmentForwarder_U1 is
return commitments[_commitmentId].maxPrincipal;
}

function getCommitmentPrincipalTokenAddress(uint256 _commitmentId)
external
view
returns (address)
{
return commitments[_commitmentId].principalTokenAddress;
}

function getCommitmentCollateralTokenAddress(uint256 _commitmentId)
external
view
returns (address)
{
return commitments[_commitmentId].collateralTokenAddress;
}



//Overrides
function _msgSender()
internal
Expand Down
Loading

0 comments on commit fd9ba4c

Please sign in to comment.