Merge branch 'feature/V2' of github.com:DODOEX/contractV2 into feature/V2
This commit is contained in:
@@ -14,16 +14,26 @@ import {SafeMath} from "../../lib/SafeMath.sol";
|
||||
import {DODOMath} from "../../lib/DODOMath.sol";
|
||||
import {DecimalMath} from "../../lib/DecimalMath.sol";
|
||||
import {IPermissionManager} from "../../lib/PermissionManager.sol";
|
||||
<<<<<<< HEAD
|
||||
import {IExternalValue} from "../../lib/ExternalValue.sol";
|
||||
import {IFeeRateModel} from "../../intf/IFeeRateModel.sol";
|
||||
import {IERC20} from "../../intf/IERC20.sol";
|
||||
=======
|
||||
import {IGasPriceSource} from "../../lib/GasPriceSource.sol";
|
||||
import {IFeeRateModel} from "../../intf/IFeeRateModel.sol";
|
||||
import {IDVMVault} from "../intf/IDVMVault.sol";
|
||||
>>>>>>> bd21a14b5398693ec5ad47e52fed6a8ea1193e9b
|
||||
|
||||
contract DVMStorage is InitializableOwnable, ReentrancyGuard {
|
||||
using SafeMath for uint256;
|
||||
|
||||
// ============ Variables for Control ============
|
||||
|
||||
<<<<<<< HEAD
|
||||
IExternalValue public _GAS_PRICE_LIMIT_;
|
||||
=======
|
||||
IGasPriceSource public _GAS_PRICE_LIMIT_;
|
||||
>>>>>>> bd21a14b5398693ec5ad47e52fed6a8ea1193e9b
|
||||
|
||||
// ============ Advanced Controls ============
|
||||
|
||||
@@ -36,6 +46,7 @@ contract DVMStorage is InitializableOwnable, ReentrancyGuard {
|
||||
|
||||
address public _MAINTAINER_; // collect maintainer fee
|
||||
|
||||
<<<<<<< HEAD
|
||||
IERC20 public _BASE_TOKEN_;
|
||||
IERC20 public _QUOTE_TOKEN_;
|
||||
|
||||
@@ -51,6 +62,10 @@ contract DVMStorage is InitializableOwnable, ReentrancyGuard {
|
||||
uint256 public totalSupply;
|
||||
mapping(address => uint256) internal _SHARES_;
|
||||
mapping(address => mapping(address => uint256)) internal _ALLOWED_;
|
||||
=======
|
||||
address public _BASE_TOKEN_;
|
||||
address public _QUOTE_TOKEN_;
|
||||
>>>>>>> bd21a14b5398693ec5ad47e52fed6a8ea1193e9b
|
||||
|
||||
// ============ Variables for Pricing ============
|
||||
|
||||
@@ -59,6 +74,44 @@ contract DVMStorage is InitializableOwnable, ReentrancyGuard {
|
||||
uint256 public _K_;
|
||||
uint256 public _I_;
|
||||
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
IDVMVault public _VAULT_;
|
||||
|
||||
// ============ Modifiers ============
|
||||
|
||||
modifier isBuyAllow(address trader) {
|
||||
require(!_BUYING_CLOSE_ && _TRADE_PERMISSION_.isAllowed(trader), "TRADER_BUY_NOT_ALLOWED");
|
||||
_;
|
||||
}
|
||||
|
||||
modifier isSellAllow(address trader) {
|
||||
require(
|
||||
!_SELLING_CLOSE_ && _TRADE_PERMISSION_.isAllowed(trader),
|
||||
"TRADER_SELL_NOT_ALLOWED"
|
||||
);
|
||||
_;
|
||||
}
|
||||
|
||||
modifier limitGasPrice() {
|
||||
require(tx.gasprice <= _GAS_PRICE_LIMIT_.getGasPrice(), "GAS_PRICE_EXCEED");
|
||||
_;
|
||||
}
|
||||
|
||||
// ============ Helper Functions ============
|
||||
|
||||
function calculateBase0(uint256 baseAmount, uint256 quoteAmount) public view returns (uint256) {
|
||||
uint256 fairAmount = DecimalMath.divFloor(quoteAmount, _I_);
|
||||
return DODOMath._SolveQuadraticFunctionForTarget(baseAmount, _K_, fairAmount);
|
||||
}
|
||||
|
||||
function getBase0() public view returns (uint256) {
|
||||
(uint256 baseAmount, uint256 quoteAmount) = _VAULT_.getVaultReserve();
|
||||
uint256 fairAmount = DecimalMath.divFloor(quoteAmount, _I_);
|
||||
return DODOMath._SolveQuadraticFunctionForTarget(baseAmount, _K_, fairAmount);
|
||||
}
|
||||
|
||||
>>>>>>> bd21a14b5398693ec5ad47e52fed6a8ea1193e9b
|
||||
// ============ Setting Functions ============
|
||||
|
||||
function setLpFeeRateModel(address newLpFeeRateModel) external onlyOwner {
|
||||
@@ -78,7 +131,11 @@ contract DVMStorage is InitializableOwnable, ReentrancyGuard {
|
||||
}
|
||||
|
||||
function setGasPriceSource(address newGasPriceLimitSource) external onlyOwner {
|
||||
<<<<<<< HEAD
|
||||
_GAS_PRICE_LIMIT_ = IExternalValue(newGasPriceLimitSource);
|
||||
=======
|
||||
_GAS_PRICE_LIMIT_ = IGasPriceSource(newGasPriceLimitSource);
|
||||
>>>>>>> bd21a14b5398693ec5ad47e52fed6a8ea1193e9b
|
||||
}
|
||||
|
||||
function setBuy(bool open) external onlyOwner {
|
||||
|
||||
71
contracts/DODOVendorMachine/intf/IDVMVault.sol
Normal file
71
contracts/DODOVendorMachine/intf/IDVMVault.sol
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
interface IDVMVault {
|
||||
function init(
|
||||
address owner,
|
||||
address _baseToken,
|
||||
address _quoteToken
|
||||
) external;
|
||||
|
||||
function _BASE_TOKEN_() external returns (address);
|
||||
|
||||
function _QUOTE_TOKEN_() external returns (address);
|
||||
|
||||
function _BASE_RESERVE_() external returns (uint256);
|
||||
|
||||
function _QUOTE_RESERVE_() external returns (uint256);
|
||||
|
||||
function symbol() external returns (string memory);
|
||||
|
||||
function decimals() external returns (uint256);
|
||||
|
||||
function name() external returns (string memory);
|
||||
|
||||
function totalSupply() external returns (uint256);
|
||||
|
||||
function getVaultBalance() external view returns (uint256 baseBalance, uint256 quoteBalance);
|
||||
|
||||
function getVaultReserve() external view returns (uint256 baseReserve, uint256 quoteReserve);
|
||||
|
||||
function getBaseBalance() external view returns (uint256 baseBalance);
|
||||
|
||||
function getQuoteBalance() external view returns (uint256 quoteBalance);
|
||||
|
||||
function getBaseInput() external view returns (uint256 input);
|
||||
|
||||
function getQuoteInput() external view returns (uint256 input);
|
||||
|
||||
function sync() external;
|
||||
|
||||
function transferBaseOut(address to, uint256 amount) external;
|
||||
|
||||
function transferQuoteOut(address to, uint256 amount) external;
|
||||
|
||||
function transfer(address to, uint256 amount) external returns (bool);
|
||||
|
||||
function balanceOf(address owner) external view returns (uint256 balance);
|
||||
|
||||
function shareRatioOf(address owner) external view returns (uint256 shareRatio);
|
||||
|
||||
function transferFrom(
|
||||
address from,
|
||||
address to,
|
||||
uint256 amount
|
||||
) external returns (bool);
|
||||
|
||||
function approve(address spender, uint256 amount) external returns (bool);
|
||||
|
||||
function allowance(address owner, address spender) external view returns (uint256);
|
||||
|
||||
function mint(address user, uint256 value) external;
|
||||
|
||||
function burn(address user, uint256 value) external;
|
||||
}
|
||||
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,7 +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 {
|
||||
using SafeMath for uint256;
|
||||
@@ -25,6 +25,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,11 +37,20 @@ 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 {}
|
||||
|
||||
receive() external payable {}
|
||||
|
||||
function dodoSwap(
|
||||
IERC20 fromToken,
|
||||
IERC20 toToken,
|
||||
@@ -54,25 +64,37 @@ 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++) {
|
||||
uint256 curDirection = directions[i];
|
||||
address curDodoPair = dodoPairs[i];
|
||||
if(curDirection == 0){
|
||||
if (curDirection == 0) {
|
||||
address curDodoBase = IDODO(curDodoPair)._BASE_TOKEN_();
|
||||
uint256 curAmountIn = IERC20(curDodoBase).balanceOf(address(this));
|
||||
IERC20(curDodoBase).approve(curDodoPair,curAmountIn);
|
||||
IERC20(curDodoBase).universalApprove(curDodoPair, curAmountIn);
|
||||
IDODO(curDodoPair).sellBaseToken(curAmountIn, 0, "");
|
||||
}else {
|
||||
} else {
|
||||
address curDodoQuote = IDODO(curDodoPair)._QUOTE_TOKEN_();
|
||||
uint256 curAmountIn = IERC20(curDodoQuote).balanceOf(address(this));
|
||||
IERC20(curDodoQuote).approve(curDodoPair,curAmountIn);
|
||||
uint256 canBuyBaseAmount = dodoSellHelper.querySellQuoteToken(curDodoPair,curAmountIn);
|
||||
IERC20(curDodoQuote).universalApprove(curDodoPair, curAmountIn);
|
||||
uint256 canBuyBaseAmount = dodoSellHelper.querySellQuoteToken(
|
||||
curDodoPair,
|
||||
curAmountIn
|
||||
);
|
||||
IDODO(curDodoPair).buyBaseToken(canBuyBaseAmount, curAmountIn, "");
|
||||
}
|
||||
}
|
||||
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");
|
||||
@@ -80,7 +102,6 @@ contract SmartSwap is Ownable {
|
||||
emit OrderHistory(fromToken, toToken, msg.sender, fromTokenAmount, returnAmount);
|
||||
}
|
||||
|
||||
|
||||
function externalSwap(
|
||||
IERC20 fromToken,
|
||||
IERC20 toToken,
|
||||
@@ -91,15 +112,16 @@ contract SmartSwap is Ownable {
|
||||
uint256 minReturnAmount,
|
||||
bytes memory callDataConcat
|
||||
) public payable returns (uint256 returnAmount) {
|
||||
|
||||
require(minReturnAmount > 0, "Min return should be bigger then 0.");
|
||||
|
||||
if (fromToken != ETH_ADDRESS) {
|
||||
smartApprove.claimTokens(fromToken, msg.sender, address(this), fromTokenAmount);
|
||||
fromToken.approve(approveTarget, fromTokenAmount);
|
||||
fromToken.universalApprove(approveTarget, fromTokenAmount);
|
||||
}
|
||||
|
||||
(bool success, ) = to.call{value: msg.value, gas: gasSwap}(callDataConcat);
|
||||
(bool success, ) = to.call{value: fromToken == ETH_ADDRESS ? msg.value : 0, gas: gasSwap}(
|
||||
callDataConcat
|
||||
);
|
||||
|
||||
require(success, "Contract Swap execution Failed");
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
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_;
|
||||
}
|
||||
}
|
||||
@@ -12,18 +12,26 @@ import {IERC20} from "../intf/IERC20.sol";
|
||||
import {SafeERC20} from "./SafeERC20.sol";
|
||||
|
||||
library UniversalERC20 {
|
||||
|
||||
using SafeMath for uint256;
|
||||
using SafeERC20 for IERC20;
|
||||
|
||||
IERC20 private constant ZERO_ADDRESS = IERC20(0x0000000000000000000000000000000000000000);
|
||||
IERC20 private constant ETH_ADDRESS = IERC20(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE);
|
||||
|
||||
function universalTransfer(IERC20 token, address to, uint256 amount) internal {
|
||||
function universalTransfer(
|
||||
IERC20 token,
|
||||
address to,
|
||||
uint256 amount
|
||||
) internal {
|
||||
universalTransfer(token, to, amount, false);
|
||||
}
|
||||
|
||||
function universalTransfer(IERC20 token, address to, uint256 amount, bool mayFail) internal returns(bool) {
|
||||
function universalTransfer(
|
||||
IERC20 token,
|
||||
address to,
|
||||
uint256 amount,
|
||||
bool mayFail
|
||||
) internal returns (bool) {
|
||||
if (amount == 0) {
|
||||
return true;
|
||||
}
|
||||
@@ -41,13 +49,33 @@ library UniversalERC20 {
|
||||
}
|
||||
}
|
||||
|
||||
function universalApprove(IERC20 token, address to, uint256 amount) internal {
|
||||
function universalApprove(
|
||||
IERC20 token,
|
||||
address to,
|
||||
uint256 amount
|
||||
) internal {
|
||||
if (token != ZERO_ADDRESS && token != ETH_ADDRESS) {
|
||||
token.safeApprove(to, amount);
|
||||
if (amount == 0) {
|
||||
token.safeApprove(to, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
uint256 allowance = token.allowance(address(this), to);
|
||||
if (allowance < amount) {
|
||||
if (allowance > 0) {
|
||||
token.safeApprove(to, 0);
|
||||
}
|
||||
token.safeApprove(to, amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function universalTransferFrom(IERC20 token, address from, address to, uint256 amount) internal {
|
||||
function universalTransferFrom(
|
||||
IERC20 token,
|
||||
address from,
|
||||
address to,
|
||||
uint256 amount
|
||||
) internal {
|
||||
if (amount == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -72,4 +100,4 @@ library UniversalERC20 {
|
||||
return token.balanceOf(who);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user