Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
ethereumdegen committed Nov 8, 2024
1 parent fd9ba4c commit 51613c5
Show file tree
Hide file tree
Showing 58 changed files with 3,034 additions and 659 deletions.
54 changes: 4 additions & 50 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,19 +70,6 @@ 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 @@ -267,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 @@ -277,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 @@ -309,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 @@ -336,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 @@ -632,4 +586,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[_commitmentId] <= commitment.maxPrincipal,
commitmentPrincipalAccepted[bidId] <= commitment.maxPrincipal,
"Invalid loan max principal"
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ 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 @@ -186,17 +194,23 @@ 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 @@ -521,7 +535,7 @@ contract LenderCommitmentForwarder_U1 is
);

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

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

return
if (_collateralTokenType == CommitmentCollateralType.ERC20) {
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 @@ -787,8 +814,8 @@ contract LenderCommitmentForwarder_U1 is
(sqrtPriceX96, , , , , , ) = IUniswapV3Pool(uniswapV3Pool).slot0();
} else {
uint32[] memory secondsAgos = new uint32[](2);
secondsAgos[0] = twapInterval; // from (before)
secondsAgos[1] = 0; // to (now)
secondsAgos[0] = twapInterval + 1; // from (before)
secondsAgos[1] = 1; // one block prior

(int56[] memory tickCumulatives, ) = IUniswapV3Pool(uniswapV3Pool)
.observe(secondsAgos);
Expand Down Expand Up @@ -888,6 +915,24 @@ 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 51613c5

Please sign in to comment.