Skip to content

Commit

Permalink
Merge pull request #94 from meTokens/diamond
Browse files Browse the repository at this point in the history
refactor: diamond storage
  • Loading branch information
Carter Carlson authored Feb 16, 2022
2 parents 8cf98ee + 89ca216 commit 0d803bd
Show file tree
Hide file tree
Showing 59 changed files with 3,531 additions and 3,385 deletions.
64 changes: 64 additions & 0 deletions contracts/Diamond.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

/******************************************************************************\
* Author: Nick Mudge <nick@perfectabstractions.com> (https://twitter.com/mudgen)
* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535
*
* Implementation of a diamond.
/******************************************************************************/

import {LibDiamond} from "./libs/LibDiamond.sol";
import {IDiamondCut} from "./interfaces/IDiamondCut.sol";
import {LibAppStorage} from "./libs/Details.sol";

contract Diamond {
constructor(address _firstController, address _diamondCutFacet) payable {
LibAppStorage.initControllers(_firstController);

// Add the diamondCut external function from the diamondCutFacet
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), "");
}

receive() external payable {}

// Find facet for function that is called and execute the
// function if a facet is found and return any value.
/* solhint-disable */
fallback() external payable {
LibDiamond.DiamondStorage storage ds;
bytes32 position = LibDiamond.DIAMOND_STORAGE_POSITION;
// get diamond storage
assembly {
ds.slot := position
}
// get facet from function selector
address facet = ds.selectorToFacetAndPosition[msg.sig].facetAddress;
require(facet != address(0), "Diamond: Function does not exist");
// Execute external function from facet using delegatecall and return any value.
assembly {
// copy function selector and any arguments
calldatacopy(0, 0, calldatasize())
// execute function call using the facet
let result := delegatecall(gas(), facet, 0, calldatasize(), 0, 0)
// get any return value
returndatacopy(0, 0, returndatasize())
// return any return value or error back to the caller
switch result
case 0 {
revert(0, returndatasize())
}
default {
return(0, returndatasize())
}
}
}
}
55 changes: 55 additions & 0 deletions contracts/DiamondInit.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import {IERC173} from "./interfaces/IERC173.sol";
import {IRegistry} from "./interfaces/IRegistry.sol";
import {IMigrationRegistry} from "./interfaces/IMigrationRegistry.sol";
import {IDiamondCut} from "./interfaces/IDiamondCut.sol";
import {IDiamondLoupe} from "./interfaces/IDiamondLoupe.sol";
import {LibDiamond} from "./libs/LibDiamond.sol";
import "./libs/Details.sol";

contract DiamondInit {
struct Args {
uint256 mintFee;
uint256 burnBuyerFee;
uint256 burnOwnerFee;
uint256 transferFee;
uint256 interestFee;
uint256 yieldFee;
address diamond;
IRegistry vaultRegistry;
IRegistry curveRegistry;
IMigrationRegistry migrationRegistry;
address meTokenFactory;
}

AppStorage internal s; // solhint-disable-line

// TODO: access control?
function init(Args memory _args) external {
s.diamond = _args.diamond;
s.vaultRegistry = _args.vaultRegistry;
s.curveRegistry = _args.curveRegistry;
s.migrationRegistry = _args.migrationRegistry;
s.meTokenFactory = _args.meTokenFactory;
s.mintFee = _args.mintFee;
s.burnBuyerFee = _args.burnBuyerFee;
s.burnOwnerFee = _args.burnOwnerFee;
s.transferFee = _args.transferFee;
s.interestFee = _args.interestFee;
s.yieldFee = _args.yieldFee;

s.MAX_REFUND_RATIO = 1e6;
s.PRECISION = 1e18;

LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();

// Adding erc165 data
ds.supportedInterfaces[type(IDiamondCut).interfaceId] = true;
ds.supportedInterfaces[type(IDiamondLoupe).interfaceId] = true;
ds.supportedInterfaces[type(IERC165).interfaceId] = true;
ds.supportedInterfaces[type(IERC173).interfaceId] = true;
}
}
107 changes: 0 additions & 107 deletions contracts/Fees.sol

This file was deleted.

Loading

0 comments on commit 0d803bd

Please sign in to comment.