第一次走查

This commit is contained in:
mingda
2020-11-28 17:44:39 +08:00
parent 7fc0ede7ea
commit 13904ae413
27 changed files with 667 additions and 856 deletions

View File

@@ -38,7 +38,9 @@ contract DVM is DVMTrader, DVMFunding {
_GAS_PRICE_LIMIT_ = IExternalValue(gasPriceSource);
_MAINTAINER_ = maintainer;
require(i > 0 && i < 10**36);
require(_BASE_TOKEN_ != _QUOTE_TOKEN_, "BASE_QUOTE_CAN_NOT_BE_SAME");
require(i > 0 && i <= 10**36);
_I_ = i;
require(k > 0 && k <= 10**18);
@@ -46,7 +48,13 @@ contract DVM is DVMTrader, DVMFunding {
string memory connect = "_";
string memory suffix = "DLP";
string memory uid = string(abi.encodePacked(address(this)));
uint32 uid = uint32(address(this));
bytes memory id = new bytes(4);
id[0] = bytes1(uint8(48 + (uid % 10)));
id[1] = bytes1(uint8(48 + ((uid / 10) % 10)));
id[2] = bytes1(uint8(48 + ((uid / 100) % 10)));
id[3] = bytes1(uint8(48 + ((uid / 1000) % 10)));
name = string(
abi.encodePacked(
suffix,
@@ -55,7 +63,7 @@ contract DVM is DVMTrader, DVMFunding {
connect,
_QUOTE_TOKEN_.symbol(),
connect,
uid
string(id)
)
);
symbol = "DLP";

View File

@@ -10,9 +10,9 @@ pragma experimental ABIEncoderV2;
import {IDVM} from "../intf/IDVM.sol";
import {InitializableOwnable} from "../../lib/InitializableOwnable.sol";
import {IExternalValue} from "../../lib/ExternalValue.sol";
contract DVMAdmin is InitializableOwnable {
address public _DVM_;
function init(address owner, address dvm) external {
@@ -20,25 +20,33 @@ contract DVMAdmin is InitializableOwnable {
_DVM_ = dvm;
}
function setLpFeeRateModel(address newLpFeeRateModel) external onlyOwner {
IDVM(_DVM_).setLpFeeRateModel(newLpFeeRateModel);
// function setLpFeeRateModel(address newLpFeeRateModel) external onlyOwner {
// IDVM(_DVM_).setLpFeeRateModel(newLpFeeRateModel);
// }
function setLpFeeRateValue(uint256 newLpFeeRate) external onlyOwner {
IExternalValue(IDVM(_DVM_)._LP_FEE_RATE_MODEL_()).set(newLpFeeRate);
}
function setMtFeeRateModel(address newMtFeeRateModel) external onlyOwner {
IDVM(_DVM_).setMtFeeRateModel(newMtFeeRateModel);
// function setMtFeeRateModel(address newMtFeeRateModel) external onlyOwner {
// IDVM(_DVM_).setMtFeeRateModel(newMtFeeRateModel);
// }
function setMtFeeRateValue(uint256 newMtFeeRate) external onlyOwner {
IExternalValue(IDVM(_DVM_)._MT_FEE_RATE_MODEL_()).set(newMtFeeRate);
}
function setTradePermissionManager(address newTradePermissionManager) external onlyOwner {
IDVM(_DVM_).setTradePermissionManager(newTradePermissionManager);
}
// function setTradePermissionManager(address newTradePermissionManager) external onlyOwner {
// IDVM(_DVM_).setTradePermissionManager(newTradePermissionManager);
// }
function setMaintainer(address newMaintainer) external onlyOwner {
IDVM(_DVM_).setMaintainer(newMaintainer);
}
function setGasPriceSource(address newGasPriceLimitSource) external onlyOwner {
IDVM(_DVM_).setGasPriceSource(newGasPriceLimitSource);
}
// function setGasPriceSource(address newGasPriceLimitSource) external onlyOwner {
// IDVM(_DVM_).setGasPriceSource(newGasPriceLimitSource);
// }
function setBuy(bool open) external onlyOwner {
IDVM(_DVM_).setBuy(open);

View File

@@ -44,11 +44,10 @@ contract DVMTrader is DVMVault {
external
preventReentrant
limitGasPrice
isSellAllow(to)
isSellAllow(to) // set DVM address in trade permission
returns (uint256 receiveQuoteAmount)
{
uint256 baseInput = getBaseInput();
require(baseInput > 0, "INSUFFICIENT_BASE_INPUT");
uint256 mtFee;
(receiveQuoteAmount, mtFee) = querySellBase(tx.origin, baseInput);
_transferQuoteOut(to, receiveQuoteAmount);
@@ -65,7 +64,6 @@ contract DVMTrader is DVMVault {
returns (uint256 receiveBaseAmount)
{
uint256 quoteInput = getQuoteInput();
require(quoteInput > 0, "INSUFFICIENT_QUOTE_INPUT");
uint256 mtFee;
(receiveBaseAmount, mtFee) = querySellQuote(tx.origin, quoteInput);
_transferBaseOut(to, receiveBaseAmount);
@@ -74,8 +72,6 @@ contract DVMTrader is DVMVault {
return receiveBaseAmount;
}
// 这是一个试验性质的函数
// 没有走标准库,需要仔细考虑下
function flashLoan(
uint256 baseAmount,
uint256 quoteAmount,
@@ -96,9 +92,6 @@ contract DVMTrader is DVMVault {
"FLASH_LOAN_FAILED"
);
// no output -> pure profit
if (baseBalance >= _BASE_RESERVE_ && quoteBalance >= _QUOTE_RESERVE_) return;
if (baseBalance < _BASE_RESERVE_) {
(uint256 receiveBaseAmount, uint256 mtFee) = querySellQuote(
tx.origin,
@@ -120,6 +113,8 @@ contract DVMTrader is DVMVault {
_sync();
}
// ============ View Functions ============
function querySellBase(address trader, uint256 payBaseAmount)
public
view
@@ -129,11 +124,10 @@ contract DVMTrader is DVMVault {
uint256 lpFeeRate = _LP_FEE_RATE_MODEL_.getFeeRate(trader);
uint256 mtFeeRate = _MT_FEE_RATE_MODEL_.getFeeRate(trader);
mtFee = DecimalMath.mulCeil(receiveQuoteAmount, mtFeeRate);
receiveQuoteAmount = DecimalMath.mulFloor(
receiveQuoteAmount,
DecimalMath.ONE.sub(mtFeeRate).sub(lpFeeRate)
);
mtFee = DecimalMath.mulFloor(receiveQuoteAmount, mtFeeRate);
receiveQuoteAmount = receiveQuoteAmount
.sub(DecimalMath.mulFloor(receiveQuoteAmount, lpFeeRate))
.sub(mtFee);
return (receiveQuoteAmount, mtFee);
}
@@ -147,18 +141,13 @@ contract DVMTrader is DVMVault {
uint256 lpFeeRate = _LP_FEE_RATE_MODEL_.getFeeRate(trader);
uint256 mtFeeRate = _MT_FEE_RATE_MODEL_.getFeeRate(trader);
mtFee = DecimalMath.mulCeil(receiveBaseAmount, mtFeeRate);
receiveBaseAmount = DecimalMath.mulFloor(
receiveBaseAmount,
DecimalMath.ONE.sub(mtFeeRate).sub(lpFeeRate)
);
mtFee = DecimalMath.mulFloor(receiveBaseAmount, mtFeeRate);
receiveBaseAmount = receiveBaseAmount
.sub(DecimalMath.mulFloor(receiveBaseAmount, lpFeeRate))
.sub(mtFee);
return (receiveBaseAmount, mtFee);
}
function getMidPrice() public view returns (uint256 midPrice) {
return PMMPricing.getMidPrice(getPMMState());
}
// ============ Helper Functions ============
function getPMMState() public view returns (PMMPricing.PMMState memory state) {
@@ -166,30 +155,14 @@ contract DVMTrader is DVMVault {
state.K = _K_;
state.B = _BASE_RESERVE_;
state.Q = _QUOTE_RESERVE_;
state.B0 = calculateBase0(state.B, state.Q);
state.B0 = 0; // recalculate in adjustedTarget
state.Q0 = 0;
state.R = PMMPricing.RState.ABOVE_ONE;
PMMPricing.adjustedTarget(state);
return state;
}
function calculateBase0(uint256 baseAmount, uint256 quoteAmount) public view returns (uint256) {
return
DODOMath._SolveQuadraticFunctionForTarget(
baseAmount,
quoteAmount,
DecimalMath.reciprocalFloor(_I_),
_K_
);
}
function getBase0() public view returns (uint256) {
(uint256 baseAmount, uint256 quoteAmount) = getVaultReserve();
return
DODOMath._SolveQuadraticFunctionForTarget(
baseAmount,
quoteAmount,
DecimalMath.reciprocalFloor(_I_),
_K_
);
function getMidPrice() public view returns (uint256 midPrice) {
return PMMPricing.getMidPrice(getPMMState());
}
}