init funding test

This commit is contained in:
mingda
2020-10-27 02:53:23 +08:00
parent 081c0ecde4
commit dd60e1727f
9 changed files with 305 additions and 120 deletions

View File

@@ -23,27 +23,27 @@ contract DVMFunding is DVMStorage {
if (baseReserve == 0 && quoteReserve == 0) {
mintAmount = baseInput;
}
// // case 2. supply when quote reserve is 0
// if (baseReserve > 0 && quoteReserve == 0) {
// uint256 mintRatio = DecimalMath.divFloor(baseInput, baseReserve);
// mintAmount = DecimalMath.mulFloor(_VAULT_.totalSupply(), mintRatio);
// }
// // case 3. normal case
// if (baseReserve > 0 && quoteReserve > 0) {
// uint256 baseInputRatio = DecimalMath.divFloor(baseInput, baseReserve);
// uint256 quoteInputRatio = DecimalMath.divFloor(quoteInput, quoteReserve);
// uint256 mintRatio = baseInputRatio > quoteInputRatio ? quoteInputRatio : baseInputRatio;
// // 在提币的时候向下取整。因此永远不会出现balance为0但totalsupply不为0的情况
// // 但有可能出现reserve>0但totalSupply=0的场景
// uint256 totalShare = _VAULT_.totalSupply();
// if (totalShare > 0) {
// mintAmount = DecimalMath.mulFloor(totalShare, mintRatio);
// } else {
// mintAmount = baseInput;
// }
// }
// _VAULT_.mint(account, mintAmount);
// _VAULT_.sync();
// case 2. supply when quote reserve is 0
if (baseReserve > 0 && quoteReserve == 0) {
uint256 mintRatio = DecimalMath.divFloor(baseInput, baseReserve);
mintAmount = DecimalMath.mulFloor(_VAULT_.totalSupply(), mintRatio);
}
// case 3. normal case
if (baseReserve > 0 && quoteReserve > 0) {
uint256 baseInputRatio = DecimalMath.divFloor(baseInput, baseReserve);
uint256 quoteInputRatio = DecimalMath.divFloor(quoteInput, quoteReserve);
uint256 mintRatio = baseInputRatio > quoteInputRatio ? quoteInputRatio : baseInputRatio;
// 在提币的时候向下取整。因此永远不会出现balance为0但totalsupply不为0的情况
// 但有可能出现reserve>0但totalSupply=0的场景
uint256 totalShare = _VAULT_.totalSupply();
if (totalShare > 0) {
mintAmount = DecimalMath.mulFloor(totalShare, mintRatio);
} else {
mintAmount = baseInput;
}
}
_VAULT_.mint(account, mintAmount);
_VAULT_.sync();
}
function sellShares(

View File

@@ -46,7 +46,6 @@ contract DVMStorage is InitializableOwnable, ReentrancyGuard {
IFeeRateModel public _MT_FEE_RATE_MODEL_;
uint256 public _K_;
uint256 public _I_;
uint256 public _BASE0_;
DVMVault public _VAULT_;
DVMVault public _PROTECTION_VAULT_;
@@ -59,13 +58,9 @@ contract DVMStorage is InitializableOwnable, ReentrancyGuard {
}
// ============ Helper Functions ============
function _updateBase0() internal {
function getBase0() public view returns (uint256) {
uint256 fairAmount = DecimalMath.divFloor(_VAULT_._QUOTE_RESERVE_(), _I_);
_BASE0_ = DODOMath._SolveQuadraticFunctionForTarget(
_VAULT_._BASE_RESERVE_(),
_K_,
fairAmount
);
return DODOMath._SolveQuadraticFunctionForTarget(_VAULT_._BASE_RESERVE_(), _K_, fairAmount);
}
// ============ Version Control ============

View File

@@ -25,7 +25,6 @@ contract DVMTrader is DVMStorage {
_VAULT_.transferQuoteOut(_MAINTAINER_, mtFee);
}
_VAULT_.sync();
_updateBase0(); // 这里需要想想原则上不需要update B0. 但精度问题或者用户往合约里充值可能导致需要updateBase0
return receiveQuoteAmount;
}
@@ -38,7 +37,6 @@ contract DVMTrader is DVMStorage {
_VAULT_.transferBaseOut(_MAINTAINER_, mtFee);
}
_VAULT_.sync();
_updateBase0();
return receiveBaseAmount;
}
@@ -47,10 +45,11 @@ contract DVMTrader is DVMStorage {
view
returns (uint256 receiveQuoteAmount, uint256 mtFee)
{
uint256 B0 = getBase0();
uint256 B2 = _VAULT_._BASE_RESERVE_();
uint256 B1 = B2.add(payBaseAmount);
require(_BASE0_ >= B1, "DODO_BASE_BALANCE_NOT_ENOUGH");
uint256 Q = DODOMath._GeneralIntegrate(_BASE0_, B1, B2, _I_, _K_);
require(B0 >= B1, "DODO_BASE_BALANCE_NOT_ENOUGH");
uint256 Q = DODOMath._GeneralIntegrate(B0, B1, B2, _I_, _K_);
uint256 lpFeeRate = _LP_FEE_RATE_MODEL_.getFeeRate(Q);
uint256 mtFeeRate = _MT_FEE_RATE_MODEL_.getFeeRate(Q);
mtFee = DecimalMath.mulCeil(Q, mtFeeRate);
@@ -63,10 +62,11 @@ contract DVMTrader is DVMStorage {
view
returns (uint256 receiveBaseAmount, uint256 mtFee)
{
uint256 B0 = getBase0();
uint256 B1 = _VAULT_._BASE_RESERVE_();
uint256 fairAmount = DecimalMath.divFloor(payQuoteAmount, _I_);
uint256 newBaseReserve = DODOMath._SolveQuadraticFunctionForTrade(
_BASE0_,
B0,
B1,
fairAmount,
false,
@@ -79,4 +79,12 @@ contract DVMTrader is DVMStorage {
receiveBaseAmount = deltaBase.sub(mtFee).sub(DecimalMath.mulCeil(deltaBase, lpFeeRate));
return (receiveBaseAmount, mtFee);
}
function getMidPrice() public view returns (uint256 midPrice) {
uint256 B0 = getBase0();
uint256 B1 = _VAULT_._BASE_RESERVE_();
uint256 offsetRatio = DecimalMath.ONE.mul(B0).div(B1).mul(B0).div(B1);
uint256 offset = DecimalMath.ONE.sub(_K_).add(DecimalMath.mulFloor(offsetRatio, _K_));
return DecimalMath.mulFloor(_I_, offset);
}
}