Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ contract LenderCommitmentGroup_Pool_V2 is
uint256 principalAmount
) public view virtual returns (uint256 collateralTokensAmountToMatchValue) {

uint256 pairPriceWithTwapFromOracle = IUniswapPricingLibrary(UNISWAP_PRICING_HELPER)
( uint256 pairPriceWithTwapFromOracle, uint256 oracle_exp_factor ) = IUniswapPricingLibrary(UNISWAP_PRICING_HELPER)
.getUniswapPriceRatioForPoolRoutes(poolOracleRoutes);


Expand All @@ -716,10 +716,19 @@ contract LenderCommitmentGroup_Pool_V2 is
);


uint256 exp_factor = 1e18;


if ( maxPrincipalPerCollateralAmount == 0 || pairPriceWithTwapFromOracle < maxPrincipalPerCollateralAmount ) {

exp_factor = oracle_exp_factor ;
}

return
getRequiredCollateral(
principalAmount,
principalPerCollateralAmount
principalPerCollateralAmount ,
exp_factor // for principalPerCollateralAmount
);
}

Expand All @@ -729,64 +738,42 @@ contract LenderCommitmentGroup_Pool_V2 is
* @dev Calls the UniswapPricingLibrary to get TWAP (Time-Weighted Average Price) for the specified routes
* @dev This is a low-level internal function that handles direct Uniswap oracle interaction
* @param poolOracleRoutes Array of pool route configurations to use for price calculation
* @return The Uniswap price ratio expanded by the Uniswap expansion factor (2^96)
* @return The Uniswap price ratio expanded
*/
function getUniswapPriceRatioForPoolRoutes(
IUniswapPricingLibrary.PoolRouteConfig[] memory poolOracleRoutes
) internal view virtual returns (uint256 ) {
) internal view virtual returns (uint256, uint256 ) {

uint256 pairPriceWithTwapFromOracle = IUniswapPricingLibrary(UNISWAP_PRICING_HELPER)
(uint256 pairPriceWithTwapFromOracle , uint256 exp_factor) = IUniswapPricingLibrary(UNISWAP_PRICING_HELPER)
.getUniswapPriceRatioForPoolRoutes(poolOracleRoutes);


return pairPriceWithTwapFromOracle;
return (pairPriceWithTwapFromOracle, exp_factor) ;
}

/**
* @notice Calculates the principal token amount per collateral token based on Uniswap oracle prices
* @dev Uses Uniswap TWAP and applies any configured maximum limits
* @dev Returns the lesser of the oracle price or the configured maximum (if set)
* @param poolOracleRoutes Array of pool route configurations to use for price calculation
* @return The principal per collateral ratio, expanded by the Uniswap expansion factor
*/
function getPrincipalForCollateralForPoolRoutes(
IUniswapPricingLibrary.PoolRouteConfig[] memory poolOracleRoutes
) external view virtual returns (uint256 ) {

uint256 pairPriceWithTwapFromOracle = IUniswapPricingLibrary(UNISWAP_PRICING_HELPER)
.getUniswapPriceRatioForPoolRoutes(poolOracleRoutes);


uint256 principalPerCollateralAmount = maxPrincipalPerCollateralAmount == 0
? pairPriceWithTwapFromOracle
: Math.min(
pairPriceWithTwapFromOracle,
maxPrincipalPerCollateralAmount //this is expanded by uniswap exp factor
);


return principalPerCollateralAmount;
}



/**
* @notice Calculates the amount of collateral tokens required for a given principal amount
* @dev Converts principal amount to equivalent collateral based on current price ratio
* @dev Uses the Math.mulDiv function with rounding up to ensure sufficient collateral
* @param _principalAmount The amount of principal tokens to be borrowed
* @param _maxPrincipalPerCollateralAmount The exchange rate between principal and collateral (expanded by STANDARD_EXPANSION_FACTOR)
* @param _maxPrincipalPerCollateralAmount The exchange rate between principal and collateral (expanded by _maxPrincipalPerCollateralAmountExpFactor)
* @return The required amount of collateral tokens, rounded up to ensure sufficient collateralization
*/
function getRequiredCollateral(
uint256 _principalAmount,
uint256 _maxPrincipalPerCollateralAmount
uint256 _maxPrincipalPerCollateralAmount ,

uint256 _maxPrincipalPerCollateralAmountExpFactor

) internal view virtual returns (uint256) {

return
MathUpgradeable.mulDiv(
_principalAmount,
STANDARD_EXPANSION_FACTOR,
_maxPrincipalPerCollateralAmountExpFactor,
_maxPrincipalPerCollateralAmount,
MathUpgradeable.Rounding.Up
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface IUniswapPricingLibrary {

function getUniswapPriceRatioForPoolRoutes(
PoolRouteConfig[] memory poolRoutes
) external view returns (uint256 priceRatio);
) external view returns (uint256 priceRatio, uint256 exp_factor);


function getUniswapPriceRatioForPool(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
pragma solidity >=0.8.0 <0.9.0;
// SPDX-License-Identifier: MIT


import "forge-std/console.sol";


import {IUniswapPricingLibrary} from "../interfaces/IUniswapPricingLibrary.sol";
Expand Down Expand Up @@ -31,15 +29,13 @@ import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";

contract UniswapPricingHelper
{


// use uniswap exp helper instead ?

uint256 constant STANDARD_EXPANSION_FACTOR = 1e18;


// no longer dividing by EXP_FACTOR here - just leave it
function getUniswapPriceRatioForPoolRoutes(
IUniswapPricingLibrary.PoolRouteConfig[] memory poolRoutes
) public view returns (uint256 priceRatio) {
) public view returns (uint256 priceRatio, uint256 exp_factor) {
require(poolRoutes.length <= 2, "invalid pool routes length");

if (poolRoutes.length == 2) {
Expand All @@ -51,19 +47,14 @@ contract UniswapPricingHelper
poolRoutes[1]
);


console.log("ratio");
console.logUint(pool0PriceRatio);
console.logUint(pool1PriceRatio);

return
FullMath.mulDiv(
( FullMath.mulDiv(
pool0PriceRatio,
pool1PriceRatio,
STANDARD_EXPANSION_FACTOR
);
1
) , 1e36 );
} else if (poolRoutes.length == 1) {
return getUniswapPriceRatioForPool(poolRoutes[0]);
return ( getUniswapPriceRatioForPool(poolRoutes[0]) , 1e18 );
}

//else return 0
Expand All @@ -78,7 +69,7 @@ contract UniswapPricingHelper



// this is expanded by 2**96 or 1e28
// this is expanded by 2**96 or ~ 1e28
uint160 sqrtPriceX96 = getSqrtTwapX96(
_poolRouteConfig.pool,
_poolRouteConfig.twapInterval
Expand Down
4 changes: 2 additions & 2 deletions packages/contracts/deployments/katana/BorrowSwap.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"address": "0x21499A1Fd9f36ce527CB4A9032072ef1bb7a22F6",
"address": "0x128bb34bbe1e9a6ac07268dfa475459334500e89",
"abi": [
{
"type": "constructor",
Expand Down Expand Up @@ -302,5 +302,5 @@
"blockNumber": null
},
"numDeployments": 1,
"implementation": "0xdc00F6F4083391b5a8DbC15CDf9A8D4A51640AcA"
"implementation": "0xe9d02c0e0449a396a09a42fe03ff06300c972db5"
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ contract LenderCommitmentGroup_Pool_V2_Override is LenderCommitmentGroup_Pool_V2

function getRequiredCollateral(
uint256 _principalAmount,
uint256 maxPrincipalPerCollateralAmount
uint256 maxPrincipalPerCollateralAmount,
uint256 _maxPrincipalPerCollateralAmountExpFactor

) internal view override returns (uint256 collateralTokensAmountToMatchValue) {
return mockRequiredCollateralAmount;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/contracts/tests_fork/BorrowSwap_Test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ contract BorrowSwap_Fork_Test is Test {
SmartCommitmentForwarder smartCommitmentForwarder = SmartCommitmentForwarder(0x9Fa5A22A3c0b8030147d363f68A763DEB9f00acB);

// !!!! NEEDED !!!!!!!!!!!
vm.prank(borrowerAddress);
smartCommitmentForwarder.addExtension( address(borrowSwap) );
// vm.prank(borrowerAddress);
// smartCommitmentForwarder.addExtension( address(borrowSwap) );



Expand Down
26 changes: 25 additions & 1 deletion packages/contracts/tests_fork/PoolBorrow_Test.sol
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,36 @@ contract DeployPool_Fork_Test is Test {
}


function etch_mog_pool() public {

address tellerV2Address = 0x00182FdB0B880eE24D428e3Cc39383717677C37e;
address scfAddress = 0x80314D77E86d70A67126DA86EC823F5fc018c010;
address uniswapV3Factory = 0x1F98431c8aD98523631AE4a59f267346ea31F984 ;
address uniswapPricingHelper = 0x6B38aD36f17dd55bE44217d184DB8A01536aa104;


address poolAddress = 0x5F610ca9Ff0a0Ad9FbF91B8EB85A892fb0eBC620;


LenderCommitmentGroup_Pool_V2 newPool = new LenderCommitmentGroup_Pool_V2(
tellerV2Address,
scfAddress,
uniswapV3Factory,
uniswapPricingHelper
);

// Then replace the code at the deployed address
vm.etch( address(poolAddress) , address(newPool).code );

function test_pool_collateral_calc() public {

}




function test_pool_collateral_calc() public {

etch_mog_pool();
etch_uniswap_pricing_helper();


Expand Down