set gas price to external contract
This commit is contained in:
@@ -10,6 +10,7 @@ pragma experimental ABIEncoderV2;
|
|||||||
|
|
||||||
import {IFeeRateModel} from "../../intf/IFeeRateModel.sol";
|
import {IFeeRateModel} from "../../intf/IFeeRateModel.sol";
|
||||||
import {IPermissionManager} from "../../lib/PermissionManager.sol";
|
import {IPermissionManager} from "../../lib/PermissionManager.sol";
|
||||||
|
import {IGasPriceSource} from "../../lib/GasPriceSource.sol";
|
||||||
import {DVMTrader} from "./DVMTrader.sol";
|
import {DVMTrader} from "./DVMTrader.sol";
|
||||||
import {DVMFunding} from "./DVMFunding.sol";
|
import {DVMFunding} from "./DVMFunding.sol";
|
||||||
import {DVMVault} from "./DVMVault.sol";
|
import {DVMVault} from "./DVMVault.sol";
|
||||||
@@ -22,6 +23,7 @@ contract DVM is DVMTrader, DVMFunding {
|
|||||||
address lpFeeRateModel,
|
address lpFeeRateModel,
|
||||||
address mtFeeRateModel,
|
address mtFeeRateModel,
|
||||||
address tradePermissionManager,
|
address tradePermissionManager,
|
||||||
|
address gasPriceSource,
|
||||||
uint256 i,
|
uint256 i,
|
||||||
uint256 k
|
uint256 k
|
||||||
) external {
|
) external {
|
||||||
@@ -32,10 +34,10 @@ contract DVM is DVMTrader, DVMFunding {
|
|||||||
_LP_FEE_RATE_MODEL_ = IFeeRateModel(lpFeeRateModel);
|
_LP_FEE_RATE_MODEL_ = IFeeRateModel(lpFeeRateModel);
|
||||||
_MT_FEE_RATE_MODEL_ = IFeeRateModel(mtFeeRateModel);
|
_MT_FEE_RATE_MODEL_ = IFeeRateModel(mtFeeRateModel);
|
||||||
_TRADE_PERMISSION_ = IPermissionManager(tradePermissionManager);
|
_TRADE_PERMISSION_ = IPermissionManager(tradePermissionManager);
|
||||||
|
_GAS_PRICE_LIMIT_ = IGasPriceSource(gasPriceSource);
|
||||||
_MAINTAINER_ = maintainer;
|
_MAINTAINER_ = maintainer;
|
||||||
_I_ = i;
|
_I_ = i;
|
||||||
_K_ = k;
|
_K_ = k;
|
||||||
_GAS_PRICE_LIMIT_ = uint256(-1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============ Version Control ============
|
// ============ Version Control ============
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import {SafeMath} from "../../lib/SafeMath.sol";
|
|||||||
import {DODOMath} from "../../lib/DODOMath.sol";
|
import {DODOMath} from "../../lib/DODOMath.sol";
|
||||||
import {DecimalMath} from "../../lib/DecimalMath.sol";
|
import {DecimalMath} from "../../lib/DecimalMath.sol";
|
||||||
import {IPermissionManager} from "../../lib/PermissionManager.sol";
|
import {IPermissionManager} from "../../lib/PermissionManager.sol";
|
||||||
|
import {IGasPriceSource} from "../../lib/GasPriceSource.sol";
|
||||||
import {IFeeRateModel} from "../../intf/IFeeRateModel.sol";
|
import {IFeeRateModel} from "../../intf/IFeeRateModel.sol";
|
||||||
import {DVMVault} from "./DVMVault.sol";
|
import {DVMVault} from "./DVMVault.sol";
|
||||||
|
|
||||||
@@ -22,7 +23,7 @@ contract DVMStorage is InitializableOwnable, ReentrancyGuard {
|
|||||||
|
|
||||||
// ============ Variables for Control ============
|
// ============ Variables for Control ============
|
||||||
|
|
||||||
uint256 public _GAS_PRICE_LIMIT_;
|
IGasPriceSource public _GAS_PRICE_LIMIT_;
|
||||||
|
|
||||||
// ============ Advanced Controls ============
|
// ============ 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 ============
|
// ============ Helper Functions ============
|
||||||
|
|
||||||
function calculateBase0(uint256 baseAmount, uint256 quoteAmount) public view returns (uint256) {
|
function calculateBase0(uint256 baseAmount, uint256 quoteAmount) public view returns (uint256) {
|
||||||
@@ -93,8 +99,8 @@ contract DVMStorage is InitializableOwnable, ReentrancyGuard {
|
|||||||
_MAINTAINER_ = newMaintainer;
|
_MAINTAINER_ = newMaintainer;
|
||||||
}
|
}
|
||||||
|
|
||||||
function setGasPriceLimit(uint256 newGasPriceLimit) external onlyOwner {
|
function setGasPriceSource(address newGasPriceLimitSource) external onlyOwner {
|
||||||
_GAS_PRICE_LIMIT_ = newGasPriceLimit;
|
_GAS_PRICE_LIMIT_ = IGasPriceSource(newGasPriceLimitSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setBuy(bool open) external onlyOwner {
|
function setBuy(bool open) external onlyOwner {
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ contract DVMTrader is DVMStorage {
|
|||||||
function sellBase(address to)
|
function sellBase(address to)
|
||||||
external
|
external
|
||||||
preventReentrant
|
preventReentrant
|
||||||
|
limitGasPrice
|
||||||
isSellAllow(to)
|
isSellAllow(to)
|
||||||
returns (uint256 receiveQuoteAmount)
|
returns (uint256 receiveQuoteAmount)
|
||||||
{
|
{
|
||||||
@@ -37,6 +38,7 @@ contract DVMTrader is DVMStorage {
|
|||||||
function sellQuote(address to)
|
function sellQuote(address to)
|
||||||
external
|
external
|
||||||
preventReentrant
|
preventReentrant
|
||||||
|
limitGasPrice
|
||||||
isBuyAllow(to)
|
isBuyAllow(to)
|
||||||
returns (uint256 receiveBaseAmount)
|
returns (uint256 receiveBaseAmount)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ interface IDVM {
|
|||||||
address lpFeeRateModel,
|
address lpFeeRateModel,
|
||||||
address mtFeeRateModel,
|
address mtFeeRateModel,
|
||||||
address tradePermissionManager,
|
address tradePermissionManager,
|
||||||
|
address gasPriceSource,
|
||||||
uint256 i,
|
uint256 i,
|
||||||
uint256 k
|
uint256 k
|
||||||
) external;
|
) external;
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ contract DVMFactory is Ownable {
|
|||||||
address public _FEE_RATE_MODEL_TEMPLATE_;
|
address public _FEE_RATE_MODEL_TEMPLATE_;
|
||||||
address public _PERMISSION_MANAGER_TEMPLATE_;
|
address public _PERMISSION_MANAGER_TEMPLATE_;
|
||||||
|
|
||||||
|
address public _DEFAULT_GAS_PRICE_SOURCE_;
|
||||||
|
|
||||||
// base -> quote -> DVM address list
|
// base -> quote -> DVM address list
|
||||||
mapping(address => mapping(address => address[])) _REGISTRY_;
|
mapping(address => mapping(address => address[])) _REGISTRY_;
|
||||||
|
|
||||||
@@ -30,13 +32,15 @@ contract DVMFactory is Ownable {
|
|||||||
address vaultTemplate,
|
address vaultTemplate,
|
||||||
address dvmTemplate,
|
address dvmTemplate,
|
||||||
address feeRateModelTemplate,
|
address feeRateModelTemplate,
|
||||||
address permissionManagerTemplate
|
address permissionManagerTemplate,
|
||||||
|
address defaultGasPriceSource
|
||||||
) public {
|
) public {
|
||||||
_CLONE_FACTORY_ = cloneFactory;
|
_CLONE_FACTORY_ = cloneFactory;
|
||||||
_VAULT_TEMPLATE_ = vaultTemplate;
|
_VAULT_TEMPLATE_ = vaultTemplate;
|
||||||
_DVM_TEMPLATE_ = dvmTemplate;
|
_DVM_TEMPLATE_ = dvmTemplate;
|
||||||
_FEE_RATE_MODEL_TEMPLATE_ = feeRateModelTemplate;
|
_FEE_RATE_MODEL_TEMPLATE_ = feeRateModelTemplate;
|
||||||
_PERMISSION_MANAGER_TEMPLATE_ = permissionManagerTemplate;
|
_PERMISSION_MANAGER_TEMPLATE_ = permissionManagerTemplate;
|
||||||
|
_DEFAULT_GAS_PRICE_SOURCE_ = defaultGasPriceSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
function createStandardDODOVendorMachine(
|
function createStandardDODOVendorMachine(
|
||||||
@@ -59,6 +63,7 @@ contract DVMFactory is Ownable {
|
|||||||
createConstFeeRateModel(msg.sender, lpFeeRate),
|
createConstFeeRateModel(msg.sender, lpFeeRate),
|
||||||
createConstFeeRateModel(msg.sender, mtFeeRate),
|
createConstFeeRateModel(msg.sender, mtFeeRate),
|
||||||
createPermissionManager(msg.sender),
|
createPermissionManager(msg.sender),
|
||||||
|
_DEFAULT_GAS_PRICE_SOURCE_,
|
||||||
i,
|
i,
|
||||||
k
|
k
|
||||||
);
|
);
|
||||||
|
|||||||
29
contracts/lib/GasPriceSource.sol
Normal file
29
contracts/lib/GasPriceSource.sol
Normal 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_;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -143,13 +143,13 @@ describe("Funding", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("sell shares", () => {
|
describe("sell shares", () => {
|
||||||
it("sell shares", async () => {
|
it.only("sell shares", async () => {
|
||||||
await ctx.Route.methods
|
await ctx.Route.methods
|
||||||
.depositToDVM(ctx.DVM.options.address, lp, decimalStr("10"), decimalStr("100"))
|
.depositToDVM(ctx.DVM.options.address, lp, decimalStr("10"), decimalStr("100"))
|
||||||
.send(ctx.sendParam(lp));
|
.send(ctx.sendParam(lp));
|
||||||
var vaultShares = await ctx.Vault.methods.balanceOf(lp).call()
|
var vaultShares = await ctx.Vault.methods.balanceOf(lp).call()
|
||||||
var bob = ctx.SpareAccounts[0]
|
var bob = ctx.SpareAccounts[5]
|
||||||
await ctx.DVM.methods.sellShares(bob, vaultShares).send(ctx.sendParam(lp))
|
await ctx.DVM.methods.sellShares(bob, vaultShares, "0x").send(ctx.sendParam(lp))
|
||||||
assert.equal(await ctx.BASE.methods.balanceOf(bob).call(), decimalStr("10"))
|
assert.equal(await ctx.BASE.methods.balanceOf(bob).call(), decimalStr("10"))
|
||||||
assert.equal(await ctx.QUOTE.methods.balanceOf(bob).call(), decimalStr("100"))
|
assert.equal(await ctx.QUOTE.methods.balanceOf(bob).call(), decimalStr("100"))
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ export const DVM_FACTORY_NAME = "DVMFactory"
|
|||||||
export const SMART_ROUTE_NAME = "SmartRoute"
|
export const SMART_ROUTE_NAME = "SmartRoute"
|
||||||
export const CONST_FEE_RATE_MODEL_NAME = "ConstFeeRateModel"
|
export const CONST_FEE_RATE_MODEL_NAME = "ConstFeeRateModel"
|
||||||
export const PERMISSION_MANAGER_NAME = "PermissionManager"
|
export const PERMISSION_MANAGER_NAME = "PermissionManager"
|
||||||
|
export const GAS_PRICE_SOURCE_NAME = "GasPriceSource"
|
||||||
|
|
||||||
interface ContractJson {
|
interface ContractJson {
|
||||||
abi: any;
|
abi: any;
|
||||||
|
|||||||
@@ -73,13 +73,16 @@ export class DVMContext {
|
|||||||
var dvmTemplate = await contracts.newContract(contracts.DVM_NAME)
|
var dvmTemplate = await contracts.newContract(contracts.DVM_NAME)
|
||||||
var feeRateModelTemplate = await contracts.newContract(contracts.CONST_FEE_RATE_MODEL_NAME)
|
var feeRateModelTemplate = await contracts.newContract(contracts.CONST_FEE_RATE_MODEL_NAME)
|
||||||
var permissionManagerTemplate = await contracts.newContract(contracts.PERMISSION_MANAGER_NAME)
|
var permissionManagerTemplate = await contracts.newContract(contracts.PERMISSION_MANAGER_NAME)
|
||||||
|
var gasPriceSource = await contracts.newContract(contracts.GAS_PRICE_SOURCE_NAME)
|
||||||
|
|
||||||
this.DVMFactory = await contracts.newContract(contracts.DVM_FACTORY_NAME,
|
this.DVMFactory = await contracts.newContract(contracts.DVM_FACTORY_NAME,
|
||||||
[cloneFactory.options.address,
|
[cloneFactory.options.address,
|
||||||
vaultTemplate.options.address,
|
vaultTemplate.options.address,
|
||||||
dvmTemplate.options.address,
|
dvmTemplate.options.address,
|
||||||
feeRateModelTemplate.options.address,
|
feeRateModelTemplate.options.address,
|
||||||
permissionManagerTemplate.options.address])
|
permissionManagerTemplate.options.address,
|
||||||
|
gasPriceSource.options.address,
|
||||||
|
])
|
||||||
|
|
||||||
this.BASE = await contracts.newContract(
|
this.BASE = await contracts.newContract(
|
||||||
contracts.MINTABLE_ERC20_CONTRACT_NAME,
|
contracts.MINTABLE_ERC20_CONTRACT_NAME,
|
||||||
@@ -109,6 +112,7 @@ export class DVMContext {
|
|||||||
this.Vault = contracts.getContractWithAddress(contracts.DVM_VAULT_NAME, await this.DVM.methods._VAULT_().call())
|
this.Vault = contracts.getContractWithAddress(contracts.DVM_VAULT_NAME, await this.DVM.methods._VAULT_().call())
|
||||||
|
|
||||||
await this.DVM.methods.setMaintainer(this.Maintainer).send(this.sendParam(this.Deployer))
|
await this.DVM.methods.setMaintainer(this.Maintainer).send(this.sendParam(this.Deployer))
|
||||||
|
await gasPriceSource.methods.setGasPrice(MAX_UINT256).send(this.sendParam(this.Deployer))
|
||||||
|
|
||||||
console.log(log.blueText("[Init DVM context]"));
|
console.log(log.blueText("[Init DVM context]"));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user