-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from meTokens/diamond
refactor: diamond storage
- Loading branch information
Showing
59 changed files
with
3,531 additions
and
3,385 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.