From 39e21342a6008afe8739bba049d773e09124d9e9 Mon Sep 17 00:00:00 2001 From: mingda Date: Wed, 11 Nov 2020 16:42:00 +0800 Subject: [PATCH] set gas price to external contract --- contracts/DODOVendorMachine/impl/DVM.sol | 4 ++- .../DODOVendorMachine/impl/DVMStorage.sol | 12 ++++++-- .../DODOVendorMachine/impl/DVMTrader.sol | 2 ++ contracts/DODOVendorMachine/intf/IDVM.sol | 1 + contracts/Factory/DVMFactory.sol | 7 ++++- contracts/lib/GasPriceSource.sol | 29 +++++++++++++++++++ test/DVM/funding.test.ts | 6 ++-- test/utils/Contracts.ts | 1 + test/utils/DVMContext.ts | 6 +++- 9 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 contracts/lib/GasPriceSource.sol diff --git a/contracts/DODOVendorMachine/impl/DVM.sol b/contracts/DODOVendorMachine/impl/DVM.sol index 70ea6d2..4b437ad 100644 --- a/contracts/DODOVendorMachine/impl/DVM.sol +++ b/contracts/DODOVendorMachine/impl/DVM.sol @@ -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 ============ diff --git a/contracts/DODOVendorMachine/impl/DVMStorage.sol b/contracts/DODOVendorMachine/impl/DVMStorage.sol index e2712f3..dd5e988 100644 --- a/contracts/DODOVendorMachine/impl/DVMStorage.sol +++ b/contracts/DODOVendorMachine/impl/DVMStorage.sol @@ -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 { diff --git a/contracts/DODOVendorMachine/impl/DVMTrader.sol b/contracts/DODOVendorMachine/impl/DVMTrader.sol index 6981e1e..cd79126 100644 --- a/contracts/DODOVendorMachine/impl/DVMTrader.sol +++ b/contracts/DODOVendorMachine/impl/DVMTrader.sol @@ -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) { diff --git a/contracts/DODOVendorMachine/intf/IDVM.sol b/contracts/DODOVendorMachine/intf/IDVM.sol index 854a6dd..cfca4e0 100644 --- a/contracts/DODOVendorMachine/intf/IDVM.sol +++ b/contracts/DODOVendorMachine/intf/IDVM.sol @@ -16,6 +16,7 @@ interface IDVM { address lpFeeRateModel, address mtFeeRateModel, address tradePermissionManager, + address gasPriceSource, uint256 i, uint256 k ) external; diff --git a/contracts/Factory/DVMFactory.sol b/contracts/Factory/DVMFactory.sol index 1dd6414..a8d4312 100644 --- a/contracts/Factory/DVMFactory.sol +++ b/contracts/Factory/DVMFactory.sol @@ -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 ); diff --git a/contracts/lib/GasPriceSource.sol b/contracts/lib/GasPriceSource.sol new file mode 100644 index 0000000..d1a71a5 --- /dev/null +++ b/contracts/lib/GasPriceSource.sol @@ -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_; + } +} diff --git a/test/DVM/funding.test.ts b/test/DVM/funding.test.ts index 93b7625..3fa01fc 100644 --- a/test/DVM/funding.test.ts +++ b/test/DVM/funding.test.ts @@ -143,13 +143,13 @@ describe("Funding", () => { }); describe("sell shares", () => { - it("sell shares", async () => { + it.only("sell shares", async () => { await ctx.Route.methods .depositToDVM(ctx.DVM.options.address, lp, decimalStr("10"), decimalStr("100")) .send(ctx.sendParam(lp)); var vaultShares = await ctx.Vault.methods.balanceOf(lp).call() - var bob = ctx.SpareAccounts[0] - await ctx.DVM.methods.sellShares(bob, vaultShares).send(ctx.sendParam(lp)) + var bob = ctx.SpareAccounts[5] + 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.QUOTE.methods.balanceOf(bob).call(), decimalStr("100")) }) diff --git a/test/utils/Contracts.ts b/test/utils/Contracts.ts index f781f46..7964523 100644 --- a/test/utils/Contracts.ts +++ b/test/utils/Contracts.ts @@ -34,6 +34,7 @@ export const DVM_FACTORY_NAME = "DVMFactory" export const SMART_ROUTE_NAME = "SmartRoute" export const CONST_FEE_RATE_MODEL_NAME = "ConstFeeRateModel" export const PERMISSION_MANAGER_NAME = "PermissionManager" +export const GAS_PRICE_SOURCE_NAME = "GasPriceSource" interface ContractJson { abi: any; diff --git a/test/utils/DVMContext.ts b/test/utils/DVMContext.ts index 2f35a1e..02d721b 100644 --- a/test/utils/DVMContext.ts +++ b/test/utils/DVMContext.ts @@ -73,13 +73,16 @@ export class DVMContext { var dvmTemplate = await contracts.newContract(contracts.DVM_NAME) var feeRateModelTemplate = await contracts.newContract(contracts.CONST_FEE_RATE_MODEL_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, [cloneFactory.options.address, vaultTemplate.options.address, dvmTemplate.options.address, feeRateModelTemplate.options.address, - permissionManagerTemplate.options.address]) + permissionManagerTemplate.options.address, + gasPriceSource.options.address, + ]) this.BASE = await contracts.newContract( 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()) 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]")); }