Files
dodo-contractV2/contracts/DODOVendingMachine/impl/DVMTrader.sol

181 lines
5.6 KiB
Solidity
Raw Normal View History

2020-10-23 01:16:52 +08:00
/*
Copyright 2020 DODO ZOO.
SPDX-License-Identifier: Apache-2.0
*/
pragma solidity 0.6.9;
pragma experimental ABIEncoderV2;
2020-11-18 17:51:50 +08:00
import {DVMVault} from "./DVMVault.sol";
2020-10-23 01:16:52 +08:00
import {SafeMath} from "../../lib/SafeMath.sol";
import {DecimalMath} from "../../lib/DecimalMath.sol";
import {DODOMath} from "../../lib/DODOMath.sol";
2020-11-06 16:03:18 +08:00
import {IDODOCallee} from "../../intf/IDODOCallee.sol";
2020-11-20 18:58:35 +08:00
import {PMMPricing} from "../../lib/PMMPricing.sol";
2020-10-23 17:11:50 +08:00
2020-11-18 17:51:50 +08:00
contract DVMTrader is DVMVault {
2020-10-23 17:11:50 +08:00
using SafeMath for uint256;
2020-10-23 01:16:52 +08:00
2020-11-29 17:38:13 +08:00
// ============ Events ============
2020-11-29 23:51:58 +08:00
event DODOSwap(
2020-12-14 01:22:30 +08:00
address fromToken,
address toToken,
2020-11-29 17:38:13 +08:00
uint256 fromAmount,
uint256 toAmount,
2021-01-15 13:24:48 +08:00
address trader,
address receiver
2020-11-29 17:38:13 +08:00
);
2020-12-18 01:11:33 +08:00
event DODOFlashLoan(
address borrower,
address assetTo,
uint256 baseAmount,
uint256 quoteAmount
);
// ============ Trade Functions ============
2020-11-18 17:51:50 +08:00
2020-11-06 16:03:18 +08:00
function sellBase(address to)
external
preventReentrant
returns (uint256 receiveQuoteAmount)
{
2020-12-30 12:23:52 +08:00
uint256 baseBalance = _BASE_TOKEN_.balanceOf(address(this));
uint256 baseInput = baseBalance.sub(uint256(_BASE_RESERVE_));
2020-10-23 17:11:50 +08:00
uint256 mtFee;
2020-11-18 17:51:50 +08:00
(receiveQuoteAmount, mtFee) = querySellBase(tx.origin, baseInput);
2020-11-29 23:51:58 +08:00
2020-11-18 17:51:50 +08:00
_transferQuoteOut(to, receiveQuoteAmount);
_transferQuoteOut(_MAINTAINER_, mtFee);
2020-12-30 12:23:52 +08:00
_setReserve(baseBalance, _QUOTE_TOKEN_.balanceOf(address(this)));
2020-11-29 23:51:58 +08:00
emit DODOSwap(
2020-11-29 17:38:13 +08:00
address(_BASE_TOKEN_),
address(_QUOTE_TOKEN_),
baseInput,
receiveQuoteAmount,
2021-01-15 13:24:48 +08:00
msg.sender,
to
2020-11-29 17:38:13 +08:00
);
2020-10-23 01:16:52 +08:00
}
2020-11-06 16:03:18 +08:00
function sellQuote(address to)
external
preventReentrant
returns (uint256 receiveBaseAmount)
{
2020-12-30 12:23:52 +08:00
uint256 quoteBalance = _QUOTE_TOKEN_.balanceOf(address(this));
uint256 quoteInput = quoteBalance.sub(uint256(_QUOTE_RESERVE_));
2020-10-23 17:11:50 +08:00
uint256 mtFee;
2020-11-18 17:51:50 +08:00
(receiveBaseAmount, mtFee) = querySellQuote(tx.origin, quoteInput);
2020-11-29 23:51:58 +08:00
2020-11-18 17:51:50 +08:00
_transferBaseOut(to, receiveBaseAmount);
_transferBaseOut(_MAINTAINER_, mtFee);
2020-12-30 12:23:52 +08:00
_setReserve(_BASE_TOKEN_.balanceOf(address(this)), quoteBalance);
2020-11-29 23:51:58 +08:00
emit DODOSwap(
2020-11-29 17:38:13 +08:00
address(_QUOTE_TOKEN_),
address(_BASE_TOKEN_),
quoteInput,
receiveBaseAmount,
2021-01-15 13:24:48 +08:00
msg.sender,
to
2020-11-29 17:38:13 +08:00
);
2020-10-23 01:16:52 +08:00
}
2020-11-05 00:26:45 +08:00
function flashLoan(
uint256 baseAmount,
uint256 quoteAmount,
address assetTo,
bytes calldata data
2020-12-30 12:23:52 +08:00
) external preventReentrant {
2020-11-18 17:51:50 +08:00
_transferBaseOut(assetTo, baseAmount);
_transferQuoteOut(assetTo, quoteAmount);
2020-11-06 16:03:18 +08:00
if (data.length > 0)
IDODOCallee(assetTo).DVMFlashLoanCall(msg.sender, baseAmount, quoteAmount, data);
2020-11-05 00:26:45 +08:00
2020-11-29 17:38:13 +08:00
uint256 baseBalance = _BASE_TOKEN_.balanceOf(address(this));
uint256 quoteBalance = _QUOTE_TOKEN_.balanceOf(address(this));
2020-12-18 01:11:33 +08:00
2020-11-27 01:42:46 +08:00
// no input -> pure loss
require(
baseBalance >= _BASE_RESERVE_ || quoteBalance >= _QUOTE_RESERVE_,
"FLASH_LOAN_FAILED"
);
2020-11-29 17:38:13 +08:00
// sell quote
2020-11-27 01:42:46 +08:00
if (baseBalance < _BASE_RESERVE_) {
2020-12-30 12:23:52 +08:00
uint256 quoteInput = quoteBalance.sub(uint256(_QUOTE_RESERVE_));
2020-11-29 17:38:13 +08:00
(uint256 receiveBaseAmount, uint256 mtFee) = querySellQuote(tx.origin, quoteInput);
2020-12-30 12:23:52 +08:00
require(uint256(_BASE_RESERVE_).sub(baseBalance) <= receiveBaseAmount, "FLASH_LOAN_FAILED");
2020-11-29 23:51:58 +08:00
2020-11-27 01:42:46 +08:00
_transferBaseOut(_MAINTAINER_, mtFee);
2020-11-29 23:51:58 +08:00
emit DODOSwap(
2020-11-29 17:38:13 +08:00
address(_QUOTE_TOKEN_),
address(_BASE_TOKEN_),
quoteInput,
receiveBaseAmount,
2021-01-15 13:24:48 +08:00
msg.sender,
assetTo
2020-11-29 17:38:13 +08:00
);
2020-11-06 00:31:30 +08:00
}
2020-11-27 01:42:46 +08:00
2020-11-29 17:38:13 +08:00
// sell base
2020-11-27 01:42:46 +08:00
if (quoteBalance < _QUOTE_RESERVE_) {
2020-12-30 12:23:52 +08:00
uint256 baseInput = baseBalance.sub(uint256(_BASE_RESERVE_));
2020-11-29 17:38:13 +08:00
(uint256 receiveQuoteAmount, uint256 mtFee) = querySellBase(tx.origin, baseInput);
2020-12-30 12:23:52 +08:00
require(uint256(_QUOTE_RESERVE_).sub(quoteBalance) <= receiveQuoteAmount, "FLASH_LOAN_FAILED");
2020-11-29 23:51:58 +08:00
2020-11-27 01:42:46 +08:00
_transferQuoteOut(_MAINTAINER_, mtFee);
2020-11-29 23:51:58 +08:00
emit DODOSwap(
2020-11-29 17:38:13 +08:00
address(_BASE_TOKEN_),
address(_QUOTE_TOKEN_),
baseInput,
receiveQuoteAmount,
2021-01-15 13:24:48 +08:00
msg.sender,
assetTo
2020-11-29 17:38:13 +08:00
);
2020-11-06 00:31:30 +08:00
}
2020-11-18 17:51:50 +08:00
_sync();
2020-12-18 01:11:33 +08:00
emit DODOFlashLoan(msg.sender, assetTo, baseAmount, quoteAmount);
2020-11-05 00:26:45 +08:00
}
2020-11-29 17:38:13 +08:00
// ============ Query Functions ============
2020-11-28 17:44:39 +08:00
2020-11-05 00:26:45 +08:00
function querySellBase(address trader, uint256 payBaseAmount)
2020-10-23 17:11:50 +08:00
public
view
returns (uint256 receiveQuoteAmount, uint256 mtFee)
{
2020-11-18 17:51:50 +08:00
(receiveQuoteAmount, ) = PMMPricing.sellBaseToken(getPMMState(), payBaseAmount);
2020-11-05 00:26:45 +08:00
2020-12-30 18:41:13 +08:00
uint256 lpFeeRate = _LP_FEE_RATE_;
2020-11-06 00:31:30 +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
2020-12-30 18:41:13 +08:00
.sub(DecimalMath.mulFloor(receiveQuoteAmount, lpFeeRate))
2020-11-28 17:44:39 +08:00
.sub(mtFee);
2020-10-23 17:11:50 +08:00
}
2020-10-23 01:16:52 +08:00
2020-11-05 00:26:45 +08:00
function querySellQuote(address trader, uint256 payQuoteAmount)
2020-10-23 17:11:50 +08:00
public
view
returns (uint256 receiveBaseAmount, uint256 mtFee)
{
2020-11-18 17:51:50 +08:00
(receiveBaseAmount, ) = PMMPricing.sellQuoteToken(getPMMState(), payQuoteAmount);
2020-11-05 00:26:45 +08:00
2020-12-30 18:41:13 +08:00
uint256 lpFeeRate = _LP_FEE_RATE_;
2020-11-06 00:31:30 +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
2020-12-30 18:41:13 +08:00
.sub(DecimalMath.mulFloor(receiveBaseAmount, lpFeeRate))
2020-11-28 17:44:39 +08:00
.sub(mtFee);
2020-11-06 16:03:18 +08:00
}
2020-10-23 17:11:50 +08:00
}