Skip to content

Commit 542e52d

Browse files
committed
chore: Apply timelock to users decrease Uniswap v3 liquidity
1 parent ef0764e commit 542e52d

File tree

11 files changed

+191
-207
lines changed

11 files changed

+191
-207
lines changed

contracts/interfaces/INTokenUniswapV3.sol

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,19 @@ interface INTokenUniswapV3 {
1010
* @param liquidityDecrease The amount of liquidity to remove of LP
1111
* @param amount0Min The minimum amount to remove of token0
1212
* @param amount1Min The minimum amount to remove of token1
13-
* @param receiveEthAsWeth If convert weth to ETH
1413
*/
1514
function decreaseUniswapV3Liquidity(
1615
address user,
1716
uint256 tokenId,
1817
uint128 liquidityDecrease,
1918
uint256 amount0Min,
20-
uint256 amount1Min,
21-
bool receiveEthAsWeth
22-
) external;
19+
uint256 amount1Min
20+
)
21+
external
22+
returns (
23+
address token0,
24+
address token1,
25+
uint256 amount0,
26+
uint256 amount1
27+
);
2328
}

contracts/interfaces/IPoolCore.sol

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,15 +308,13 @@ interface IPoolCore {
308308
* @param liquidityDecrease The amount of liquidity to remove of LP
309309
* @param amount0Min The minimum amount to remove of token0
310310
* @param amount1Min The minimum amount to remove of token1
311-
* @param receiveEthAsWeth If convert weth to ETH
312311
*/
313312
function decreaseUniswapV3Liquidity(
314313
address asset,
315314
uint256 tokenId,
316315
uint128 liquidityDecrease,
317316
uint256 amount0Min,
318-
uint256 amount1Min,
319-
bool receiveEthAsWeth
317+
uint256 amount1Min
320318
) external;
321319

322320
/**

contracts/protocol/libraries/helpers/Helpers.sol

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@ import {IERC20} from "../../../dependencies/openzeppelin/contracts/IERC20.sol";
55
import {DataTypes} from "../types/DataTypes.sol";
66
import {WadRayMath} from "../../libraries/math/WadRayMath.sol";
77
import {IAtomicCollateralizableERC721} from "../../../interfaces/IAtomicCollateralizableERC721.sol";
8+
import {UserConfiguration} from "../configuration/UserConfiguration.sol";
89

910
/**
1011
* @title Helpers library
1112
*
1213
*/
1314
library Helpers {
1415
using WadRayMath for uint256;
16+
using UserConfiguration for DataTypes.UserConfigurationMap;
17+
18+
// See `IPool` for descriptions
19+
event ReserveUsedAsCollateralEnabled(
20+
address indexed reserve,
21+
address indexed user
22+
);
1523

1624
/**
1725
* @notice Fetches the user current stable and variable debt balances
@@ -36,4 +44,25 @@ library Helpers {
3644
.getTraitMultiplier(tokenId);
3745
return assetPrice.wadMul(multiplier);
3846
}
47+
48+
/**
49+
* @notice Set user's collateral status for specified asset, if current collateral status is true, skip it.
50+
* @param userConfig The user configuration mapping that tracks the supplied/borrowed assets
51+
* @param reservesData The state of all the reserves
52+
* @param token The asset address
53+
* @param user The user address
54+
**/
55+
function setAssetUsedAsCollateral(
56+
DataTypes.UserConfigurationMap storage userConfig,
57+
mapping(address => DataTypes.ReserveData) storage reservesData,
58+
address token,
59+
address user
60+
) internal {
61+
uint16 reserveId = reservesData[token].id;
62+
bool currentStatus = userConfig.isUsingAsCollateral(reserveId);
63+
if (!currentStatus) {
64+
userConfig.setUsingAsCollateral(reserveId, true);
65+
emit ReserveUsedAsCollateralEnabled(token, user);
66+
}
67+
}
3968
}

contracts/protocol/libraries/logic/LiquidationLogic.sol

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {IVariableDebtToken} from "../../../interfaces/IVariableDebtToken.sol";
2929
import {IPriceOracleGetter} from "../../../interfaces/IPriceOracleGetter.sol";
3030
import {IPoolAddressesProvider} from "../../../interfaces/IPoolAddressesProvider.sol";
3131
import {Errors} from "../../libraries/helpers/Errors.sol";
32+
import {Helpers} from "../helpers/Helpers.sol";
3233

3334
/**
3435
* @title LiquidationLogic library
@@ -592,16 +593,12 @@ library LiquidationLogic {
592593
})
593594
);
594595

595-
if (!userConfig.isUsingAsCollateral(vars.liquidationAssetReserveId)) {
596-
userConfig.setUsingAsCollateral(
597-
vars.liquidationAssetReserveId,
598-
true
599-
);
600-
emit ReserveUsedAsCollateralEnabled(
601-
params.liquidationAsset,
602-
params.borrower
603-
);
604-
}
596+
Helpers.setAssetUsedAsCollateral(
597+
userConfig,
598+
reservesData,
599+
params.liquidationAsset,
600+
params.borrower
601+
);
605602
}
606603

607604
/**

contracts/protocol/libraries/logic/SupplyLogic.sol

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {XTokenType} from "../../../interfaces/IXTokenType.sol";
2222
import {INTokenUniswapV3} from "../../../interfaces/INTokenUniswapV3.sol";
2323
import {GenericLogic} from "./GenericLogic.sol";
2424
import {IPriceOracleGetter} from "../../../interfaces/IPriceOracleGetter.sol";
25+
import {Helpers} from "../helpers/Helpers.sol";
2526

2627
/**
2728
* @title SupplyLogic library
@@ -85,7 +86,7 @@ library SupplyLogic {
8586
mapping(address => DataTypes.ReserveData) storage reservesData,
8687
DataTypes.UserConfigurationMap storage userConfig,
8788
DataTypes.ExecuteSupplyParams memory params
88-
) external {
89+
) public {
8990
DataTypes.ReserveData storage reserve = reservesData[params.asset];
9091
DataTypes.ReserveCache memory reserveCache = reserve.cache();
9192

@@ -206,15 +207,12 @@ library SupplyLogic {
206207
tokenType == XTokenType.NTokenBAYC ||
207208
tokenType == XTokenType.NTokenMAYC
208209
) {
209-
uint16 sApeReserveId = reservesData[DataTypes.SApeAddress].id;
210-
bool currentStatus = userConfig.isUsingAsCollateral(sApeReserveId);
211-
if (!currentStatus) {
212-
userConfig.setUsingAsCollateral(sApeReserveId, true);
213-
emit ReserveUsedAsCollateralEnabled(
214-
DataTypes.SApeAddress,
215-
params.onBehalfOf
216-
);
217-
}
210+
Helpers.setAssetUsedAsCollateral(
211+
userConfig,
212+
reservesData,
213+
DataTypes.SApeAddress,
214+
params.onBehalfOf
215+
);
218216
}
219217
for (uint256 index = 0; index < params.tokenData.length; index++) {
220218
IERC721(params.asset).safeTransferFrom(
@@ -465,7 +463,6 @@ library SupplyLogic {
465463

466464
function executeDecreaseUniswapV3Liquidity(
467465
mapping(address => DataTypes.ReserveData) storage reservesData,
468-
mapping(uint256 => address) storage reservesList,
469466
DataTypes.UserConfigurationMap storage userConfig,
470467
DataTypes.ExecuteDecreaseUniswapV3LiquidityParams memory params
471468
) external {
@@ -490,29 +487,61 @@ library SupplyLogic {
490487
tokenIds
491488
);
492489

493-
INTokenUniswapV3(reserveCache.xTokenAddress).decreaseUniswapV3Liquidity(
494-
params.user,
495-
params.tokenId,
496-
params.liquidityDecrease,
497-
params.amount0Min,
498-
params.amount1Min,
499-
params.receiveEthAsWeth
500-
);
501-
490+
(
491+
address token0,
492+
address token1,
493+
uint256 amount0,
494+
uint256 amount1
495+
) = INTokenUniswapV3(reserveCache.xTokenAddress)
496+
.decreaseUniswapV3Liquidity(
497+
params.user,
498+
params.tokenId,
499+
params.liquidityDecrease,
500+
params.amount0Min,
501+
params.amount1Min
502+
);
502503
bool isUsedAsCollateral = ICollateralizableERC721(
503504
reserveCache.xTokenAddress
504505
).isUsedAsCollateral(params.tokenId);
505-
if (isUsedAsCollateral) {
506-
if (userConfig.isBorrowingAny()) {
507-
ValidationLogic.validateHFAndLtvERC721(
506+
if (amount0 > 0) {
507+
executeSupply(
508+
reservesData,
509+
userConfig,
510+
DataTypes.ExecuteSupplyParams({
511+
asset: token0,
512+
amount: amount0,
513+
onBehalfOf: params.user,
514+
payer: address(this),
515+
referralCode: 0
516+
})
517+
);
518+
if (isUsedAsCollateral) {
519+
Helpers.setAssetUsedAsCollateral(
520+
userConfig,
508521
reservesData,
509-
reservesList,
522+
token0,
523+
params.user
524+
);
525+
}
526+
}
527+
if (amount1 > 0) {
528+
executeSupply(
529+
reservesData,
530+
userConfig,
531+
DataTypes.ExecuteSupplyParams({
532+
asset: token1,
533+
amount: amount1,
534+
onBehalfOf: params.user,
535+
payer: address(this),
536+
referralCode: 0
537+
})
538+
);
539+
if (isUsedAsCollateral) {
540+
Helpers.setAssetUsedAsCollateral(
510541
userConfig,
511-
params.asset,
512-
tokenIds,
513-
params.user,
514-
params.reservesCount,
515-
params.oracle
542+
reservesData,
543+
token1,
544+
params.user
516545
);
517546
}
518547
}

contracts/protocol/libraries/types/DataTypes.sol

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,6 @@ library DataTypes {
188188
uint128 liquidityDecrease;
189189
uint256 amount0Min;
190190
uint256 amount1Min;
191-
bool receiveEthAsWeth;
192-
address oracle;
193191
}
194192

195193
struct FinalizeTransferParams {

contracts/protocol/pool/PoolApeStaking.sol

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {WadRayMath} from "../libraries/math/WadRayMath.sol";
2525
import {Math} from "../../dependencies/openzeppelin/contracts/Math.sol";
2626
import {ISwapRouter} from "../../dependencies/univ3/interfaces/ISwapRouter.sol";
2727
import {IPriceOracleGetter} from "../../interfaces/IPriceOracleGetter.sol";
28+
import {Helpers} from "../libraries/helpers/Helpers.sol";
2829

2930
contract PoolApeStaking is
3031
ParaVersionedInitializable,
@@ -373,18 +374,15 @@ contract PoolApeStaking is
373374
);
374375

375376
//7 collateralize sAPE
376-
uint16 sApeReserveId = ps._reserves[DataTypes.SApeAddress].id;
377377
DataTypes.UserConfigurationMap storage userConfig = ps._usersConfig[
378378
msg.sender
379379
];
380-
bool currentStatus = userConfig.isUsingAsCollateral(sApeReserveId);
381-
if (!currentStatus) {
382-
userConfig.setUsingAsCollateral(sApeReserveId, true);
383-
emit ReserveUsedAsCollateralEnabled(
384-
DataTypes.SApeAddress,
385-
msg.sender
386-
);
387-
}
380+
Helpers.setAssetUsedAsCollateral(
381+
userConfig,
382+
ps._reserves,
383+
DataTypes.SApeAddress,
384+
msg.sender
385+
);
388386
}
389387

390388
/// @inheritdoc IPoolApeStaking
@@ -786,12 +784,12 @@ contract PoolApeStaking is
786784
referralCode: 0
787785
})
788786
);
789-
DataTypes.ReserveData storage assetReserve = ps._reserves[asset];
790-
uint16 reserveId = assetReserve.id;
791-
if (!userConfig.isUsingAsCollateral(reserveId)) {
792-
userConfig.setUsingAsCollateral(reserveId, true);
793-
emit ReserveUsedAsCollateralEnabled(asset, onBehalfOf);
794-
}
787+
Helpers.setAssetUsedAsCollateral(
788+
userConfig,
789+
ps._reserves,
790+
asset,
791+
onBehalfOf
792+
);
795793
}
796794

797795
function _repayForUser(

contracts/protocol/pool/PoolCore.sol

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,13 @@ contract PoolCore is
242242
uint256 tokenId,
243243
uint128 liquidityDecrease,
244244
uint256 amount0Min,
245-
uint256 amount1Min,
246-
bool receiveEthAsWeth
245+
uint256 amount1Min
247246
) external virtual override nonReentrant {
248247
DataTypes.PoolStorage storage ps = poolStorage();
249248

250249
return
251250
SupplyLogic.executeDecreaseUniswapV3Liquidity(
252251
ps._reserves,
253-
ps._reservesList,
254252
ps._usersConfig[msg.sender],
255253
DataTypes.ExecuteDecreaseUniswapV3LiquidityParams({
256254
user: msg.sender,
@@ -259,9 +257,7 @@ contract PoolCore is
259257
reservesCount: ps._reservesCount,
260258
liquidityDecrease: liquidityDecrease,
261259
amount0Min: amount0Min,
262-
amount1Min: amount1Min,
263-
receiveEthAsWeth: receiveEthAsWeth,
264-
oracle: ADDRESSES_PROVIDER.getPriceOracle()
260+
amount1Min: amount1Min
265261
})
266262
);
267263
}

0 commit comments

Comments
 (0)