init test env

This commit is contained in:
mingda
2020-10-26 19:19:10 +08:00
parent f36410f8f1
commit 081c0ecde4
26 changed files with 147 additions and 55 deletions

View File

@@ -31,7 +31,7 @@ contract DVMFactory is Ownable {
_CONTROLLER_TEMPLATE_ = controllerTemplate;
}
function createDODOVenderMachine(
function createDODOVendorMachine(
address maintainer,
address baseToken,
address quoteToken,
@@ -40,17 +40,15 @@ contract DVMFactory is Ownable {
uint256 i,
uint256 k,
uint256 gasPriceLimit
) external returns (address newVenderMachine) {
DVMController controller = DVMController(
ICloneFactory(_CLONE_FACTORY_).clone(_CONTROLLER_TEMPLATE_)
);
DVMVault vault = DVMVault(ICloneFactory(_CLONE_FACTORY_).clone(_VAULT_TEMPLATE_));
vault.init(address(controller), baseToken, quoteToken); // vault owner is controller
) external returns (address newVendorMachine) {
newVendorMachine = ICloneFactory(_CLONE_FACTORY_).clone(_CONTROLLER_TEMPLATE_);
address vault = ICloneFactory(_CLONE_FACTORY_).clone(_VAULT_TEMPLATE_);
DVMVault(vault).init(newVendorMachine, baseToken, quoteToken); // vault owner is controller
controller.init(
DVMController(newVendorMachine).init(
msg.sender,
maintainer,
address(vault),
vault,
lpFeeRateModel,
mtFeeRateModel,
i,
@@ -58,8 +56,15 @@ contract DVMFactory is Ownable {
gasPriceLimit
);
newVenderMachine = address(controller);
_REGISTRY_[baseToken][quoteToken].push(newVenderMachine);
return newVenderMachine;
_REGISTRY_[baseToken][quoteToken].push(newVendorMachine);
return newVendorMachine;
}
function getVendorMachine(address baseToken, address quoteToken)
external
view
returns (address[] memory machines)
{
return _REGISTRY_[baseToken][quoteToken];
}
}

View File

@@ -27,8 +27,9 @@ contract DVMController is DVMTrader, DVMFunding, DVMAdmin {
) external {
initOwner(owner);
_MAINTAINER_ = maintainer;
_BASE_TOKEN_ = DVMVault(vault)._BASE_TOKEN_();
_QUOTE_TOKEN_ = DVMVault(vault)._QUOTE_TOKEN_();
_VAULT_ = DVMVault(vault);
_BASE_TOKEN_ = _VAULT_._BASE_TOKEN_();
_QUOTE_TOKEN_ = _VAULT_._QUOTE_TOKEN_();
_LP_FEE_RATE_MODEL_ = IFeeRateModel(lpFeeRateModel);
_MT_FEE_RATE_MODEL_ = IFeeRateModel(mtFeeRateModel);
_I_ = i;

View File

@@ -15,41 +15,35 @@ contract DVMFunding is DVMStorage {
function buyShares(address account) external returns (uint256) {
uint256 baseInput = _VAULT_.getBaseInput();
uint256 quoteInput = _VAULT_.getQuoteInput();
require(baseInput > 0, "NO_BASE_INPUT");
uint256 baseReserve = _VAULT_._BASE_RESERVE_();
uint256 quoteReserve = _VAULT_._QUOTE_RESERVE_();
uint256 mintAmount;
// case 1. initial supply
if (baseReserve == 0 && quoteReserve == 0) {
mintAmount = baseInput;
}
// case 2. supply when quote reserve is 0
if (baseReserve > 0 && quoteReserve == 0) {
uint256 mintRatio = DecimalMath.divFloor(baseInput, baseReserve);
mintAmount = DecimalMath.mulFloor(_VAULT_.totalSupply(), mintRatio);
}
// case 3. normal case
if (baseReserve > 0 && quoteReserve > 0) {
uint256 baseInputRatio = DecimalMath.divFloor(baseInput, baseReserve);
uint256 quoteInputRatio = DecimalMath.divFloor(quoteInput, quoteReserve);
uint256 mintRatio = baseInputRatio > quoteInputRatio ? quoteInputRatio : baseInputRatio;
// balance为0但totalsupply不为0的情况
// reserve>0totalSupply=0
uint256 totalShare = _VAULT_.totalSupply();
if (totalShare > 0) {
mintAmount = DecimalMath.mulFloor(totalShare, mintRatio);
} else {
mintAmount = baseInput;
}
}
_VAULT_.mint(account, mintAmount);
_VAULT_.sync();
// // case 2. supply when quote reserve is 0
// if (baseReserve > 0 && quoteReserve == 0) {
// uint256 mintRatio = DecimalMath.divFloor(baseInput, baseReserve);
// mintAmount = DecimalMath.mulFloor(_VAULT_.totalSupply(), mintRatio);
// }
// // case 3. normal case
// if (baseReserve > 0 && quoteReserve > 0) {
// uint256 baseInputRatio = DecimalMath.divFloor(baseInput, baseReserve);
// uint256 quoteInputRatio = DecimalMath.divFloor(quoteInput, quoteReserve);
// uint256 mintRatio = baseInputRatio > quoteInputRatio ? quoteInputRatio : baseInputRatio;
// // balance为0但totalsupply不为0的情况
// // reserve>0totalSupply=0
// uint256 totalShare = _VAULT_.totalSupply();
// if (totalShare > 0) {
// mintAmount = DecimalMath.mulFloor(totalShare, mintRatio);
// } else {
// mintAmount = baseInput;
// }
// }
// _VAULT_.mint(account, mintAmount);
// _VAULT_.sync();
}
function sellShares(

View File

@@ -66,6 +66,8 @@ contract DVMVault is InitializableOwnable {
);
symbol = "DLP";
decimals = IERC20(_baseToken).decimals();
_BASE_TOKEN_ = _baseToken;
_QUOTE_TOKEN_ = _quoteToken;
}
// Vault related

View File

@@ -8,8 +8,8 @@
pragma solidity 0.6.9;
import {Ownable} from "../lib/Ownable.sol";
import {DVMController} from "../DODOVenderMachine/impl/DVMController.sol";
import {DVMVault} from "../DODOVenderMachine/impl/DVMVault.sol";
import {DVMController} from "../DODOVendorMachine/impl/DVMController.sol";
import {DVMVault} from "../DODOVendorMachine/impl/DVMVault.sol";
import {IERC20} from "../intf/IERC20.sol";
import {SafeERC20} from "../lib/SafeERC20.sol";
import {SafeMath} from "../lib/SafeMath.sol";

View File

@@ -9,10 +9,11 @@ pragma solidity 0.6.9;
import {SafeMath} from "../../lib/SafeMath.sol";
contract TestERC20 {
contract MintableERC20 {
using SafeMath for uint256;
string public name;
string public symbol;
uint8 public decimals;
mapping(address => uint256) balances;
@@ -21,8 +22,13 @@ contract TestERC20 {
event Transfer(address indexed from, address indexed to, uint256 amount);
event Approval(address indexed owner, address indexed spender, uint256 amount);
constructor(string memory _name, uint8 _decimals) public {
constructor(
string memory _name,
string memory _symbol,
uint8 _decimals
) public {
name = _name;
symbol = _symbol;
decimals = _decimals;
}