fix mint function

This commit is contained in:
cryptoDev222 2021-08-09 15:08:48 -05:00
parent 1ae2a5c9e5
commit cf543bd26f
2 changed files with 63 additions and 28 deletions

View File

@ -21,26 +21,54 @@ abstract contract Helpers is DSMath, Basic {
ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564); ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564);
struct MintParams { struct MintParams {
address token0; address tokenA;
address token1; address tokenB;
uint24 fee; uint24 fee;
int24 tickLower; int24 tickLower;
int24 tickUpper; int24 tickUpper;
uint256 amt1; uint256 amtA;
uint256 unitAmt; uint256 unitAmt;
uint slippage; uint256 slippage;
} }
function getMinAmount( function getMinAmount(
TokenInterface token, TokenInterface token,
uint amt, uint256 amt,
uint slippage uint256 slippage
) internal view returns(uint minAmt) { ) internal view returns (uint256 minAmt) {
uint _amt18 = convertTo18(token.decimals(), amt); uint256 _amt18 = convertTo18(token.decimals(), amt);
minAmt = wmul(_amt18, sub(WAD, slippage)); minAmt = wmul(_amt18, sub(WAD, slippage));
minAmt = convert18ToDec(token.decimals(), minAmt); minAmt = convert18ToDec(token.decimals(), minAmt);
} }
function sortTokens(
address tokenA,
address tokenB,
uint256 amtA,
uint256 amtB
)
internal
pure
returns (
TokenInterface token0,
TokenInterface token1,
uint256 amt0,
uint256 amt1
)
{
if (tokenA > tokenB) {
token1 = TokenInterface(tokenA);
token0 = TokenInterface(tokenB);
amt1 = amtA;
amt0 = amtB;
} else {
token0 = TokenInterface(tokenA);
token1 = TokenInterface(tokenB);
amt0 = amtA;
amt1 = amtB;
}
}
/** /**
* @dev Mint function which interact with Uniswap v3 * @dev Mint function which interact with Uniswap v3
*/ */
@ -54,28 +82,35 @@ abstract contract Helpers is DSMath, Basic {
) )
{ {
(TokenInterface _token0, TokenInterface _token1) = changeEthAddress( (TokenInterface _token0, TokenInterface _token1) = changeEthAddress(
params.token0, params.tokenA,
params.token1 params.tokenB
); );
uint256 _amount1 = params.amt1 == uint256(-1) uint256 _amount0 = params.amtA == uint256(-1)
? getTokenBal(TokenInterface(params.token1)) ? getTokenBal(TokenInterface(params.tokenA))
: params.amt1; : params.amtA;
uint256 _amount0 = convert18ToDec( uint256 _amount1 = convert18ToDec(
_token1.decimals(), _token1.decimals(),
wmul(params.unitAmt, convertTo18(_token1.decimals(), _amount1)) wmul(params.unitAmt, convertTo18(_token1.decimals(), _amount0))
); );
_token0.approve(address(nftManager), _amount0); (_token0, _token1, _amount0, _amount1) = sortTokens(
_token1.approve(address(nftManager), _amount1); params.tokenA,
params.tokenB,
_amount0,
_amount1
);
uint _minAmt0 = getMinAmount(_token0, _amount0, params.slippage); approve(_token0, address(nftManager), _amount0);
uint _minAmt1 = getMinAmount(_token1, _amount1, params.slippage); approve(_token1, address(nftManager), _amount1);
uint256 _minAmt0 = getMinAmount(_token0, _amount0, params.slippage);
uint256 _minAmt1 = getMinAmount(_token1, _amount1, params.slippage);
INonfungiblePositionManager.MintParams INonfungiblePositionManager.MintParams
memory params = INonfungiblePositionManager.MintParams( memory params = INonfungiblePositionManager.MintParams(
params.tokenA, address(_token0),
params.tokenB, address(_token1),
params.fee, params.fee,
params.tickLower, params.tickLower,
params.tickUpper, params.tickUpper,
@ -97,7 +132,7 @@ abstract contract Helpers is DSMath, Basic {
uint256 _amount0Desired, uint256 _amount0Desired,
uint256 _amount1Desired, uint256 _amount1Desired,
uint256 _amount0Min, uint256 _amount0Min,
uint256 _amount1Min, uint256 _amount1Min
) )
internal internal
returns ( returns (
@ -125,14 +160,14 @@ abstract contract Helpers is DSMath, Basic {
uint256 _tokenId, uint256 _tokenId,
uint128 _liquidity, uint128 _liquidity,
uint256 _amount0Min, uint256 _amount0Min,
uint256 _amount1Min, uint256 _amount1Min
) internal returns (uint256 amount0, uint256 amount1) { ) internal returns (uint256 amount0, uint256 amount1) {
INonfungiblePositionManager.DecreaseLiquidityParams INonfungiblePositionManager.DecreaseLiquidityParams
memory params = INonfungiblePositionManager.DecreaseLiquidityParams( memory params = INonfungiblePositionManager.DecreaseLiquidityParams(
_tokenId, _tokenId,
_liquidity, _liquidity,
_amount0Min, _amount0Min,
_amount0Min, _amount1Min,
block.timestamp block.timestamp
); );
(amount0, amount1) = nftManager.decreaseLiquidity(params); (amount0, amount1) = nftManager.decreaseLiquidity(params);

View File

@ -26,8 +26,8 @@ abstract contract UniswapResolver is Helpers, Events {
payable payable
returns (string memory _eventName, bytes memory _eventParam) returns (string memory _eventName, bytes memory _eventParam)
{ {
uint256 _amt = getUint(getId, params.amtA); params.amtA = getUint(getId, params.amtA);
params.amtA = _amt;
( (
uint256 _tokenID, uint256 _tokenID,
uint256 _amtA, uint256 _amtA,
@ -170,8 +170,8 @@ abstract contract UniswapResolver is Helpers, Events {
payable payable
returns (string memory _eventName, bytes memory _eventParam) returns (string memory _eventName, bytes memory _eventParam)
{ {
uint128 _amount0Max = getUint(getIds[0], amount0Max); uint128 _amount0Max = uint128(getUint(getIds[0], amount0Max));
uint128 _amount1Max = getUint(getIds[1], amount1Max); uint128 _amount1Max = uint128(getUint(getIds[1], amount1Max));
(uint256 amount0, uint256 amount1) = _collect( (uint256 amount0, uint256 amount1) = _collect(
tokenId, tokenId,
_amount0Max, _amount0Max,