mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
fix issues
This commit is contained in:
parent
77bcdb8049
commit
04a26554b7
|
@ -1,27 +1,27 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
contract Events {
|
||||
event LogNewPositionMint(
|
||||
uint256 indexed tokenId,
|
||||
uint256 amtA,
|
||||
uint256 amtB,
|
||||
uint256 liquidity
|
||||
);
|
||||
|
||||
event LogAddLiquidity(
|
||||
uint256 indexed tokenId,
|
||||
uint256 amtA,
|
||||
uint256 amtB,
|
||||
uint256 liquidity
|
||||
);
|
||||
|
||||
event LogDecreaseLiquidity(
|
||||
event LogMint(
|
||||
uint256 indexed tokenId,
|
||||
uint256 liquidity,
|
||||
uint256 amtA,
|
||||
uint256 amtB
|
||||
);
|
||||
|
||||
event LogDeposit(
|
||||
uint256 indexed tokenId,
|
||||
uint256 liquidity,
|
||||
uint256 amountA,
|
||||
uint256 amountB
|
||||
);
|
||||
|
||||
event LogWithdraw(
|
||||
uint256 indexed tokenId,
|
||||
uint256 liquidity,
|
||||
uint256 amountA,
|
||||
uint256 amountB
|
||||
);
|
||||
|
||||
event Swap(
|
||||
address indexed tokenIn,
|
||||
address indexed tokenOut,
|
||||
|
@ -29,5 +29,5 @@ contract Events {
|
|||
uint256 amtOut
|
||||
);
|
||||
|
||||
event BurnPosition(uint256 tokenId);
|
||||
event LogBurn(uint256 tokenId);
|
||||
}
|
||||
|
|
|
@ -30,6 +30,19 @@ abstract contract Helpers is DSMath, Basic {
|
|||
uint256 slippage;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Get Last NFT Index
|
||||
* @param user: User address
|
||||
*/
|
||||
function _getLastNftId(address user)
|
||||
internal
|
||||
view
|
||||
returns (uint256 tokenId)
|
||||
{
|
||||
uint256 len = nftManager.balanceOf(user);
|
||||
tokenId = nftManager.tokenOfOwnerByIndex(user, len - 1);
|
||||
}
|
||||
|
||||
function getMinAmount(
|
||||
TokenInterface token,
|
||||
uint256 amt,
|
||||
|
@ -48,8 +61,8 @@ abstract contract Helpers is DSMath, Basic {
|
|||
returns (
|
||||
uint256 tokenId,
|
||||
uint128 liquidity,
|
||||
uint256 amount0,
|
||||
uint256 amount1
|
||||
uint256 amountA,
|
||||
uint256 amountB
|
||||
)
|
||||
{
|
||||
(TokenInterface _token0, TokenInterface _token1) = changeEthAddress(
|
||||
|
@ -64,10 +77,8 @@ abstract contract Helpers is DSMath, Basic {
|
|||
? getTokenBal(TokenInterface(params.tokenB))
|
||||
: params.amtB;
|
||||
|
||||
uint256 isEth = address(_token0) == wethAddr ? 0 : 2;
|
||||
isEth = address(_token1) == wethAddr ? 1 : 2;
|
||||
convertEthToWeth(isEth == 0, _token0, _amount0);
|
||||
convertEthToWeth(isEth == 1, _token1, _amount1);
|
||||
convertEthToWeth(address(_token0) == wethAddr, _token0, _amount0);
|
||||
convertEthToWeth(address(_token1) == wethAddr, _token1, _amount1);
|
||||
|
||||
approve(_token0, address(nftManager), _amount0);
|
||||
approve(_token1, address(nftManager), _amount1);
|
||||
|
@ -90,7 +101,7 @@ abstract contract Helpers is DSMath, Basic {
|
|||
block.timestamp
|
||||
);
|
||||
|
||||
(tokenId, liquidity, amount0, amount1) = nftManager.mint(params);
|
||||
(tokenId, liquidity, amountA, amountB) = nftManager.mint(params);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -103,10 +114,10 @@ abstract contract Helpers is DSMath, Basic {
|
|||
) internal {
|
||||
(, , address token0, address token1, , , , , , , , ) = nftManager
|
||||
.positions(_tokenId);
|
||||
uint256 isEth = token0 == wethAddr ? 0 : 2;
|
||||
isEth = token1 == wethAddr ? 1 : 2;
|
||||
convertEthToWeth(isEth == 0, TokenInterface(token0), _amount0);
|
||||
convertEthToWeth(isEth == 1, TokenInterface(token1), _amount1);
|
||||
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);
|
||||
}
|
||||
|
@ -118,8 +129,7 @@ abstract contract Helpers is DSMath, Basic {
|
|||
uint256 _tokenId,
|
||||
uint256 _amount0,
|
||||
uint256 _amount1,
|
||||
uint256 _amount0Min,
|
||||
uint256 _amount1Min
|
||||
uint256 _slippage
|
||||
)
|
||||
internal
|
||||
returns (
|
||||
|
@ -129,6 +139,20 @@ abstract contract Helpers is DSMath, Basic {
|
|||
)
|
||||
{
|
||||
_checkETH(_tokenId, _amount0, _amount1);
|
||||
|
||||
(, , address _token0, address _token1, , , , , , , , ) = nftManager
|
||||
.positions(_tokenId);
|
||||
|
||||
uint256 _amount0Min = getMinAmount(
|
||||
TokenInterface(_token0),
|
||||
_amount0,
|
||||
_slippage
|
||||
);
|
||||
uint256 _amount1Min = getMinAmount(
|
||||
TokenInterface(_token1),
|
||||
_amount1,
|
||||
_slippage
|
||||
);
|
||||
INonfungiblePositionManager.IncreaseLiquidityParams
|
||||
memory params = INonfungiblePositionManager.IncreaseLiquidityParams(
|
||||
_tokenId,
|
||||
|
@ -167,15 +191,15 @@ abstract contract Helpers is DSMath, Basic {
|
|||
*/
|
||||
function _collect(
|
||||
uint256 _tokenId,
|
||||
uint256 _amount0Max,
|
||||
uint256 _amount1Max
|
||||
uint128 _amount0Max,
|
||||
uint128 _amount1Max
|
||||
) internal returns (uint256 amount0, uint256 amount1) {
|
||||
INonfungiblePositionManager.CollectParams
|
||||
memory params = INonfungiblePositionManager.CollectParams(
|
||||
_tokenId,
|
||||
address(this),
|
||||
uint128(_amount0Max),
|
||||
uint128(_amount1Max)
|
||||
_amount0Max,
|
||||
_amount1Max
|
||||
);
|
||||
(amount0, amount1) = nftManager.collect(params);
|
||||
}
|
||||
|
|
|
@ -11,7 +11,6 @@ import {Helpers} from "./helpers.sol";
|
|||
import {Events} from "./events.sol";
|
||||
|
||||
abstract contract UniswapResolver is Helpers, Events {
|
||||
uint256 private _lastMintIndex = 1;
|
||||
/**
|
||||
* @dev Mint New Position
|
||||
* @param params: parameter for mint.
|
||||
|
@ -33,15 +32,21 @@ abstract contract UniswapResolver is Helpers, Events {
|
|||
(
|
||||
uint256 _tokenId,
|
||||
uint256 liquidity,
|
||||
uint256 _amtA,
|
||||
uint256 _amtB
|
||||
uint256 amountA,
|
||||
uint256 amountB
|
||||
) = _mint(params);
|
||||
|
||||
setUint(setId, liquidity);
|
||||
|
||||
_lastMintIndex = _tokenId;
|
||||
|
||||
_eventName = "LogNewPositionMint(uint256,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(_tokenId, _amtA, _amtB, liquidity);
|
||||
_eventName = "LogMint(uint256,uint256,uint256,uint256,int24,int24)";
|
||||
_eventParam = abi.encode(
|
||||
_tokenId,
|
||||
liquidity,
|
||||
amountA,
|
||||
amountB,
|
||||
params.tickLower,
|
||||
params.tickUpper
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -49,17 +54,15 @@ abstract contract UniswapResolver is Helpers, Events {
|
|||
* @param tokenId: NFT LP Token ID.
|
||||
* @param amountA: tokenA amounts.
|
||||
* @param amountB: tokenB amounts.
|
||||
* @param amountAMin: Min amount of tokenA.
|
||||
* @param amountBMin: Min amount of tokenB.
|
||||
* @param slippage: slippage.
|
||||
* @param getIds: IDs to retrieve token amounts
|
||||
* @param setId: stores the amount of LP token
|
||||
* @param setId: stores the liquidity amount
|
||||
*/
|
||||
function addLiquidity(
|
||||
function deposit(
|
||||
uint256 tokenId,
|
||||
uint256 amountA,
|
||||
uint256 amountB,
|
||||
uint256 amountAMin,
|
||||
uint256 amountBMin,
|
||||
uint256 slippage,
|
||||
uint256[] calldata getIds,
|
||||
uint256 setId
|
||||
)
|
||||
|
@ -67,7 +70,7 @@ abstract contract UniswapResolver is Helpers, Events {
|
|||
payable
|
||||
returns (string memory _eventName, bytes memory _eventParam)
|
||||
{
|
||||
if (tokenId == 0) tokenId = _lastMintIndex;
|
||||
if (tokenId == 0) tokenId = _getLastNftId(address(this));
|
||||
amountA = getUint(getIds[0], amountA);
|
||||
amountB = getUint(getIds[1], amountB);
|
||||
|
||||
|
@ -75,13 +78,12 @@ abstract contract UniswapResolver is Helpers, Events {
|
|||
tokenId,
|
||||
amountA,
|
||||
amountB,
|
||||
amountAMin,
|
||||
amountBMin
|
||||
slippage
|
||||
);
|
||||
setUint(setId, _liquidity);
|
||||
|
||||
_eventName = "LogAddLiquidity(uint256,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(tokenId, _amtA, _amtB, _liquidity);
|
||||
_eventName = "LogDeposit(uint256,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(tokenId, _liquidity, _amtA, _amtB);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -93,9 +95,9 @@ abstract contract UniswapResolver is Helpers, Events {
|
|||
* @param getId: ID to retrieve LP token amounts
|
||||
* @param setIds: stores the amount of output tokens
|
||||
*/
|
||||
function decreaseLiquidity(
|
||||
function withdraw(
|
||||
uint256 tokenId,
|
||||
uint128 liquidity,
|
||||
uint256 liquidity,
|
||||
uint256 amountAMin,
|
||||
uint256 amountBMin,
|
||||
uint256 getId,
|
||||
|
@ -105,7 +107,7 @@ abstract contract UniswapResolver is Helpers, Events {
|
|||
payable
|
||||
returns (string memory _eventName, bytes memory _eventParam)
|
||||
{
|
||||
if (tokenId == 0) tokenId = _lastMintIndex;
|
||||
if (tokenId == 0) tokenId = _getLastNftId(address(this));
|
||||
uint128 _liquidity = uint128(getUint(getId, liquidity));
|
||||
|
||||
(uint256 _amtA, uint256 _amtB) = _decreaseLiquidity(
|
||||
|
@ -118,45 +120,10 @@ abstract contract UniswapResolver is Helpers, Events {
|
|||
setUint(setIds[0], _amtA);
|
||||
setUint(setIds[1], _amtB);
|
||||
|
||||
_eventName = "LogDecreaseLiquidity(uint256,uint256,uint256,uint256)";
|
||||
_eventName = "LogWithdraw(uint256,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(tokenId, _liquidity, _amtA, _amtB);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Swap Function
|
||||
* @param tokenIn: Token Address for input
|
||||
* @param tokenOut: Token Address for output
|
||||
* @param fee: Fee amount
|
||||
* @param amountIn: Amount for input
|
||||
* @param getId: ID to retrieve amountIn
|
||||
* @param setId: stores the amount of Out token
|
||||
*/
|
||||
function swapToken(
|
||||
address tokenIn,
|
||||
address tokenOut,
|
||||
uint24 fee,
|
||||
uint256 amountIn,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
)
|
||||
external
|
||||
payable
|
||||
returns (string memory _eventName, bytes memory _eventParam)
|
||||
{
|
||||
uint256 _amountIn = getUint(getId, amountIn);
|
||||
uint256 amountOut = _exactInputSingle(
|
||||
tokenIn,
|
||||
tokenOut,
|
||||
fee,
|
||||
_amountIn
|
||||
);
|
||||
|
||||
setUint(setId, amountOut);
|
||||
|
||||
_eventName = "Swap(address,address,uint256,uint256)";
|
||||
_eventParam = abi.encode(tokenIn, tokenOut, _amountIn, amountOut);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Collect function
|
||||
* @param tokenId: NFT LP Token ID.
|
||||
|
@ -167,8 +134,8 @@ abstract contract UniswapResolver is Helpers, Events {
|
|||
*/
|
||||
function collect(
|
||||
uint256 tokenId,
|
||||
uint128 amount0Max,
|
||||
uint128 amount1Max,
|
||||
uint256 amount0Max,
|
||||
uint256 amount1Max,
|
||||
uint256[] calldata getIds,
|
||||
uint256[] calldata setIds
|
||||
)
|
||||
|
@ -176,7 +143,7 @@ abstract contract UniswapResolver is Helpers, Events {
|
|||
payable
|
||||
returns (string memory _eventName, bytes memory _eventParam)
|
||||
{
|
||||
if (tokenId == 0) tokenId = _lastMintIndex;
|
||||
if (tokenId == 0) tokenId = _getLastNftId(address(this));
|
||||
uint128 _amount0Max = uint128(getUint(getIds[0], amount0Max));
|
||||
uint128 _amount1Max = uint128(getUint(getIds[1], amount1Max));
|
||||
(uint256 amount0, uint256 amount1) = _collect(
|
||||
|
@ -187,21 +154,21 @@ abstract contract UniswapResolver is Helpers, Events {
|
|||
|
||||
setUint(setIds[0], amount0);
|
||||
setUint(setIds[1], amount1);
|
||||
_eventName = "Collect(uint256,uint256,uint256)";
|
||||
_eventName = "LogCollect(uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(tokenId, amount0, amount1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Burn Function
|
||||
*/
|
||||
function burnNFT(uint256 tokenId)
|
||||
function burn(uint256 tokenId)
|
||||
external
|
||||
payable
|
||||
returns (string memory _eventName, bytes memory _eventParam)
|
||||
{
|
||||
if (tokenId == 0) tokenId = _lastMintIndex;
|
||||
if (tokenId == 0) tokenId = _getLastNftId(address(this));
|
||||
_burn(tokenId);
|
||||
_eventName = "BurnPosition(uint256)";
|
||||
_eventName = "LogBurnPosition(uint256)";
|
||||
_eventParam = abi.encode(tokenId);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -131,8 +131,8 @@ describe("UniswapV3", function () {
|
|||
method: "mint",
|
||||
args: [
|
||||
{
|
||||
tokenB: USDT_ADDR,
|
||||
tokenA: DAI_ADDR,
|
||||
tokenB: USDT_ADDR,
|
||||
fee: FeeAmount.MEDIUM,
|
||||
tickUpper: getMaxTick(TICK_SPACINGS[FeeAmount.MEDIUM]),
|
||||
tickLower: getMinTick(TICK_SPACINGS[FeeAmount.MEDIUM]),
|
||||
|
@ -143,6 +143,24 @@ describe("UniswapV3", function () {
|
|||
getIds,
|
||||
setId
|
||||
],
|
||||
},
|
||||
{
|
||||
connector: connectorName,
|
||||
method: "mint",
|
||||
args: [
|
||||
{
|
||||
tokenA: ethAddress,
|
||||
tokenB: USDT_ADDR,
|
||||
fee: FeeAmount.MEDIUM,
|
||||
tickUpper: getMaxTick(TICK_SPACINGS[FeeAmount.MEDIUM]),
|
||||
tickLower: getMinTick(TICK_SPACINGS[FeeAmount.MEDIUM]),
|
||||
amtA: ethAmount,
|
||||
amtB: usdtAmount,
|
||||
slippage: "300000000000000000"
|
||||
},
|
||||
getIds,
|
||||
setId
|
||||
],
|
||||
}
|
||||
]
|
||||
|
||||
|
@ -150,11 +168,11 @@ describe("UniswapV3", function () {
|
|||
let receipt = await tx.wait()
|
||||
let castEvent = new Promise((resolve, reject) => {
|
||||
dsaWallet0.on('LogCast', (origin, sender, value, targetNames, targets, eventNames, eventParams, event) => {
|
||||
const params = abiCoder.decode(["uint256", "uint256", "uint256", "uint256"], eventParams[0]);
|
||||
const params1 = abiCoder.decode(["uint256", "uint256", "uint256", "uint256"], eventParams[1]);
|
||||
const params = abiCoder.decode(["uint256", "uint256", "uint256", "uint256", "int24", "int24"], eventParams[0]);
|
||||
const params1 = abiCoder.decode(["uint256", "uint256", "uint256", "uint256", "int24", "int24"], eventParams[2]);
|
||||
tokenIds.push(params[0]);
|
||||
tokenIds.push(params1[0]);
|
||||
liquidities.push(params[3]);
|
||||
liquidities.push(params[1]);
|
||||
event.removeListener();
|
||||
|
||||
resolve({
|
||||
|
@ -174,7 +192,7 @@ describe("UniswapV3", function () {
|
|||
expect(data.liquidity).to.be.equals(liquidities[0]);
|
||||
})
|
||||
|
||||
it("Should increaseLiquidity successfully", async function () {
|
||||
it("Should deposit successfully", async function () {
|
||||
const daiAmount = ethers.utils.parseEther("400") // 1 ETH
|
||||
const ethAmount = ethers.utils.parseEther("0.1") // 1 ETH
|
||||
const usdtAmount = ethers.utils.parseEther("400") / Math.pow(10, 12) // 1 ETH
|
||||
|
@ -186,13 +204,12 @@ describe("UniswapV3", function () {
|
|||
const spells = [
|
||||
{
|
||||
connector: connectorName,
|
||||
method: "addLiquidity",
|
||||
method: "deposit",
|
||||
args: [
|
||||
tokenIds[0],
|
||||
daiAmount,
|
||||
ethAmount,
|
||||
0,
|
||||
0,
|
||||
"500000000000000000",
|
||||
getIds,
|
||||
setId
|
||||
],
|
||||
|
@ -205,7 +222,7 @@ describe("UniswapV3", function () {
|
|||
let castEvent = new Promise((resolve, reject) => {
|
||||
dsaWallet0.on('LogCast', (origin, sender, value, targetNames, targets, eventNames, eventParams, event) => {
|
||||
const params = abiCoder.decode(["uint256", "uint256", "uint256", "uint256"], eventParams[0]);
|
||||
liquidities[0] = liquidities[0].add(params[3]);
|
||||
liquidities[0] = liquidities[0].add(params[1]);
|
||||
event.removeListener();
|
||||
|
||||
resolve({
|
||||
|
@ -224,7 +241,7 @@ describe("UniswapV3", function () {
|
|||
expect(data.liquidity).to.be.equals(liquidities[0]);
|
||||
})
|
||||
|
||||
it("Should decreaseLiquidity successfully", async function () {
|
||||
it("Should withdraw successfully", async function () {
|
||||
|
||||
const getId = "0"
|
||||
const setIds = ["0", "0"]
|
||||
|
@ -235,7 +252,7 @@ describe("UniswapV3", function () {
|
|||
const spells = [
|
||||
{
|
||||
connector: connectorName,
|
||||
method: "decreaseLiquidity",
|
||||
method: "withdraw",
|
||||
args: [
|
||||
tokenIds[0],
|
||||
data.liquidity,
|
||||
|
@ -247,7 +264,7 @@ describe("UniswapV3", function () {
|
|||
},
|
||||
{
|
||||
connector: connectorName,
|
||||
method: "decreaseLiquidity",
|
||||
method: "withdraw",
|
||||
args: [
|
||||
0,
|
||||
data1.liquidity,
|
||||
|
@ -296,7 +313,7 @@ describe("UniswapV3", function () {
|
|||
const spells = [
|
||||
{
|
||||
connector: connectorName,
|
||||
method: "burnNFT",
|
||||
method: "burn",
|
||||
args: [
|
||||
tokenIds[0]
|
||||
],
|
||||
|
|
Loading…
Reference in New Issue
Block a user