Files
dodo-contractV2/contracts/SmartRoute/adapter/CurveAdapter.sol

45 lines
1.5 KiB
Solidity
Raw Normal View History

2021-05-08 12:42:25 +08:00
/*
2021-08-26 16:01:24 +08:00
Copyright 2021 DODO ZOO.
2021-05-08 12:42:25 +08:00
SPDX-License-Identifier: Apache-2.0
*/
pragma solidity 0.6.9;
import {IDODOAdapter} from "../intf/IDODOAdapter.sol";
2021-05-11 14:43:12 +08:00
import {ICurve} from "../intf/ICurve.sol";
2021-05-08 12:42:25 +08:00
import {IERC20} from "../../intf/IERC20.sol";
import {SafeMath} from "../../lib/SafeMath.sol";
import {UniversalERC20} from "../lib/UniversalERC20.sol";
import {SafeERC20} from "../../lib/SafeERC20.sol";
2021-08-26 16:01:24 +08:00
// for two tokens; to adapter like dodo V1
contract CurveAdapter is IDODOAdapter {
2021-05-08 12:42:25 +08:00
using SafeMath for uint;
2021-08-26 16:01:24 +08:00
using UniversalERC20 for IERC20;
2021-05-08 12:42:25 +08:00
2021-05-12 14:56:50 +08:00
function _curveSwap(address to, address pool, bytes memory moreInfo) internal {
2021-08-26 16:01:24 +08:00
(bool noLending, address fromToken, address toToken, int128 i, int128 j) = abi.decode(moreInfo, (bool, address, address, int128, int128));
2021-05-12 14:56:50 +08:00
uint256 sellAmount = IERC20(fromToken).balanceOf(address(this));
2021-05-08 12:42:25 +08:00
// approve
2021-08-26 16:01:24 +08:00
IERC20(fromToken).universalApproveMax(pool, sellAmount);
2021-05-08 12:42:25 +08:00
// swap
2021-08-26 16:01:24 +08:00
if(noLending) {
ICurve(pool).exchange(i, j, sellAmount, 0);
} else {
ICurve(pool).exchange_underlying(i, j, sellAmount, 0);
}
2021-05-08 12:42:25 +08:00
if(to != address(this)) {
SafeERC20.safeTransfer(IERC20(toToken), to, IERC20(toToken).balanceOf(address(this)));
}
}
2021-05-12 14:56:50 +08:00
function sellBase(address to, address pool, bytes memory moreInfo) external override {
_curveSwap(to, pool, moreInfo);
}
2021-05-08 12:42:25 +08:00
function sellQuote(address to, address pool, bytes memory moreInfo) external override {
2021-05-12 14:56:50 +08:00
_curveSwap(to, pool, moreInfo);
2021-05-08 12:42:25 +08:00
}
}