mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
Limit order connector added
This commit is contained in:
parent
8feed9c9e6
commit
219ec18ae7
24
contracts/polygon/connectors/limitOrder/events.sol
Normal file
24
contracts/polygon/connectors/limitOrder/events.sol
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
pragma solidity ^0.8.1;
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
contract Events {
|
||||||
|
event LogCreate(
|
||||||
|
uint256 indexed tokenId,
|
||||||
|
uint256 liquidity,
|
||||||
|
uint256 amount,
|
||||||
|
int24 tickLower,
|
||||||
|
int24 tickUpper
|
||||||
|
);
|
||||||
|
|
||||||
|
event LogWithdrawMid(
|
||||||
|
uint256 indexed tokenId,
|
||||||
|
uint256 liquidity,
|
||||||
|
uint256 amountA,
|
||||||
|
uint256 amountB
|
||||||
|
);
|
||||||
|
|
||||||
|
event LogWithdrawFull(
|
||||||
|
uint256 indexed tokenId,
|
||||||
|
uint256 liquidity
|
||||||
|
);
|
||||||
|
}
|
86
contracts/polygon/connectors/limitOrder/helpers.sol
Normal file
86
contracts/polygon/connectors/limitOrder/helpers.sol
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
pragma solidity ^0.8.1;
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
import { Basic } from "../../common/basic.sol";
|
||||||
|
import "./interface.sol";
|
||||||
|
|
||||||
|
contract Helpers is Basic {
|
||||||
|
|
||||||
|
UniLimitOrder limitCon_ = UniLimitOrder(0xfC428E6535dC5Fee30fb57cFc93EBB1D92fdCf6e);
|
||||||
|
|
||||||
|
function sortTokenAddress(address _token0, address _token1)
|
||||||
|
internal
|
||||||
|
pure
|
||||||
|
returns (address token0, address token1)
|
||||||
|
{
|
||||||
|
if (_token0 > _token1) {
|
||||||
|
(token0, token1) = (_token1, _token0);
|
||||||
|
} else {
|
||||||
|
(token0, token1) = (_token0, _token1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct MintParams {
|
||||||
|
address token0;
|
||||||
|
address token1;
|
||||||
|
uint24 fee;
|
||||||
|
int24 tickLower;
|
||||||
|
int24 tickUpper;
|
||||||
|
uint256 amount;
|
||||||
|
bool token0to1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Mint function which interact with Uniswap v3
|
||||||
|
*/
|
||||||
|
function _createPosition (MintParams memory params_)
|
||||||
|
internal
|
||||||
|
returns (
|
||||||
|
uint256 tokenId_,
|
||||||
|
uint128 liquidity_,
|
||||||
|
uint256 mintAmount_
|
||||||
|
)
|
||||||
|
{
|
||||||
|
uint256 amountSend_;
|
||||||
|
|
||||||
|
(TokenInterface token0_, TokenInterface token1_) = changeMaticAddress(
|
||||||
|
params_.token0,
|
||||||
|
params_.token1
|
||||||
|
);
|
||||||
|
|
||||||
|
if(params_.token0to1){
|
||||||
|
amountSend_ = params_.amount == type(uint128).max ? getTokenBal(TokenInterface(params_.token0)) : params_.amount;
|
||||||
|
convertMaticToWmatic(address(token0_) == wmaticAddr, token0_, amountSend_);
|
||||||
|
approve(TokenInterface(token0_), address(limitCon_), amountSend_);
|
||||||
|
} else {
|
||||||
|
amountSend_ = params_.amount == type(uint128).max ? getTokenBal(TokenInterface(params_.token1)) : params_.amount;
|
||||||
|
convertMaticToWmatic(address(token1_) == wmaticAddr, token1_, amountSend_);
|
||||||
|
approve(TokenInterface(token1_), address(limitCon_), amountSend_);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
(address token0, ) = sortTokenAddress(
|
||||||
|
address(token0_),
|
||||||
|
address(token1_)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (token0 != address(token0_)) {
|
||||||
|
(token0_, token1_) = (token1_, token0_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
UniLimitOrder.MintParams memory parameter =
|
||||||
|
UniLimitOrder.MintParams(
|
||||||
|
address(token0_),
|
||||||
|
address(token1_),
|
||||||
|
params_.fee,
|
||||||
|
params_.tickLower,
|
||||||
|
params_.tickUpper,
|
||||||
|
amountSend_,
|
||||||
|
params_.token0to1
|
||||||
|
);
|
||||||
|
|
||||||
|
(tokenId_, liquidity_, mintAmount_) = limitCon_.createPosition(parameter);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
43
contracts/polygon/connectors/limitOrder/interface.sol
Normal file
43
contracts/polygon/connectors/limitOrder/interface.sol
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
pragma solidity ^0.8.1;
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
interface UniLimitOrder {
|
||||||
|
|
||||||
|
function NftToOwner(uint256) external view returns (address);
|
||||||
|
function token0to1(uint256) external view returns (bool);
|
||||||
|
|
||||||
|
struct MintParams {
|
||||||
|
address token0;
|
||||||
|
address token1;
|
||||||
|
uint24 fee;
|
||||||
|
int24 tickLower;
|
||||||
|
int24 tickUpper;
|
||||||
|
uint256 amount;
|
||||||
|
bool token0To1;
|
||||||
|
}
|
||||||
|
|
||||||
|
function createPosition(
|
||||||
|
MintParams memory params_
|
||||||
|
) external
|
||||||
|
returns (
|
||||||
|
uint256 tokenId_,
|
||||||
|
uint128 liquidity_,
|
||||||
|
uint256 mintAmount_
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
function closeMidPosition(
|
||||||
|
uint256 tokenId_,
|
||||||
|
uint256 amount0Min_,
|
||||||
|
uint256 amount1Min_
|
||||||
|
)
|
||||||
|
external
|
||||||
|
returns (uint128 liquidity_, uint256 amount0_, uint256 amount1_);
|
||||||
|
|
||||||
|
function closeFullPosition(
|
||||||
|
uint256 tokenId_
|
||||||
|
)
|
||||||
|
external
|
||||||
|
returns (uint128 liquidity_);
|
||||||
|
|
||||||
|
}
|
103
contracts/polygon/connectors/limitOrder/main.sol
Normal file
103
contracts/polygon/connectors/limitOrder/main.sol
Normal file
|
@ -0,0 +1,103 @@
|
||||||
|
pragma solidity ^0.8.1;
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
import {Helpers} from "./helpers.sol";
|
||||||
|
import {UniLimitOrder} from "./interface.sol";
|
||||||
|
import {TokenInterface} from "../../common/interfaces.sol";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @title LimitOrderConnector.
|
||||||
|
* @dev Connector for Limit Order Swap on Uni V3.
|
||||||
|
*/
|
||||||
|
contract LimitOrderConnector is Helpers {
|
||||||
|
|
||||||
|
function createPosition(
|
||||||
|
address token0_,
|
||||||
|
address token1_,
|
||||||
|
uint24 fee_,
|
||||||
|
int24 tickLower_,
|
||||||
|
int24 tickUpper_,
|
||||||
|
uint256 amount_,
|
||||||
|
bool token0to1_,
|
||||||
|
uint256 getId_,
|
||||||
|
uint256 setId_
|
||||||
|
)
|
||||||
|
external
|
||||||
|
payable
|
||||||
|
returns (string memory eventName_, bytes memory eventParam_)
|
||||||
|
{
|
||||||
|
|
||||||
|
MintParams memory params_ = MintParams(
|
||||||
|
token0_,
|
||||||
|
token1_,
|
||||||
|
fee_,
|
||||||
|
tickLower_,
|
||||||
|
tickUpper_,
|
||||||
|
amount_,
|
||||||
|
token0to1_
|
||||||
|
);
|
||||||
|
|
||||||
|
params_.amount = getUint(getId_, amount_);
|
||||||
|
|
||||||
|
(
|
||||||
|
uint256 tokenId_,
|
||||||
|
uint256 liquidity_,
|
||||||
|
uint256 minAmount_
|
||||||
|
) = _createPosition(params_);
|
||||||
|
|
||||||
|
setUint(setId_, liquidity_);
|
||||||
|
|
||||||
|
eventName_ = "LogCreate(uint256,uint256,uint256,int24,int24)";
|
||||||
|
_eventParam = abi.encode(
|
||||||
|
tokenId_,
|
||||||
|
liquidity_,
|
||||||
|
minAmount_,
|
||||||
|
params_.tickLower,
|
||||||
|
params_.tickUpper
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function withdrawMid(
|
||||||
|
uint256 tokenId_,
|
||||||
|
uint256 amountAMin_,
|
||||||
|
uint256 amountBMin_,
|
||||||
|
uint256[] calldata setIds_
|
||||||
|
)
|
||||||
|
external
|
||||||
|
payable
|
||||||
|
returns (string memory eventName_, bytes memory eventParam_)
|
||||||
|
{
|
||||||
|
|
||||||
|
(uint128 liquidity_, uint256 amount0, uint256 amount1) = limitCon_.closeMidPosition(tokenId_, amountAMin_, amountBMin_);
|
||||||
|
|
||||||
|
setUint(setIds_[0], amount0);
|
||||||
|
setUint(setIds_[1], amount1);
|
||||||
|
|
||||||
|
eventName_ = "LogWithdrawMid(uint256,uint256,uint256,uint256)";
|
||||||
|
eventParam_ = abi.encode(tokenId_, liquidity_, amount0, amount1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function withdrawFull(
|
||||||
|
uint256 tokenId_,
|
||||||
|
uint256 setId_
|
||||||
|
)
|
||||||
|
external
|
||||||
|
payable
|
||||||
|
returns (string memory eventName_, bytes memory eventParam_)
|
||||||
|
{
|
||||||
|
|
||||||
|
(uint256 closeAmount_) = limitCon_.closeFullPosition(tokenId_);
|
||||||
|
|
||||||
|
setUint(setId_, closeAmount_);
|
||||||
|
|
||||||
|
eventName_ = "LogWithdrawFull(uint256,uint256)";
|
||||||
|
eventParam_ = abi.encode(tokenId_, closeAmount_);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
contract ConnectV2LimitOrder is LimitOrderConnector {
|
||||||
|
string public constant name = "Limit-Order-Connector";
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user