Add buying, selling and capital limit controls

These advanced controls are disabled by default.

Also disable depositing and trading when creating a new DODO.
This commit is contained in:
Michael Zhou
2020-09-08 19:45:17 +08:00
parent 8a5cca991f
commit 30040834f6
9 changed files with 651 additions and 266 deletions

View File

@@ -10,6 +10,7 @@ pragma experimental ABIEncoderV2;
import {Storage} from "./Storage.sol";
/**
* @title Admin
* @author DODO Breeder
@@ -92,4 +93,30 @@ contract Admin is Storage {
function enableBaseDeposit() external onlyOwner notClosed {
_DEPOSIT_BASE_ALLOWED_ = true;
}
// ============ Advanced Control Functions ============
function disableBuying() external onlySupervisorOrOwner {
_BUYING_ALLOWED_ = false;
}
function enableBuying() external onlyOwner notClosed {
_BUYING_ALLOWED_ = true;
}
function disableSelling() external onlySupervisorOrOwner {
_SELLING_ALLOWED_ = false;
}
function enableSelling() external onlyOwner notClosed {
_SELLING_ALLOWED_ = true;
}
function setBaseBalanceLimit(uint256 newBaseBalanceLimit) external onlyOwner notClosed {
_BASE_BALANCE_LIMIT_ = newBaseBalanceLimit;
}
function setQuoteBalanceLimit(uint256 newQuoteBalanceLimit) external onlyOwner notClosed {
_QUOTE_BALANCE_LIMIT_ = newQuoteBalanceLimit;
}
}

View File

@@ -15,6 +15,7 @@ import {Types} from "../lib/Types.sol";
import {IERC20} from "../intf/IERC20.sol";
import {Storage} from "./Storage.sol";
/**
* @title Settlement
* @author DODO Breeder
@@ -34,11 +35,16 @@ contract Settlement is Storage {
// ============ Assets IN/OUT Functions ============
function _baseTokenTransferIn(address from, uint256 amount) internal {
require(_BASE_BALANCE_.add(amount) <= _BASE_BALANCE_LIMIT_, "BASE_BALANCE_LIMIT_EXCEEDED");
IERC20(_BASE_TOKEN_).safeTransferFrom(from, address(this), amount);
_BASE_BALANCE_ = _BASE_BALANCE_.add(amount);
}
function _quoteTokenTransferIn(address from, uint256 amount) internal {
require(
_QUOTE_BALANCE_.add(amount) <= _QUOTE_BALANCE_LIMIT_,
"QUOTE_BALANCE_LIMIT_EXCEEDED"
);
IERC20(_QUOTE_TOKEN_).safeTransferFrom(from, address(this), amount);
_QUOTE_BALANCE_ = _QUOTE_BALANCE_.add(amount);
}

View File

@@ -16,6 +16,7 @@ import {IOracle} from "../intf/IOracle.sol";
import {IDODOLpToken} from "../intf/IDODOLpToken.sol";
import {Types} from "../lib/Types.sol";
/**
* @title Storage
* @author DODO Breeder
@@ -34,6 +35,12 @@ contract Storage is InitializableOwnable, ReentrancyGuard {
bool public _TRADE_ALLOWED_;
uint256 public _GAS_PRICE_LIMIT_;
// ============ Advanced Controls ============
bool public _BUYING_ALLOWED_;
bool public _SELLING_ALLOWED_;
uint256 public _BASE_BALANCE_LIMIT_;
uint256 public _QUOTE_BALANCE_LIMIT_;
// ============ Core Address ============
address public _SUPERVISOR_; // could freeze system in emergency
@@ -106,6 +113,6 @@ contract Storage is InitializableOwnable, ReentrancyGuard {
// ============ Version Control ============
function version() external pure returns (uint256) {
return 100; // 1.0.0
return 101; // 1.0.1
}
}

View File

@@ -16,6 +16,7 @@ import {Storage} from "./Storage.sol";
import {Pricing} from "./Pricing.sol";
import {Settlement} from "./Settlement.sol";
/**
* @title Trader
* @author DODO Breeder
@@ -40,6 +41,16 @@ contract Trader is Storage, Pricing, Settlement {
_;
}
modifier buyingAllowed() {
require(_BUYING_ALLOWED_, "BUYING_NOT_ALLOWED");
_;
}
modifier sellingAllowed() {
require(_SELLING_ALLOWED_, "SELLING_NOT_ALLOWED");
_;
}
modifier gasPriceLimit() {
require(tx.gasprice <= _GAS_PRICE_LIMIT_, "GAS_PRICE_EXCEED");
_;
@@ -51,7 +62,7 @@ contract Trader is Storage, Pricing, Settlement {
uint256 amount,
uint256 minReceiveQuote,
bytes calldata data
) external tradeAllowed gasPriceLimit preventReentrant returns (uint256) {
) external tradeAllowed sellingAllowed gasPriceLimit preventReentrant returns (uint256) {
// query price
(
uint256 receiveQuote,
@@ -95,7 +106,7 @@ contract Trader is Storage, Pricing, Settlement {
uint256 amount,
uint256 maxPayQuote,
bytes calldata data
) external tradeAllowed gasPriceLimit preventReentrant returns (uint256) {
) external tradeAllowed buyingAllowed gasPriceLimit preventReentrant returns (uint256) {
// query price
(
uint256 payQuote,