Skip to content

Commit 5f611f9

Browse files
inital commit with gemcutter working
1 parent 8bd3b68 commit 5f611f9

File tree

13 files changed

+1251
-128
lines changed

13 files changed

+1251
-128
lines changed

Diff for: .gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ yarn-error.log*
2727

2828
# local env files
2929
.env*.local
30+
.env
3031

3132
# vercel
3233
.vercel
@@ -39,3 +40,7 @@ typechain
3940
#Hardhat files
4041
cache
4142
artifacts
43+
44+
*diamond.json
45+
metadata
46+
types

Diff for: DIAMONDFILE

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hardhat diamond:deploy
2+
hardhat diamond:add --remote --save-metadata --address 0x3D004D6B32D5D6A5e7B7504c3dac672456ed95dB
3+
hardhat diamond:cut

Diff for: contracts/Greeter.sol

-22
This file was deleted.

Diff for: contracts/_diamond/Diamond.sol

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
/******************************************************************************\
5+
* Author: Nick Mudge <[email protected]> (https://twitter.com/mudgen)
6+
* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535
7+
*
8+
* Implementation of a diamond.
9+
/******************************************************************************/
10+
11+
import { LibDiamond } from "./libraries/LibDiamond.sol";
12+
import { IDiamondCut } from "./interfaces/IDiamondCut.sol";
13+
import { IDiamondLoupe } from "./interfaces/IDiamondLoupe.sol";
14+
15+
contract Diamond {
16+
17+
constructor(address _contractOwner, address _diamondCutFacet, address _diamondLoupeFacet, address _facetsRepository) payable {
18+
LibDiamond.setContractOwner(_contractOwner);
19+
20+
// Add the diamondCut external function from the diamondCutFacet
21+
IDiamondCut.FacetCut[] memory cut = new IDiamondCut.FacetCut[](2);
22+
23+
// cut diamondCut
24+
bytes4[] memory functionSelectors1 = new bytes4[](1);
25+
functionSelectors1[0] = IDiamondCut.diamondCut.selector;
26+
cut[0] = IDiamondCut.FacetCut({
27+
facetAddress: _diamondCutFacet,
28+
action: IDiamondCut.FacetCutAction.Add,
29+
functionSelectors: functionSelectors1
30+
});
31+
32+
// cut diamondLoupe
33+
bytes4[] memory functionSelectors2 = new bytes4[](4);
34+
functionSelectors2[0] = IDiamondLoupe.facets.selector;
35+
functionSelectors2[1] = IDiamondLoupe.facetFunctionSelectors.selector;
36+
functionSelectors2[2] = IDiamondLoupe.facetAddresses.selector;
37+
functionSelectors2[3] = IDiamondLoupe.facetAddress.selector;
38+
cut[1] = IDiamondCut.FacetCut({
39+
facetAddress: _diamondLoupeFacet,
40+
action: IDiamondCut.FacetCutAction.Add,
41+
functionSelectors: functionSelectors2
42+
});
43+
44+
LibDiamond.diamondCut(cut, address(0), "");
45+
}
46+
47+
// Find facet for function that is called and execute the
48+
// function if a facet is found and return any value.
49+
fallback() external payable {
50+
LibDiamond.DiamondStorage storage ds;
51+
bytes32 position = LibDiamond.DIAMOND_STORAGE_POSITION;
52+
// get diamond storage
53+
assembly {
54+
ds.slot := position
55+
}
56+
// get facet from function selector
57+
address facet = address(bytes20(ds.facets[msg.sig]));
58+
require(facet != address(0), "Diamond: Function does not exist");
59+
// Execute external function from facet using delegatecall and return any value.
60+
assembly {
61+
// copy function selector and any arguments
62+
calldatacopy(0, 0, calldatasize())
63+
// execute function call using the facet
64+
let result := delegatecall(gas(), facet, 0, calldatasize(), 0, 0)
65+
// get any return value
66+
returndatacopy(0, 0, returndatasize())
67+
// return any return value or error back to the caller
68+
switch result
69+
case 0 {
70+
revert(0, returndatasize())
71+
}
72+
default {
73+
return(0, returndatasize())
74+
}
75+
}
76+
}
77+
78+
receive() external payable {}
79+
}

Diff for: contracts/_diamond/interfaces/IDiamondCut.sol

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
/******************************************************************************\
5+
* Author: Nick Mudge <[email protected]> (https://twitter.com/mudgen)
6+
* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535
7+
/******************************************************************************/
8+
9+
interface IDiamondCut {
10+
enum FacetCutAction {Add, Replace, Remove}
11+
// Add=0, Replace=1, Remove=2
12+
13+
struct FacetCut {
14+
address facetAddress;
15+
FacetCutAction action;
16+
bytes4[] functionSelectors;
17+
}
18+
19+
/// @notice Add/replace/remove any number of functions and optionally execute
20+
/// a function with delegatecall
21+
/// @param _diamondCut Contains the facet addresses and function selectors
22+
/// @param _init The address of the contract or facet to execute _calldata
23+
/// @param _calldata A function call, including function selector and arguments
24+
/// _calldata is executed with delegatecall on _init
25+
function diamondCut(
26+
FacetCut[] calldata _diamondCut,
27+
address _init,
28+
bytes calldata _calldata
29+
) external;
30+
31+
event DiamondCut(FacetCut[] _diamondCut, address _init, bytes _calldata);
32+
}

Diff for: contracts/_diamond/interfaces/IDiamondLoupe.sol

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
/******************************************************************************\
5+
* Author: Nick Mudge <[email protected]> (https://twitter.com/mudgen)
6+
* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535
7+
/******************************************************************************/
8+
9+
// A loupe is a small magnifying glass used to look at diamonds.
10+
// These functions look at diamonds
11+
interface IDiamondLoupe {
12+
/// These functions are expected to be called frequently
13+
/// by tools.
14+
15+
struct Facet {
16+
address facetAddress;
17+
bytes4[] functionSelectors;
18+
}
19+
20+
/// @notice Gets all facet addresses and their four byte function selectors.
21+
/// @return facets_ Facet
22+
function facets() external view returns (Facet[] memory facets_);
23+
24+
/// @notice Gets all the function selectors supported by a specific facet.
25+
/// @param _facet The facet address.
26+
/// @return facetFunctionSelectors_
27+
function facetFunctionSelectors(address _facet) external view returns (bytes4[] memory facetFunctionSelectors_);
28+
29+
/// @notice Get all the facet addresses used by a diamond.
30+
/// @return facetAddresses_
31+
function facetAddresses() external view returns (address[] memory facetAddresses_);
32+
33+
/// @notice Gets the facet that supports the given selector.
34+
/// @dev If facet is not found return address(0).
35+
/// @param _functionSelector The function selector.
36+
/// @return facetAddress_ The facet address.
37+
function facetAddress(bytes4 _functionSelector) external view returns (address facetAddress_);
38+
}

0 commit comments

Comments
 (0)