mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
Merge pull request #175 from Instadapp/v3_router
[Opt + Arb] uniswap v3 swap router
This commit is contained in:
commit
018a1d28cc
|
@ -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,80 @@
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
import { TokenInterface } from "../../../common/interfaces.sol";
|
||||||
|
import { DSMath } from "../../../common/math.sol";
|
||||||
|
import { Basic } from "../../../common/basic.sol";
|
||||||
|
import { SwapData } from "./interface.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
|
||||||
|
*/
|
||||||
|
function _swapHelper(SwapData memory swapData)
|
||||||
|
internal
|
||||||
|
returns (uint256 buyAmt)
|
||||||
|
{
|
||||||
|
(uint256 _buyDec, uint256 _sellDec) = getTokensDec(
|
||||||
|
swapData.buyToken,
|
||||||
|
swapData.sellToken
|
||||||
|
);
|
||||||
|
uint256 _sellAmt18 = convertTo18(_sellDec, swapData._sellAmt);
|
||||||
|
uint256 _slippageAmt = convert18ToDec(
|
||||||
|
_buyDec,
|
||||||
|
wmul(swapData.unitAmt, _sellAmt18)
|
||||||
|
);
|
||||||
|
|
||||||
|
uint256 initalBal = getTokenBal(swapData.buyToken);
|
||||||
|
|
||||||
|
// solium-disable-next-line security/no-call-value
|
||||||
|
(bool success, ) = V3_SWAP_ROUTER_ADDRESS.call(swapData.callData);
|
||||||
|
if (!success) revert("uniswapV3-swap-failed");
|
||||||
|
|
||||||
|
uint256 finalBal = getTokenBal(swapData.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, uint256 setId)
|
||||||
|
internal
|
||||||
|
returns (SwapData memory)
|
||||||
|
{
|
||||||
|
bool isEthSellToken = address(swapData.sellToken) == ethAddr;
|
||||||
|
bool isEthBuyToken = address(swapData.buyToken) == ethAddr;
|
||||||
|
|
||||||
|
swapData.sellToken = isEthSellToken
|
||||||
|
? TokenInterface(wethAddr)
|
||||||
|
: swapData.sellToken;
|
||||||
|
swapData.buyToken = isEthBuyToken
|
||||||
|
? TokenInterface(wethAddr)
|
||||||
|
: swapData.buyToken;
|
||||||
|
|
||||||
|
convertEthToWeth(isEthSellToken, swapData.sellToken, swapData._sellAmt);
|
||||||
|
|
||||||
|
approve(
|
||||||
|
TokenInterface(swapData.sellToken),
|
||||||
|
V3_SWAP_ROUTER_ADDRESS,
|
||||||
|
swapData._sellAmt
|
||||||
|
);
|
||||||
|
|
||||||
|
swapData._buyAmt = _swapHelper(swapData);
|
||||||
|
|
||||||
|
convertWethToEth(isEthBuyToken, swapData.buyToken, swapData._buyAmt);
|
||||||
|
|
||||||
|
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;
|
||||||
|
uint256 _sellAmt;
|
||||||
|
uint256 _buyAmt;
|
||||||
|
uint256 unitAmt;
|
||||||
|
bytes callData;
|
||||||
|
}
|
|
@ -0,0 +1,64 @@
|
||||||
|
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,
|
||||||
|
uint256 sellAmt,
|
||||||
|
uint256 unitAmt,
|
||||||
|
bytes calldata callData,
|
||||||
|
uint256 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 ConnectV2UniswapV3AutoRouterArbitrum is AutoRouter {
|
||||||
|
string public name = "UniswapV3-Auto-Router-v1";
|
||||||
|
}
|
|
@ -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,80 @@
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
import { TokenInterface } from "../../../common/interfaces.sol";
|
||||||
|
import { DSMath } from "../../../common/math.sol";
|
||||||
|
import { Basic } from "../../../common/basic.sol";
|
||||||
|
import { SwapData } from "./interface.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
|
||||||
|
*/
|
||||||
|
function _swapHelper(SwapData memory swapData)
|
||||||
|
internal
|
||||||
|
returns (uint256 buyAmt)
|
||||||
|
{
|
||||||
|
(uint256 _buyDec, uint256 _sellDec) = getTokensDec(
|
||||||
|
swapData.buyToken,
|
||||||
|
swapData.sellToken
|
||||||
|
);
|
||||||
|
uint256 _sellAmt18 = convertTo18(_sellDec, swapData._sellAmt);
|
||||||
|
uint256 _slippageAmt = convert18ToDec(
|
||||||
|
_buyDec,
|
||||||
|
wmul(swapData.unitAmt, _sellAmt18)
|
||||||
|
);
|
||||||
|
|
||||||
|
uint256 initalBal = getTokenBal(swapData.buyToken);
|
||||||
|
|
||||||
|
// solium-disable-next-line security/no-call-value
|
||||||
|
(bool success, ) = V3_SWAP_ROUTER_ADDRESS.call(swapData.callData);
|
||||||
|
if (!success) revert("uniswapV3-swap-failed");
|
||||||
|
|
||||||
|
uint256 finalBal = getTokenBal(swapData.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, uint256 setId)
|
||||||
|
internal
|
||||||
|
returns (SwapData memory)
|
||||||
|
{
|
||||||
|
bool isEthSellToken = address(swapData.sellToken) == ethAddr;
|
||||||
|
bool isEthBuyToken = address(swapData.buyToken) == ethAddr;
|
||||||
|
|
||||||
|
swapData.sellToken = isEthSellToken
|
||||||
|
? TokenInterface(wethAddr)
|
||||||
|
: swapData.sellToken;
|
||||||
|
swapData.buyToken = isEthBuyToken
|
||||||
|
? TokenInterface(wethAddr)
|
||||||
|
: swapData.buyToken;
|
||||||
|
|
||||||
|
convertEthToWeth(isEthSellToken, swapData.sellToken, swapData._sellAmt);
|
||||||
|
|
||||||
|
approve(
|
||||||
|
TokenInterface(swapData.sellToken),
|
||||||
|
V3_SWAP_ROUTER_ADDRESS,
|
||||||
|
swapData._sellAmt
|
||||||
|
);
|
||||||
|
|
||||||
|
swapData._buyAmt = _swapHelper(swapData);
|
||||||
|
|
||||||
|
convertWethToEth(isEthBuyToken, swapData.buyToken, swapData._buyAmt);
|
||||||
|
|
||||||
|
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;
|
||||||
|
uint256 _sellAmt;
|
||||||
|
uint256 _buyAmt;
|
||||||
|
uint256 unitAmt;
|
||||||
|
bytes callData;
|
||||||
|
}
|
|
@ -0,0 +1,64 @@
|
||||||
|
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,
|
||||||
|
uint256 sellAmt,
|
||||||
|
uint256 unitAmt,
|
||||||
|
bytes calldata callData,
|
||||||
|
uint256 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 ConnectV2UniswapV3AutoRouterOptimism is AutoRouter {
|
||||||
|
string public name = "UniswapV3-Auto-Router-v1";
|
||||||
|
}
|
|
@ -13,6 +13,7 @@ import { HardhatUserConfig } from "hardhat/config";
|
||||||
import { NetworkUserConfig } from "hardhat/types";
|
import { NetworkUserConfig } from "hardhat/types";
|
||||||
import { utils } from "ethers";
|
import { utils } from "ethers";
|
||||||
import Web3 from "web3";
|
import Web3 from "web3";
|
||||||
|
import { network } from "hardhat";
|
||||||
|
|
||||||
dotenvConfig({ path: resolve(__dirname, "./.env") });
|
dotenvConfig({ path: resolve(__dirname, "./.env") });
|
||||||
|
|
||||||
|
@ -35,6 +36,7 @@ const PRIVATE_KEY = process.env.PRIVATE_KEY;
|
||||||
const ETHERSCAN_API = process.env.ETHERSCAN_API_KEY;
|
const ETHERSCAN_API = process.env.ETHERSCAN_API_KEY;
|
||||||
const POLYGONSCAN_API = process.env.POLYGON_API_KEY;
|
const POLYGONSCAN_API = process.env.POLYGON_API_KEY;
|
||||||
const ARBISCAN_API = process.env.ARBISCAN_API_KEY;
|
const ARBISCAN_API = process.env.ARBISCAN_API_KEY;
|
||||||
|
const OPTIMISM_API = process.env.OPTIMISM_API_KEY;
|
||||||
const SNOWTRACE_API = process.env.SNOWTRACE_API_KEY;
|
const SNOWTRACE_API = process.env.SNOWTRACE_API_KEY;
|
||||||
const mnemonic =
|
const mnemonic =
|
||||||
process.env.MNEMONIC ??
|
process.env.MNEMONIC ??
|
||||||
|
@ -51,7 +53,7 @@ function createConfig(network: string) {
|
||||||
return {
|
return {
|
||||||
url: getNetworkUrl(network),
|
url: getNetworkUrl(network),
|
||||||
accounts: !!PRIVATE_KEY ? [`0x${PRIVATE_KEY}`] : { mnemonic },
|
accounts: !!PRIVATE_KEY ? [`0x${PRIVATE_KEY}`] : { mnemonic },
|
||||||
// gasPrice: 1000000, // 0.0001 GWEI
|
gasPrice: 1000000, // 0.0001 GWEI
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,6 +73,7 @@ function getScanApiKey(networkType: string) {
|
||||||
if (networkType === "avalanche") return SNOWTRACE_API;
|
if (networkType === "avalanche") return SNOWTRACE_API;
|
||||||
else if (networkType === "polygon") return POLYGONSCAN_API;
|
else if (networkType === "polygon") return POLYGONSCAN_API;
|
||||||
else if (networkType === "arbitrum") return ARBISCAN_API;
|
else if (networkType === "arbitrum") return ARBISCAN_API;
|
||||||
|
else if (networkType === "optimism") return OPTIMISM_API;
|
||||||
else return ETHERSCAN_API;
|
else return ETHERSCAN_API;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,7 +126,7 @@ const config: HardhatUserConfig = {
|
||||||
tests: "./test",
|
tests: "./test",
|
||||||
},
|
},
|
||||||
etherscan: {
|
etherscan: {
|
||||||
apiKey: getScanApiKey(String(process.env.networkType)),
|
apiKey: getScanApiKey(String(process.env.networkType)),
|
||||||
},
|
},
|
||||||
typechain: {
|
typechain: {
|
||||||
outDir: "typechain",
|
outDir: "typechain",
|
||||||
|
|
|
@ -11,6 +11,7 @@ function getCurrentCommitSha() {
|
||||||
.toString()
|
.toString()
|
||||||
.trim();
|
.trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
// The SHA provied by GITHUB_SHA is the merge (PR) commit.
|
// The SHA provied by GITHUB_SHA is the merge (PR) commit.
|
||||||
// We need to get the current commit sha ourself.
|
// We need to get the current commit sha ourself.
|
||||||
const sha = getCurrentCommitSha();
|
const sha = getCurrentCommitSha();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user