mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
add to multichain
This commit is contained in:
parent
cb6563b816
commit
59aa71214f
12
contracts/arbitrum/connectors/kyber_v3/events.sol
Normal file
12
contracts/arbitrum/connectors/kyber_v3/events.sol
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
//SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
contract Events {
|
||||||
|
event LogSwap(
|
||||||
|
address buyToken,
|
||||||
|
address sellToken,
|
||||||
|
uint256 buyAmt,
|
||||||
|
uint256 sellAmt,
|
||||||
|
uint256 setId
|
||||||
|
);
|
||||||
|
}
|
66
contracts/arbitrum/connectors/kyber_v3/helpers.sol
Normal file
66
contracts/arbitrum/connectors/kyber_v3/helpers.sol
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
//SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
import {DSMath} from "../../common/math.sol";
|
||||||
|
import {Basic} from "../../common/basic.sol";
|
||||||
|
import {TokenInterface} from "../../common/interfaces.sol";
|
||||||
|
|
||||||
|
abstract contract Helpers is DSMath, Basic {
|
||||||
|
struct SwapData {
|
||||||
|
TokenInterface sellToken;
|
||||||
|
TokenInterface buyToken;
|
||||||
|
uint256 _sellAmt;
|
||||||
|
uint256 _buyAmt;
|
||||||
|
uint256 unitAmt;
|
||||||
|
bytes callData;
|
||||||
|
}
|
||||||
|
|
||||||
|
address internal constant kyberswap =
|
||||||
|
0x6131B5fae19EA4f9D964eAc0408E4408b66337b5;
|
||||||
|
|
||||||
|
function _swapHelper(SwapData memory swapData, uint256 wethAmt)
|
||||||
|
internal
|
||||||
|
returns (uint256 buyAmt)
|
||||||
|
{
|
||||||
|
TokenInterface buyToken = swapData.buyToken;
|
||||||
|
(uint256 _buyDec, uint256 _sellDec) = getTokensDec(
|
||||||
|
buyToken,
|
||||||
|
swapData.sellToken
|
||||||
|
);
|
||||||
|
uint256 _sellAmt18 = convertTo18(_sellDec, swapData._sellAmt);
|
||||||
|
uint256 _slippageAmt = convert18ToDec(
|
||||||
|
_buyDec,
|
||||||
|
wmul(swapData.unitAmt, _sellAmt18)
|
||||||
|
);
|
||||||
|
|
||||||
|
uint256 initalBal = getTokenBal(buyToken);
|
||||||
|
(bool success, ) = kyberswap.call{value: wethAmt}(swapData.callData);
|
||||||
|
if (!success) revert("kyberswap-failed");
|
||||||
|
|
||||||
|
uint256 finalBal = getTokenBal(buyToken);
|
||||||
|
|
||||||
|
buyAmt = sub(finalBal, initalBal);
|
||||||
|
|
||||||
|
require(_slippageAmt <= buyAmt, "Too much slippage");
|
||||||
|
}
|
||||||
|
|
||||||
|
function _swap(SwapData memory swapData, uint256 setId)
|
||||||
|
internal
|
||||||
|
returns (SwapData memory)
|
||||||
|
{
|
||||||
|
TokenInterface _sellAddr = swapData.sellToken;
|
||||||
|
|
||||||
|
uint256 ethAmt;
|
||||||
|
|
||||||
|
if (address(_sellAddr) == ethAddr) {
|
||||||
|
ethAmt = swapData._sellAmt;
|
||||||
|
} else {
|
||||||
|
approve(TokenInterface(_sellAddr), kyberswap, swapData._sellAmt);
|
||||||
|
}
|
||||||
|
swapData._buyAmt = _swapHelper(swapData, ethAmt);
|
||||||
|
|
||||||
|
setUint(setId, swapData._buyAmt);
|
||||||
|
|
||||||
|
return swapData;
|
||||||
|
}
|
||||||
|
}
|
6
contracts/arbitrum/connectors/kyber_v3/interface.sol
Normal file
6
contracts/arbitrum/connectors/kyber_v3/interface.sol
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
//SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
interface AugustusSwapperInterface {
|
||||||
|
function getTokenTransferProxy() external view returns (address);
|
||||||
|
}
|
59
contracts/arbitrum/connectors/kyber_v3/main.sol
Normal file
59
contracts/arbitrum/connectors/kyber_v3/main.sol
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
//SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @title Kyber.
|
||||||
|
* @dev DEX Aggregator.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {TokenInterface} from "../../common/interfaces.sol";
|
||||||
|
import {Stores} from "../../common/stores.sol";
|
||||||
|
import {Helpers} from "./helpers.sol";
|
||||||
|
|
||||||
|
abstract contract KyberArbitrumResolver is Helpers {
|
||||||
|
/**
|
||||||
|
* @dev Sell ETH/ERC20_Token using KyberSwap.
|
||||||
|
* @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain.
|
||||||
|
* @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 kyberswap API.
|
||||||
|
* @param setId ID stores the amount of token brought.
|
||||||
|
*/
|
||||||
|
function swap(
|
||||||
|
address buyAddr,
|
||||||
|
address sellAddr,
|
||||||
|
uint256 sellAmt,
|
||||||
|
uint256 unitAmt,
|
||||||
|
bytes calldata callData,
|
||||||
|
uint256 setId
|
||||||
|
)
|
||||||
|
external
|
||||||
|
payable
|
||||||
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
|
{
|
||||||
|
Helpers.SwapData memory swapData = Helpers.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)";
|
||||||
|
_eventParam = abi.encode(
|
||||||
|
address(swapData.buyToken),
|
||||||
|
address(swapData.sellToken),
|
||||||
|
swapData._buyAmt,
|
||||||
|
swapData._sellAmt,
|
||||||
|
setId
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
contract ConnectV2KyberV3Arbitrum is KyberArbitrumResolver {
|
||||||
|
string public name = "Kyber-v3";
|
||||||
|
}
|
12
contracts/avalanche/connectors/kyber_v3/events.sol
Normal file
12
contracts/avalanche/connectors/kyber_v3/events.sol
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
//SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
contract Events {
|
||||||
|
event LogSwap(
|
||||||
|
address buyToken,
|
||||||
|
address sellToken,
|
||||||
|
uint256 buyAmt,
|
||||||
|
uint256 sellAmt,
|
||||||
|
uint256 setId
|
||||||
|
);
|
||||||
|
}
|
66
contracts/avalanche/connectors/kyber_v3/helpers.sol
Normal file
66
contracts/avalanche/connectors/kyber_v3/helpers.sol
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
//SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
import {DSMath} from "../../common/math.sol";
|
||||||
|
import {Basic} from "../../common/basic.sol";
|
||||||
|
import {TokenInterface} from "../../common/interfaces.sol";
|
||||||
|
|
||||||
|
abstract contract Helpers is DSMath, Basic {
|
||||||
|
struct SwapData {
|
||||||
|
TokenInterface sellToken;
|
||||||
|
TokenInterface buyToken;
|
||||||
|
uint256 _sellAmt;
|
||||||
|
uint256 _buyAmt;
|
||||||
|
uint256 unitAmt;
|
||||||
|
bytes callData;
|
||||||
|
}
|
||||||
|
|
||||||
|
address internal constant kyberswap =
|
||||||
|
0x6131B5fae19EA4f9D964eAc0408E4408b66337b5;
|
||||||
|
|
||||||
|
function _swapHelper(SwapData memory swapData, uint256 wethAmt)
|
||||||
|
internal
|
||||||
|
returns (uint256 buyAmt)
|
||||||
|
{
|
||||||
|
TokenInterface buyToken = swapData.buyToken;
|
||||||
|
(uint256 _buyDec, uint256 _sellDec) = getTokensDec(
|
||||||
|
buyToken,
|
||||||
|
swapData.sellToken
|
||||||
|
);
|
||||||
|
uint256 _sellAmt18 = convertTo18(_sellDec, swapData._sellAmt);
|
||||||
|
uint256 _slippageAmt = convert18ToDec(
|
||||||
|
_buyDec,
|
||||||
|
wmul(swapData.unitAmt, _sellAmt18)
|
||||||
|
);
|
||||||
|
|
||||||
|
uint256 initalBal = getTokenBal(buyToken);
|
||||||
|
(bool success, ) = kyberswap.call{value: wethAmt}(swapData.callData);
|
||||||
|
if (!success) revert("kyberswap-failed");
|
||||||
|
|
||||||
|
uint256 finalBal = getTokenBal(buyToken);
|
||||||
|
|
||||||
|
buyAmt = sub(finalBal, initalBal);
|
||||||
|
|
||||||
|
require(_slippageAmt <= buyAmt, "Too much slippage");
|
||||||
|
}
|
||||||
|
|
||||||
|
function _swap(SwapData memory swapData, uint256 setId)
|
||||||
|
internal
|
||||||
|
returns (SwapData memory)
|
||||||
|
{
|
||||||
|
TokenInterface _sellAddr = swapData.sellToken;
|
||||||
|
|
||||||
|
uint256 ethAmt;
|
||||||
|
|
||||||
|
if (address(_sellAddr) == avaxAddr) {
|
||||||
|
ethAmt = swapData._sellAmt;
|
||||||
|
} else {
|
||||||
|
approve(TokenInterface(_sellAddr), kyberswap, swapData._sellAmt);
|
||||||
|
}
|
||||||
|
swapData._buyAmt = _swapHelper(swapData, ethAmt);
|
||||||
|
|
||||||
|
setUint(setId, swapData._buyAmt);
|
||||||
|
|
||||||
|
return swapData;
|
||||||
|
}
|
||||||
|
}
|
6
contracts/avalanche/connectors/kyber_v3/interface.sol
Normal file
6
contracts/avalanche/connectors/kyber_v3/interface.sol
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
//SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
interface AugustusSwapperInterface {
|
||||||
|
function getTokenTransferProxy() external view returns (address);
|
||||||
|
}
|
59
contracts/avalanche/connectors/kyber_v3/main.sol
Normal file
59
contracts/avalanche/connectors/kyber_v3/main.sol
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
//SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @title Kyber.
|
||||||
|
* @dev DEX Aggregator.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {TokenInterface} from "../../common/interfaces.sol";
|
||||||
|
import {Stores} from "../../common/stores.sol";
|
||||||
|
import {Helpers} from "./helpers.sol";
|
||||||
|
|
||||||
|
abstract contract KyberAvalancheResolver is Helpers {
|
||||||
|
/**
|
||||||
|
* @dev Sell ETH/ERC20_Token using KyberSwap.
|
||||||
|
* @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain.
|
||||||
|
* @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 kyberswap API.
|
||||||
|
* @param setId ID stores the amount of token brought.
|
||||||
|
*/
|
||||||
|
function swap(
|
||||||
|
address buyAddr,
|
||||||
|
address sellAddr,
|
||||||
|
uint256 sellAmt,
|
||||||
|
uint256 unitAmt,
|
||||||
|
bytes calldata callData,
|
||||||
|
uint256 setId
|
||||||
|
)
|
||||||
|
external
|
||||||
|
payable
|
||||||
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
|
{
|
||||||
|
Helpers.SwapData memory swapData = Helpers.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)";
|
||||||
|
_eventParam = abi.encode(
|
||||||
|
address(swapData.buyToken),
|
||||||
|
address(swapData.sellToken),
|
||||||
|
swapData._buyAmt,
|
||||||
|
swapData._sellAmt,
|
||||||
|
setId
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
contract ConnectV2KyberV3Avalanche is KyberAvalancheResolver {
|
||||||
|
string public name = "Kyber-v3";
|
||||||
|
}
|
12
contracts/fantom/connectors/kyber_v3/events.sol
Normal file
12
contracts/fantom/connectors/kyber_v3/events.sol
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
//SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
contract Events {
|
||||||
|
event LogSwap(
|
||||||
|
address buyToken,
|
||||||
|
address sellToken,
|
||||||
|
uint256 buyAmt,
|
||||||
|
uint256 sellAmt,
|
||||||
|
uint256 setId
|
||||||
|
);
|
||||||
|
}
|
66
contracts/fantom/connectors/kyber_v3/helpers.sol
Normal file
66
contracts/fantom/connectors/kyber_v3/helpers.sol
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
//SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
import {DSMath} from "../../common/math.sol";
|
||||||
|
import {Basic} from "../../common/basic.sol";
|
||||||
|
import {TokenInterface} from "../../common/interfaces.sol";
|
||||||
|
|
||||||
|
abstract contract Helpers is DSMath, Basic {
|
||||||
|
struct SwapData {
|
||||||
|
TokenInterface sellToken;
|
||||||
|
TokenInterface buyToken;
|
||||||
|
uint256 _sellAmt;
|
||||||
|
uint256 _buyAmt;
|
||||||
|
uint256 unitAmt;
|
||||||
|
bytes callData;
|
||||||
|
}
|
||||||
|
|
||||||
|
address internal constant kyberswap =
|
||||||
|
0x6131B5fae19EA4f9D964eAc0408E4408b66337b5;
|
||||||
|
|
||||||
|
function _swapHelper(SwapData memory swapData, uint256 wethAmt)
|
||||||
|
internal
|
||||||
|
returns (uint256 buyAmt)
|
||||||
|
{
|
||||||
|
TokenInterface buyToken = swapData.buyToken;
|
||||||
|
(uint256 _buyDec, uint256 _sellDec) = getTokensDec(
|
||||||
|
buyToken,
|
||||||
|
swapData.sellToken
|
||||||
|
);
|
||||||
|
uint256 _sellAmt18 = convertTo18(_sellDec, swapData._sellAmt);
|
||||||
|
uint256 _slippageAmt = convert18ToDec(
|
||||||
|
_buyDec,
|
||||||
|
wmul(swapData.unitAmt, _sellAmt18)
|
||||||
|
);
|
||||||
|
|
||||||
|
uint256 initalBal = getTokenBal(buyToken);
|
||||||
|
(bool success, ) = kyberswap.call{value: wethAmt}(swapData.callData);
|
||||||
|
if (!success) revert("kyberswap-failed");
|
||||||
|
|
||||||
|
uint256 finalBal = getTokenBal(buyToken);
|
||||||
|
|
||||||
|
buyAmt = sub(finalBal, initalBal);
|
||||||
|
|
||||||
|
require(_slippageAmt <= buyAmt, "Too much slippage");
|
||||||
|
}
|
||||||
|
|
||||||
|
function _swap(SwapData memory swapData, uint256 setId)
|
||||||
|
internal
|
||||||
|
returns (SwapData memory)
|
||||||
|
{
|
||||||
|
TokenInterface _sellAddr = swapData.sellToken;
|
||||||
|
|
||||||
|
uint256 ethAmt;
|
||||||
|
|
||||||
|
if (address(_sellAddr) == ftmAddr) {
|
||||||
|
ethAmt = swapData._sellAmt;
|
||||||
|
} else {
|
||||||
|
approve(TokenInterface(_sellAddr), kyberswap, swapData._sellAmt);
|
||||||
|
}
|
||||||
|
swapData._buyAmt = _swapHelper(swapData, ethAmt);
|
||||||
|
|
||||||
|
setUint(setId, swapData._buyAmt);
|
||||||
|
|
||||||
|
return swapData;
|
||||||
|
}
|
||||||
|
}
|
6
contracts/fantom/connectors/kyber_v3/interface.sol
Normal file
6
contracts/fantom/connectors/kyber_v3/interface.sol
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
//SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
interface AugustusSwapperInterface {
|
||||||
|
function getTokenTransferProxy() external view returns (address);
|
||||||
|
}
|
59
contracts/fantom/connectors/kyber_v3/main.sol
Normal file
59
contracts/fantom/connectors/kyber_v3/main.sol
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
//SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @title Kyber.
|
||||||
|
* @dev DEX Aggregator.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {TokenInterface} from "../../common/interfaces.sol";
|
||||||
|
import {Stores} from "../../common/stores.sol";
|
||||||
|
import {Helpers} from "./helpers.sol";
|
||||||
|
|
||||||
|
abstract contract KyberFantomResolver is Helpers {
|
||||||
|
/**
|
||||||
|
* @dev Sell ETH/ERC20_Token using KyberSwap.
|
||||||
|
* @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain.
|
||||||
|
* @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 kyberswap API.
|
||||||
|
* @param setId ID stores the amount of token brought.
|
||||||
|
*/
|
||||||
|
function swap(
|
||||||
|
address buyAddr,
|
||||||
|
address sellAddr,
|
||||||
|
uint256 sellAmt,
|
||||||
|
uint256 unitAmt,
|
||||||
|
bytes calldata callData,
|
||||||
|
uint256 setId
|
||||||
|
)
|
||||||
|
external
|
||||||
|
payable
|
||||||
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
|
{
|
||||||
|
Helpers.SwapData memory swapData = Helpers.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)";
|
||||||
|
_eventParam = abi.encode(
|
||||||
|
address(swapData.buyToken),
|
||||||
|
address(swapData.sellToken),
|
||||||
|
swapData._buyAmt,
|
||||||
|
swapData._sellAmt,
|
||||||
|
setId
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
contract ConnectV2KyberV3Fantom is KyberFantomResolver {
|
||||||
|
string public name = "Kyber-v3";
|
||||||
|
}
|
12
contracts/optimism/connectors/kyber_v3/events.sol
Normal file
12
contracts/optimism/connectors/kyber_v3/events.sol
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
//SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
contract Events {
|
||||||
|
event LogSwap(
|
||||||
|
address buyToken,
|
||||||
|
address sellToken,
|
||||||
|
uint256 buyAmt,
|
||||||
|
uint256 sellAmt,
|
||||||
|
uint256 setId
|
||||||
|
);
|
||||||
|
}
|
66
contracts/optimism/connectors/kyber_v3/helpers.sol
Normal file
66
contracts/optimism/connectors/kyber_v3/helpers.sol
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
//SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
import {DSMath} from "../../common/math.sol";
|
||||||
|
import {Basic} from "../../common/basic.sol";
|
||||||
|
import {TokenInterface} from "../../common/interfaces.sol";
|
||||||
|
|
||||||
|
abstract contract Helpers is DSMath, Basic {
|
||||||
|
struct SwapData {
|
||||||
|
TokenInterface sellToken;
|
||||||
|
TokenInterface buyToken;
|
||||||
|
uint256 _sellAmt;
|
||||||
|
uint256 _buyAmt;
|
||||||
|
uint256 unitAmt;
|
||||||
|
bytes callData;
|
||||||
|
}
|
||||||
|
|
||||||
|
address internal constant kyberswap =
|
||||||
|
0x6131B5fae19EA4f9D964eAc0408E4408b66337b5;
|
||||||
|
|
||||||
|
function _swapHelper(SwapData memory swapData, uint256 wethAmt)
|
||||||
|
internal
|
||||||
|
returns (uint256 buyAmt)
|
||||||
|
{
|
||||||
|
TokenInterface buyToken = swapData.buyToken;
|
||||||
|
(uint256 _buyDec, uint256 _sellDec) = getTokensDec(
|
||||||
|
buyToken,
|
||||||
|
swapData.sellToken
|
||||||
|
);
|
||||||
|
uint256 _sellAmt18 = convertTo18(_sellDec, swapData._sellAmt);
|
||||||
|
uint256 _slippageAmt = convert18ToDec(
|
||||||
|
_buyDec,
|
||||||
|
wmul(swapData.unitAmt, _sellAmt18)
|
||||||
|
);
|
||||||
|
|
||||||
|
uint256 initalBal = getTokenBal(buyToken);
|
||||||
|
(bool success, ) = kyberswap.call{value: wethAmt}(swapData.callData);
|
||||||
|
if (!success) revert("kyberswap-failed");
|
||||||
|
|
||||||
|
uint256 finalBal = getTokenBal(buyToken);
|
||||||
|
|
||||||
|
buyAmt = sub(finalBal, initalBal);
|
||||||
|
|
||||||
|
require(_slippageAmt <= buyAmt, "Too much slippage");
|
||||||
|
}
|
||||||
|
|
||||||
|
function _swap(SwapData memory swapData, uint256 setId)
|
||||||
|
internal
|
||||||
|
returns (SwapData memory)
|
||||||
|
{
|
||||||
|
TokenInterface _sellAddr = swapData.sellToken;
|
||||||
|
|
||||||
|
uint256 ethAmt;
|
||||||
|
|
||||||
|
if (address(_sellAddr) == ethAddr) {
|
||||||
|
ethAmt = swapData._sellAmt;
|
||||||
|
} else {
|
||||||
|
approve(TokenInterface(_sellAddr), kyberswap, swapData._sellAmt);
|
||||||
|
}
|
||||||
|
swapData._buyAmt = _swapHelper(swapData, ethAmt);
|
||||||
|
|
||||||
|
setUint(setId, swapData._buyAmt);
|
||||||
|
|
||||||
|
return swapData;
|
||||||
|
}
|
||||||
|
}
|
6
contracts/optimism/connectors/kyber_v3/interface.sol
Normal file
6
contracts/optimism/connectors/kyber_v3/interface.sol
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
//SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
interface AugustusSwapperInterface {
|
||||||
|
function getTokenTransferProxy() external view returns (address);
|
||||||
|
}
|
59
contracts/optimism/connectors/kyber_v3/main.sol
Normal file
59
contracts/optimism/connectors/kyber_v3/main.sol
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
//SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @title Kyber.
|
||||||
|
* @dev DEX Aggregator.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {TokenInterface} from "../../common/interfaces.sol";
|
||||||
|
import {Stores} from "../../common/stores.sol";
|
||||||
|
import {Helpers} from "./helpers.sol";
|
||||||
|
|
||||||
|
abstract contract KyberOptimismResolver is Helpers {
|
||||||
|
/**
|
||||||
|
* @dev Sell ETH/ERC20_Token using KyberSwap.
|
||||||
|
* @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain.
|
||||||
|
* @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 kyberswap API.
|
||||||
|
* @param setId ID stores the amount of token brought.
|
||||||
|
*/
|
||||||
|
function swap(
|
||||||
|
address buyAddr,
|
||||||
|
address sellAddr,
|
||||||
|
uint256 sellAmt,
|
||||||
|
uint256 unitAmt,
|
||||||
|
bytes calldata callData,
|
||||||
|
uint256 setId
|
||||||
|
)
|
||||||
|
external
|
||||||
|
payable
|
||||||
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
|
{
|
||||||
|
Helpers.SwapData memory swapData = Helpers.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)";
|
||||||
|
_eventParam = abi.encode(
|
||||||
|
address(swapData.buyToken),
|
||||||
|
address(swapData.sellToken),
|
||||||
|
swapData._buyAmt,
|
||||||
|
swapData._sellAmt,
|
||||||
|
setId
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
contract ConnectV2KyberV3Optimism is KyberOptimismResolver {
|
||||||
|
string public name = "Kyber-v3";
|
||||||
|
}
|
12
contracts/polygon/connectors/kyber_v3/events.sol
Normal file
12
contracts/polygon/connectors/kyber_v3/events.sol
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
//SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
contract Events {
|
||||||
|
event LogSwap(
|
||||||
|
address buyToken,
|
||||||
|
address sellToken,
|
||||||
|
uint256 buyAmt,
|
||||||
|
uint256 sellAmt,
|
||||||
|
uint256 setId
|
||||||
|
);
|
||||||
|
}
|
66
contracts/polygon/connectors/kyber_v3/helpers.sol
Normal file
66
contracts/polygon/connectors/kyber_v3/helpers.sol
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
//SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
import {DSMath} from "../../common/math.sol";
|
||||||
|
import {Basic} from "../../common/basic.sol";
|
||||||
|
import {TokenInterface} from "../../common/interfaces.sol";
|
||||||
|
|
||||||
|
abstract contract Helpers is DSMath, Basic {
|
||||||
|
struct SwapData {
|
||||||
|
TokenInterface sellToken;
|
||||||
|
TokenInterface buyToken;
|
||||||
|
uint256 _sellAmt;
|
||||||
|
uint256 _buyAmt;
|
||||||
|
uint256 unitAmt;
|
||||||
|
bytes callData;
|
||||||
|
}
|
||||||
|
|
||||||
|
address internal constant kyberswap =
|
||||||
|
0x6131B5fae19EA4f9D964eAc0408E4408b66337b5;
|
||||||
|
|
||||||
|
function _swapHelper(SwapData memory swapData, uint256 wethAmt)
|
||||||
|
internal
|
||||||
|
returns (uint256 buyAmt)
|
||||||
|
{
|
||||||
|
TokenInterface buyToken = swapData.buyToken;
|
||||||
|
(uint256 _buyDec, uint256 _sellDec) = getTokensDec(
|
||||||
|
buyToken,
|
||||||
|
swapData.sellToken
|
||||||
|
);
|
||||||
|
uint256 _sellAmt18 = convertTo18(_sellDec, swapData._sellAmt);
|
||||||
|
uint256 _slippageAmt = convert18ToDec(
|
||||||
|
_buyDec,
|
||||||
|
wmul(swapData.unitAmt, _sellAmt18)
|
||||||
|
);
|
||||||
|
|
||||||
|
uint256 initalBal = getTokenBal(buyToken);
|
||||||
|
(bool success, ) = kyberswap.call{value: wethAmt}(swapData.callData);
|
||||||
|
if (!success) revert("kyberswap-failed");
|
||||||
|
|
||||||
|
uint256 finalBal = getTokenBal(buyToken);
|
||||||
|
|
||||||
|
buyAmt = sub(finalBal, initalBal);
|
||||||
|
|
||||||
|
require(_slippageAmt <= buyAmt, "Too much slippage");
|
||||||
|
}
|
||||||
|
|
||||||
|
function _swap(SwapData memory swapData, uint256 setId)
|
||||||
|
internal
|
||||||
|
returns (SwapData memory)
|
||||||
|
{
|
||||||
|
TokenInterface _sellAddr = swapData.sellToken;
|
||||||
|
|
||||||
|
uint256 ethAmt;
|
||||||
|
|
||||||
|
if (address(_sellAddr) == maticAddr) {
|
||||||
|
ethAmt = swapData._sellAmt;
|
||||||
|
} else {
|
||||||
|
approve(TokenInterface(_sellAddr), kyberswap, swapData._sellAmt);
|
||||||
|
}
|
||||||
|
swapData._buyAmt = _swapHelper(swapData, ethAmt);
|
||||||
|
|
||||||
|
setUint(setId, swapData._buyAmt);
|
||||||
|
|
||||||
|
return swapData;
|
||||||
|
}
|
||||||
|
}
|
6
contracts/polygon/connectors/kyber_v3/interface.sol
Normal file
6
contracts/polygon/connectors/kyber_v3/interface.sol
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
//SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
interface AugustusSwapperInterface {
|
||||||
|
function getTokenTransferProxy() external view returns (address);
|
||||||
|
}
|
59
contracts/polygon/connectors/kyber_v3/main.sol
Normal file
59
contracts/polygon/connectors/kyber_v3/main.sol
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
//SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @title Kyber.
|
||||||
|
* @dev DEX Aggregator.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {TokenInterface} from "../../common/interfaces.sol";
|
||||||
|
import {Stores} from "../../common/stores.sol";
|
||||||
|
import {Helpers} from "./helpers.sol";
|
||||||
|
|
||||||
|
abstract contract KyberPolygonResolver is Helpers {
|
||||||
|
/**
|
||||||
|
* @dev Sell ETH/ERC20_Token using KyberSwap.
|
||||||
|
* @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain.
|
||||||
|
* @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 kyberswap API.
|
||||||
|
* @param setId ID stores the amount of token brought.
|
||||||
|
*/
|
||||||
|
function swap(
|
||||||
|
address buyAddr,
|
||||||
|
address sellAddr,
|
||||||
|
uint256 sellAmt,
|
||||||
|
uint256 unitAmt,
|
||||||
|
bytes calldata callData,
|
||||||
|
uint256 setId
|
||||||
|
)
|
||||||
|
external
|
||||||
|
payable
|
||||||
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
|
{
|
||||||
|
Helpers.SwapData memory swapData = Helpers.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)";
|
||||||
|
_eventParam = abi.encode(
|
||||||
|
address(swapData.buyToken),
|
||||||
|
address(swapData.sellToken),
|
||||||
|
swapData._buyAmt,
|
||||||
|
swapData._sellAmt,
|
||||||
|
setId
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
contract ConnectV2KyberV3Polygon is KyberPolygonResolver {
|
||||||
|
string public name = "Kyber-v3";
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user