第二遍走查 snapshot

This commit is contained in:
mingda
2020-11-29 17:38:13 +08:00
parent c52e7960b6
commit d90e031a30
17 changed files with 285 additions and 314 deletions

View File

@@ -30,15 +30,10 @@ contract DVM is DVMTrader, DVMFunding {
uint256 k
) external {
initOwner(owner);
require(baseTokenAddress != quoteTokenAddress, "BASE_QUOTE_CAN_NOT_BE_SAME");
_BASE_TOKEN_ = IERC20(baseTokenAddress);
_QUOTE_TOKEN_ = IERC20(quoteTokenAddress);
_LP_FEE_RATE_MODEL_ = IFeeRateModel(lpFeeRateModel);
_MT_FEE_RATE_MODEL_ = IFeeRateModel(mtFeeRateModel);
_TRADE_PERMISSION_ = IPermissionManager(tradePermissionManager);
_GAS_PRICE_LIMIT_ = IExternalValue(gasPriceSource);
_MAINTAINER_ = maintainer;
require(_BASE_TOKEN_ != _QUOTE_TOKEN_, "BASE_QUOTE_CAN_NOT_BE_SAME");
require(i > 0 && i <= 10**36);
_I_ = i;
@@ -46,32 +41,35 @@ contract DVM is DVMTrader, DVMFunding {
require(k > 0 && k <= 10**18);
_K_ = k;
_LP_FEE_RATE_MODEL_ = IFeeRateModel(lpFeeRateModel);
_MT_FEE_RATE_MODEL_ = IFeeRateModel(mtFeeRateModel);
_TRADE_PERMISSION_ = IPermissionManager(tradePermissionManager);
_GAS_PRICE_LIMIT_ = IExternalValue(gasPriceSource);
_MAINTAINER_ = maintainer;
string memory connect = "_";
string memory suffix = "DLP";
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,
connect,
_BASE_TOKEN_.symbol(),
connect,
_QUOTE_TOKEN_.symbol(),
connect,
string(id)
)
);
name = string(abi.encodePacked(suffix, connect, addressToShortString(address(this))));
symbol = "DLP";
decimals = _BASE_TOKEN_.decimals();
}
function addressToShortString(address _addr) public pure returns (string memory) {
bytes32 value = bytes32(uint256(_addr));
bytes memory alphabet = "0123456789abcdef";
bytes memory str = new bytes(8);
for (uint256 i = 0; i < 4; i++) {
str[i * 2] = alphabet[uint8(value[i + 12] >> 4)];
str[1 + i * 2] = alphabet[uint8(value[i + 12] & 0x0f)];
}
return string(str);
}
// ============ Version Control ============
function version() external pure returns (uint256) {
return 100; // 1.0.0
function version() external pure returns (string memory) {
return "DVM 1.0.0";
}
}

View File

@@ -13,27 +13,39 @@ import {DecimalMath} from "../../lib/DecimalMath.sol";
import {IDODOCallee} from "../../intf/IDODOCallee.sol";
contract DVMFunding is DVMVault {
// shares [round down]
// ============ Events ============
event BuyShares(address indexed user, uint256 increaseShares, uint256 totalShares);
event SellShares(address indexed user, uint256 decreaseShares, uint256 totalShares);
// ============ Buy & Sell Shares ============
// buy shares [round down]
function buyShares(address to)
external
preventReentrant
returns (
uint256 shares,
uint256 baseAmount,
uint256 quoteAmount
uint256 baseInput,
uint256 quoteInput
)
{
uint256 baseInput = getBaseInput();
uint256 quoteInput = getQuoteInput();
require(baseInput > 0, "NO_BASE_INPUT");
uint256 baseBalance = _BASE_TOKEN_.balanceOf(address(this));
uint256 quoteBalance = _QUOTE_TOKEN_.balanceOf(address(this));
uint256 baseReserve = _BASE_RESERVE_;
uint256 quoteReserve = _QUOTE_RESERVE_;
baseInput = baseBalance.sub(baseReserve);
quoteInput = quoteBalance.sub(quoteReserve);
require(baseInput > 0, "NO_BASE_INPUT");
// case 1. initial supply
// 包含了 baseReserve == 0 && quoteReserve == 0 的情况
// 在提币的时候向下取整。因此永远不会出现balance为0但totalsupply不为0的情况
// 但有可能出现reserve>0但totalSupply=0的场景
if (totalSupply == 0) {
shares = getBaseBalance(); // 以免出现balance很大但shares很小的情况
shares = baseBalance; // 以免出现balance很大但shares很小的情况
} else if (baseReserve > 0 && quoteReserve == 0) {
// case 2. supply when quote reserve is 0
shares = baseInput.mul(totalSupply).div(baseReserve);
@@ -46,10 +58,10 @@ contract DVMFunding is DVMVault {
}
_mint(to, shares);
_sync();
return (shares, baseInput, quoteInput);
emit BuyShares(to, shares, _SHARES_[to]);
}
// withdraw amount [round down]
// sell shares [round down]
function sellShares(
uint256 shareAmount,
address to,
@@ -57,12 +69,20 @@ contract DVMFunding is DVMVault {
uint256 quoteMinAmount,
bytes calldata data
) external preventReentrant returns (uint256 baseAmount, uint256 quoteAmount) {
(uint256 baseBalance, uint256 quoteBalance) = getVaultBalance();
uint256 baseBalance = _BASE_TOKEN_.balanceOf(address(this));
uint256 quoteBalance = _QUOTE_TOKEN_.balanceOf(address(this));
uint256 totalShares = totalSupply;
require(shareAmount <= _SHARES_[msg.sender], "DLP_NOT_ENOUGH");
baseAmount = baseBalance.mul(shareAmount).div(totalShares);
quoteAmount = quoteBalance.mul(shareAmount).div(totalShares);
require(baseAmount >= baseMinAmount && quoteAmount >= quoteMinAmount,'WITHDRAW_DLP_NOT_ENOUGH');
require(
baseAmount >= baseMinAmount && quoteAmount >= quoteMinAmount,
"WITHDRAW_NOT_ENOUGH"
);
_burn(msg.sender, shareAmount);
_transferBaseOut(to, baseAmount);
_transferQuoteOut(to, quoteAmount);

View File

@@ -21,20 +21,17 @@ import {IERC20} from "../../intf/IERC20.sol";
contract DVMStorage is InitializableOwnable, ReentrancyGuard {
using SafeMath for uint256;
// ============ Variables for Control ============
IExternalValue public _GAS_PRICE_LIMIT_;
// ============ Advanced Controls ============
bool public _BUYING_CLOSE_;
bool public _SELLING_CLOSE_;
IPermissionManager public _TRADE_PERMISSION_;
IExternalValue public _GAS_PRICE_LIMIT_;
// ============ Core Address ============
address public _MAINTAINER_; // collect maintainer fee
address public _MAINTAINER_;
IERC20 public _BASE_TOKEN_;
IERC20 public _QUOTE_TOKEN_;
@@ -42,7 +39,7 @@ contract DVMStorage is InitializableOwnable, ReentrancyGuard {
uint256 public _BASE_RESERVE_;
uint256 public _QUOTE_RESERVE_;
// ============ Shares ============
// ============ Shares (ERC20) ============
string public symbol;
uint256 public decimals;
@@ -59,43 +56,56 @@ contract DVMStorage is InitializableOwnable, ReentrancyGuard {
uint256 public _K_;
uint256 public _I_;
// ============ Events ============
event SetLpFeeRateModel(address indexed oldAddr, address indexed newAddr);
event SetMtFeeRateModel(address indexed oldAddr, address indexed newAddr);
event SetTradePermissionManager(address indexed oldAddr, address indexed newAddr);
event SetMaintainer(address indexed oldAddr, address indexed newAddr);
event SetGasPriceSource(address indexed oldAddr, address indexed newAddr);
event SetBuy(bool allow);
event SetSell(bool allow);
// ============ Setting Functions ============
function setLpFeeRateModel(address newLpFeeRateModel) external onlyOwner {
emit SetLpFeeRateModel(address(_LP_FEE_RATE_MODEL_), newLpFeeRateModel);
_LP_FEE_RATE_MODEL_ = IFeeRateModel(newLpFeeRateModel);
}
function setMtFeeRateModel(address newMtFeeRateModel) external onlyOwner {
emit SetMtFeeRateModel(address(_MT_FEE_RATE_MODEL_), newMtFeeRateModel);
_MT_FEE_RATE_MODEL_ = IFeeRateModel(newMtFeeRateModel);
}
function setTradePermissionManager(address newTradePermissionManager) external onlyOwner {
emit SetTradePermissionManager(address(_TRADE_PERMISSION_), newTradePermissionManager);
_TRADE_PERMISSION_ = IPermissionManager(newTradePermissionManager);
}
function setMaintainer(address newMaintainer) external onlyOwner {
emit SetMaintainer(address(_MAINTAINER_), newMaintainer);
_MAINTAINER_ = newMaintainer;
}
function setGasPriceSource(address newGasPriceLimitSource) external onlyOwner {
emit SetGasPriceSource(address(_GAS_PRICE_LIMIT_), newGasPriceLimitSource);
_GAS_PRICE_LIMIT_ = IExternalValue(newGasPriceLimitSource);
}
function setBuy(bool open) external onlyOwner {
emit SetBuy(open);
_BUYING_CLOSE_ = !open;
}
function setSell(bool open) external onlyOwner {
emit SetSell(open);
_SELLING_CLOSE_ = !open;
}
// ============ View Functions ============
function getLpFeeRate(address trader) external view returns (uint256 feeRate) {
return _LP_FEE_RATE_MODEL_.getFeeRate(trader);
}
function getMtFeeRate(address trader) external view returns (uint256 feeRate) {
return _MT_FEE_RATE_MODEL_.getFeeRate(trader);
}
}

View File

@@ -18,6 +18,16 @@ import {PMMPricing} from "../../lib/PMMPricing.sol";
contract DVMTrader is DVMVault {
using SafeMath for uint256;
// ============ Events ============
event DVMSwap(
address indexed fromToken,
address indexed toToken,
uint256 fromAmount,
uint256 toAmount,
address trader
);
// ============ Modifiers ============
modifier isBuyAllow(address trader) {
@@ -53,7 +63,13 @@ contract DVMTrader is DVMVault {
_transferQuoteOut(to, receiveQuoteAmount);
_transferQuoteOut(_MAINTAINER_, mtFee);
_sync();
return receiveQuoteAmount;
emit DVMSwap(
address(_BASE_TOKEN_),
address(_QUOTE_TOKEN_),
baseInput,
receiveQuoteAmount,
tx.origin
);
}
function sellQuote(address to)
@@ -69,7 +85,13 @@ contract DVMTrader is DVMVault {
_transferBaseOut(to, receiveBaseAmount);
_transferBaseOut(_MAINTAINER_, mtFee);
_sync();
return receiveBaseAmount;
emit DVMSwap(
address(_QUOTE_TOKEN_),
address(_BASE_TOKEN_),
quoteInput,
receiveBaseAmount,
tx.origin
);
}
function flashLoan(
@@ -84,7 +106,8 @@ contract DVMTrader is DVMVault {
if (data.length > 0)
IDODOCallee(assetTo).DVMFlashLoanCall(msg.sender, baseAmount, quoteAmount, data);
(uint256 baseBalance, uint256 quoteBalance) = getVaultBalance();
uint256 baseBalance = _BASE_TOKEN_.balanceOf(address(this));
uint256 quoteBalance = _QUOTE_TOKEN_.balanceOf(address(this));
// no input -> pure loss
require(
@@ -92,28 +115,40 @@ contract DVMTrader is DVMVault {
"FLASH_LOAN_FAILED"
);
// sell quote
if (baseBalance < _BASE_RESERVE_) {
(uint256 receiveBaseAmount, uint256 mtFee) = querySellQuote(
tx.origin,
quoteBalance.sub(_QUOTE_RESERVE_)
);
uint256 quoteInput = quoteBalance.sub(_QUOTE_RESERVE_);
(uint256 receiveBaseAmount, uint256 mtFee) = querySellQuote(tx.origin, quoteInput);
require(_BASE_RESERVE_.sub(baseBalance) <= receiveBaseAmount, "FLASH_LOAN_FAILED");
_transferBaseOut(_MAINTAINER_, mtFee);
emit DVMSwap(
address(_QUOTE_TOKEN_),
address(_BASE_TOKEN_),
quoteInput,
receiveBaseAmount,
tx.origin
);
}
// sell base
if (quoteBalance < _QUOTE_RESERVE_) {
(uint256 receiveQuoteAmount, uint256 mtFee) = querySellBase(
tx.origin,
baseBalance.sub(_BASE_RESERVE_)
);
uint256 baseInput = baseBalance.sub(_BASE_RESERVE_);
(uint256 receiveQuoteAmount, uint256 mtFee) = querySellBase(tx.origin, baseInput);
require(_QUOTE_RESERVE_.sub(quoteBalance) <= receiveQuoteAmount, "FLASH_LOAN_FAILED");
_transferQuoteOut(_MAINTAINER_, mtFee);
emit DVMSwap(
address(_BASE_TOKEN_),
address(_QUOTE_TOKEN_),
baseInput,
receiveQuoteAmount,
tx.origin
);
}
_sync();
}
// ============ View Functions ============
// ============ Query Functions ============
function querySellBase(address trader, uint256 payBaseAmount)
public
@@ -128,8 +163,6 @@ contract DVMTrader is DVMVault {
receiveQuoteAmount = receiveQuoteAmount
.sub(DecimalMath.mulFloor(receiveQuoteAmount, lpFeeRate))
.sub(mtFee);
return (receiveQuoteAmount, mtFee);
}
function querySellQuote(address trader, uint256 payQuoteAmount)
@@ -145,7 +178,6 @@ contract DVMTrader is DVMVault {
receiveBaseAmount = receiveBaseAmount
.sub(DecimalMath.mulFloor(receiveBaseAmount, lpFeeRate))
.sub(mtFee);
return (receiveBaseAmount, mtFee);
}
// ============ Helper Functions ============
@@ -155,11 +187,10 @@ contract DVMTrader is DVMVault {
state.K = _K_;
state.B = _BASE_RESERVE_;
state.Q = _QUOTE_RESERVE_;
state.B0 = 0; // recalculate in adjustedTarget
state.B0 = 0; // will be calculated in adjustedTarget
state.Q0 = 0;
state.R = PMMPricing.RState.ABOVE_ONE;
PMMPricing.adjustedTarget(state);
return state;
}
function getMidPrice() public view returns (uint256 midPrice) {

View File

@@ -28,23 +28,7 @@ contract DVMVault is DVMStorage {
event Burn(address indexed user, uint256 value);
// Vault related
function getVaultBalance() public view returns (uint256 baseBalance, uint256 quoteBalance) {
return (_BASE_TOKEN_.balanceOf(address(this)), _QUOTE_TOKEN_.balanceOf(address(this)));
}
function getVaultReserve() public view returns (uint256 baseReserve, uint256 quoteReserve) {
return (_BASE_RESERVE_, _QUOTE_RESERVE_);
}
function getBaseBalance() public view returns (uint256 baseBalance) {
return _BASE_TOKEN_.balanceOf(address(this));
}
function getQuoteBalance() public view returns (uint256 quoteBalance) {
return _QUOTE_TOKEN_.balanceOf(address(this));
}
// ============ Asset In ============
function getBaseInput() public view returns (uint256 input) {
return _BASE_TOKEN_.balanceOf(address(this)).sub(_BASE_RESERVE_);
@@ -54,8 +38,11 @@ contract DVMVault is DVMStorage {
return _QUOTE_TOKEN_.balanceOf(address(this)).sub(_QUOTE_RESERVE_);
}
// ============ Set States ============
function _sync() internal {
(uint256 baseBalance, uint256 quoteBalance) = getVaultBalance();
uint256 baseBalance = _BASE_TOKEN_.balanceOf(address(this));
uint256 quoteBalance = _QUOTE_TOKEN_.balanceOf(address(this));
if (baseBalance != _BASE_RESERVE_) {
_BASE_RESERVE_ = baseBalance;
}
@@ -64,6 +51,8 @@ contract DVMVault is DVMStorage {
}
}
// ============ Asset Out ============
function _transferBaseOut(address to, uint256 amount) internal {
if (amount > 0) {
_BASE_TOKEN_.safeTransfer(to, amount);
@@ -76,7 +65,8 @@ contract DVMVault is DVMStorage {
}
}
// Shares related
// ============ Shares (ERC20) ============
/**
* @dev transfer token for a specified address
* @param to The address to transfer to.
@@ -100,10 +90,6 @@ contract DVMVault is DVMStorage {
return _SHARES_[owner];
}
function shareRatioOf(address owner) external view returns (uint256 shareRatio) {
return DecimalMath.divFloor(_SHARES_[owner], totalSupply);
}
/**
* @dev Transfer tokens from one address to another
* @param from address The address which you want to send tokens from