编译器
0.6.12+commit.27d51765
文件 1 的 10:DerivaDEX.sol
pragma solidity 0.6.12;
pragma experimental ABIEncoderV2;
import { LibDiamondCut } from "./diamond/LibDiamondCut.sol";
import { DiamondFacet } from "./diamond/DiamondFacet.sol";
import { OwnershipFacet } from "./diamond/OwnershipFacet.sol";
import { LibDiamondStorage } from "./diamond/LibDiamondStorage.sol";
import { IDiamondCut } from "./diamond/IDiamondCut.sol";
import { IDiamondLoupe } from "./diamond/IDiamondLoupe.sol";
import { IERC165 } from "./diamond/IERC165.sol";
import { LibDiamondStorageDerivaDEX } from "./storage/LibDiamondStorageDerivaDEX.sol";
import { IDDX } from "./tokens/interfaces/IDDX.sol";
contract DerivaDEX {
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor(IDDX _ddxToken) public {
LibDiamondStorage.DiamondStorage storage ds = LibDiamondStorage.diamondStorage();
LibDiamondStorageDerivaDEX.DiamondStorageDerivaDEX storage dsDerivaDEX =
LibDiamondStorageDerivaDEX.diamondStorageDerivaDEX();
dsDerivaDEX.admin = msg.sender;
require(address(_ddxToken) != address(0), "DerivaDEX: ddx token is zero address.");
dsDerivaDEX.ddxToken = _ddxToken;
emit OwnershipTransferred(address(0), msg.sender);
DiamondFacet diamondFacet = new DiamondFacet();
OwnershipFacet ownershipFacet = new OwnershipFacet();
IDiamondCut.FacetCut[] memory diamondCut = new IDiamondCut.FacetCut[](2);
diamondCut[0].facetAddress = address(diamondFacet);
diamondCut[0].action = IDiamondCut.FacetCutAction.Add;
diamondCut[0].functionSelectors = new bytes4[](6);
diamondCut[0].functionSelectors[0] = DiamondFacet.diamondCut.selector;
diamondCut[0].functionSelectors[1] = DiamondFacet.facetFunctionSelectors.selector;
diamondCut[0].functionSelectors[2] = DiamondFacet.facets.selector;
diamondCut[0].functionSelectors[3] = DiamondFacet.facetAddress.selector;
diamondCut[0].functionSelectors[4] = DiamondFacet.facetAddresses.selector;
diamondCut[0].functionSelectors[5] = DiamondFacet.supportsInterface.selector;
diamondCut[1].facetAddress = address(ownershipFacet);
diamondCut[1].action = IDiamondCut.FacetCutAction.Add;
diamondCut[1].functionSelectors = new bytes4[](2);
diamondCut[1].functionSelectors[0] = OwnershipFacet.transferOwnershipToSelf.selector;
diamondCut[1].functionSelectors[1] = OwnershipFacet.getAdmin.selector;
LibDiamondCut.diamondCut(diamondCut, address(0), new bytes(0));
ds.supportedInterfaces[IERC165.supportsInterface.selector] = true;
ds.supportedInterfaces[IDiamondCut.diamondCut.selector] = true;
bytes4 interfaceID =
IDiamondLoupe.facets.selector ^
IDiamondLoupe.facetFunctionSelectors.selector ^
IDiamondLoupe.facetAddresses.selector ^
IDiamondLoupe.facetAddress.selector;
ds.supportedInterfaces[interfaceID] = true;
}
receive() external payable {
revert("DerivaDEX does not directly accept ether.");
}
fallback() external payable {
LibDiamondStorage.DiamondStorage storage ds;
bytes32 position = LibDiamondStorage.DIAMOND_STORAGE_POSITION;
assembly {
ds_slot := position
}
address facet = ds.selectorToFacetAndPosition[msg.sig].facetAddress;
require(facet != address(0), "Function does not exist.");
assembly {
calldatacopy(0, 0, calldatasize())
let result := delegatecall(gas(), facet, 0, calldatasize(), 0, 0)
let size := returndatasize()
returndatacopy(0, 0, size)
switch result
case 0 {
revert(0, size)
}
default {
return(0, size)
}
}
}
}
文件 2 的 10:DiamondFacet.sol
pragma solidity 0.6.12;
pragma experimental ABIEncoderV2;
import "./LibDiamondStorage.sol";
import "./LibDiamondCut.sol";
import "../storage/LibDiamondStorageDerivaDEX.sol";
import "./IDiamondCut.sol";
import "./IDiamondLoupe.sol";
import "./IERC165.sol";
contract DiamondFacet is IDiamondCut, IDiamondLoupe, IERC165 {
function diamondCut(
FacetCut[] calldata _diamondCut,
address _init,
bytes calldata _calldata
) external override {
LibDiamondStorageDerivaDEX.DiamondStorageDerivaDEX storage dsDerivaDEX =
LibDiamondStorageDerivaDEX.diamondStorageDerivaDEX();
require(msg.sender == dsDerivaDEX.admin, "DiamondFacet: Must own the contract");
require(_diamondCut.length > 0, "DiamondFacet: No facets to cut");
for (uint256 facetIndex; facetIndex < _diamondCut.length; facetIndex++) {
LibDiamondCut.addReplaceRemoveFacetSelectors(
_diamondCut[facetIndex].facetAddress,
_diamondCut[facetIndex].action,
_diamondCut[facetIndex].functionSelectors
);
}
emit DiamondCut(_diamondCut, _init, _calldata);
LibDiamondCut.initializeDiamondCut(_init, _calldata);
}
function facets() external view override returns (Facet[] memory facets_) {
LibDiamondStorage.DiamondStorage storage ds = LibDiamondStorage.diamondStorage();
uint256 numFacets = ds.facetAddresses.length;
facets_ = new Facet[](numFacets);
for (uint256 i; i < numFacets; i++) {
address facetAddress_ = ds.facetAddresses[i];
facets_[i].facetAddress = facetAddress_;
facets_[i].functionSelectors = ds.facetFunctionSelectors[facetAddress_].functionSelectors;
}
}
function facetFunctionSelectors(address _facet)
external
view
override
returns (bytes4[] memory facetFunctionSelectors_)
{
LibDiamondStorage.DiamondStorage storage ds = LibDiamondStorage.diamondStorage();
facetFunctionSelectors_ = ds.facetFunctionSelectors[_facet].functionSelectors;
}
function facetAddresses() external view override returns (address[] memory facetAddresses_) {
LibDiamondStorage.DiamondStorage storage ds = LibDiamondStorage.diamondStorage();
facetAddresses_ = ds.facetAddresses;
}
function facetAddress(bytes4 _functionSelector) external view override returns (address facetAddress_) {
LibDiamondStorage.DiamondStorage storage ds = LibDiamondStorage.diamondStorage();
facetAddress_ = ds.selectorToFacetAndPosition[_functionSelector].facetAddress;
}
function supportsInterface(bytes4 _interfaceId) external view override returns (bool) {
LibDiamondStorage.DiamondStorage storage ds = LibDiamondStorage.diamondStorage();
return ds.supportedInterfaces[_interfaceId];
}
}
文件 3 的 10:IDDX.sol
pragma solidity 0.6.12;
pragma experimental ABIEncoderV2;
interface IDDX {
function transfer(address _recipient, uint256 _amount) external returns (bool);
function mint(address _recipient, uint256 _amount) external;
function delegate(address _delegatee) external;
function transferFrom(
address _sender,
address _recipient,
uint256 _amount
) external returns (bool);
function approve(address _spender, uint256 _amount) external returns (bool);
function getPriorVotes(address account, uint256 blockNumber) external view returns (uint96);
function totalSupply() external view returns (uint256);
}
文件 4 的 10:IDiamondCut.sol
pragma solidity 0.6.12;
pragma experimental ABIEncoderV2;
interface IDiamondCut {
enum FacetCutAction { Add, Replace, Remove }
struct FacetCut {
address facetAddress;
FacetCutAction action;
bytes4[] functionSelectors;
}
event DiamondCut(FacetCut[] _diamondCut, address _init, bytes _calldata);
function diamondCut(
FacetCut[] calldata _diamondCut,
address _init,
bytes calldata _calldata
) external;
}
文件 5 的 10:IDiamondLoupe.sol
pragma solidity 0.6.12;
pragma experimental ABIEncoderV2;
import "./IDiamondCut.sol";
interface IDiamondLoupe {
struct Facet {
address facetAddress;
bytes4[] functionSelectors;
}
function facets() external view returns (Facet[] memory facets_);
function facetFunctionSelectors(address _facet) external view returns (bytes4[] memory facetFunctionSelectors_);
function facetAddresses() external view returns (address[] memory facetAddresses_);
function facetAddress(bytes4 _functionSelector) external view returns (address facetAddress_);
}
文件 6 的 10:IERC165.sol
pragma solidity 0.6.12;
pragma experimental ABIEncoderV2;
interface IERC165 {
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
文件 7 的 10:LibDiamondCut.sol
pragma solidity 0.6.12;
pragma experimental ABIEncoderV2;
import "./LibDiamondStorage.sol";
import "./IDiamondCut.sol";
library LibDiamondCut {
event DiamondCut(IDiamondCut.FacetCut[] _diamondCut, address _init, bytes _calldata);
function diamondCut(
IDiamondCut.FacetCut[] memory _diamondCut,
address _init,
bytes memory _calldata
) internal {
require(_diamondCut.length > 0, "LibDiamondCut: No facets to cut");
for (uint256 facetIndex; facetIndex < _diamondCut.length; facetIndex++) {
addReplaceRemoveFacetSelectors(
_diamondCut[facetIndex].facetAddress,
_diamondCut[facetIndex].action,
_diamondCut[facetIndex].functionSelectors
);
}
emit DiamondCut(_diamondCut, _init, _calldata);
initializeDiamondCut(_init, _calldata);
}
function addReplaceRemoveFacetSelectors(
address _newFacetAddress,
IDiamondCut.FacetCutAction _action,
bytes4[] memory _selectors
) internal {
LibDiamondStorage.DiamondStorage storage ds = LibDiamondStorage.diamondStorage();
require(_selectors.length > 0, "LibDiamondCut: No selectors in facet to cut");
if (_newFacetAddress != address(0)) {
uint256 facetAddressPosition = ds.facetFunctionSelectors[_newFacetAddress].facetAddressPosition;
if (
facetAddressPosition == 0 && ds.facetFunctionSelectors[_newFacetAddress].functionSelectors.length == 0
) {
ensureHasContractCode(_newFacetAddress, "LibDiamondCut: New facet has no code");
facetAddressPosition = ds.facetAddresses.length;
ds.facetAddresses.push(_newFacetAddress);
ds.facetFunctionSelectors[_newFacetAddress].facetAddressPosition = uint16(facetAddressPosition);
}
for (uint256 selectorIndex; selectorIndex < _selectors.length; selectorIndex++) {
bytes4 selector = _selectors[selectorIndex];
address oldFacetAddress = ds.selectorToFacetAndPosition[selector].facetAddress;
if (_action == IDiamondCut.FacetCutAction.Add) {
require(oldFacetAddress == address(0), "LibDiamondCut: Can't add function that already exists");
addSelector(_newFacetAddress, selector);
} else if (_action == IDiamondCut.FacetCutAction.Replace) {
require(
oldFacetAddress != _newFacetAddress,
"LibDiamondCut: Can't replace function with same function"
);
removeSelector(oldFacetAddress, selector);
addSelector(_newFacetAddress, selector);
} else {
revert("LibDiamondCut: Incorrect FacetCutAction");
}
}
} else {
require(
_action == IDiamondCut.FacetCutAction.Remove,
"LibDiamondCut: action not set to FacetCutAction.Remove"
);
for (uint256 selectorIndex; selectorIndex < _selectors.length; selectorIndex++) {
bytes4 selector = _selectors[selectorIndex];
removeSelector(ds.selectorToFacetAndPosition[selector].facetAddress, selector);
}
}
}
function addSelector(address _newFacet, bytes4 _selector) internal {
LibDiamondStorage.DiamondStorage storage ds = LibDiamondStorage.diamondStorage();
uint256 selectorPosition = ds.facetFunctionSelectors[_newFacet].functionSelectors.length;
ds.facetFunctionSelectors[_newFacet].functionSelectors.push(_selector);
ds.selectorToFacetAndPosition[_selector].facetAddress = _newFacet;
ds.selectorToFacetAndPosition[_selector].functionSelectorPosition = uint16(selectorPosition);
}
function removeSelector(address _oldFacetAddress, bytes4 _selector) internal {
LibDiamondStorage.DiamondStorage storage ds = LibDiamondStorage.diamondStorage();
require(_oldFacetAddress != address(0), "LibDiamondCut: Can't remove or replace function that doesn't exist");
uint256 selectorPosition = ds.selectorToFacetAndPosition[_selector].functionSelectorPosition;
uint256 lastSelectorPosition = ds.facetFunctionSelectors[_oldFacetAddress].functionSelectors.length - 1;
bytes4 lastSelector = ds.facetFunctionSelectors[_oldFacetAddress].functionSelectors[lastSelectorPosition];
if (lastSelector != _selector) {
ds.facetFunctionSelectors[_oldFacetAddress].functionSelectors[selectorPosition] = lastSelector;
ds.selectorToFacetAndPosition[lastSelector].functionSelectorPosition = uint16(selectorPosition);
}
ds.facetFunctionSelectors[_oldFacetAddress].functionSelectors.pop();
delete ds.selectorToFacetAndPosition[_selector];
if (lastSelectorPosition == 0) {
uint256 lastFacetAddressPosition = ds.facetAddresses.length - 1;
address lastFacetAddress = ds.facetAddresses[lastFacetAddressPosition];
uint256 facetAddressPosition = ds.facetFunctionSelectors[_oldFacetAddress].facetAddressPosition;
if (_oldFacetAddress != lastFacetAddress) {
ds.facetAddresses[facetAddressPosition] = lastFacetAddress;
ds.facetFunctionSelectors[lastFacetAddress].facetAddressPosition = uint16(facetAddressPosition);
}
ds.facetAddresses.pop();
delete ds.facetFunctionSelectors[_oldFacetAddress];
}
}
function initializeDiamondCut(address _init, bytes memory _calldata) internal {
if (_init == address(0)) {
require(_calldata.length == 0, "LibDiamondCut: _init is address(0) but_calldata is not empty");
} else {
require(_calldata.length > 0, "LibDiamondCut: _calldata is empty but _init is not address(0)");
if (_init != address(this)) {
LibDiamondCut.ensureHasContractCode(_init, "LibDiamondCut: _init address has no code");
}
(bool success, bytes memory error) = _init.delegatecall(_calldata);
if (!success) {
if (error.length > 0) {
revert(string(error));
} else {
revert("LibDiamondCut: _init function reverted");
}
}
}
}
function ensureHasContractCode(address _contract, string memory _errorMessage) internal view {
uint256 contractSize;
assembly {
contractSize := extcodesize(_contract)
}
require(contractSize > 0, _errorMessage);
}
}
文件 8 的 10:LibDiamondStorage.sol
pragma solidity 0.6.12;
pragma experimental ABIEncoderV2;
library LibDiamondStorage {
struct FacetAddressAndPosition {
address facetAddress;
uint16 functionSelectorPosition;
}
struct FacetFunctionSelectors {
bytes4[] functionSelectors;
uint16 facetAddressPosition;
}
struct DiamondStorage {
mapping(bytes4 => FacetAddressAndPosition) selectorToFacetAndPosition;
mapping(address => FacetFunctionSelectors) facetFunctionSelectors;
address[] facetAddresses;
mapping(bytes4 => bool) supportedInterfaces;
}
bytes32 constant DIAMOND_STORAGE_POSITION = keccak256("diamond.standard.diamond.storage");
function diamondStorage() internal pure returns (DiamondStorage storage ds) {
bytes32 position = DIAMOND_STORAGE_POSITION;
assembly {
ds_slot := position
}
}
}
文件 9 的 10:LibDiamondStorageDerivaDEX.sol
pragma solidity 0.6.12;
pragma experimental ABIEncoderV2;
import { IDDX } from "../tokens/interfaces/IDDX.sol";
library LibDiamondStorageDerivaDEX {
struct DiamondStorageDerivaDEX {
string name;
address admin;
IDDX ddxToken;
}
bytes32 constant DIAMOND_STORAGE_POSITION_DERIVADEX =
keccak256("diamond.standard.diamond.storage.DerivaDEX.DerivaDEX");
function diamondStorageDerivaDEX() internal pure returns (DiamondStorageDerivaDEX storage ds) {
bytes32 position = DIAMOND_STORAGE_POSITION_DERIVADEX;
assembly {
ds_slot := position
}
}
}
文件 10 的 10:OwnershipFacet.sol
pragma solidity 0.6.12;
import { LibDiamondStorageDerivaDEX } from "../storage/LibDiamondStorageDerivaDEX.sol";
import { LibDiamondStorage } from "../diamond/LibDiamondStorage.sol";
import { IERC165 } from "./IERC165.sol";
contract OwnershipFacet {
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
function transferOwnershipToSelf() external {
LibDiamondStorageDerivaDEX.DiamondStorageDerivaDEX storage dsDerivaDEX =
LibDiamondStorageDerivaDEX.diamondStorageDerivaDEX();
require(msg.sender == dsDerivaDEX.admin, "Not authorized");
dsDerivaDEX.admin = address(this);
emit OwnershipTransferred(msg.sender, address(this));
}
function getAdmin() external view returns (address) {
LibDiamondStorageDerivaDEX.DiamondStorageDerivaDEX storage dsDerivaDEX =
LibDiamondStorageDerivaDEX.diamondStorageDerivaDEX();
return dsDerivaDEX.admin;
}
}
{
"compilationTarget": {
"contracts/DerivaDEX.sol": "DerivaDEX"
},
"evmVersion": "istanbul",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"details": {
"constantOptimizer": true,
"cse": true,
"deduplicate": true,
"jumpdestRemover": true,
"orderLiterals": true,
"peephole": true,
"yul": false
},
"runs": 50000
},
"remappings": []
}
[{"inputs":[{"internalType":"contract IDDX","name":"_ddxToken","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"stateMutability":"payable","type":"fallback"},{"stateMutability":"payable","type":"receive"}]