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

52 lines
2.1 KiB
Solidity
Raw Normal View History

2021-05-08 12:42:25 +08:00
/*
Copyright 2020 DODO ZOO.
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";
// for two tokens
2021-05-11 13:42:47 +08:00
contract CurveUnderlyingAdapter is IDODOAdapter {
2021-05-08 12:42:25 +08:00
using SafeMath for uint;
//fromToken == token[0], underlying
function sellBase(address to, address pool, bytes memory moreInfo) external override {
(address fromToken, address toToken, int128 i, int128 j) = abi.decode(moreInfo, (address, address, int128, int128));
2021-05-11 14:43:12 +08:00
require(fromToken == ICurve(pool).underlying_coins(i), 'DepthAdapter: WRONG_TOKEN');
require(toToken == ICurve(pool).underlying_coins(j), 'DepthAdapter: WRONG_TOKEN');
2021-05-08 12:42:25 +08:00
uint256 sellBaseAmount = IERC20(fromToken).balanceOf(address(this));
// approve
IERC20(fromToken).approve(pool, sellBaseAmount);
// swap
2021-05-11 14:43:12 +08:00
ICurve(pool).exchange_underlying(i, j, sellBaseAmount, 0);
2021-05-08 12:42:25 +08:00
if(to != address(this)) {
SafeERC20.safeTransfer(IERC20(toToken), to, IERC20(toToken).balanceOf(address(this)));
}
}
//fromToken == token[1], underlying
function sellQuote(address to, address pool, bytes memory moreInfo) external override {
(address fromToken, address toToken, int128 i, int128 j) = abi.decode(moreInfo, (address, address, int128, int128));
2021-05-11 14:43:12 +08:00
require(fromToken == ICurve(pool).underlying_coins(i), 'DepthAdapter: WRONG_TOKEN');
require(toToken == ICurve(pool).underlying_coins(j), 'DepthAdapter: WRONG_TOKEN');
2021-05-08 12:42:25 +08:00
uint256 sellQuoteAmount = IERC20(toToken).balanceOf(address(this));
// approve
IERC20(toToken).approve(pool, sellQuoteAmount);
// swap
2021-05-11 14:43:12 +08:00
ICurve(pool).exchange_underlying(i, j, sellQuoteAmount, 0);
2021-05-08 12:42:25 +08:00
if(to != address(this)) {
SafeERC20.safeTransfer(IERC20(fromToken), to, IERC20(fromToken).balanceOf(address(this)));
}
}
}