add curveAdapter

This commit is contained in:
owen05
2021-08-26 16:01:24 +08:00
parent c5a6904834
commit ff84835f3c
9 changed files with 72 additions and 11 deletions

View File

@@ -1,8 +1,6 @@
/*
Copyright 2020 DODO ZOO.
Copyright 2021 DODO ZOO.
SPDX-License-Identifier: Apache-2.0
*/
pragma solidity 0.6.9;
@@ -14,20 +12,24 @@ import {SafeMath} from "../../lib/SafeMath.sol";
import {UniversalERC20} from "../lib/UniversalERC20.sol";
import {SafeERC20} from "../../lib/SafeERC20.sol";
// for two tokens
contract CurveUnderlyingAdapter is IDODOAdapter {
// for two tokens; to adapter like dodo V1
contract CurveAdapter is IDODOAdapter {
using SafeMath for uint;
using UniversalERC20 for IERC20;
function _curveSwap(address to, address pool, bytes memory moreInfo) internal {
(address fromToken, address toToken, int128 i, int128 j) = abi.decode(moreInfo, (address, address, int128, int128));
require(fromToken == ICurve(pool).underlying_coins(i), 'CurveAdapter: WRONG_TOKEN');
require(toToken == ICurve(pool).underlying_coins(j), 'CurveAdapter: WRONG_TOKEN');
(bool noLending, address fromToken, address toToken, int128 i, int128 j) = abi.decode(moreInfo, (bool, address, address, int128, int128));
uint256 sellAmount = IERC20(fromToken).balanceOf(address(this));
// approve
IERC20(fromToken).approve(pool, sellAmount);
IERC20(fromToken).universalApproveMax(pool, sellAmount);
// swap
ICurve(pool).exchange_underlying(i, j, sellAmount, 0);
if(noLending) {
ICurve(pool).exchange(i, j, sellAmount, 0);
} else {
ICurve(pool).exchange_underlying(i, j, sellAmount, 0);
}
if(to != address(this)) {
SafeERC20.safeTransfer(IERC20(toToken), to, IERC20(toToken).balanceOf(address(this)));
}

View File

@@ -163,7 +163,7 @@ contract DODONFTProxy is ReentrancyGuard, InitializableOwnable {
_deposit(msg.sender, fragment, IFragment(fragment)._QUOTE_(), curRequireQuote, flag == 1);
IFragment(fragment).buyout(msg.sender);
IDODONFTRegistry(_NFT_REGISTY_).removeRegistry(fragment);
// IDODONFTRegistry(_NFT_REGISTY_).removeRegistry(fragment);
// refund dust eth
if (flag == 1 && msg.value > curRequireQuote) msg.sender.transfer(msg.value - curRequireQuote);

View File

@@ -0,0 +1,37 @@
/*
Copyright 2021 DODO ZOO.
SPDX-License-Identifier: Apache-2.0
*/
pragma solidity 0.6.9;
pragma experimental ABIEncoderV2;
import "../intf/ICurve.sol";
contract CurveSampler {
function sampleFromCurve(
address curveAddress,
int128 fromTokenIdx,
int128 toTokenIdx,
uint256[] memory takerTokenAmounts
)
public
view
returns (uint256[] memory makerTokenAmounts)
{
uint256 numSamples = takerTokenAmounts.length;
makerTokenAmounts = new uint256[](numSamples);
for (uint256 i = 0; i < numSamples; i++) {
uint256 buyAmount = ICurve(curveAddress).get_dy_underlying(fromTokenIdx, toTokenIdx, takerTokenAmounts[i]);
makerTokenAmounts[i] = buyAmount;
// Break early if there are 0 amounts
if (makerTokenAmounts[i] == 0) {
break;
}
}
return makerTokenAmounts;
}
}