# MezzMigrator

[Git Source](https://github.com/EntreDevelopers-Lab-Inc/Mezz-Companies/blob/f7a3e84e3dd5bb33c4bd7f77283983f9e8ba20b2/src/MezzMigrator.sol)

**Inherits:** ReentrancyGuardUpgradeable, HubOwnableUUPSUpgradeable, IMezzMigrator

**Author:** Daniel Yamagata & Naveen Ailawadi

The Mezz Migrator is responsible for upgrading core contracts to newer versions, resetting the latest versions to patched implementations, and providing the address of the latest version of an implementation

*The data used for a reinitialization function is validated such that a team cannot manage its owners, modules, or guard during an upgrade*

## State Variables

### MezzMigratorStorageLocation

```solidity
bytes32 private constant MezzMigratorStorageLocation =
    0x14b40c2177fe6203daba99a4176ffdb237b2208d924b24ad9173fc87b13b6a00;
```

## Functions

### \_getMezzMigratorStorage

```solidity
function _getMezzMigratorStorage() internal pure returns (MezzMigratorStorage storage $);
```

### constructor

```solidity
constructor(address _mezzHub) HubOwnableUUPSUpgradeable(_mezzHub);
```

### init

Initializer for the Mezz Migrator. Sets up the V1 implementations for the given Core IDs. Only callabe by the owner of the Mezz Hub

*Even though Mezz Migrator is not UUPSUpgradeable, an initializer is used to set up the V1 implementations This is due to the fact that implementations need to be aware of the Mezz Migrator upon construction Therefore, the Mezz Migrator is unable to use them in its own construction*

```solidity
function init(bytes32[] memory coreIds, address[] memory implementations) external initializer onlyHubOwner;
```

**Parameters**

| Name              | Type        | Description                          |
| ----------------- | ----------- | ------------------------------------ |
| `coreIds`         | `bytes32[]` | The coreIds of the implementations   |
| `implementations` | `address[]` | The addresses of the implementations |

### \_\_MezzMigrator\_init

```solidity
function __MezzMigrator_init(bytes32[] memory coreIds, address[] memory implementations) internal onlyInitializing;
```

### setNewCoreVersion

Sets a 'newImplementation' for the latest version of 'coreId'. Only callable by the Mezz Hub Owner

```solidity
function setNewCoreVersion(bytes32 coreId, address newImplementation) external onlyHubOwner;
```

**Parameters**

| Name                | Type      | Description                           |
| ------------------- | --------- | ------------------------------------- |
| `coreId`            | `bytes32` | The coreId of the implementation      |
| `newImplementation` | `address` | The address of the new implementation |

### resetLatestCoreVersion

Resets the latest version associated with the Core ID back to the prior version. Only callable by the Mezz Hub Owner

*Does not change the latest core version. Only resets the latest version's implementation for a given core ID*

```solidity
function resetLatestCoreVersion(bytes32 coreId, address newImplementation) external onlyHubOwner;
```

**Parameters**

| Name                | Type      | Description                                         |
| ------------------- | --------- | --------------------------------------------------- |
| `coreId`            | `bytes32` | The core ID to reset the latest version for         |
| `newImplementation` | `address` | The new implementation to set the latest version to |

### upgradeToNewerVersion

Upgrades the caller, who is assumed to be a contract, to a newer version

*This function is not access controlled*

```solidity
function upgradeToNewerVersion(uint256 newVersion, bytes memory data) external pausable nonReentrant;
```

### resetToPatchedLatestVersion

Resets the caller's contract to the latest version, which should be patched beforehand. Only callabe if the protocol state is paused or frozen

*The caller must be the latest version to be able to reset the latest patched version*

```solidity
function resetToPatchedLatestVersion(bytes memory data) external nonReentrant;
```

**Parameters**

| Name   | Type    | Description                      |
| ------ | ------- | -------------------------------- |
| `data` | `bytes` | Data used for the initialization |

### getLatestVersion

Returns the latest version for a given core implementation

```solidity
function getLatestVersion(bytes32 coreId) external view returns (uint256);
```

**Returns**

| Name     | Type      | Description                     |
| -------- | --------- | ------------------------------- |
| `<none>` | `uint256` | The latest version as a uint256 |

### getLatestCoreImplementation

Returns the latest implementation for a given core ID

```solidity
function getLatestCoreImplementation(bytes32 coreId) external view returns (address);
```

### getCoreImplementationByVersion

Returns the implementation for a given core ID and version

```solidity
function getCoreImplementationByVersion(bytes32 coreId, uint256 version) external view returns (address);
```

### \_setUpV1CoreIds

*Validates and sets the initial core IDs and implementations in the constructor*

```solidity
function _setUpV1CoreIds(bytes32[] memory coreIds, address[] memory implementations) internal;
```

### \_validateImplementationSupportsCredentialedInterface

*Validates that the implementation supports the 'ICredentialed' interface*

```solidity
function _validateImplementationSupportsCredentialedInterface(address implementation) internal view;
```

### \_validateLatestVersion

*Reverts if the latest version is zero*

```solidity
function _validateLatestVersion(uint256 latestVersion, bytes32 coreId) internal pure;
```

### \_validateVersion

*Reverts if 'version is zero or greater than 'latestVersion'*

```solidity
function _validateVersion(uint256 version, uint256 latestVersion) internal pure;
```

### \_validateImplementationVersion

*Validates that the implementation version is equal to the 'expectedVersion'*

```solidity
function _validateImplementationVersion(uint256 expectedVersion, address implementation) internal pure;
```

### \_validateImplementationCoreId

*Validates that the implementation core ID is equal to the passed in core ID*

```solidity
function _validateImplementationCoreId(bytes32 coreId, address implementation) internal pure;
```

## Structs

### MezzMigratorStorage

```solidity
struct MezzMigratorStorage {
    mapping(bytes32 => mapping(uint256 => address)) _implementationByCoreIdByVersion;
    mapping(bytes32 => uint256) _implementationVersion;
}
```
