Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(uniswap): add deadline to decrease liquidity #314

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion contracts/interfaces/INTokenUniswapV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface INTokenUniswapV3 {
uint128 liquidityDecrease,
uint256 amount0Min,
uint256 amount1Min,
bool receiveEthAsWeth
bool receiveEthAsWeth,
uint256 deadline
) external;
}
3 changes: 2 additions & 1 deletion contracts/interfaces/IPoolCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ interface IPoolCore {
uint128 liquidityDecrease,
uint256 amount0Min,
uint256 amount1Min,
bool receiveEthAsWeth
bool receiveEthAsWeth,
uint256 deadline
) external;

/**
Expand Down
3 changes: 2 additions & 1 deletion contracts/protocol/libraries/logic/SupplyLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ library SupplyLogic {
params.liquidityDecrease,
params.amount0Min,
params.amount1Min,
params.receiveEthAsWeth
params.receiveEthAsWeth,
params.deadline
);

bool isUsedAsCollateral = ICollateralizableERC721(
Expand Down
1 change: 1 addition & 0 deletions contracts/protocol/libraries/types/DataTypes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ library DataTypes {
uint256 amount0Min;
uint256 amount1Min;
bool receiveEthAsWeth;
uint256 deadline;
address oracle;
}

Expand Down
4 changes: 3 additions & 1 deletion contracts/protocol/pool/PoolCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -258,6 +259,7 @@ contract PoolCore is
amount0Min: amount0Min,
amount1Min: amount1Min,
receiveEthAsWeth: receiveEthAsWeth,
deadline: deadline,
oracle: ADDRESSES_PROVIDER.getPriceOracle()
})
);
Expand Down
21 changes: 12 additions & 9 deletions contracts/protocol/tokenization/NTokenUniswapV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -66,7 +67,7 @@ contract NTokenUniswapV3 is NToken, INTokenUniswapV3 {
liquidity: liquidityDecrease,
amount0Min: amount0Min,
amount1Min: amount1Min,
deadline: block.timestamp
deadline: deadline
});

INonfungiblePositionManager(_underlyingAsset).decreaseLiquidity(
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);

Expand All @@ -137,7 +139,8 @@ contract NTokenUniswapV3 is NToken, INTokenUniswapV3 {
liquidityDecrease,
amount0Min,
amount1Min,
receiveEthAsWeth
receiveEthAsWeth,
deadline
);
}

Expand Down
8 changes: 7 additions & 1 deletion test/_uniswapv3_pool_operation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
}
Expand All @@ -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,
}
Expand Down Expand Up @@ -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,
}
Expand Down
5 changes: 4 additions & 1 deletion test/_uniswapv3_position_control.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -218,6 +218,7 @@ describe("Uniswap V3 NFT position control", () => {
0,
0,
false,
(await time.latest()) + 100,
{
gasLimit: 12_450_000,
}
Expand Down Expand Up @@ -259,6 +260,7 @@ describe("Uniswap V3 NFT position control", () => {
0,
0,
true,
(await time.latest()) + 100,
{
gasLimit: 12_450_000,
}
Expand Down Expand Up @@ -314,6 +316,7 @@ describe("Uniswap V3 NFT position control", () => {
0,
0,
false,
(await time.latest()) + 100,
{
gasLimit: 12_450_000,
}
Expand Down