Skip to content

Commit

Permalink
Add Uniswap liquidity to BorrowerLens and improve reliability of UniN…
Browse files Browse the repository at this point in the history
…FT import
  • Loading branch information
haydenshively committed Feb 14, 2024
1 parent 67451e8 commit 05cac22
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
10 changes: 6 additions & 4 deletions periphery/src/BorrowerLens.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,18 @@ contract BorrowerLens {
return (false, pool);
}

function getUniswapFees(Borrower account) external view returns (bytes32[] memory keys, uint256[] memory fees) {
function getUniswapPositions(
Borrower account
) external view returns (int24[] memory positions, uint128[] memory liquidity, uint256[] memory fees) {
IUniswapV3Pool pool = account.UNISWAP_POOL();
Uniswap.FeeComputationCache memory c;
{
(, int24 tick, , , , , ) = pool.slot0();
c = Uniswap.FeeComputationCache(tick, pool.feeGrowthGlobal0X128(), pool.feeGrowthGlobal1X128());
}

int24[] memory positions = account.getUniswapPositions();
keys = new bytes32[](positions.length >> 1);
positions = account.getUniswapPositions();
liquidity = new uint128[](positions.length >> 1);
fees = new uint256[](positions.length);

unchecked {
Expand All @@ -104,7 +106,7 @@ contract BorrowerLens {

(uint256 temp0, uint256 temp1) = position.fees(pool, info, c);

keys[i >> 1] = keccak256(abi.encodePacked(address(account), l, u));
liquidity[i >> 1] = info.liquidity;
fees[i] = temp0;
fees[i + 1] = temp1;
}
Expand Down
10 changes: 5 additions & 5 deletions periphery/src/managers/UniswapNFTManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,22 @@ contract UniswapNFTManager is IManager {
int24 upper;
// The change in the NFT's liquidity. Negative values move NFT-->Borrower, positives do the opposite
int128 liquidity;
(tokenId, lower, upper, liquidity, positions) = abi.decode(data[20:], (uint256, int24, int24, int128, uint144));
(tokenId, lower, upper, liquidity, positions) = abi.decode(data[20:], (uint256, int24, int24, int128, uint208));

// move position from NonfungiblePositionManager to Borrower
if (liquidity < 0) {
// safety checks since this contract will be approved to manager users' positions
require(owner == UNISWAP_NFT.ownerOf(tokenId));

_withdrawFromNFT(tokenId, uint128(-liquidity), msg.sender);
borrower.uniswapDeposit(lower, upper, uint128(-liquidity));
borrower.uniswapDeposit(lower, upper, uint128((uint256(uint128(-liquidity)) * 999) / 1000));
}
// move position from Borrower to NonfungiblePositionManager (position must exist already)
else {
ERC20 token0 = borrower.TOKEN0();
ERC20 token1 = borrower.TOKEN1();

(uint256 burned0, uint256 burned1, uint256 collected0, uint256 collected1) = borrower.uniswapWithdraw(
(uint256 burned0, uint256 burned1, , ) = borrower.uniswapWithdraw(
lower,
upper,
uint128(liquidity),
Expand All @@ -77,8 +77,8 @@ contract UniswapNFTManager is IManager {
})
);

token0.safeTransfer(owner, collected0 - burned0);
token1.safeTransfer(owner, collected1 - burned1);
token0.safeTransfer(owner, token0.balanceOf(address(this)));
token1.safeTransfer(owner, token1.balanceOf(address(this)));
}
}

Expand Down

0 comments on commit 05cac22

Please sign in to comment.