# GNSDiamondCut

_Based on EIP-2535: Diamonds (_[_https://eips.ethereum.org/EIPS/eip-2535_](https://eips.ethereum.org/EIPS/eip-2535)_) Follows diamond-3 implementation (_[_https://github.com/mudgen/diamond-3-hardhat/_](https://github.com/mudgen/diamond-3-hardhat/)_) Manages all actions (calls, updates and initializations) related to the diamond and its facets._

## fallback

```solidity
fallback() external payable
```

_Forwards call to the right facet using msg.sig using delegatecall. Reverts if signature is not known._

## receive

```solidity
receive() external payable
```

_Allows the contract to receive ether_

## diamondCut

```solidity
function diamondCut(struct IDiamondStorage.FacetCut[] _faceCut, address _init, bytes _calldata) external
```

Add/replace/remove any number of functions and optionally execute a function with delegatecall

### Parameters

| Name       | Type                                                                               | Description                                                                                                   |
| ---------- | ---------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------- |
| \_faceCut  | [IDiamondStorage.FacetCut\[\]](../../../interfaces/types/idiamondstorage#facetcut) |                                                                                                               |
| \_init     | address                                                                            | The address of the contract or facet to execute \_calldata                                                    |
| \_calldata | bytes                                                                              | A function call, including function selector and arguments \_calldata is executed with delegatecall on \_init |

## \_diamondCut

```solidity
function _diamondCut(struct IDiamondStorage.FacetCut[] _facetCut, address _init, bytes _calldata) internal
```

_Internal function for diamondCut()_

## \_addFunctions

```solidity
function _addFunctions(address _facetAddress, bytes4[] _functionSelectors) internal
```

_Adds the facet if it wasn't added yet, and adds its functions to the diamond_

### Parameters

| Name                | Type      | Description                   |
| ------------------- | --------- | ----------------------------- |
| \_facetAddress      | address   | address of the facet contract |
| \_functionSelectors | bytes4\[] | array of function selectors   |

## \_replaceFunctions

```solidity
function _replaceFunctions(address _facetAddress, bytes4[] _functionSelectors) internal
```

_Updates facet contract address for given function selectors_

### Parameters

| Name                | Type      | Description                   |
| ------------------- | --------- | ----------------------------- |
| \_facetAddress      | address   | address of the facet contract |
| \_functionSelectors | bytes4\[] | array of function selectors   |

## \_removeFunctions

```solidity
function _removeFunctions(address _facetAddress, bytes4[] _functionSelectors) internal
```

_Removes some function selectors of a facet from diamond_

### Parameters

| Name                | Type      | Description                   |
| ------------------- | --------- | ----------------------------- |
| \_facetAddress      | address   | address of the facet contract |
| \_functionSelectors | bytes4\[] | array of function selectors   |

## \_addFacet

```solidity
function _addFacet(struct IDiamondStorage.DiamondStorage s, address _facetAddress) internal
```

_Adds a new facet contract address to the diamond_

### Parameters

| Name           | Type                                                                                       | Description                       |
| -------------- | ------------------------------------------------------------------------------------------ | --------------------------------- |
| s              | [IDiamondStorage.DiamondStorage](../../../interfaces/types/idiamondstorage#diamondstorage) | diamond storage pointer           |
| \_facetAddress | address                                                                                    | address of the new facet contract |

## \_addFunction

```solidity
function _addFunction(struct IDiamondStorage.DiamondStorage s, bytes4 _selector, uint96 _selectorPosition, address _facetAddress) internal
```

_Adds a new function to the diamond for a given facet contract_

### Parameters

| Name               | Type                                                                                       | Description                                                    |
| ------------------ | ------------------------------------------------------------------------------------------ | -------------------------------------------------------------- |
| s                  | [IDiamondStorage.DiamondStorage](../../../interfaces/types/idiamondstorage#diamondstorage) | diamond storage pointer                                        |
| \_selector         | bytes4                                                                                     | function selector                                              |
| \_selectorPosition | uint96                                                                                     | position of the function selector in the facet selectors array |
| \_facetAddress     | address                                                                                    | address of the facet contract                                  |

## \_removeFunction

```solidity
function _removeFunction(struct IDiamondStorage.DiamondStorage s, address _facetAddress, bytes4 _selector) internal
```

_Removes a function from a facet of the diamond_

### Parameters

| Name           | Type                                                                                       | Description                   |
| -------------- | ------------------------------------------------------------------------------------------ | ----------------------------- |
| s              | [IDiamondStorage.DiamondStorage](../../../interfaces/types/idiamondstorage#diamondstorage) | diamond storage pointer       |
| \_facetAddress | address                                                                                    | address of the facet contract |
| \_selector     | bytes4                                                                                     | function selector             |

## \_initializeDiamondCut

```solidity
function _initializeDiamondCut(address _init, bytes _calldata) internal
```

_Initializes a facet after updating the diamond using delegatecall_

### Parameters

| Name       | Type    | Description                                   |
| ---------- | ------- | --------------------------------------------- |
| \_init     | address | address of the contract to execute \_calldata |
| \_calldata | bytes   | function call (selector and arguments)        |

## \_enforceHasContractCode

```solidity
function _enforceHasContractCode(address _contract) internal view
```

_Reverts if the given address is not a contract_

### Parameters

| Name       | Type    | Description      |
| ---------- | ------- | ---------------- |
| \_contract | address | address to check |
