Files
dodo-contractV2/contracts/DODOPrivatePool/impl/DPPTrader.sol

245 lines
7.7 KiB
Solidity
Raw Normal View History

2020-11-18 17:51:50 +08:00
/*
Copyright 2020 DODO ZOO.
SPDX-License-Identifier: Apache-2.0
*/
pragma solidity 0.6.9;
pragma experimental ABIEncoderV2;
import {DPPVault} from "./DPPVault.sol";
import {SafeMath} from "../../lib/SafeMath.sol";
import {DecimalMath} from "../../lib/DecimalMath.sol";
2020-11-20 18:58:35 +08:00
import {PMMPricing} from "../../lib/PMMPricing.sol";
2020-11-19 01:02:31 +08:00
import {IDODOCallee} from "../../intf/IDODOCallee.sol";
2020-11-18 17:51:50 +08:00
contract DPPTrader is DPPVault {
using SafeMath for uint256;
2020-11-29 23:51:58 +08:00
// ============ Events ============
event DODOSwap(
2020-12-14 01:22:30 +08:00
address fromToken,
address toToken,
2020-11-29 23:51:58 +08:00
uint256 fromAmount,
uint256 toAmount,
2021-01-15 13:24:48 +08:00
address trader,
address receiver
2020-11-29 23:51:58 +08:00
);
2020-12-18 01:11:33 +08:00
event DODOFlashLoan(
address borrower,
address assetTo,
uint256 baseAmount,
uint256 quoteAmount
);
2020-12-18 15:42:16 +08:00
event RChange(PMMPricing.RState newRState);
2020-11-18 17:51:50 +08:00
// ============ Trade Functions ============
function sellBase(address to)
external
preventReentrant
returns (uint256 receiveQuoteAmount)
{
2020-12-30 18:41:13 +08:00
uint256 baseBalance = _BASE_TOKEN_.balanceOf(address(this));
uint256 baseInput = baseBalance.sub(uint256(_BASE_RESERVE_));
2020-11-18 17:51:50 +08:00
uint256 mtFee;
uint256 newBaseTarget;
2020-11-20 18:58:35 +08:00
PMMPricing.RState newRState;
2020-11-23 10:43:12 +08:00
(receiveQuoteAmount, mtFee, newRState, newBaseTarget) = querySellBase(tx.origin, baseInput);
2020-11-18 17:51:50 +08:00
_transferQuoteOut(to, receiveQuoteAmount);
_transferQuoteOut(_MAINTAINER_, mtFee);
2021-01-21 19:22:56 +08:00
2020-11-18 17:51:50 +08:00
// update TARGET
2021-01-19 17:10:46 +08:00
if (_RState_ != uint32(newRState)) {
require(newBaseTarget <= uint112(-1),"OVERFLOW");
_BASE_TARGET_ = uint112(newBaseTarget);
_RState_ = uint32(newRState);
2020-12-18 15:42:16 +08:00
emit RChange(newRState);
2020-11-18 17:51:50 +08:00
}
2021-01-21 19:22:56 +08:00
_setReserve(baseBalance, _QUOTE_TOKEN_.balanceOf(address(this)));
2020-11-29 23:51:58 +08:00
emit DODOSwap(
address(_BASE_TOKEN_),
address(_QUOTE_TOKEN_),
baseInput,
receiveQuoteAmount,
2021-01-15 13:24:48 +08:00
msg.sender,
to
2020-11-29 23:51:58 +08:00
);
2020-11-18 17:51:50 +08:00
}
function sellQuote(address to)
external
preventReentrant
returns (uint256 receiveBaseAmount)
{
2020-12-30 18:41:13 +08:00
uint256 quoteBalance = _QUOTE_TOKEN_.balanceOf(address(this));
uint256 quoteInput = quoteBalance.sub(uint256(_QUOTE_RESERVE_));
2020-11-18 17:51:50 +08:00
uint256 mtFee;
uint256 newQuoteTarget;
2020-11-20 18:58:35 +08:00
PMMPricing.RState newRState;
2020-11-27 01:42:46 +08:00
(receiveBaseAmount, mtFee, newRState, newQuoteTarget) = querySellQuote(
tx.origin,
quoteInput
);
2020-11-18 17:51:50 +08:00
_transferBaseOut(to, receiveBaseAmount);
_transferBaseOut(_MAINTAINER_, mtFee);
// update TARGET
2021-01-19 17:10:46 +08:00
if (_RState_ != uint32(newRState)) {
require(newQuoteTarget <= uint112(-1),"OVERFLOW");
_QUOTE_TARGET_ = uint112(newQuoteTarget);
_RState_ = uint32(newRState);
2020-12-18 15:42:16 +08:00
emit RChange(newRState);
2020-11-18 17:51:50 +08:00
}
2021-01-21 19:22:56 +08:00
_setReserve(_BASE_TOKEN_.balanceOf(address(this)), quoteBalance);
2020-11-29 23:51:58 +08:00
emit DODOSwap(
address(_QUOTE_TOKEN_),
address(_BASE_TOKEN_),
quoteInput,
receiveBaseAmount,
2021-01-15 13:24:48 +08:00
msg.sender,
to
2020-11-29 23:51:58 +08:00
);
2020-11-18 17:51:50 +08:00
}
2020-11-19 01:02:31 +08:00
function flashLoan(
uint256 baseAmount,
uint256 quoteAmount,
address assetTo,
bytes calldata data
2020-12-30 18:41:13 +08:00
) external preventReentrant {
2020-11-19 01:02:31 +08:00
_transferBaseOut(assetTo, baseAmount);
_transferQuoteOut(assetTo, quoteAmount);
2020-11-29 23:51:58 +08:00
2020-11-19 01:02:31 +08:00
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 input -> pure loss
require(
baseBalance >= _BASE_RESERVE_ || quoteBalance >= _QUOTE_RESERVE_,
"FLASH_LOAN_FAILED"
);
// sell quote case
// quote input + base output
if (baseBalance < _BASE_RESERVE_) {
2020-12-30 18:41:13 +08:00
uint256 quoteInput = quoteBalance.sub(uint256(_QUOTE_RESERVE_));
2020-11-19 01:02:31 +08:00
(
uint256 receiveBaseAmount,
uint256 mtFee,
2020-11-20 18:58:35 +08:00
PMMPricing.RState newRState,
2020-11-19 01:02:31 +08:00
uint256 newQuoteTarget
2020-11-29 23:51:58 +08:00
) = querySellQuote(tx.origin, quoteInput); // revert if quoteBalance<quoteReserve
2020-12-30 18:41:13 +08:00
require(uint256(_BASE_RESERVE_).sub(baseBalance) <= receiveBaseAmount, "FLASH_LOAN_FAILED");
2020-11-19 01:02:31 +08:00
_transferBaseOut(_MAINTAINER_, mtFee);
2021-01-19 17:10:46 +08:00
if (_RState_ != uint32(newRState)) {
require(newQuoteTarget <= uint112(-1),"OVERFLOW");
_QUOTE_TARGET_ = uint112(newQuoteTarget);
_RState_ = uint32(newRState);
2020-12-18 15:42:16 +08:00
emit RChange(newRState);
2020-11-19 01:02:31 +08:00
}
2020-11-29 23:51:58 +08:00
emit DODOSwap(
address(_QUOTE_TOKEN_),
address(_BASE_TOKEN_),
quoteInput,
receiveBaseAmount,
2021-01-15 13:24:48 +08:00
msg.sender,
assetTo
2020-11-29 23:51:58 +08:00
);
2020-11-19 01:02:31 +08:00
}
// sell base case
// base input + quote output
if (quoteBalance < _QUOTE_RESERVE_) {
2020-12-30 18:41:13 +08:00
uint256 baseInput = baseBalance.sub(uint256(_BASE_RESERVE_));
2020-11-19 01:02:31 +08:00
(
uint256 receiveQuoteAmount,
uint256 mtFee,
2020-11-20 18:58:35 +08:00
PMMPricing.RState newRState,
2020-11-19 01:02:31 +08:00
uint256 newBaseTarget
2020-11-29 23:51:58 +08:00
) = querySellBase(tx.origin, baseInput); // revert if baseBalance<baseReserve
2020-12-30 18:41:13 +08:00
require(uint256(_QUOTE_RESERVE_).sub(quoteBalance) <= receiveQuoteAmount, "FLASH_LOAN_FAILED");
2020-11-19 01:02:31 +08:00
_transferQuoteOut(_MAINTAINER_, mtFee);
2021-01-19 17:10:46 +08:00
if (_RState_ != uint32(newRState)) {
require(newBaseTarget <= uint112(-1),"OVERFLOW");
_BASE_TARGET_ = uint112(newBaseTarget);
_RState_ = uint32(newRState);
2020-12-18 15:42:16 +08:00
emit RChange(newRState);
2020-11-19 01:02:31 +08:00
}
2020-11-29 23:51:58 +08:00
emit DODOSwap(
address(_BASE_TOKEN_),
address(_QUOTE_TOKEN_),
baseInput,
receiveQuoteAmount,
2021-01-15 13:24:48 +08:00
msg.sender,
assetTo
2020-11-29 23:51:58 +08:00
);
2020-11-19 01:02:31 +08:00
}
2020-11-28 17:44:39 +08:00
2020-11-29 23:51:58 +08:00
_sync();
2020-12-18 01:11:33 +08:00
emit DODOFlashLoan(msg.sender, assetTo, baseAmount, quoteAmount);
2020-11-19 01:02:31 +08:00
}
2020-11-18 17:51:50 +08:00
// ============ Query Functions ============
function querySellBase(address trader, uint256 payBaseAmount)
public
view
returns (
uint256 receiveQuoteAmount,
uint256 mtFee,
2020-11-20 18:58:35 +08:00
PMMPricing.RState newRState,
2020-11-18 17:51:50 +08:00
uint256 newBaseTarget
)
{
2020-11-20 18:58:35 +08:00
PMMPricing.PMMState memory state = getPMMState();
2020-11-18 17:51:50 +08:00
(receiveQuoteAmount, newRState) = PMMPricing.sellBaseToken(state, payBaseAmount);
2020-12-30 18:41:13 +08:00
uint256 lpFeeRate = _LP_FEE_RATE_;
2020-11-18 17:51:50 +08:00
uint256 mtFeeRate = _MT_FEE_RATE_MODEL_.getFeeRate(trader);
2020-11-28 17:44:39 +08:00
mtFee = DecimalMath.mulFloor(receiveQuoteAmount, mtFeeRate);
receiveQuoteAmount = receiveQuoteAmount
.sub(DecimalMath.mulFloor(receiveQuoteAmount, lpFeeRate))
.sub(mtFee);
2020-11-29 23:51:58 +08:00
newBaseTarget = state.B0;
2020-11-18 17:51:50 +08:00
}
function querySellQuote(address trader, uint256 payQuoteAmount)
public
view
returns (
uint256 receiveBaseAmount,
uint256 mtFee,
2020-11-20 18:58:35 +08:00
PMMPricing.RState newRState,
2020-11-18 17:51:50 +08:00
uint256 newQuoteTarget
)
{
2020-11-20 18:58:35 +08:00
PMMPricing.PMMState memory state = getPMMState();
2020-11-18 17:51:50 +08:00
(receiveBaseAmount, newRState) = PMMPricing.sellQuoteToken(state, payQuoteAmount);
2020-12-30 18:41:13 +08:00
uint256 lpFeeRate = _LP_FEE_RATE_;
2020-11-18 17:51:50 +08:00
uint256 mtFeeRate = _MT_FEE_RATE_MODEL_.getFeeRate(trader);
2020-11-28 17:44:39 +08:00
mtFee = DecimalMath.mulFloor(receiveBaseAmount, mtFeeRate);
receiveBaseAmount = receiveBaseAmount
.sub(DecimalMath.mulFloor(receiveBaseAmount, lpFeeRate))
.sub(mtFee);
2020-11-29 23:51:58 +08:00
newQuoteTarget = state.Q0;
2020-11-18 17:51:50 +08:00
}
}