set gas price to external contract

This commit is contained in:
mingda
2020-11-11 16:42:00 +08:00
parent 4d9804b373
commit 39e21342a6
9 changed files with 59 additions and 9 deletions

View File

@@ -10,6 +10,7 @@ pragma experimental ABIEncoderV2;
import {IFeeRateModel} from "../../intf/IFeeRateModel.sol";
import {IPermissionManager} from "../../lib/PermissionManager.sol";
import {IGasPriceSource} from "../../lib/GasPriceSource.sol";
import {DVMTrader} from "./DVMTrader.sol";
import {DVMFunding} from "./DVMFunding.sol";
import {DVMVault} from "./DVMVault.sol";
@@ -22,6 +23,7 @@ contract DVM is DVMTrader, DVMFunding {
address lpFeeRateModel,
address mtFeeRateModel,
address tradePermissionManager,
address gasPriceSource,
uint256 i,
uint256 k
) external {
@@ -32,10 +34,10 @@ contract DVM is DVMTrader, DVMFunding {
_LP_FEE_RATE_MODEL_ = IFeeRateModel(lpFeeRateModel);
_MT_FEE_RATE_MODEL_ = IFeeRateModel(mtFeeRateModel);
_TRADE_PERMISSION_ = IPermissionManager(tradePermissionManager);
_GAS_PRICE_LIMIT_ = IGasPriceSource(gasPriceSource);
_MAINTAINER_ = maintainer;
_I_ = i;
_K_ = k;
_GAS_PRICE_LIMIT_ = uint256(-1);
}
// ============ Version Control ============

View File

@@ -14,6 +14,7 @@ import {SafeMath} from "../../lib/SafeMath.sol";
import {DODOMath} from "../../lib/DODOMath.sol";
import {DecimalMath} from "../../lib/DecimalMath.sol";
import {IPermissionManager} from "../../lib/PermissionManager.sol";
import {IGasPriceSource} from "../../lib/GasPriceSource.sol";
import {IFeeRateModel} from "../../intf/IFeeRateModel.sol";
import {DVMVault} from "./DVMVault.sol";
@@ -22,7 +23,7 @@ contract DVMStorage is InitializableOwnable, ReentrancyGuard {
// ============ Variables for Control ============
uint256 public _GAS_PRICE_LIMIT_;
IGasPriceSource public _GAS_PRICE_LIMIT_;
// ============ Advanced Controls ============
@@ -62,6 +63,11 @@ contract DVMStorage is InitializableOwnable, ReentrancyGuard {
_;
}
modifier limitGasPrice() {
require(tx.gasprice <= _GAS_PRICE_LIMIT_.getGasPrice(), "GAS_PRICE_EXCEED");
_;
}
// ============ Helper Functions ============
function calculateBase0(uint256 baseAmount, uint256 quoteAmount) public view returns (uint256) {
@@ -93,8 +99,8 @@ contract DVMStorage is InitializableOwnable, ReentrancyGuard {
_MAINTAINER_ = newMaintainer;
}
function setGasPriceLimit(uint256 newGasPriceLimit) external onlyOwner {
_GAS_PRICE_LIMIT_ = newGasPriceLimit;
function setGasPriceSource(address newGasPriceLimitSource) external onlyOwner {
_GAS_PRICE_LIMIT_ = IGasPriceSource(newGasPriceLimitSource);
}
function setBuy(bool open) external onlyOwner {

View File

@@ -20,6 +20,7 @@ contract DVMTrader is DVMStorage {
function sellBase(address to)
external
preventReentrant
limitGasPrice
isSellAllow(to)
returns (uint256 receiveQuoteAmount)
{
@@ -37,6 +38,7 @@ contract DVMTrader is DVMStorage {
function sellQuote(address to)
external
preventReentrant
limitGasPrice
isBuyAllow(to)
returns (uint256 receiveBaseAmount)
{

View File

@@ -16,6 +16,7 @@ interface IDVM {
address lpFeeRateModel,
address mtFeeRateModel,
address tradePermissionManager,
address gasPriceSource,
uint256 i,
uint256 k
) external;

View File

@@ -22,6 +22,8 @@ contract DVMFactory is Ownable {
address public _FEE_RATE_MODEL_TEMPLATE_;
address public _PERMISSION_MANAGER_TEMPLATE_;
address public _DEFAULT_GAS_PRICE_SOURCE_;
// base -> quote -> DVM address list
mapping(address => mapping(address => address[])) _REGISTRY_;
@@ -30,13 +32,15 @@ contract DVMFactory is Ownable {
address vaultTemplate,
address dvmTemplate,
address feeRateModelTemplate,
address permissionManagerTemplate
address permissionManagerTemplate,
address defaultGasPriceSource
) public {
_CLONE_FACTORY_ = cloneFactory;
_VAULT_TEMPLATE_ = vaultTemplate;
_DVM_TEMPLATE_ = dvmTemplate;
_FEE_RATE_MODEL_TEMPLATE_ = feeRateModelTemplate;
_PERMISSION_MANAGER_TEMPLATE_ = permissionManagerTemplate;
_DEFAULT_GAS_PRICE_SOURCE_ = defaultGasPriceSource;
}
function createStandardDODOVendorMachine(
@@ -59,6 +63,7 @@ contract DVMFactory is Ownable {
createConstFeeRateModel(msg.sender, lpFeeRate),
createConstFeeRateModel(msg.sender, mtFeeRate),
createPermissionManager(msg.sender),
_DEFAULT_GAS_PRICE_SOURCE_,
i,
k
);

View File

@@ -0,0 +1,29 @@
/*
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_;
}
}