Merge branch 'feature/V2' of github.com:DODOEX/contractV2 into feature/V2
This commit is contained in:
@@ -10,6 +10,7 @@ pragma experimental ABIEncoderV2;
|
||||
|
||||
interface ICloneFactory {
|
||||
function clone(address prototype) external returns (address proxy);
|
||||
function clone2(address prototype,bytes32 salt) external returns (address proxy);
|
||||
}
|
||||
|
||||
// introduction of proxy mode design: https://docs.openzeppelin.com/upgrades/2.8/
|
||||
@@ -30,4 +31,20 @@ contract CloneFactory is ICloneFactory {
|
||||
}
|
||||
return proxy;
|
||||
}
|
||||
|
||||
|
||||
function clone2(address prototype, bytes32 salt) 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 := create2(0, clone, 0x37, salt)
|
||||
}
|
||||
return proxy;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,17 +8,15 @@
|
||||
pragma solidity 0.6.9;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
import {IFeeRateModel} from "../intf/IFeeRateModel.sol";
|
||||
import {Ownable} from "../lib/Ownable.sol";
|
||||
import {InitializableOwnable} from "../lib/InitializableOwnable.sol";
|
||||
|
||||
interface IConstFeeRateModel is IFeeRateModel {
|
||||
interface IConstFeeRateModel {
|
||||
function init(address owner, uint256 feeRate) external;
|
||||
|
||||
function setFeeRate(uint256 newFeeRate) external;
|
||||
function getFeeRate(address trader) external view returns (uint256);
|
||||
}
|
||||
|
||||
contract ConstFeeRateModel is InitializableOwnable, IFeeRateModel {
|
||||
contract ConstFeeRateModel is InitializableOwnable {
|
||||
uint256 public _FEE_RATE_;
|
||||
|
||||
function init(address owner, uint256 feeRate) external {
|
||||
@@ -26,11 +24,11 @@ contract ConstFeeRateModel is InitializableOwnable, IFeeRateModel {
|
||||
_FEE_RATE_ = feeRate;
|
||||
}
|
||||
|
||||
function setFeeRate(uint256 newFeeRate) external {
|
||||
function setFeeRate(uint256 newFeeRate) external onlyOwner {
|
||||
_FEE_RATE_ = newFeeRate;
|
||||
}
|
||||
|
||||
function getFeeRate(address) external override view returns (uint256) {
|
||||
function getFeeRate(address trader) external view returns (uint256) {
|
||||
return _FEE_RATE_;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,23 +8,28 @@
|
||||
pragma solidity 0.6.9;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
import {Ownable} from "./Ownable.sol";
|
||||
import {InitializableOwnable} from "./InitializableOwnable.sol";
|
||||
import {InitializableOwnable} from "../lib/InitializableOwnable.sol";
|
||||
|
||||
interface IExternalValue {
|
||||
function set(uint256) external;
|
||||
|
||||
function init(address owner, uint256 value) external;
|
||||
function set(uint256 value) external;
|
||||
function get() external view returns (uint256);
|
||||
}
|
||||
|
||||
contract ExternalValue is IExternalValue, InitializableOwnable {
|
||||
|
||||
contract ExternalValue is InitializableOwnable {
|
||||
uint256 public _VALUE_;
|
||||
|
||||
function set(uint256 value) external override onlyOwner {
|
||||
function init(address owner, uint256 value) external {
|
||||
initOwner(owner);
|
||||
_VALUE_ = value;
|
||||
}
|
||||
|
||||
function get() external override view returns (uint256) {
|
||||
function set(uint256 value) external onlyOwner {
|
||||
_VALUE_ = value;
|
||||
}
|
||||
|
||||
function get() external view returns (uint256) {
|
||||
return _VALUE_;
|
||||
}
|
||||
}
|
||||
|
||||
44
contracts/lib/FeeRateModel.sol
Normal file
44
contracts/lib/FeeRateModel.sol
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
import {InitializableOwnable} from "../lib/InitializableOwnable.sol";
|
||||
|
||||
interface IFeeRateModel {
|
||||
function getFeeRate(address trader) external view returns (uint256);
|
||||
function init(address owner, uint256 feeRate) external;
|
||||
function setFeeRate(uint256 newFeeRate) external;
|
||||
}
|
||||
|
||||
contract FeeRateModel is InitializableOwnable {
|
||||
//DEFAULT
|
||||
uint256 public _FEE_RATE_;
|
||||
mapping(address => uint256) feeMapping;
|
||||
|
||||
function init(address owner, uint256 feeRate) external {
|
||||
initOwner(owner);
|
||||
_FEE_RATE_ = feeRate;
|
||||
}
|
||||
|
||||
function setSpecificFeeRate(address trader, uint256 feeRate) external onlyOwner {
|
||||
require(trader != address(0), "INVALID ADDRESS!");
|
||||
feeMapping[trader] = feeRate;
|
||||
}
|
||||
|
||||
function setFeeRate(uint256 newFeeRate) external onlyOwner {
|
||||
_FEE_RATE_ = newFeeRate;
|
||||
}
|
||||
|
||||
function getFeeRate(address trader) external view returns (uint256) {
|
||||
uint256 feeRate = feeMapping[trader];
|
||||
if(feeRate == 0)
|
||||
return _FEE_RATE_;
|
||||
return feeRate;
|
||||
}
|
||||
}
|
||||
@@ -18,48 +18,26 @@ library UniversalERC20 {
|
||||
IERC20 private constant ZERO_ADDRESS = IERC20(0x0000000000000000000000000000000000000000);
|
||||
IERC20 private constant ETH_ADDRESS = IERC20(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE);
|
||||
|
||||
function universalTransfer(
|
||||
IERC20 token,
|
||||
address to,
|
||||
uint256 amount
|
||||
) internal {
|
||||
universalTransfer(token, to, amount, false);
|
||||
function isETH(IERC20 token) internal pure returns (bool) {
|
||||
return (token == ZERO_ADDRESS || token == ETH_ADDRESS);
|
||||
}
|
||||
|
||||
function universalTransfer(
|
||||
IERC20 token,
|
||||
address to,
|
||||
uint256 amount,
|
||||
bool mayFail
|
||||
) internal returns (bool) {
|
||||
if (amount == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (token == ZERO_ADDRESS || token == ETH_ADDRESS) {
|
||||
if (mayFail) {
|
||||
return address(uint160(to)).send(amount);
|
||||
function universalTransfer(IERC20 token, address payable to, uint256 amount) internal {
|
||||
if (amount > 0) {
|
||||
if (isETH(token)) {
|
||||
to.transfer(amount);
|
||||
} else {
|
||||
address(uint160(to)).transfer(amount);
|
||||
return true;
|
||||
token.safeTransfer(to, amount);
|
||||
}
|
||||
} else {
|
||||
token.safeTransfer(to, amount);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function universalApprove(
|
||||
IERC20 token,
|
||||
address to,
|
||||
uint256 amount
|
||||
) internal {
|
||||
if (token != ZERO_ADDRESS && token != ETH_ADDRESS) {
|
||||
if (amount == 0) {
|
||||
token.safeApprove(to, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
function universalApprove(IERC20 token, address to, uint256 amount) internal {
|
||||
require(!isETH(token), "ETH Don't need approve");
|
||||
if (amount == 0) {
|
||||
token.safeApprove(to, 0);
|
||||
} else {
|
||||
uint256 allowance = token.allowance(address(this), to);
|
||||
if (allowance < amount) {
|
||||
if (allowance > 0) {
|
||||
@@ -70,31 +48,8 @@ library UniversalERC20 {
|
||||
}
|
||||
}
|
||||
|
||||
function universalTransferFrom(
|
||||
IERC20 token,
|
||||
address from,
|
||||
address to,
|
||||
uint256 amount
|
||||
) internal {
|
||||
if (amount == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (token == ZERO_ADDRESS || token == ETH_ADDRESS) {
|
||||
require(from == msg.sender && msg.value >= amount, "msg.value is zero");
|
||||
if (to != address(this)) {
|
||||
address(uint160(to)).transfer(amount);
|
||||
}
|
||||
if (msg.value > amount) {
|
||||
msg.sender.transfer(msg.value.sub(amount));
|
||||
}
|
||||
} else {
|
||||
token.safeTransferFrom(from, to, amount);
|
||||
}
|
||||
}
|
||||
|
||||
function universalBalanceOf(IERC20 token, address who) internal view returns (uint256) {
|
||||
if (token == ZERO_ADDRESS || token == ETH_ADDRESS) {
|
||||
if (isETH(token)) {
|
||||
return who.balance;
|
||||
} else {
|
||||
return token.balanceOf(who);
|
||||
|
||||
Reference in New Issue
Block a user