mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
added uniswap v3 swap router connector
This commit is contained in:
parent
a7cd775056
commit
442d6fe0bd
|
@ -0,0 +1,12 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
contract Events {
|
||||
event LogSwap(
|
||||
address indexed buyToken,
|
||||
address indexed sellToken,
|
||||
uint256 buyAmt,
|
||||
uint256 sellAmt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
import { TokenInterface } from "../../../common/interfaces.sol";
|
||||
import { DSMath } from "../../../common/math.sol";
|
||||
import { Basic } from "../../../common/basic.sol";
|
||||
|
||||
|
||||
abstract contract Helpers is DSMath, Basic {
|
||||
/**
|
||||
* @dev UniswapV3 Swap Router Address
|
||||
*/
|
||||
address internal constant V3_SWAP_ROUTER_ADDRESS = 0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45;
|
||||
|
||||
|
||||
/**
|
||||
* @dev UniswapV3 swapHelper
|
||||
* @param swapData - Struct defined in interfaces.sol
|
||||
* @param ethAmt - Eth to swap for .value()
|
||||
*/
|
||||
function _SwapHelper(
|
||||
SwapData memory swapData,
|
||||
uint ethAmt
|
||||
) internal returns (uint buyAmt) {
|
||||
TokenInterface buyToken = swapData.buyToken;
|
||||
(uint _buyDec, uint _sellDec) = getTokensDec(buyToken, swapData.sellToken);
|
||||
uint _sellAmt18 = convertTo18(_sellDec, swapData._sellAmt);
|
||||
uint _slippageAmt = convert18ToDec(_buyDec, wmul(swapData.unitAmt, _sellAmt18));
|
||||
|
||||
uint initalBal = getTokenBal(buyToken);
|
||||
|
||||
// solium-disable-next-line security/no-call-value
|
||||
(bool success, ) = V3_SWAP_ROUTER_ADDRESS.call{value: ethAmt}(swapData.callData);
|
||||
if (!success) revert("uniswapV3-swap-failed");
|
||||
|
||||
uint finalBal = getTokenBal(buyToken);
|
||||
|
||||
buyAmt = sub(finalBal, initalBal);
|
||||
|
||||
require(_slippageAmt <= buyAmt, "Too much slippage");
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Gets the swapping data from auto router sdk
|
||||
* @param swapData Struct with multiple swap data defined in interfaces.sol
|
||||
* @param setId Set token amount at this ID in `InstaMemory` Contract.
|
||||
*/
|
||||
function _Swap(
|
||||
SwapData memory swapData,
|
||||
uint setId
|
||||
) internal returns (SwapData memory) {
|
||||
TokenInterface _sellAddr = swapData.sellToken;
|
||||
|
||||
uint ethAmt;
|
||||
if (address(_sellAddr) == ethAddr) {
|
||||
ethAmt = swapData._sellAmt;
|
||||
} else {
|
||||
approve(TokenInterface(_sellAddr), V3_SWAP_ROUTER_ADDRESS, swapData._sellAmt);
|
||||
}
|
||||
|
||||
swapData._buyAmt = _SwapHelper(swapData, ethAmt);
|
||||
setUint(setId, swapData._buyAmt);
|
||||
|
||||
return swapData;
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
import { TokenInterface } from "../../../common/interfaces.sol";
|
||||
|
||||
struct SwapData {
|
||||
TokenInterface sellToken;
|
||||
TokenInterface buyToken;
|
||||
uint _sellAmt;
|
||||
uint _buyAmt;
|
||||
uint unitAmt;
|
||||
bytes callData;
|
||||
}
|
53
contracts/mainnet/connectors/uniswap/v3_auto_router/main.sol
Normal file
53
contracts/mainnet/connectors/uniswap/v3_auto_router/main.sol
Normal file
|
@ -0,0 +1,53 @@
|
|||
pragma solidity ^0.7.0;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
/**
|
||||
* @title UniswapV3_autoRouter.
|
||||
* @dev DEX.
|
||||
*/
|
||||
|
||||
// import files from common directory
|
||||
import { TokenInterface , MemoryInterface } from "../../../common/interfaces.sol";
|
||||
import { Stores } from "../../../common/stores.sol";
|
||||
import { SwapData } from "./interface.sol";
|
||||
import { Helpers } from "./helpers.sol";
|
||||
import { Events } from "./events.sol";
|
||||
|
||||
abstract contract AutoRouter is Helpers, Events {
|
||||
/**
|
||||
* @dev Sell ETH/ERC20_Token using uniswap v3 auto router.
|
||||
* @notice Swap tokens from getting an optimized trade routes
|
||||
* @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param sellAddr The address of the token to sell.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param sellAmt The amount of the token to sell.
|
||||
* @param unitAmt The amount of buyAmt/sellAmt with slippage.
|
||||
* @param callData Data from Uniswap V3 auto router SDK.
|
||||
* @param setId ID stores the amount of token brought.
|
||||
*/
|
||||
function sell(
|
||||
address buyAddr,
|
||||
address sellAddr,
|
||||
uint sellAmt,
|
||||
uint unitAmt,
|
||||
bytes calldata callData,
|
||||
uint setId
|
||||
) external payable returns (string memory _eventName, bytes memory _eventParam) {
|
||||
SwapData memory swapData = SwapData({
|
||||
buyToken: TokenInterface(buyAddr),
|
||||
sellToken: TokenInterface(sellAddr),
|
||||
unitAmt: unitAmt,
|
||||
callData: callData,
|
||||
_sellAmt: sellAmt,
|
||||
_buyAmt: 0
|
||||
});
|
||||
|
||||
swapData = _Swap(swapData, setId);
|
||||
|
||||
_eventName = "LogSwap(address,address,uint256,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(buyAddr, sellAddr, swapData._buyAmt, swapData._sellAmt, 0, setId);
|
||||
}
|
||||
}
|
||||
|
||||
contract ConnectV2UniswapV3AutoRouter is AutoRouter {
|
||||
string public name = "UniswapV3-Auto-Router-v1";
|
||||
}
|
Loading…
Reference in New Issue
Block a user