fix dvm test context
This commit is contained in:
@@ -16,7 +16,7 @@ import {IPermissionManager} from "../../lib/PermissionManager.sol";
|
||||
import {IExternalValue} from "../../lib/ExternalValue.sol";
|
||||
import {IFeeRateModel} from "../../intf/IFeeRateModel.sol";
|
||||
import {IERC20} from "../../intf/IERC20.sol";
|
||||
import {RState} from "../../lib/PMMPricing.sol";
|
||||
import {PMMPricing} from "../../lib/PMMPricing.sol";
|
||||
|
||||
/**
|
||||
* @title Storage
|
||||
@@ -49,7 +49,7 @@ contract DPPStorage is InitializableOwnable, ReentrancyGuard {
|
||||
uint256 public _QUOTE_RESERVE_;
|
||||
uint256 public _BASE_TARGET_;
|
||||
uint256 public _QUOTE_TARGET_;
|
||||
RState public _RState_;
|
||||
PMMPricing.RState public _RState_;
|
||||
|
||||
// ============ Variables for Pricing ============
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ pragma experimental ABIEncoderV2;
|
||||
import {DPPVault} from "./DPPVault.sol";
|
||||
import {SafeMath} from "../../lib/SafeMath.sol";
|
||||
import {DecimalMath} from "../../lib/DecimalMath.sol";
|
||||
import {RState, PMMState, PMMPricing} from "../../lib/PMMPricing.sol";
|
||||
import {PMMPricing} from "../../lib/PMMPricing.sol";
|
||||
import {IDODOCallee} from "../../intf/IDODOCallee.sol";
|
||||
|
||||
contract DPPTrader is DPPVault {
|
||||
@@ -49,7 +49,7 @@ contract DPPTrader is DPPVault {
|
||||
uint256 baseInput = getBaseInput();
|
||||
uint256 mtFee;
|
||||
uint256 newBaseTarget;
|
||||
RState newRState;
|
||||
PMMPricing.RState newRState;
|
||||
(receiveQuoteAmount, mtFee, newRState, newBaseTarget) = querySellBase(tx.origin, baseInput);
|
||||
|
||||
_transferQuoteOut(to, receiveQuoteAmount);
|
||||
@@ -76,7 +76,7 @@ contract DPPTrader is DPPVault {
|
||||
uint256 quoteInput = getQuoteInput();
|
||||
uint256 mtFee;
|
||||
uint256 newQuoteTarget;
|
||||
RState newRState;
|
||||
PMMPricing.RState newRState;
|
||||
(receiveBaseAmount, mtFee, newRState, newQuoteTarget) = querySellQuote(
|
||||
tx.origin,
|
||||
quoteInput
|
||||
@@ -125,7 +125,7 @@ contract DPPTrader is DPPVault {
|
||||
(
|
||||
uint256 receiveBaseAmount,
|
||||
uint256 mtFee,
|
||||
RState newRState,
|
||||
PMMPricing.RState newRState,
|
||||
uint256 newQuoteTarget
|
||||
) = querySellQuote(tx.origin, quoteBalance.sub(_QUOTE_RESERVE_)); // revert if quoteBalance<quoteReserve
|
||||
|
||||
@@ -146,7 +146,7 @@ contract DPPTrader is DPPVault {
|
||||
(
|
||||
uint256 receiveQuoteAmount,
|
||||
uint256 mtFee,
|
||||
RState newRState,
|
||||
PMMPricing.RState newRState,
|
||||
uint256 newBaseTarget
|
||||
) = querySellBase(tx.origin, baseBalance.sub(_BASE_RESERVE_)); // revert if baseBalance<baseReserve
|
||||
|
||||
@@ -170,11 +170,11 @@ contract DPPTrader is DPPVault {
|
||||
returns (
|
||||
uint256 receiveQuoteAmount,
|
||||
uint256 mtFee,
|
||||
RState newRState,
|
||||
PMMPricing.RState newRState,
|
||||
uint256 newBaseTarget
|
||||
)
|
||||
{
|
||||
PMMState memory state = getPMMState();
|
||||
PMMPricing.PMMState memory state = getPMMState();
|
||||
(receiveQuoteAmount, newRState) = PMMPricing.sellBaseToken(state, payBaseAmount);
|
||||
|
||||
uint256 lpFeeRate = _LP_FEE_RATE_MODEL_.getFeeRate(trader);
|
||||
@@ -194,11 +194,11 @@ contract DPPTrader is DPPVault {
|
||||
returns (
|
||||
uint256 receiveBaseAmount,
|
||||
uint256 mtFee,
|
||||
RState newRState,
|
||||
PMMPricing.RState newRState,
|
||||
uint256 newQuoteTarget
|
||||
)
|
||||
{
|
||||
PMMState memory state = getPMMState();
|
||||
PMMPricing.PMMState memory state = getPMMState();
|
||||
(receiveBaseAmount, newRState) = PMMPricing.sellQuoteToken(state, payQuoteAmount);
|
||||
|
||||
uint256 lpFeeRate = _LP_FEE_RATE_MODEL_.getFeeRate(trader);
|
||||
@@ -213,7 +213,7 @@ contract DPPTrader is DPPVault {
|
||||
|
||||
// ============ Helper Functions ============
|
||||
|
||||
function getPMMState() public view returns (PMMState memory state) {
|
||||
function getPMMState() public view returns (PMMPricing.PMMState memory state) {
|
||||
state.i = _I_.get();
|
||||
state.K = _K_.get();
|
||||
state.B = _BASE_RESERVE_;
|
||||
|
||||
@@ -13,7 +13,7 @@ import {SafeMath} from "../../lib/SafeMath.sol";
|
||||
import {DecimalMath} from "../../lib/DecimalMath.sol";
|
||||
import {DODOMath} from "../../lib/DODOMath.sol";
|
||||
import {IDODOCallee} from "../../intf/IDODOCallee.sol";
|
||||
import {RState, PMMState, PMMPricing} from "../../lib/PMMPricing.sol";
|
||||
import {PMMPricing} from "../../lib/PMMPricing.sol";
|
||||
|
||||
contract DVMTrader is DVMVault {
|
||||
using SafeMath for uint256;
|
||||
@@ -210,14 +210,14 @@ contract DVMTrader is DVMVault {
|
||||
|
||||
// ============ Helper Functions ============
|
||||
|
||||
function getPMMState() public view returns (PMMState memory state) {
|
||||
function getPMMState() public view returns (PMMPricing.PMMState memory state) {
|
||||
state.i = _I_;
|
||||
state.K = _K_;
|
||||
state.B = _BASE_RESERVE_;
|
||||
state.Q = _QUOTE_RESERVE_;
|
||||
state.B0 = calculateBase0(state.B, state.Q);
|
||||
state.Q0 = 0;
|
||||
state.R = RState.ABOVE_ONE;
|
||||
state.R = PMMPricing.RState.ABOVE_ONE;
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -21,4 +21,16 @@ interface IDVM {
|
||||
uint256 i,
|
||||
uint256 k
|
||||
) external;
|
||||
|
||||
function _BASE_TOKEN_() external returns (address);
|
||||
|
||||
function _QUOTE_TOKEN_() external returns (address);
|
||||
|
||||
function getVaultReserve() external returns (uint256 baseReserve, uint256 quoteReserve);
|
||||
|
||||
function sellBase(address to) external returns (uint256);
|
||||
|
||||
function sellQuote(address to) external returns (uint256);
|
||||
|
||||
function buyShares(address to) external returns (uint256);
|
||||
}
|
||||
@@ -11,7 +11,7 @@ pragma experimental ABIEncoderV2;
|
||||
import {Ownable} from "../lib/Ownable.sol";
|
||||
import {ICloneFactory} from "../lib/CloneFactory.sol";
|
||||
import {IConstFeeRateModel} from "../lib/ConstFeeRateModel.sol";
|
||||
import {IDVM} from "../DODOVendorMachine/intf/IDVM.sol";
|
||||
import {IDVM} from "../DODOVendingMachine/intf/IDVM.sol";
|
||||
import {IPermissionManager} from "../lib/PermissionManager.sol";
|
||||
|
||||
contract DVMFactory is Ownable {
|
||||
@@ -39,7 +39,7 @@ contract DVMFactory is Ownable {
|
||||
_DEFAULT_GAS_PRICE_SOURCE_ = defaultGasPriceSource;
|
||||
}
|
||||
|
||||
function createStandardDODOVendorMachine(
|
||||
function createStandardDODOVendingMachine(
|
||||
address baseToken,
|
||||
address quoteToken,
|
||||
uint256 lpFeeRate,
|
||||
|
||||
@@ -7,15 +7,13 @@
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
|
||||
import {Ownable} from "../lib/Ownable.sol";
|
||||
import {DVM} from "../DODOVendorMachine/impl/DVM.sol";
|
||||
import {DVMVault} from "../DODOVendorMachine/impl/DVMVault.sol";
|
||||
import {IDVM} from "../DODOVendingMachine/intf/IDVM.sol";
|
||||
import {IERC20} from "../intf/IERC20.sol";
|
||||
import {SafeERC20} from "../lib/SafeERC20.sol";
|
||||
import {SafeMath} from "../lib/SafeMath.sol";
|
||||
import {DecimalMath} from "../lib/DecimalMath.sol";
|
||||
|
||||
contract SmartRoute is Ownable {
|
||||
contract DVMProxy {
|
||||
using SafeMath for uint256;
|
||||
using SafeERC20 for IERC20;
|
||||
|
||||
@@ -25,8 +23,12 @@ contract SmartRoute is Ownable {
|
||||
uint256 baseAmount,
|
||||
uint256 minReceive
|
||||
) public returns (uint256 receiveAmount) {
|
||||
IERC20(DVM(DVMAddress)._BASE_TOKEN_()).safeTransferFrom(msg.sender, DVMAddress, baseAmount);
|
||||
receiveAmount = DVM(DVMAddress).sellBase(to);
|
||||
IERC20(IDVM(DVMAddress)._BASE_TOKEN_()).safeTransferFrom(
|
||||
msg.sender,
|
||||
DVMAddress,
|
||||
baseAmount
|
||||
);
|
||||
receiveAmount = IDVM(DVMAddress).sellBase(to);
|
||||
require(receiveAmount >= minReceive, "RECEIVE_NOT_ENOUGH");
|
||||
return receiveAmount;
|
||||
}
|
||||
@@ -37,12 +39,12 @@ contract SmartRoute is Ownable {
|
||||
uint256 quoteAmount,
|
||||
uint256 minReceive
|
||||
) public returns (uint256 receiveAmount) {
|
||||
IERC20(DVM(DVMAddress)._QUOTE_TOKEN_()).safeTransferFrom(
|
||||
IERC20(IDVM(DVMAddress)._QUOTE_TOKEN_()).safeTransferFrom(
|
||||
msg.sender,
|
||||
DVMAddress,
|
||||
quoteAmount
|
||||
);
|
||||
receiveAmount = DVM(DVMAddress).sellQuote(to);
|
||||
receiveAmount = IDVM(DVMAddress).sellQuote(to);
|
||||
require(receiveAmount >= minReceive, "RECEIVE_NOT_ENOUGU");
|
||||
return receiveAmount;
|
||||
}
|
||||
@@ -53,10 +55,9 @@ contract SmartRoute is Ownable {
|
||||
uint256 baseAmount,
|
||||
uint256 quoteAmount
|
||||
) public returns (uint256 shares) {
|
||||
address vault = DVMAddress;
|
||||
uint256 adjustedBaseAmount;
|
||||
uint256 adjustedQuoteAmount;
|
||||
(uint256 baseReserve, uint256 quoteReserve) = DVM(DVMAddress).getVaultReserve();
|
||||
(uint256 baseReserve, uint256 quoteReserve) = IDVM(DVMAddress).getVaultReserve();
|
||||
|
||||
if (quoteReserve == 0 && baseReserve == 0) {
|
||||
adjustedBaseAmount = baseAmount;
|
||||
@@ -80,18 +81,18 @@ contract SmartRoute is Ownable {
|
||||
}
|
||||
}
|
||||
|
||||
IERC20(DVM(DVMAddress)._BASE_TOKEN_()).safeTransferFrom(
|
||||
IERC20(IDVM(DVMAddress)._BASE_TOKEN_()).safeTransferFrom(
|
||||
msg.sender,
|
||||
vault,
|
||||
DVMAddress,
|
||||
adjustedBaseAmount
|
||||
);
|
||||
IERC20(DVM(DVMAddress)._QUOTE_TOKEN_()).safeTransferFrom(
|
||||
IERC20(IDVM(DVMAddress)._QUOTE_TOKEN_()).safeTransferFrom(
|
||||
msg.sender,
|
||||
vault,
|
||||
DVMAddress,
|
||||
adjustedQuoteAmount
|
||||
);
|
||||
|
||||
shares = DVM(DVMAddress).buyShares(to);
|
||||
shares = IDVM(DVMAddress).buyShares(to);
|
||||
|
||||
return shares;
|
||||
}
|
||||
@@ -9,6 +9,7 @@ pragma solidity 0.6.9;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
import {Ownable} from "./Ownable.sol";
|
||||
import {InitializableOwnable} from "./InitializableOwnable.sol";
|
||||
|
||||
interface IExternalValue {
|
||||
function set(uint256) external;
|
||||
@@ -16,10 +17,10 @@ interface IExternalValue {
|
||||
function get() external view returns (uint256);
|
||||
}
|
||||
|
||||
contract ExternalValue is IExternalValue, Ownable {
|
||||
contract ExternalValue is IExternalValue, InitializableOwnable {
|
||||
uint256 public _VALUE_;
|
||||
|
||||
function set(uint256 value) external override {
|
||||
function set(uint256 value) external override onlyOwner {
|
||||
_VALUE_ = value;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
import {Ownable} from "./Ownable.sol";
|
||||
|
||||
interface IGasPriceSource {
|
||||
function setGasPrice(uint256) external;
|
||||
|
||||
function getGasPrice() external view returns (uint256);
|
||||
}
|
||||
|
||||
contract GasPriceSource is IGasPriceSource, Ownable {
|
||||
uint256 public _GAS_PRICE_;
|
||||
|
||||
function setGasPrice(uint256 gasPrice) external override {
|
||||
_GAS_PRICE_ = gasPrice;
|
||||
}
|
||||
|
||||
function getGasPrice() external override view returns (uint256) {
|
||||
return _GAS_PRICE_;
|
||||
}
|
||||
}
|
||||
@@ -19,23 +19,23 @@ import {DODOMath} from "../lib/DODOMath.sol";
|
||||
* @notice DODO Pricing model
|
||||
*/
|
||||
|
||||
enum RState {ONE, ABOVE_ONE, BELOW_ONE}
|
||||
|
||||
struct PMMState {
|
||||
uint256 i;
|
||||
uint256 K;
|
||||
uint256 B;
|
||||
uint256 Q;
|
||||
uint256 B0;
|
||||
uint256 Q0;
|
||||
RState R;
|
||||
}
|
||||
|
||||
library PMMPricing {
|
||||
using SafeMath for uint256;
|
||||
|
||||
enum RState {ONE, ABOVE_ONE, BELOW_ONE}
|
||||
|
||||
struct PMMState {
|
||||
uint256 i;
|
||||
uint256 K;
|
||||
uint256 B;
|
||||
uint256 Q;
|
||||
uint256 B0;
|
||||
uint256 Q0;
|
||||
RState R;
|
||||
}
|
||||
|
||||
function sellBaseToken(PMMState memory state, uint256 payBaseAmount)
|
||||
public
|
||||
internal
|
||||
pure
|
||||
returns (uint256 receiveQuoteAmount, RState newR)
|
||||
{
|
||||
@@ -80,7 +80,7 @@ library PMMPricing {
|
||||
}
|
||||
|
||||
function sellQuoteToken(PMMState memory state, uint256 payQuoteAmount)
|
||||
public
|
||||
internal
|
||||
pure
|
||||
returns (uint256 receiveBaseAmount, RState newR)
|
||||
{
|
||||
@@ -214,7 +214,7 @@ library PMMPricing {
|
||||
// ============ Helper functions ============
|
||||
|
||||
// todo 我不确定这个函数是不是能改state的状态
|
||||
function adjustedTarget(PMMState memory state) public pure {
|
||||
function adjustedTarget(PMMState memory state) internal pure {
|
||||
if (state.R == RState.BELOW_ONE) {
|
||||
uint256 fairAmount = DecimalMath.mulFloor(state.B.sub(state.B0), state.i);
|
||||
state.Q0 = DODOMath._SolveQuadraticFunctionForTarget(state.B, state.K, fairAmount);
|
||||
@@ -224,7 +224,7 @@ library PMMPricing {
|
||||
}
|
||||
}
|
||||
|
||||
function getMidPrice(PMMState memory state) public pure returns (uint256 midPrice) {
|
||||
function getMidPrice(PMMState memory state) internal pure returns (uint256 midPrice) {
|
||||
if (state.R == RState.BELOW_ONE) {
|
||||
uint256 R = DecimalMath.divFloor(state.Q0.mul(state.Q0).div(state.Q), state.Q);
|
||||
R = DecimalMath.ONE.sub(state.K).add(DecimalMath.mul(state.K, R));
|
||||
|
||||
Reference in New Issue
Block a user