From 0ad2780e019be085d9fe1d9f8146ec8384ee0c03 Mon Sep 17 00:00:00 2001 From: 0xwalid Date: Thu, 5 Jan 2023 17:25:16 -0800 Subject: [PATCH 1/3] fix(uniswap): add deadline to decrease liquidity --- contracts/interfaces/INTokenUniswapV3.sol | 3 ++- contracts/interfaces/IPoolCore.sol | 3 ++- .../protocol/libraries/logic/SupplyLogic.sol | 3 ++- .../protocol/libraries/types/DataTypes.sol | 1 + contracts/protocol/pool/PoolCore.sol | 4 +++- .../protocol/tokenization/NTokenUniswapV3.sol | 21 +++++++++++-------- test/_uniswapv3_pool_operation.spec.ts | 10 +++++++-- test/_uniswapv3_position_control.spec.ts | 3 +++ 8 files changed, 33 insertions(+), 15 deletions(-) diff --git a/contracts/interfaces/INTokenUniswapV3.sol b/contracts/interfaces/INTokenUniswapV3.sol index ddb633ad7..bf8a143e9 100644 --- a/contracts/interfaces/INTokenUniswapV3.sol +++ b/contracts/interfaces/INTokenUniswapV3.sol @@ -18,6 +18,7 @@ interface INTokenUniswapV3 { uint128 liquidityDecrease, uint256 amount0Min, uint256 amount1Min, - bool receiveEthAsWeth + bool receiveEthAsWeth, + uint256 deadline ) external; } diff --git a/contracts/interfaces/IPoolCore.sol b/contracts/interfaces/IPoolCore.sol index 939b5a525..4df26ad9f 100644 --- a/contracts/interfaces/IPoolCore.sol +++ b/contracts/interfaces/IPoolCore.sol @@ -315,7 +315,8 @@ interface IPoolCore { uint128 liquidityDecrease, uint256 amount0Min, uint256 amount1Min, - bool receiveEthAsWeth + bool receiveEthAsWeth, + uint256 deadline ) external; /** diff --git a/contracts/protocol/libraries/logic/SupplyLogic.sol b/contracts/protocol/libraries/logic/SupplyLogic.sol index c062ba058..7689cf34f 100644 --- a/contracts/protocol/libraries/logic/SupplyLogic.sol +++ b/contracts/protocol/libraries/logic/SupplyLogic.sol @@ -440,7 +440,8 @@ library SupplyLogic { params.liquidityDecrease, params.amount0Min, params.amount1Min, - params.receiveEthAsWeth + params.receiveEthAsWeth, + params.deadline ); bool isUsedAsCollateral = ICollateralizableERC721( diff --git a/contracts/protocol/libraries/types/DataTypes.sol b/contracts/protocol/libraries/types/DataTypes.sol index 63b825d9f..1d4f285b3 100644 --- a/contracts/protocol/libraries/types/DataTypes.sol +++ b/contracts/protocol/libraries/types/DataTypes.sol @@ -185,6 +185,7 @@ library DataTypes { uint256 amount0Min; uint256 amount1Min; bool receiveEthAsWeth; + uint256 deadline; address oracle; } diff --git a/contracts/protocol/pool/PoolCore.sol b/contracts/protocol/pool/PoolCore.sol index 987793456..e8c7abb08 100644 --- a/contracts/protocol/pool/PoolCore.sol +++ b/contracts/protocol/pool/PoolCore.sol @@ -240,7 +240,8 @@ contract PoolCore is uint128 liquidityDecrease, uint256 amount0Min, uint256 amount1Min, - bool receiveEthAsWeth + bool receiveEthAsWeth, + uint256 deadline ) external virtual override nonReentrant { DataTypes.PoolStorage storage ps = poolStorage(); @@ -258,6 +259,7 @@ contract PoolCore is amount0Min: amount0Min, amount1Min: amount1Min, receiveEthAsWeth: receiveEthAsWeth, + deadline: deadline, oracle: ADDRESSES_PROVIDER.getPriceOracle() }) ); diff --git a/contracts/protocol/tokenization/NTokenUniswapV3.sol b/contracts/protocol/tokenization/NTokenUniswapV3.sol index cd4f52dad..cb3d91e2b 100644 --- a/contracts/protocol/tokenization/NTokenUniswapV3.sol +++ b/contracts/protocol/tokenization/NTokenUniswapV3.sol @@ -54,7 +54,8 @@ contract NTokenUniswapV3 is NToken, INTokenUniswapV3 { uint128 liquidityDecrease, uint256 amount0Min, uint256 amount1Min, - bool receiveEthAsWeth + bool receiveEthAsWeth, + uint256 deadline ) internal returns (uint256 amount0, uint256 amount1) { if (liquidityDecrease > 0) { // amount0Min and amount1Min are price slippage checks @@ -66,7 +67,7 @@ contract NTokenUniswapV3 is NToken, INTokenUniswapV3 { liquidity: liquidityDecrease, amount0Min: amount0Min, amount1Min: amount1Min, - deadline: block.timestamp + deadline: deadline }); INonfungiblePositionManager(_underlyingAsset).decreaseLiquidity( @@ -105,14 +106,14 @@ contract NTokenUniswapV3 is NToken, INTokenUniswapV3 { .collect(collectParams); if (receiveEthAsWeth) { - uint256 balanceWeth = IERC20(weth).balanceOf(address(this)); - if (balanceWeth > 0) { - IWETH(weth).withdraw(balanceWeth); - _safeTransferETH(user, balanceWeth); + uint256 balanceToken = IERC20(weth).balanceOf(address(this)); + if (balanceToken > 0) { + IWETH(weth).withdraw(balanceToken); + _safeTransferETH(user, balanceToken); } address pairToken = (token0 == weth) ? token1 : token0; - uint256 balanceToken = IERC20(pairToken).balanceOf(address(this)); + balanceToken = IERC20(pairToken).balanceOf(address(this)); if (balanceToken > 0) { IERC20(pairToken).safeTransfer(user, balanceToken); } @@ -126,7 +127,8 @@ contract NTokenUniswapV3 is NToken, INTokenUniswapV3 { uint128 liquidityDecrease, uint256 amount0Min, uint256 amount1Min, - bool receiveEthAsWeth + bool receiveEthAsWeth, + uint256 deadline ) external onlyPool nonReentrant { require(user == ownerOf(tokenId), Errors.NOT_THE_OWNER); @@ -137,7 +139,8 @@ contract NTokenUniswapV3 is NToken, INTokenUniswapV3 { liquidityDecrease, amount0Min, amount1Min, - receiveEthAsWeth + receiveEthAsWeth, + deadline ); } diff --git a/test/_uniswapv3_pool_operation.spec.ts b/test/_uniswapv3_pool_operation.spec.ts index 92430dc44..bac64390c 100644 --- a/test/_uniswapv3_pool_operation.spec.ts +++ b/test/_uniswapv3_pool_operation.spec.ts @@ -14,7 +14,7 @@ import {encodeSqrtRatioX96} from "@uniswap/v3-sdk"; import {getUniswapV3OracleWrapper} from "../helpers/contracts-getters"; import {ProtocolErrors} from "../helpers/types"; import {snapshot} from "./helpers/snapshot-manager"; -import {loadFixture} from "@nomicfoundation/hardhat-network-helpers"; +import {loadFixture, time} from "@nomicfoundation/hardhat-network-helpers"; import {testEnvFixture} from "./helpers/setup-env"; describe("Uniswap V3 NFT supply, withdraw, setCollateral, liquidation and transfer test", () => { @@ -442,7 +442,7 @@ describe("Uniswap V3 NFT supply, withdraw, setCollateral, liquidation and transf await waitForTx(await configurator.setReserveActive(weth.address, false)); const beforeLiquidity = (await nftPositionManager.positions(1)).liquidity; - + await expect( pool .connect(user1.signer) @@ -453,6 +453,7 @@ describe("Uniswap V3 NFT supply, withdraw, setCollateral, liquidation and transf 0, 0, false, + (await time.latest()) + 100, { gasLimit: 12_450_000, } @@ -485,6 +486,7 @@ describe("Uniswap V3 NFT supply, withdraw, setCollateral, liquidation and transf 0, 0, false, + (await time.latest()) + 100, { gasLimit: 12_450_000, } @@ -517,6 +519,7 @@ describe("Uniswap V3 NFT supply, withdraw, setCollateral, liquidation and transf 0, 0, false, + (await time.latest()) + 100, { gasLimit: 12_450_000, } @@ -549,6 +552,7 @@ describe("Uniswap V3 NFT supply, withdraw, setCollateral, liquidation and transf 0, 0, false, + (await time.latest()) + 100, { gasLimit: 12_450_000, } @@ -573,6 +577,7 @@ describe("Uniswap V3 NFT supply, withdraw, setCollateral, liquidation and transf 0, 0, false, + (await time.latest()) + 100, { gasLimit: 12_450_000, } @@ -694,6 +699,7 @@ describe("Uniswap V3 NFT supply, withdraw, setCollateral, liquidation and transf 0, 0, false, + (await time.latest()) + 100, { gasLimit: 12_450_000, } diff --git a/test/_uniswapv3_position_control.spec.ts b/test/_uniswapv3_position_control.spec.ts index b84badfae..c3bd9ed7c 100644 --- a/test/_uniswapv3_position_control.spec.ts +++ b/test/_uniswapv3_position_control.spec.ts @@ -218,6 +218,7 @@ describe("Uniswap V3 NFT position control", () => { 0, 0, false, + (await time.latest()) + 100, { gasLimit: 12_450_000, } @@ -259,6 +260,7 @@ describe("Uniswap V3 NFT position control", () => { 0, 0, true, + (await time.latest()) + 100, { gasLimit: 12_450_000, } @@ -314,6 +316,7 @@ describe("Uniswap V3 NFT position control", () => { 0, 0, false, + (await time.latest()) + 100, { gasLimit: 12_450_000, } From 05f66273b35452c2dc318a7d825c3aac8372a3e5 Mon Sep 17 00:00:00 2001 From: 0xwalid Date: Thu, 5 Jan 2023 17:32:31 -0800 Subject: [PATCH 2/3] fix(uniswap): fix test --- test/_uniswapv3_position_control.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/_uniswapv3_position_control.spec.ts b/test/_uniswapv3_position_control.spec.ts index c3bd9ed7c..4ebdf2ca1 100644 --- a/test/_uniswapv3_position_control.spec.ts +++ b/test/_uniswapv3_position_control.spec.ts @@ -13,7 +13,7 @@ import { } from "./helpers/uniswapv3-helper"; import {encodeSqrtRatioX96} from "@uniswap/v3-sdk"; import {DRE} from "../helpers/misc-utils"; -import {loadFixture} from "@nomicfoundation/hardhat-network-helpers"; +import {loadFixture, time} from "@nomicfoundation/hardhat-network-helpers"; import {testEnvFixture} from "./helpers/setup-env"; describe("Uniswap V3 NFT position control", () => { From 6c8360b4a0a945ededeec53dfbe457cda03dd8a7 Mon Sep 17 00:00:00 2001 From: 0xwalid Date: Thu, 5 Jan 2023 17:40:28 -0800 Subject: [PATCH 3/3] fix(uniswap): fix test --- test/_uniswapv3_pool_operation.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/_uniswapv3_pool_operation.spec.ts b/test/_uniswapv3_pool_operation.spec.ts index bac64390c..afe94a916 100644 --- a/test/_uniswapv3_pool_operation.spec.ts +++ b/test/_uniswapv3_pool_operation.spec.ts @@ -442,7 +442,7 @@ describe("Uniswap V3 NFT supply, withdraw, setCollateral, liquidation and transf await waitForTx(await configurator.setReserveActive(weth.address, false)); const beforeLiquidity = (await nftPositionManager.positions(1)).liquidity; - + await expect( pool .connect(user1.signer)