From da543d711a7ae5d64ab406191f9d6b2880da4062 Mon Sep 17 00:00:00 2001 From: mingda Date: Wed, 25 Nov 2020 22:22:07 +0800 Subject: [PATCH] init DPP test --- contracts/DODOPrivatePool/impl/DPPVault.sol | 15 ++- test/utils/DPPContext.ts | 137 ++++++++++++++++++++ 2 files changed, 147 insertions(+), 5 deletions(-) create mode 100644 test/utils/DPPContext.ts diff --git a/contracts/DODOPrivatePool/impl/DPPVault.sol b/contracts/DODOPrivatePool/impl/DPPVault.sol index 252d837..83e859b 100644 --- a/contracts/DODOPrivatePool/impl/DPPVault.sol +++ b/contracts/DODOPrivatePool/impl/DPPVault.sol @@ -15,7 +15,7 @@ import {SafeMath} from "../../lib/SafeMath.sol"; import {DecimalMath} from "../../lib/DecimalMath.sol"; import {SafeERC20} from "../../lib/SafeERC20.sol"; import {Ownable} from "../../lib/Ownable.sol"; -import {ISmartApprove} from '../../intf/ISmartApprove.sol'; +import {ISmartApprove} from "../../intf/ISmartApprove.sol"; contract DPPVault is DPPStorage { using SafeMath for uint256; @@ -50,7 +50,7 @@ contract DPPVault is DPPStorage { _QUOTE_TARGET_ = quoteTarget; _checkStatus(); } - + function _syncReserve() internal { _BASE_RESERVE_ = _BASE_TOKEN_.balanceOf(address(this)); _QUOTE_RESERVE_ = _QUOTE_TOKEN_.balanceOf(address(this)); @@ -72,10 +72,15 @@ contract DPPVault is DPPStorage { uint256 baseOutAmount, uint256 quoteOutAmount ) public { - require(msg.sender == ISmartApprove(_DODO_SMART_APPROVE_).getSmartSwap() && from == _OPERATOR_, "RESET FORBIDDEN!"); + require( + msg.sender == _OWNER_ || + (msg.sender == ISmartApprove(_DODO_SMART_APPROVE_).getSmartSwap() && + from == _OPERATOR_), + "RESET FORBIDDEN!" + ); require(newK > 0 && newK <= 10**18, "K OUT OF RANGE!"); - if(baseOutAmount > 0) _transferBaseOut(from, baseOutAmount); - if(quoteOutAmount > 0) _transferQuoteOut(from, quoteOutAmount); + if (baseOutAmount > 0) _transferBaseOut(from, baseOutAmount); + if (quoteOutAmount > 0) _transferQuoteOut(from, quoteOutAmount); _resetTargetAndReserve(); _LP_FEE_RATE_MODEL_.setFeeRate(newLpFeeRate); _MT_FEE_RATE_MODEL_.setFeeRate(newMtFeeRate); diff --git a/test/utils/DPPContext.ts b/test/utils/DPPContext.ts new file mode 100644 index 0000000..b5f961b --- /dev/null +++ b/test/utils/DPPContext.ts @@ -0,0 +1,137 @@ +/* + + Copyright 2020 DODO ZOO. + SPDX-License-Identifier: Apache-2.0 + +*/ + +import BigNumber from 'bignumber.js'; +import Web3 from 'web3'; +import { Contract } from 'web3-eth-contract'; + +import * as contracts from './Contracts'; +import { decimalStr, MAX_UINT256 } from './Converter'; +import { EVM, getDefaultWeb3 } from './EVM'; +import * as log from './Log'; + +BigNumber.config({ + EXPONENTIAL_AT: 1000, + DECIMAL_PLACES: 80, +}); + +export interface DPPContextInitConfig { + lpFeeRate: string; + mtFeeRate: string; + k: string; + i: string; +} + +/* + price curve when k=0.1 + +──────────────────────+───────────────+ + | purchase percentage | avg slippage | + +──────────────────────+───────────────+ + | 1% | 0.1% | + | 5% | 0.5% | + | 10% | 1.1% | + | 20% | 2.5% | + | 50% | 10% | + | 70% | 23.3% | + +──────────────────────+───────────────+ +*/ +export let DefaultDPPContextInitConfig = { + lpFeeRate: decimalStr("0.002"), + mtFeeRate: decimalStr("0.001"), + k: decimalStr("0.1"), + i: decimalStr("100"), +}; + +export class DPPContext { + EVM: EVM; + Web3: Web3; + DPP: Contract; + BASE: Contract; + QUOTE: Contract; + Deployer: string; + Maintainer: string; + SpareAccounts: string[]; + + constructor() { } + + async init(config: DPPContextInitConfig) { + this.EVM = new EVM(); + this.Web3 = getDefaultWeb3(); + + this.DPP = await contracts.newContract(contracts.DPP_NAME) + var lpFeeRateModel = await contracts.newContract(contracts.CONST_FEE_RATE_MODEL_NAME) + var mtFeeRateModel = await contracts.newContract(contracts.CONST_FEE_RATE_MODEL_NAME) + var permissionManager = await contracts.newContract(contracts.PERMISSION_MANAGER_NAME) + var gasPriceSource = await contracts.newContract(contracts.EXTERNAL_VALUE_NAME) + + this.BASE = await contracts.newContract( + contracts.MINTABLE_ERC20_CONTRACT_NAME, + ["TestBase", "BASE", 18] + ); + this.QUOTE = await contracts.newContract( + contracts.MINTABLE_ERC20_CONTRACT_NAME, + ["TestQuote", "QUOTE", 18] + ); + + const allAccounts = await this.Web3.eth.getAccounts(); + this.Deployer = allAccounts[0]; + this.Maintainer = allAccounts[1]; + this.SpareAccounts = allAccounts.slice(2, 10); + + await this.DVM.methods.init( + this.Deployer, + this.Maintainer, + this.BASE.options.address, + this.QUOTE.options.address, + lpFeeRateModel.options.address, + mtFeeRateModel.options.address, + permissionManager.options.address, + gasPriceSource.options.address, + config.i, + config.k + ).send(this.sendParam(this.Deployer)) + + await gasPriceSource.methods.initOwner(this.Deployer).send(this.sendParam(this.Deployer)) + await gasPriceSource.methods.set(MAX_UINT256).send(this.sendParam(this.Deployer)) + await lpFeeRateModel.methods.init(this.Deployer, config.lpFeeRate).send(this.sendParam(this.Deployer)) + await mtFeeRateModel.methods.init(this.Deployer, config.mtFeeRate).send(this.sendParam(this.Deployer)) + + console.log(log.blueText("[Init DVM context]")); + } + + sendParam(sender, value = "0") { + return { + from: sender, + gas: process.env["COVERAGE"] ? 10000000000 : 7000000, + gasPrice: process.env.GAS_PRICE, + value: decimalStr(value), + }; + } + + async mintTestToken(to: string, base: string, quote: string) { + await this.BASE.methods.mint(to, base).send(this.sendParam(this.Deployer)); + await this.QUOTE.methods + .mint(to, quote) + .send(this.sendParam(this.Deployer)); + } + + async transferBaseToDVM(account: string, amount: string) { + await this.BASE.methods.transfer(this.DVM.options.address, amount).send(this.sendParam(account)) + } + + async transferQuoteToDVM(account: string, amount: string) { + await this.QUOTE.methods.transfer(this.DVM.options.address, amount).send(this.sendParam(account)) + } +} + +export async function getDVMContext( + config: DVMContextInitConfig = DefaultDVMContextInitConfig +): Promise { + var context = new DVMContext(); + await context.init(config); + return context; +}