编译器
0.8.16+commit.07a7930e
文件 1 的 7:Diamond.sol
pragma solidity >=0.8.16;
import { LibDiamond } from "./libraries/LibDiamond.sol";
import { IDiamondCut } from "./interfaces/IDiamondCut.sol";
import { AppStorage } from "./libraries/LibAppStorage.sol";
contract Diamond {
AppStorage internal s;
receive() external payable {}
constructor(address _contractOwner, address _diamondCutFacet) payable {
LibDiamond.setContractOwner(_contractOwner);
IDiamondCut.FacetCut[] memory cut = new IDiamondCut.FacetCut[](1);
bytes4[] memory functionSelectors = new bytes4[](1);
functionSelectors[0] = IDiamondCut.diamondCut.selector;
cut[0] = IDiamondCut.FacetCut({
facetAddress: _diamondCutFacet,
action: IDiamondCut.FacetCutAction.Add,
functionSelectors: functionSelectors
});
LibDiamond.diamondCut(cut, address(0), "");
}
fallback() external payable {
LibDiamond.DiamondStorage storage ds;
bytes32 position = LibDiamond.DIAMOND_STORAGE_POSITION;
assembly {
ds.slot := position
}
address facet = ds.facetAddressAndSelectorPosition[msg.sig].facetAddress;
require(facet != address(0), "Diamond: Function does not exist");
assembly {
calldatacopy(0, 0, calldatasize())
let result := delegatecall(gas(), facet, 0, calldatasize(), 0, 0)
returndatacopy(0, 0, returndatasize())
switch result
case 0 {
revert(0, returndatasize())
}
default {
return(0, returndatasize())
}
}
}
}
文件 2 的 7:IDiamondCut.sol
pragma solidity >=0.8.16;
interface IDiamondCut {
enum FacetCutAction {
Add,
Replace,
Remove
}
struct FacetCut {
address facetAddress;
FacetCutAction action;
bytes4[] functionSelectors;
}
function diamondCut(
FacetCut[] calldata _diamondCut,
address _init,
bytes calldata _calldata
) external;
event DiamondCut(FacetCut[] _diamondCut, address _init, bytes _calldata);
}
文件 3 的 7:IDiamondLoupe.sol
pragma solidity >=0.8.16;
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_);
}
文件 4 的 7:IERC165.sol
pragma solidity >=0.8.16;
interface IERC165 {
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
文件 5 的 7:LibAppStorage.sol
pragma solidity >=0.8.16;
import "./LibEnums.sol";
struct Hedger {
address addr;
string[] pricingWssURLs;
string[] marketsHttpsURLs;
}
struct Market {
uint256 marketId;
string identifier;
MarketType marketType;
TradingSession tradingSession;
bool active;
string baseCurrency;
string quoteCurrency;
string symbol;
}
struct RequestForQuote {
uint256 rfqId;
RequestForQuoteState state;
PositionType positionType;
OrderType orderType;
address partyA;
address partyB;
HedgerMode hedgerMode;
uint256 marketId;
Side side;
uint256 notionalUsd;
uint16 leverageUsed;
uint256 lockedMargin;
uint256 protocolFee;
uint256 liquidationFee;
uint256 cva;
uint256 minExpectedUnits;
uint256 maxExpectedUnits;
uint256 creationTimestamp;
uint256 mutableTimestamp;
}
struct Fill {
uint256 fillId;
uint256 positionId;
Side side;
uint256 filledAmountUnits;
uint256 avgPriceUsd;
uint256 timestamp;
}
struct Position {
uint256 positionId;
PositionState state;
PositionType positionType;
uint256 marketId;
address partyA;
address partyB;
uint16 leverageUsed;
Side side;
uint256 lockedMargin;
uint256 protocolFeePaid;
uint256 liquidationFee;
uint256 cva;
uint256 currentBalanceUnits;
uint256 initialNotionalUsd;
uint256 creationTimestamp;
uint256 mutableTimestamp;
}
struct HedgersState {
mapping(address => Hedger) _hedgerMap;
Hedger[] _hedgerList;
}
struct MarketsState {
mapping(uint256 => Market) _marketMap;
Market[] _marketList;
}
struct MAState {
mapping(address => uint256) _accountBalances;
mapping(address => uint256) _marginBalances;
mapping(address => uint256) _lockedMargin;
mapping(address => uint256) _lockedMarginReserved;
mapping(uint256 => RequestForQuote) _requestForQuotesMap;
uint256 _requestForQuotesLength;
mapping(address => uint256[]) _openRequestForQuotesList;
mapping(uint256 => Position) _allPositionsMap;
uint256 _allPositionsLength;
mapping(address => uint256[]) _openPositionsIsolatedList;
mapping(address => uint256[]) _openPositionsCrossList;
mapping(uint256 => Fill[]) _positionFills;
}
struct AppStorage {
bool paused;
uint128 pausedAt;
uint256 reentrantStatus;
address ownerCandidate;
HedgersState hedgers;
MarketsState markets;
MAState ma;
}
library LibAppStorage {
function diamondStorage() internal pure returns (AppStorage storage ds) {
assembly {
ds.slot := 0
}
}
}
文件 6 的 7:LibDiamond.sol
pragma solidity >=0.8.16;
import { IDiamondCut } from "../interfaces/IDiamondCut.sol";
import { IDiamondLoupe } from "../interfaces/IDiamondLoupe.sol";
import { IERC165 } from "../interfaces/IERC165.sol";
library LibDiamond {
bytes32 public constant DIAMOND_STORAGE_POSITION = keccak256("diamond.standard.diamond.storage");
struct FacetAddressAndSelectorPosition {
address facetAddress;
uint16 selectorPosition;
}
struct DiamondStorage {
mapping(bytes4 => FacetAddressAndSelectorPosition) facetAddressAndSelectorPosition;
bytes4[] selectors;
mapping(bytes4 => bool) supportedInterfaces;
address contractOwner;
}
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
event DiamondCut(IDiamondCut.FacetCut[] _diamondCut, address _init, bytes _calldata);
function diamondStorage() internal pure returns (DiamondStorage storage ds) {
bytes32 position = DIAMOND_STORAGE_POSITION;
assembly {
ds.slot := position
}
}
function setContractOwner(address _newOwner) internal {
DiamondStorage storage ds = diamondStorage();
address previousOwner = ds.contractOwner;
ds.contractOwner = _newOwner;
emit OwnershipTransferred(previousOwner, _newOwner);
}
function contractOwner() internal view returns (address contractOwner_) {
contractOwner_ = diamondStorage().contractOwner;
}
function enforceIsOwnerOrContract() internal view {
require(
msg.sender == diamondStorage().contractOwner || msg.sender == address(this),
"LibDiamond: Must be contract or owner"
);
}
function enforceIsContractOwner() internal view {
require(msg.sender == diamondStorage().contractOwner, "LibDiamond: Must be contract owner");
}
function diamondCut(
IDiamondCut.FacetCut[] memory _diamondCut,
address _init,
bytes memory _calldata
) internal {
for (uint256 facetIndex; facetIndex < _diamondCut.length; facetIndex++) {
IDiamondCut.FacetCutAction action = _diamondCut[facetIndex].action;
if (action == IDiamondCut.FacetCutAction.Add) {
addFunctions(_diamondCut[facetIndex].facetAddress, _diamondCut[facetIndex].functionSelectors);
} else if (action == IDiamondCut.FacetCutAction.Replace) {
replaceFunctions(_diamondCut[facetIndex].facetAddress, _diamondCut[facetIndex].functionSelectors);
} else if (action == IDiamondCut.FacetCutAction.Remove) {
removeFunctions(_diamondCut[facetIndex].facetAddress, _diamondCut[facetIndex].functionSelectors);
} else {
revert("LibDiamondCut: Incorrect FacetCutAction");
}
}
emit DiamondCut(_diamondCut, _init, _calldata);
initializeDiamondCut(_init, _calldata);
}
function addFunctions(address _facetAddress, bytes4[] memory _functionSelectors) internal {
require(_functionSelectors.length > 0, "LibDiamondCut: No selectors in facet to cut");
DiamondStorage storage ds = diamondStorage();
uint16 selectorCount = uint16(ds.selectors.length);
require(_facetAddress != address(0), "LibDiamondCut: Add facet can't be address(0)");
enforceHasContractCode(_facetAddress, "LibDiamondCut: Add facet has no code");
for (uint256 selectorIndex; selectorIndex < _functionSelectors.length; selectorIndex++) {
bytes4 selector = _functionSelectors[selectorIndex];
address oldFacetAddress = ds.facetAddressAndSelectorPosition[selector].facetAddress;
require(oldFacetAddress == address(0), "LibDiamondCut: Can't add function that already exists");
ds.facetAddressAndSelectorPosition[selector] = FacetAddressAndSelectorPosition(
_facetAddress,
selectorCount
);
ds.selectors.push(selector);
selectorCount++;
}
}
function replaceFunctions(address _facetAddress, bytes4[] memory _functionSelectors) internal {
require(_functionSelectors.length > 0, "LibDiamondCut: No selectors in facet to cut");
DiamondStorage storage ds = diamondStorage();
require(_facetAddress != address(0), "LibDiamondCut: Replace facet can't be address(0)");
enforceHasContractCode(_facetAddress, "LibDiamondCut: Replace facet has no code");
for (uint256 selectorIndex; selectorIndex < _functionSelectors.length; selectorIndex++) {
bytes4 selector = _functionSelectors[selectorIndex];
address oldFacetAddress = ds.facetAddressAndSelectorPosition[selector].facetAddress;
require(oldFacetAddress != address(this), "LibDiamondCut: Can't replace immutable function");
require(oldFacetAddress != _facetAddress, "LibDiamondCut: Can't replace function with same function");
require(oldFacetAddress != address(0), "LibDiamondCut: Can't replace function that doesn't exist");
ds.facetAddressAndSelectorPosition[selector].facetAddress = _facetAddress;
}
}
function removeFunctions(address _facetAddress, bytes4[] memory _functionSelectors) internal {
require(_functionSelectors.length > 0, "LibDiamondCut: No selectors in facet to cut");
DiamondStorage storage ds = diamondStorage();
uint256 selectorCount = ds.selectors.length;
require(_facetAddress == address(0), "LibDiamondCut: Remove facet address must be address(0)");
for (uint256 selectorIndex; selectorIndex < _functionSelectors.length; selectorIndex++) {
bytes4 selector = _functionSelectors[selectorIndex];
FacetAddressAndSelectorPosition memory oldFacetAddressAndSelectorPosition = ds
.facetAddressAndSelectorPosition[selector];
require(
oldFacetAddressAndSelectorPosition.facetAddress != address(0),
"LibDiamondCut: Can't remove function that doesn't exist"
);
require(
oldFacetAddressAndSelectorPosition.facetAddress != address(this),
"LibDiamondCut: Can't remove immutable function."
);
selectorCount--;
if (oldFacetAddressAndSelectorPosition.selectorPosition != selectorCount) {
bytes4 lastSelector = ds.selectors[selectorCount];
ds.selectors[oldFacetAddressAndSelectorPosition.selectorPosition] = lastSelector;
ds.facetAddressAndSelectorPosition[lastSelector].selectorPosition = oldFacetAddressAndSelectorPosition
.selectorPosition;
}
ds.selectors.pop();
delete ds.facetAddressAndSelectorPosition[selector];
}
}
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)) {
enforceHasContractCode(_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 enforceHasContractCode(address _contract, string memory _errorMessage) internal view {
uint256 contractSize;
assembly {
contractSize := extcodesize(_contract)
}
require(contractSize > 0, _errorMessage);
}
}
文件 7 的 7:LibEnums.sol
pragma solidity >=0.8.16;
enum MarketType {
FOREX,
CRYPTO,
STOCK
}
enum TradingSession {
_24_7,
_24_5
}
enum Side {
BUY,
SELL
}
enum HedgerMode {
SINGLE,
HYBRID,
AUTO
}
enum OrderType {
LIMIT,
MARKET
}
enum PositionType {
ISOLATED,
CROSS
}
enum RequestForQuoteState {
ORPHAN,
CANCELATION_REQUESTED,
CANCELED,
REJECTED,
ACCEPTED
}
enum PositionState {
OPEN,
MARKET_CLOSE_REQUESTED,
MARKET_CLOSE_CANCELATION_REQUESTED,
LIMIT_CLOSE_REQUESTED,
LIMIT_CLOSE_CANCELATION_REQUESTED,
LIMIT_CLOSE_ACTIVE,
CLOSED,
LIQUIDATED
}
{
"compilationTarget": {
"contracts/Diamond.sol": "Diamond"
},
"evmVersion": "london",
"libraries": {},
"metadata": {
"bytecodeHash": "none"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": [],
"viaIR": true
}
[{"inputs":[{"internalType":"address","name":"_contractOwner","type":"address"},{"internalType":"address","name":"_diamondCutFacet","type":"address"}],"stateMutability":"payable","type":"constructor"},{"stateMutability":"payable","type":"fallback"},{"stateMutability":"payable","type":"receive"}]