finish DVM/DPP flashloan
This commit is contained in:
@@ -12,6 +12,7 @@ import {DPPVault} from "./DPPVault.sol";
|
|||||||
import {SafeMath} from "../../lib/SafeMath.sol";
|
import {SafeMath} from "../../lib/SafeMath.sol";
|
||||||
import {DecimalMath} from "../../lib/DecimalMath.sol";
|
import {DecimalMath} from "../../lib/DecimalMath.sol";
|
||||||
import {RState, PMMState, PMMPricing} from "../../lib/PMMPricing.sol";
|
import {RState, PMMState, PMMPricing} from "../../lib/PMMPricing.sol";
|
||||||
|
import {IDODOCallee} from "../../intf/IDODOCallee.sol";
|
||||||
|
|
||||||
contract DPPTrader is DPPVault {
|
contract DPPTrader is DPPVault {
|
||||||
using SafeMath for uint256;
|
using SafeMath for uint256;
|
||||||
@@ -38,8 +39,6 @@ contract DPPTrader is DPPVault {
|
|||||||
|
|
||||||
// ============ Trade Functions ============
|
// ============ Trade Functions ============
|
||||||
|
|
||||||
// todo 看看怎么能加上flash loan
|
|
||||||
|
|
||||||
function sellBase(address to)
|
function sellBase(address to)
|
||||||
external
|
external
|
||||||
preventReentrant
|
preventReentrant
|
||||||
@@ -78,7 +77,7 @@ contract DPPTrader is DPPVault {
|
|||||||
uint256 mtFee;
|
uint256 mtFee;
|
||||||
uint256 newQuoteTarget;
|
uint256 newQuoteTarget;
|
||||||
RState newRState;
|
RState newRState;
|
||||||
(receiveBaseAmount, mtFee, newRState, newQuoteTarget) = querySellBase(
|
(receiveBaseAmount, mtFee, newRState, newQuoteTarget) = querySellQuote(
|
||||||
tx.origin,
|
tx.origin,
|
||||||
quoteInput
|
quoteInput
|
||||||
);
|
);
|
||||||
@@ -97,6 +96,72 @@ contract DPPTrader is DPPVault {
|
|||||||
return receiveBaseAmount;
|
return receiveBaseAmount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function flashLoan(
|
||||||
|
uint256 baseAmount,
|
||||||
|
uint256 quoteAmount,
|
||||||
|
address assetTo,
|
||||||
|
bytes calldata data
|
||||||
|
) external preventReentrant {
|
||||||
|
_transferBaseOut(assetTo, baseAmount);
|
||||||
|
_transferQuoteOut(assetTo, quoteAmount);
|
||||||
|
if (data.length > 0)
|
||||||
|
IDODOCallee(assetTo).DPPFlashLoanCall(msg.sender, baseAmount, quoteAmount, data);
|
||||||
|
|
||||||
|
uint256 baseBalance = _BASE_TOKEN_.balanceOf(address(this));
|
||||||
|
uint256 quoteBalance = _QUOTE_TOKEN_.balanceOf(address(this));
|
||||||
|
|
||||||
|
// no output -> pure profit
|
||||||
|
if (baseBalance >= _BASE_RESERVE_ && quoteBalance >= _QUOTE_RESERVE_) return;
|
||||||
|
|
||||||
|
// no input -> pure loss
|
||||||
|
require(
|
||||||
|
baseBalance >= _BASE_RESERVE_ || quoteBalance >= _QUOTE_RESERVE_,
|
||||||
|
"FLASH_LOAN_FAILED"
|
||||||
|
);
|
||||||
|
|
||||||
|
// sell quote case
|
||||||
|
// quote input + base output
|
||||||
|
if (baseBalance < _BASE_RESERVE_) {
|
||||||
|
(
|
||||||
|
uint256 receiveBaseAmount,
|
||||||
|
uint256 mtFee,
|
||||||
|
RState newRState,
|
||||||
|
uint256 newQuoteTarget
|
||||||
|
) = querySellQuote(tx.origin, quoteBalance.sub(_QUOTE_RESERVE_)); // revert if quoteBalance<quoteReserve
|
||||||
|
|
||||||
|
require(_BASE_RESERVE_.sub(baseBalance) <= receiveBaseAmount, "FLASH_LOAN_FAILED");
|
||||||
|
|
||||||
|
_transferBaseOut(_MAINTAINER_, mtFee);
|
||||||
|
if (_RState_ != newRState) {
|
||||||
|
_RState_ = newRState;
|
||||||
|
_QUOTE_TARGET_ = newQuoteTarget;
|
||||||
|
}
|
||||||
|
|
||||||
|
_syncReserve();
|
||||||
|
}
|
||||||
|
|
||||||
|
// sell base case
|
||||||
|
// base input + quote output
|
||||||
|
if (quoteBalance < _QUOTE_RESERVE_) {
|
||||||
|
(
|
||||||
|
uint256 receiveQuoteAmount,
|
||||||
|
uint256 mtFee,
|
||||||
|
RState newRState,
|
||||||
|
uint256 newBaseTarget
|
||||||
|
) = querySellBase(tx.origin, baseBalance.sub(_BASE_RESERVE_)); // revert if baseBalance<baseReserve
|
||||||
|
|
||||||
|
require(_QUOTE_RESERVE_.sub(quoteBalance) <= receiveQuoteAmount, "FLASH_LOAN_FAILED");
|
||||||
|
|
||||||
|
_transferQuoteOut(_MAINTAINER_, mtFee);
|
||||||
|
if (_RState_ != newRState) {
|
||||||
|
_RState_ = newRState;
|
||||||
|
_BASE_TARGET_ = newBaseTarget;
|
||||||
|
}
|
||||||
|
|
||||||
|
_syncReserve();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ============ Query Functions ============
|
// ============ Query Functions ============
|
||||||
|
|
||||||
function querySellBase(address trader, uint256 payBaseAmount)
|
function querySellBase(address trader, uint256 payBaseAmount)
|
||||||
|
|||||||
@@ -95,6 +95,7 @@ contract DPPVault is DPPStorage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// todo 高级功能,需要讨论
|
// todo 高级功能,需要讨论
|
||||||
|
// 如果单独执行这个功能会导致状态失衡
|
||||||
function retrieve(
|
function retrieve(
|
||||||
address payable to,
|
address payable to,
|
||||||
address token,
|
address token,
|
||||||
|
|||||||
@@ -70,16 +70,4 @@ contract DVMFunding is DVMVault {
|
|||||||
data
|
data
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 高级功能,需要讨论加不加
|
|
||||||
// function retrieve(address to) external preventReentrant {
|
|
||||||
// (uint256 baseBalance, uint256 quoteBalance) = getVaultBalance();
|
|
||||||
// (uint256 baseReserve, uint256 quoteReserve) = getVaultReserve();
|
|
||||||
// if (baseBalance.sub(baseReserve) > 0) {
|
|
||||||
// transferBaseOut(to, baseBalance.sub(baseReserve));
|
|
||||||
// }
|
|
||||||
// if (quoteBalance.sub(quoteReserve) > 0) {
|
|
||||||
// transferQuoteOut(to, quoteBalance.sub(quoteReserve));
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,71 +0,0 @@
|
|||||||
/*
|
|
||||||
|
|
||||||
Copyright 2020 DODO ZOO.
|
|
||||||
SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
pragma solidity 0.6.9;
|
|
||||||
pragma experimental ABIEncoderV2;
|
|
||||||
|
|
||||||
interface IDVMVault {
|
|
||||||
function init(
|
|
||||||
address owner,
|
|
||||||
address _baseToken,
|
|
||||||
address _quoteToken
|
|
||||||
) external;
|
|
||||||
|
|
||||||
function _BASE_TOKEN_() external returns (address);
|
|
||||||
|
|
||||||
function _QUOTE_TOKEN_() external returns (address);
|
|
||||||
|
|
||||||
function _BASE_RESERVE_() external returns (uint256);
|
|
||||||
|
|
||||||
function _QUOTE_RESERVE_() external returns (uint256);
|
|
||||||
|
|
||||||
function symbol() external returns (string memory);
|
|
||||||
|
|
||||||
function decimals() external returns (uint256);
|
|
||||||
|
|
||||||
function name() external returns (string memory);
|
|
||||||
|
|
||||||
function totalSupply() external returns (uint256);
|
|
||||||
|
|
||||||
function getVaultBalance() external view returns (uint256 baseBalance, uint256 quoteBalance);
|
|
||||||
|
|
||||||
function getVaultReserve() external view returns (uint256 baseReserve, uint256 quoteReserve);
|
|
||||||
|
|
||||||
function getBaseBalance() external view returns (uint256 baseBalance);
|
|
||||||
|
|
||||||
function getQuoteBalance() external view returns (uint256 quoteBalance);
|
|
||||||
|
|
||||||
function getBaseInput() external view returns (uint256 input);
|
|
||||||
|
|
||||||
function getQuoteInput() external view returns (uint256 input);
|
|
||||||
|
|
||||||
function sync() external;
|
|
||||||
|
|
||||||
function transferBaseOut(address to, uint256 amount) external;
|
|
||||||
|
|
||||||
function transferQuoteOut(address to, uint256 amount) external;
|
|
||||||
|
|
||||||
function transfer(address to, uint256 amount) external returns (bool);
|
|
||||||
|
|
||||||
function balanceOf(address owner) external view returns (uint256 balance);
|
|
||||||
|
|
||||||
function shareRatioOf(address owner) external view returns (uint256 shareRatio);
|
|
||||||
|
|
||||||
function transferFrom(
|
|
||||||
address from,
|
|
||||||
address to,
|
|
||||||
uint256 amount
|
|
||||||
) external returns (bool);
|
|
||||||
|
|
||||||
function approve(address spender, uint256 amount) external returns (bool);
|
|
||||||
|
|
||||||
function allowance(address owner, address spender) external view returns (uint256);
|
|
||||||
|
|
||||||
function mint(address user, uint256 value) external;
|
|
||||||
|
|
||||||
function burn(address user, uint256 value) external;
|
|
||||||
}
|
|
||||||
@@ -24,6 +24,13 @@ interface IDODOCallee {
|
|||||||
bytes calldata data
|
bytes calldata data
|
||||||
) external;
|
) external;
|
||||||
|
|
||||||
|
function DPPFlashLoanCall(
|
||||||
|
address sender,
|
||||||
|
uint256 baseAmount,
|
||||||
|
uint256 quoteAmount,
|
||||||
|
bytes calldata data
|
||||||
|
) external;
|
||||||
|
|
||||||
function DPPWithdrawCall(
|
function DPPWithdrawCall(
|
||||||
address sender,
|
address sender,
|
||||||
uint256 baseAmount,
|
uint256 baseAmount,
|
||||||
|
|||||||
Reference in New Issue
Block a user