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

154 lines
4.9 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 {DPPStorage} from "./DPPStorage.sol";
import {IERC20} from "../../intf/IERC20.sol";
import {IDODOCallee} from "../../intf/IDODOCallee.sol";
import {SafeMath} from "../../lib/SafeMath.sol";
import {DecimalMath} from "../../lib/DecimalMath.sol";
import {SafeERC20} from "../../lib/SafeERC20.sol";
import {Ownable} from "../../lib/Ownable.sol";
2020-11-26 20:13:16 +08:00
import {PMMPricing} from "../../lib/PMMPricing.sol";
2020-11-18 17:51:50 +08:00
contract DPPVault is DPPStorage {
using SafeMath for uint256;
using SafeERC20 for IERC20;
2020-11-29 23:51:58 +08:00
// ============ Events ============
2020-12-21 23:16:55 +08:00
event Reset(uint256 newLpFeeRate, uint256 newMtFeeRate);
2020-11-29 23:51:58 +08:00
2020-11-30 00:04:57 +08:00
// ============ View Functions ============
function getVaultReserve() external view returns (uint256 baseReserve, uint256 quoteReserve) {
baseReserve = _BASE_RESERVE_;
quoteReserve = _QUOTE_RESERVE_;
}
2020-12-21 23:16:55 +08:00
function getUserFeeRate(address user)
external
view
returns (uint256 lpFeeRate, uint256 mtFeeRate)
{
2020-12-18 01:11:33 +08:00
lpFeeRate = _LP_FEE_RATE_MODEL_.getFeeRate(user);
mtFeeRate = _MT_FEE_RATE_MODEL_.getFeeRate(user);
}
2020-12-21 23:16:55 +08:00
function getUserTradePermission(address user)
external
view
returns (bool isBuyAllow, bool isSellAllow)
{
2020-12-18 01:11:33 +08:00
isBuyAllow = (!_BUYING_CLOSE_ && _TRADE_PERMISSION_.isAllowed(user));
2020-12-21 23:16:55 +08:00
isSellAllow = (!_SELLING_CLOSE_ && _TRADE_PERMISSION_.isAllowed(user));
2020-12-18 01:11:33 +08:00
}
2020-11-22 18:20:09 +08:00
// ============ Get Input ============
2020-11-18 17:51:50 +08:00
function getBaseInput() public view returns (uint256 input) {
return _BASE_TOKEN_.balanceOf(address(this)).sub(_BASE_RESERVE_);
}
function getQuoteInput() public view returns (uint256 input) {
return _QUOTE_TOKEN_.balanceOf(address(this)).sub(_QUOTE_RESERVE_);
}
2020-11-28 17:44:39 +08:00
// ============ Set States ============
2020-11-18 17:51:50 +08:00
2020-12-21 23:16:55 +08:00
function ratioSync() external preventReentrant onlyOwner {
2020-12-18 21:33:40 +08:00
uint256 baseBalance = _BASE_TOKEN_.balanceOf(address(this));
uint256 quoteBalance = _QUOTE_TOKEN_.balanceOf(address(this));
if (baseBalance != _BASE_RESERVE_) {
_BASE_TARGET_ = _BASE_TARGET_.mul(baseBalance).div(_BASE_RESERVE_);
_BASE_RESERVE_ = baseBalance;
}
if (quoteBalance != _QUOTE_RESERVE_) {
_QUOTE_TARGET_ = _QUOTE_TARGET_.mul(quoteBalance).div(_QUOTE_RESERVE_);
_QUOTE_RESERVE_ = quoteBalance;
}
}
2020-11-26 20:13:16 +08:00
function setTarget(uint256 baseTarget, uint256 quoteTarget) public preventReentrant onlyOwner {
2020-11-18 17:51:50 +08:00
_BASE_TARGET_ = baseTarget;
_QUOTE_TARGET_ = quoteTarget;
2020-11-26 20:13:16 +08:00
_setRState();
2020-11-18 17:51:50 +08:00
}
2020-11-25 22:22:07 +08:00
2020-11-22 18:20:09 +08:00
function _resetTargetAndReserve() internal {
2020-11-18 17:51:50 +08:00
_BASE_TARGET_ = _BASE_TOKEN_.balanceOf(address(this));
_QUOTE_TARGET_ = _QUOTE_TOKEN_.balanceOf(address(this));
_BASE_RESERVE_ = _BASE_TARGET_;
_QUOTE_RESERVE_ = _QUOTE_TARGET_;
2020-11-26 20:13:16 +08:00
_setRState();
2020-11-18 17:51:50 +08:00
}
2020-11-22 18:20:09 +08:00
function reset(
2020-11-26 13:22:22 +08:00
address assetTo,
2020-11-22 18:20:09 +08:00
uint256 newLpFeeRate,
uint256 newMtFeeRate,
uint256 newI,
2020-11-23 10:43:12 +08:00
uint256 newK,
uint256 baseOutAmount,
2020-12-18 11:27:45 +08:00
uint256 quoteOutAmount,
uint256 minBaseReserve,
uint256 minQuoteReserve
) public preventReentrant onlyOwner returns (bool) {
2020-12-21 23:16:55 +08:00
require(
_BASE_RESERVE_ >= minBaseReserve && _QUOTE_RESERVE_ >= minQuoteReserve,
"RESERVE_AMOUNT_IS_NOT_ENOUGH"
);
2020-11-22 18:20:09 +08:00
_LP_FEE_RATE_MODEL_.setFeeRate(newLpFeeRate);
_MT_FEE_RATE_MODEL_.setFeeRate(newMtFeeRate);
_I_.set(newI);
_K_.set(newK);
2020-11-26 20:13:16 +08:00
_transferBaseOut(assetTo, baseOutAmount);
_transferQuoteOut(assetTo, quoteOutAmount);
_resetTargetAndReserve();
2020-11-28 17:44:39 +08:00
_checkIK();
2020-12-18 01:11:33 +08:00
emit Reset(newLpFeeRate, newMtFeeRate);
2020-12-18 11:27:45 +08:00
return true;
2020-11-22 18:20:09 +08:00
}
2020-11-26 20:13:16 +08:00
function _setRState() internal {
if (_BASE_RESERVE_ == _BASE_TARGET_ && _QUOTE_RESERVE_ == _QUOTE_TARGET_) {
_RState_ = PMMPricing.RState.ONE;
2020-12-07 16:31:58 +08:00
} else if (_BASE_RESERVE_ > _BASE_TARGET_ && _QUOTE_RESERVE_ < _QUOTE_TARGET_) {
2020-11-26 20:13:16 +08:00
_RState_ = PMMPricing.RState.BELOW_ONE;
2020-12-07 16:31:58 +08:00
} else if (_BASE_RESERVE_ < _BASE_TARGET_ && _QUOTE_RESERVE_ > _QUOTE_TARGET_) {
2020-11-26 20:13:16 +08:00
_RState_ = PMMPricing.RState.ABOVE_ONE;
} else {
require(false, "R_STATE_WRONG");
}
2020-11-18 17:51:50 +08:00
}
2020-11-29 17:38:13 +08:00
// ============ Asset Out ============
2020-11-18 17:51:50 +08:00
function _transferBaseOut(address to, uint256 amount) internal {
if (amount > 0) {
_BASE_TOKEN_.safeTransfer(to, amount);
}
}
function _transferQuoteOut(address to, uint256 amount) internal {
if (amount > 0) {
_QUOTE_TOKEN_.safeTransfer(to, amount);
}
}
function retrieve(
2020-11-26 20:13:16 +08:00
address to,
2020-11-18 17:51:50 +08:00
address token,
uint256 amount
2020-11-26 20:13:16 +08:00
) external preventReentrant onlyOwner {
require(token != address(_BASE_TOKEN_) && token != address(_QUOTE_TOKEN_), "USE_RESET");
IERC20(token).safeTransfer(to, amount);
2020-11-18 17:51:50 +08:00
}
}