refactor: fix deposit/withdraw function

This commit is contained in:
cryptoDev222 2021-08-24 14:56:55 -05:00
parent 30eb39378f
commit 509d2cf3bd
4 changed files with 49 additions and 180 deletions

View File

@ -1,19 +1,11 @@
pragma solidity ^0.7.0;
contract Events {
event LogDeposit(
uint256 indexed tokenId,
uint256 liquidity,
uint256 amountA,
uint256 amountB
);
event LogDeposit(uint256 tokenId);
event LogWithdraw(
uint256 indexed tokenId,
uint256 liquidity,
uint256 amountA,
uint256 amountB
);
event LogWithdraw(uint256 indexed tokenId, address to);
event LogDepositTransfer(uint256 indexed tokenId, address to);
event LogStake(uint256 tokenId, address refundee);

View File

@ -99,113 +99,6 @@ abstract contract Helpers is DSMath, Basic {
}
}
/**
* @dev Check if token address is etherAddr and convert it to weth
*/
function _checkETH(
address _token0,
address _token1,
uint256 _amount0,
uint256 _amount1
) internal {
bool isEth0 = _token0 == wethAddr;
bool isEth1 = _token1 == wethAddr;
convertEthToWeth(isEth0, TokenInterface(_token0), _amount0);
convertEthToWeth(isEth1, TokenInterface(_token1), _amount1);
approve(TokenInterface(_token0), address(nftManager), _amount0);
approve(TokenInterface(_token1), address(nftManager), _amount1);
}
/**
* @dev addLiquidityWrapper function wrapper of _addLiquidity
*/
function _addLiquidityWrapper(
uint256 tokenId,
uint256 amountA,
uint256 amountB,
uint256 slippage
)
internal
returns (
uint256 liquidity,
uint256 amtA,
uint256 amtB
)
{
(address token0, address token1) = getNftTokenPairAddresses(tokenId);
(liquidity, amtA, amtB) = _addLiquidity(
tokenId,
token0,
token1,
amountA,
amountB,
slippage
);
}
/**
* @dev addLiquidity function which interact with Uniswap v3
*/
function _addLiquidity(
uint256 _tokenId,
address _token0,
address _token1,
uint256 _amount0,
uint256 _amount1,
uint256 _slippage
)
internal
returns (
uint128 liquidity,
uint256 amount0,
uint256 amount1
)
{
_checkETH(_token0, _token1, _amount0, _amount1);
uint256 _amount0Min = getMinAmount(
TokenInterface(_token0),
_amount0,
_slippage
);
uint256 _amount1Min = getMinAmount(
TokenInterface(_token1),
_amount1,
_slippage
);
INonfungiblePositionManager.IncreaseLiquidityParams
memory params = INonfungiblePositionManager.IncreaseLiquidityParams(
_tokenId,
_amount0,
_amount1,
_amount0Min,
_amount1Min,
block.timestamp
);
(liquidity, amount0, amount1) = nftManager.increaseLiquidity(params);
}
/**
* @dev decreaseLiquidity function which interact with Uniswap v3
*/
function _decreaseLiquidity(
uint256 _tokenId,
uint128 _liquidity,
uint256 _amount0Min,
uint256 _amount1Min
) internal returns (uint256 amount0, uint256 amount1) {
INonfungiblePositionManager.DecreaseLiquidityParams
memory params = INonfungiblePositionManager.DecreaseLiquidityParams(
_tokenId,
_liquidity,
_amount0Min,
_amount1Min,
block.timestamp
);
(amount0, amount1) = nftManager.decreaseLiquidity(params);
}
function _stake(
uint256 _tokenId,
IUniswapV3Staker.IncentiveKey memory _incentiveId

View File

@ -13,78 +13,61 @@ import {Events} from "./events.sol";
abstract contract UniswapResolver is Helpers, Events {
/**
* @dev Increase Liquidity
* @notice Increase Liquidity of NFT Position
* @param tokenId NFT LP Token ID.
* @param amountA tokenA amounts.
* @param amountB tokenB amounts.
* @param slippage slippage.
* @param getIds IDs to retrieve token amounts
* @param setId stores the liquidity amount
* @dev Deposit NFT token
* @notice Transfer deposited NFT token
* @param _tokenId NFT LP Token ID
*/
function deposit(
uint256 tokenId,
uint256 amountA,
uint256 amountB,
uint256 slippage,
uint256[] calldata getIds,
uint256 setId
)
function deposit(uint256 _tokenId)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
if (tokenId == 0) tokenId = _getLastNftId(address(this));
amountA = getUint(getIds[0], amountA);
amountB = getUint(getIds[1], amountB);
(
uint256 _liquidity,
uint256 _amtA,
uint256 _amtB
) = _addLiquidityWrapper(tokenId, amountA, amountB, slippage);
setUint(setId, _liquidity);
if (_tokenId == 0) _tokenId = _getLastNftId(address(this));
nftManager.safeTransferFrom(
address(this),
address(staker),
_tokenId,
""
);
_eventName = "LogDeposit(uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(tokenId, _liquidity, _amtA, _amtB);
_eventName = "LogDeposit(uint256)";
_eventParam = abi.encode(_tokenId);
}
/**
* @dev Decrease Liquidity
* @notice Decrease Liquidity of NFT Position
* @param tokenId NFT LP Token ID.
* @param liquidity LP Token amount.
* @param amountAMin Min amount of tokenA.
* @param amountBMin Min amount of tokenB.
* @param getId ID to retrieve LP token amounts
* @param setIds stores the amount of output tokens
* @dev Deposit Transfer
* @notice Transfer deposited NFT token
* @param _tokenId NFT LP Token ID
* @param _to address to transfer
*/
function withdraw(
uint256 tokenId,
uint256 liquidity,
uint256 amountAMin,
uint256 amountBMin,
uint256 getId,
uint256[] calldata setIds
)
function transferDeposit(uint256 _tokenId, address _to)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
if (tokenId == 0) tokenId = _getLastNftId(address(this));
uint128 _liquidity = uint128(getUint(getId, liquidity));
if (_tokenId == 0) _tokenId = _getLastNftId(address(this));
staker.transferDeposit(_tokenId, _to);
(uint256 _amtA, uint256 _amtB) = _decreaseLiquidity(
tokenId,
_liquidity,
amountAMin,
amountBMin
);
_eventName = "LogDepositTransfer(uint256,address)";
_eventParam = abi.encode(_tokenId, _to);
}
setUint(setIds[0], _amtA);
setUint(setIds[1], _amtB);
/**
* @dev Withdraw NFT LP token
* @notice Withdraw NFT LP token from staking pool
* @param _tokenId NFT LP Token ID
* @param _to address to transfer
*/
function withdraw(uint256 _tokenId, address _to)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
if (_tokenId == 0) _tokenId = _getLastNftId(address(this));
staker.withdrawToken(_tokenId, _to, "");
_eventName = "LogWithdraw(uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(tokenId, _liquidity, _amtA, _amtB);
_eventName = "LogWithdraw(uint256,address)";
_eventParam = abi.encode(_tokenId, _to);
}
/**
@ -107,12 +90,6 @@ abstract contract UniswapResolver is Helpers, Events {
payable
returns (string memory _eventName, bytes memory _eventParam)
{
nftManager.safeTransferFrom(
address(this),
address(staker),
_tokenId,
""
);
address poolAddr = getPoolAddress(_tokenId);
IUniswapV3Pool pool = IUniswapV3Pool(poolAddr);

View File

@ -186,6 +186,13 @@ describe("UniswapV3", function () {
it("Should stake successfully", async function () {
const spells = [
{
connector: connectorStaker,
method: "deposit",
args: [
tokenIds[0]
],
},
{
connector: connectorStaker,
method: "stake",