route and wrap and unwrap eth && test
This commit is contained in:
@@ -13,7 +13,7 @@ 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";
|
||||
import {IDVMVault} from "../intf/IDVMVault.sol";
|
||||
|
||||
contract DVM is DVMTrader, DVMFunding {
|
||||
function init(
|
||||
@@ -28,7 +28,7 @@ contract DVM is DVMTrader, DVMFunding {
|
||||
uint256 k
|
||||
) external {
|
||||
initOwner(owner);
|
||||
_VAULT_ = DVMVault(vault);
|
||||
_VAULT_ = IDVMVault(vault);
|
||||
_BASE_TOKEN_ = _VAULT_._BASE_TOKEN_();
|
||||
_QUOTE_TOKEN_ = _VAULT_._QUOTE_TOKEN_();
|
||||
_LP_FEE_RATE_MODEL_ = IFeeRateModel(lpFeeRateModel);
|
||||
|
||||
@@ -19,9 +19,9 @@ interface IDVMVault {
|
||||
|
||||
function _QUOTE_TOKEN_() external returns (address);
|
||||
|
||||
function _BASE_RESERVE_() external returns (address);
|
||||
function _BASE_RESERVE_() external returns (uint256);
|
||||
|
||||
function _QUOTE_RESERVE_() external returns (address);
|
||||
function _QUOTE_RESERVE_() external returns (uint256);
|
||||
|
||||
function symbol() external returns (string memory);
|
||||
|
||||
|
||||
134
contracts/DODOZoo.sol
Normal file
134
contracts/DODOZoo.sol
Normal file
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
import {Ownable} from "./lib/Ownable.sol";
|
||||
import {IDODO} from "./intf/IDODO.sol";
|
||||
import {ICloneFactory} from "./helper/CloneFactory.sol";
|
||||
|
||||
|
||||
/**
|
||||
* @title DODOZoo
|
||||
* @author DODO Breeder
|
||||
*
|
||||
* @notice Register of All DODO
|
||||
*/
|
||||
contract DODOZoo is Ownable {
|
||||
address public _DODO_LOGIC_;
|
||||
address public _CLONE_FACTORY_;
|
||||
|
||||
address public _DEFAULT_SUPERVISOR_;
|
||||
|
||||
mapping(address => mapping(address => address)) internal _DODO_REGISTER_;
|
||||
address[] public _DODOs;
|
||||
|
||||
// ============ Events ============
|
||||
|
||||
event DODOBirth(address newBorn, address baseToken, address quoteToken);
|
||||
|
||||
// ============ Constructor Function ============
|
||||
|
||||
constructor(
|
||||
address _dodoLogic,
|
||||
address _cloneFactory,
|
||||
address _defaultSupervisor
|
||||
) public {
|
||||
_DODO_LOGIC_ = _dodoLogic;
|
||||
_CLONE_FACTORY_ = _cloneFactory;
|
||||
_DEFAULT_SUPERVISOR_ = _defaultSupervisor;
|
||||
}
|
||||
|
||||
// ============ Admin Function ============
|
||||
|
||||
function setDODOLogic(address _dodoLogic) external onlyOwner {
|
||||
_DODO_LOGIC_ = _dodoLogic;
|
||||
}
|
||||
|
||||
function setCloneFactory(address _cloneFactory) external onlyOwner {
|
||||
_CLONE_FACTORY_ = _cloneFactory;
|
||||
}
|
||||
|
||||
function setDefaultSupervisor(address _defaultSupervisor) external onlyOwner {
|
||||
_DEFAULT_SUPERVISOR_ = _defaultSupervisor;
|
||||
}
|
||||
|
||||
function removeDODO(address dodo) external onlyOwner {
|
||||
address baseToken = IDODO(dodo)._BASE_TOKEN_();
|
||||
address quoteToken = IDODO(dodo)._QUOTE_TOKEN_();
|
||||
require(isDODORegistered(baseToken, quoteToken), "DODO_NOT_REGISTERED");
|
||||
_DODO_REGISTER_[baseToken][quoteToken] = address(0);
|
||||
for (uint256 i = 0; i <= _DODOs.length - 1; i++) {
|
||||
if (_DODOs[i] == dodo) {
|
||||
_DODOs[i] = _DODOs[_DODOs.length - 1];
|
||||
_DODOs.pop();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function addDODO(address dodo) public onlyOwner {
|
||||
address baseToken = IDODO(dodo)._BASE_TOKEN_();
|
||||
address quoteToken = IDODO(dodo)._QUOTE_TOKEN_();
|
||||
require(!isDODORegistered(baseToken, quoteToken), "DODO_REGISTERED");
|
||||
_DODO_REGISTER_[baseToken][quoteToken] = dodo;
|
||||
_DODOs.push(dodo);
|
||||
}
|
||||
|
||||
// ============ Breed DODO Function ============
|
||||
|
||||
function breedDODO(
|
||||
address maintainer,
|
||||
address baseToken,
|
||||
address quoteToken,
|
||||
address oracle,
|
||||
uint256 lpFeeRate,
|
||||
uint256 mtFeeRate,
|
||||
uint256 k,
|
||||
uint256 gasPriceLimit
|
||||
) external onlyOwner returns (address newBornDODO) {
|
||||
require(!isDODORegistered(baseToken, quoteToken), "DODO_REGISTERED");
|
||||
newBornDODO = ICloneFactory(_CLONE_FACTORY_).clone(_DODO_LOGIC_);
|
||||
IDODO(newBornDODO).init(
|
||||
_OWNER_,
|
||||
_DEFAULT_SUPERVISOR_,
|
||||
maintainer,
|
||||
baseToken,
|
||||
quoteToken,
|
||||
oracle,
|
||||
lpFeeRate,
|
||||
mtFeeRate,
|
||||
k,
|
||||
gasPriceLimit
|
||||
);
|
||||
addDODO(newBornDODO);
|
||||
emit DODOBirth(newBornDODO, baseToken, quoteToken);
|
||||
return newBornDODO;
|
||||
}
|
||||
|
||||
// ============ View Functions ============
|
||||
|
||||
function isDODORegistered(address baseToken, address quoteToken) public view returns (bool) {
|
||||
if (
|
||||
_DODO_REGISTER_[baseToken][quoteToken] == address(0) &&
|
||||
_DODO_REGISTER_[quoteToken][baseToken] == address(0)
|
||||
) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function getDODO(address baseToken, address quoteToken) external view returns (address) {
|
||||
return _DODO_REGISTER_[baseToken][quoteToken];
|
||||
}
|
||||
|
||||
function getDODOs() external view returns (address[] memory) {
|
||||
return _DODOs;
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import {SafeMath} from "../lib/SafeMath.sol";
|
||||
import {IDODOSellHelper} from "../intf/IDODOSellHelper.sol";
|
||||
import {ISmartApprove} from "../intf/ISmartApprove.sol";
|
||||
import {IDODO} from "../intf/IDODO.sol";
|
||||
import {IWETH} from "../intf/IWETH.sol";
|
||||
|
||||
|
||||
contract SmartSwap is Ownable {
|
||||
@@ -25,6 +26,7 @@ contract SmartSwap is Ownable {
|
||||
IERC20 constant ETH_ADDRESS = IERC20(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE);
|
||||
ISmartApprove public smartApprove;
|
||||
IDODOSellHelper public dodoSellHelper;
|
||||
address payable public _WETH_;
|
||||
|
||||
event OrderHistory(
|
||||
IERC20 indexed fromToken,
|
||||
@@ -36,9 +38,15 @@ contract SmartSwap is Ownable {
|
||||
|
||||
event ExternalRecord(address indexed to, address indexed sender);
|
||||
|
||||
constructor(address _smartApprove,address _dodoSellHelper) public {
|
||||
constructor(address _smartApprove,address _dodoSellHelper,address payable _weth) public {
|
||||
smartApprove = ISmartApprove(_smartApprove);
|
||||
dodoSellHelper = IDODOSellHelper(_dodoSellHelper);
|
||||
_WETH_ = _weth;
|
||||
}
|
||||
|
||||
|
||||
fallback() external payable {
|
||||
require(msg.sender == _WETH_, "WE_CAN_NOT_SAVED_YOUR_ETH");
|
||||
}
|
||||
|
||||
function dodoSwap(
|
||||
@@ -54,6 +62,9 @@ contract SmartSwap is Ownable {
|
||||
|
||||
if (fromToken != ETH_ADDRESS) {
|
||||
smartApprove.claimTokens(fromToken, msg.sender, address(this), fromTokenAmount);
|
||||
} else {
|
||||
require(msg.value == fromTokenAmount, "ETH_AMOUNT_NOT_MATCH");
|
||||
IWETH(_WETH_).deposit{value: fromTokenAmount}();
|
||||
}
|
||||
|
||||
for (uint256 i = 0; i < dodoPairs.length; i++) {
|
||||
@@ -73,6 +84,12 @@ contract SmartSwap is Ownable {
|
||||
}
|
||||
}
|
||||
fromToken.universalTransfer(msg.sender, fromToken.universalBalanceOf(address(this)));
|
||||
|
||||
if (toToken == ETH_ADDRESS) {
|
||||
uint256 wethAmount = IWETH(_WETH_).balanceOf(address(this));
|
||||
IWETH(_WETH_).withdraw(wethAmount);
|
||||
}
|
||||
|
||||
returnAmount = toToken.universalBalanceOf(address(this));
|
||||
|
||||
require(returnAmount >= minReturnAmount, "Return amount is not enough");
|
||||
|
||||
33
contracts/helper/CloneFactory.sol
Normal file
33
contracts/helper/CloneFactory.sol
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
interface ICloneFactory {
|
||||
function clone(address prototype) external returns (address proxy);
|
||||
}
|
||||
|
||||
// introduction of proxy mode design: https://docs.openzeppelin.com/upgrades/2.8/
|
||||
// minimum implementation of transparent proxy: https://eips.ethereum.org/EIPS/eip-1167
|
||||
|
||||
contract CloneFactory is ICloneFactory {
|
||||
function clone(address prototype) external override returns (address proxy) {
|
||||
bytes20 targetBytes = bytes20(prototype);
|
||||
assembly {
|
||||
let clone := mload(0x40)
|
||||
mstore(clone, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
|
||||
mstore(add(clone, 0x14), targetBytes)
|
||||
mstore(
|
||||
add(clone, 0x28),
|
||||
0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000
|
||||
)
|
||||
proxy := create(0, clone, 0x37)
|
||||
}
|
||||
return proxy;
|
||||
}
|
||||
}
|
||||
25
contracts/helper/NativeOracle.sol
Normal file
25
contracts/helper/NativeOracle.sol
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
import {Ownable} from "../lib/Ownable.sol";
|
||||
|
||||
|
||||
// Oracle only for test
|
||||
contract NaiveOracle is Ownable {
|
||||
uint256 public tokenPrice;
|
||||
|
||||
function setPrice(uint256 newPrice) external onlyOwner {
|
||||
tokenPrice = newPrice;
|
||||
}
|
||||
|
||||
function getPrice() external view returns (uint256) {
|
||||
return tokenPrice;
|
||||
}
|
||||
}
|
||||
74
contracts/helper/TestERC20.sol
Normal file
74
contracts/helper/TestERC20.sol
Normal file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
|
||||
import {SafeMath} from "../lib/SafeMath.sol";
|
||||
|
||||
contract TestERC20 {
|
||||
using SafeMath for uint256;
|
||||
|
||||
string public name;
|
||||
uint8 public decimals;
|
||||
string public symbol;
|
||||
|
||||
mapping(address => uint256) balances;
|
||||
mapping(address => mapping(address => uint256)) internal allowed;
|
||||
|
||||
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,string memory _symbol) public {
|
||||
name = _name;
|
||||
decimals = _decimals;
|
||||
symbol = _symbol;
|
||||
}
|
||||
|
||||
function transfer(address to, uint256 amount) public returns (bool) {
|
||||
require(to != address(0), "TO_ADDRESS_IS_EMPTY");
|
||||
require(amount <= balances[msg.sender], "BALANCE_NOT_ENOUGH");
|
||||
|
||||
balances[msg.sender] = balances[msg.sender].sub(amount);
|
||||
balances[to] = balances[to].add(amount);
|
||||
emit Transfer(msg.sender, to, amount);
|
||||
return true;
|
||||
}
|
||||
|
||||
function balanceOf(address owner) public view returns (uint256 balance) {
|
||||
return balances[owner];
|
||||
}
|
||||
|
||||
function transferFrom(
|
||||
address from,
|
||||
address to,
|
||||
uint256 amount
|
||||
) public returns (bool) {
|
||||
require(to != address(0), "TO_ADDRESS_IS_EMPTY");
|
||||
require(amount <= balances[from], "BALANCE_NOT_ENOUGH");
|
||||
require(amount <= allowed[from][msg.sender], "ALLOWANCE_NOT_ENOUGH");
|
||||
|
||||
balances[from] = balances[from].sub(amount);
|
||||
balances[to] = balances[to].add(amount);
|
||||
allowed[from][msg.sender] = allowed[from][msg.sender].sub(amount);
|
||||
emit Transfer(from, to, amount);
|
||||
return true;
|
||||
}
|
||||
|
||||
function approve(address spender, uint256 amount) public returns (bool) {
|
||||
allowed[msg.sender][spender] = amount;
|
||||
emit Approval(msg.sender, spender, amount);
|
||||
return true;
|
||||
}
|
||||
|
||||
function allowance(address owner, address spender) public view returns (uint256) {
|
||||
return allowed[owner][spender];
|
||||
}
|
||||
|
||||
function mint(address account, uint256 amount) external {
|
||||
balances[account] = balances[account].add(amount);
|
||||
}
|
||||
}
|
||||
77
contracts/helper/TestWETH.sol
Normal file
77
contracts/helper/TestWETH.sol
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
|
||||
|
||||
contract WETH9 {
|
||||
string public name = "Wrapped Ether";
|
||||
string public symbol = "WETH";
|
||||
uint8 public decimals = 18;
|
||||
|
||||
event Approval(address indexed src, address indexed guy, uint256 wad);
|
||||
event Transfer(address indexed src, address indexed dst, uint256 wad);
|
||||
event Deposit(address indexed dst, uint256 wad);
|
||||
event Withdrawal(address indexed src, uint256 wad);
|
||||
|
||||
mapping(address => uint256) public balanceOf;
|
||||
mapping(address => mapping(address => uint256)) public allowance;
|
||||
|
||||
fallback() external payable {
|
||||
deposit();
|
||||
}
|
||||
|
||||
receive() external payable {
|
||||
deposit();
|
||||
}
|
||||
|
||||
function deposit() public payable {
|
||||
balanceOf[msg.sender] += msg.value;
|
||||
emit Deposit(msg.sender, msg.value);
|
||||
}
|
||||
|
||||
function withdraw(uint256 wad) public {
|
||||
require(balanceOf[msg.sender] >= wad);
|
||||
balanceOf[msg.sender] -= wad;
|
||||
msg.sender.transfer(wad);
|
||||
emit Withdrawal(msg.sender, wad);
|
||||
}
|
||||
|
||||
function totalSupply() public view returns (uint256) {
|
||||
return address(this).balance;
|
||||
}
|
||||
|
||||
function approve(address guy, uint256 wad) public returns (bool) {
|
||||
allowance[msg.sender][guy] = wad;
|
||||
emit Approval(msg.sender, guy, wad);
|
||||
return true;
|
||||
}
|
||||
|
||||
function transfer(address dst, uint256 wad) public returns (bool) {
|
||||
return transferFrom(msg.sender, dst, wad);
|
||||
}
|
||||
|
||||
function transferFrom(
|
||||
address src,
|
||||
address dst,
|
||||
uint256 wad
|
||||
) public returns (bool) {
|
||||
require(balanceOf[src] >= wad);
|
||||
|
||||
if (src != msg.sender && allowance[src][msg.sender] != uint256(-1)) {
|
||||
require(allowance[src][msg.sender] >= wad);
|
||||
allowance[src][msg.sender] -= wad;
|
||||
}
|
||||
|
||||
balanceOf[src] -= wad;
|
||||
balanceOf[dst] += wad;
|
||||
|
||||
Transfer(src, dst, wad);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user