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

125 lines
3.8 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";
contract DPPVault is DPPStorage {
using SafeMath for uint256;
using SafeERC20 for IERC20;
2020-11-22 18:20:09 +08:00
// ============ Get Input ============
2020-11-18 17:51:50 +08:00
function getInput() public view returns (uint256 baseInput, uint256 quoteInput) {
return (
_BASE_TOKEN_.balanceOf(address(this)).sub(_BASE_RESERVE_),
_QUOTE_TOKEN_.balanceOf(address(this)).sub(_QUOTE_RESERVE_)
);
}
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-23 22:33:23 +08:00
// ============ Vault Related
function getVaultReserve() public view returns (uint256 baseReserve, uint256 quoteReserve) {
return (_BASE_RESERVE_, _QUOTE_RESERVE_);
}
2020-11-18 17:51:50 +08:00
// ============ Set Status ============
function setTarget(uint256 baseTarget, uint256 quoteTarget) public onlyOwner {
_BASE_TARGET_ = baseTarget;
_QUOTE_TARGET_ = quoteTarget;
_checkStatus();
}
2020-11-22 18:20:09 +08:00
function _syncReserve() internal {
_BASE_RESERVE_ = _BASE_TOKEN_.balanceOf(address(this));
_QUOTE_RESERVE_ = _QUOTE_TOKEN_.balanceOf(address(this));
}
2020-11-18 17:51:50 +08:00
2020-11-23 22:33:23 +08:00
//TODO:
function initTargetAndReserve() public {
require(tx.origin == _OWNER_, "INIT FORBIDDEN");
_resetTargetAndReserve();
}
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-22 18:20:09 +08:00
function reset(
uint256 newLpFeeRate,
uint256 newMtFeeRate,
uint256 newI,
2020-11-23 10:43:12 +08:00
uint256 newK,
uint256 baseOutAmount,
2020-11-23 22:33:23 +08:00
uint256 quoteOutAmount
2020-11-22 18:20:09 +08:00
) public {
2020-11-23 10:43:12 +08:00
//TODO: owner 权限可以是operator
2020-11-23 22:33:23 +08:00
require(tx.origin == _OWNER_, "RESET FORBIDDEN");
2020-11-22 18:20:09 +08:00
require(newK > 0 && newK <= 10**18, "K OUT OF RANGE!");
2020-11-23 22:33:23 +08:00
if(baseOutAmount > 0) _transferBaseOut(tx.origin, baseOutAmount);
if(quoteOutAmount > 0) _transferQuoteOut(tx.origin, quoteOutAmount);
2020-11-22 18:20:09 +08:00
_resetTargetAndReserve();
_LP_FEE_RATE_MODEL_.setFeeRate(newLpFeeRate);
_MT_FEE_RATE_MODEL_.setFeeRate(newMtFeeRate);
_I_.set(newI);
_K_.set(newK);
}
2020-11-18 17:51:50 +08:00
function _checkStatus() internal view {
require(
!(_BASE_RESERVE_ < _BASE_TARGET_ && _QUOTE_RESERVE_ < _QUOTE_TARGET_),
"STATUS_WRONG"
);
}
// ============ Assets Transfer ============
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(
address payable to,
address token,
uint256 amount
) external onlyOwner {
require(to != address(_BASE_TOKEN_) && to != address(_QUOTE_TOKEN_), "USE_WITHDRAW");
2020-11-22 18:20:09 +08:00
if (token == 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) {
2020-11-18 17:51:50 +08:00
to.transfer(amount);
} else {
IERC20(token).safeTransfer(msg.sender, amount);
}
}
}