Skip to content

Commit

Permalink
Merge pull request #11 from yearn/seperate
Browse files Browse the repository at this point in the history
feat: separate deployer and role manager
  • Loading branch information
Schlagonia authored May 14, 2024
2 parents ad8677d + cf15572 commit 29cf9cd
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 174 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

# Structure

![alt text](setup.png)
![alt text](image.png)

## [Role Manager](https://github.com/yearn/yearn-stb/blob/seperate/src/RoleManager.sol)

- Yearn governed contract to handle vault deployments and configuration

## [L1 Deployer](https://github.com/yearn/yearn-stb/blob/master/src/L1Deployer.sol)

Expand All @@ -28,11 +32,6 @@

- Allows for a Rollup Admin to update is EScrow Manager or L2 Deployer

- Holds all data for deployed vaults and escrows
- Uses 0 for the default vaults or the rollup specific ID for any custom vaults.

- Holds the Role Manager position for all vaults.


## [L2 Deployer](https://github.com/yearn/yearn-stb/blob/master/src/L2Deployer.sol)

Expand Down
Binary file added image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed setup.png
Binary file not shown.
9 changes: 4 additions & 5 deletions src/DeployerBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity ^0.8.20;
import {Positions} from "./Positions.sol";
import {Proxy} from "@zkevm-stb/Proxy.sol";
import {CREATE3} from "./libraries/CREATE3.sol";
import {IPolygonZkEVMBridge} from "./interfaces/Polygon/IPolygonZkEVMBridge.sol";

/**
* @title DeployerBase
Expand All @@ -30,15 +29,15 @@ abstract contract DeployerBase is Positions {
bytes32 public constant ESCROW_IMPLEMENTATION =
keccak256("Escrow Implementation");

/// @notice Address of the PolygonZkEVMBridge contract
IPolygonZkEVMBridge public immutable polygonZkEVMBridge;
/// @notice Address of the Bridge contract
address public immutable bridgeAddress;

Check warning on line 33 in src/DeployerBase.sol

View workflow job for this annotation

GitHub Actions / solidity

Immutable variables name are set to be in capitalized SNAKE_CASE

constructor(
address _polygonZkEVMBridge,
address _bridgeAddress,
address _l1Deployer,
address _escrowImplementation
) {
polygonZkEVMBridge = IPolygonZkEVMBridge(_polygonZkEVMBridge);
bridgeAddress = _bridgeAddress;
_setPositionHolder(L1_DEPLOYER, _l1Deployer);
_setPositionHolder(ESCROW_IMPLEMENTATION, _escrowImplementation);
}
Expand Down
64 changes: 33 additions & 31 deletions src/L1Deployer.sol
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.20;

Check warning on line 2 in src/L1Deployer.sol

View workflow job for this annotation

GitHub Actions / solidity

Compiler version ^0.8.20 does not satisfy the 0.8.18 semver requirement

import {Proxy} from "@zkevm-stb/Proxy.sol";
import {RoleManager} from "./RoleManager.sol";
import {DeployerBase} from "./DeployerBase.sol";
import {L1YearnEscrow} from "./L1YearnEscrow.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {IPolygonZkEVMBridge} from "./interfaces/Polygon/IPolygonZkEVMBridge.sol";
import {IPolygonRollupManager, IPolygonRollupContract} from "./interfaces/Polygon/IPolygonRollupManager.sol";

/// @title Polygon CDK Stake the Bridge L1 Deployer.
contract L1Deployer is RoleManager {
contract L1Deployer is DeployerBase {
/// @notice Revert message for when a contract has already been deployed.
error AlreadyDeployed(address _contract);

event RegisteredNewRollup(
uint32 indexed rollupID,
address indexed rollupContract,
Expand Down Expand Up @@ -61,6 +65,10 @@ contract L1Deployer is RoleManager {
IMMUTABLE'S
//////////////////////////////////////////////////////////////*/

/// @notice Yearn STB Role Manager.
RoleManager public immutable roleManager;

Check warning on line 69 in src/L1Deployer.sol

View workflow job for this annotation

GitHub Actions / solidity

Immutable variables name are set to be in capitalized SNAKE_CASE

/// @notice Polygon CDK Rollup Manager.
IPolygonRollupManager public immutable rollupManager;

Check warning on line 72 in src/L1Deployer.sol

View workflow job for this annotation

GitHub Actions / solidity

Immutable variables name are set to be in capitalized SNAKE_CASE

/*//////////////////////////////////////////////////////////////
Expand All @@ -71,33 +79,29 @@ contract L1Deployer is RoleManager {
mapping(uint32 => ChainConfig) internal _chainConfig;

constructor(
address _governator,
address _czar,
address _management,
address _emergencyAdmin,
address _keeper,
address _registry,
address _allocatorFactory,
address _polygonZkEVMBridge,
address _escrowImplementation
address _bridgeAddress,
address _roleManager
)
RoleManager(
_governator,
_czar,
_management,
_emergencyAdmin,
_keeper,
_registry,
_allocatorFactory,
_polygonZkEVMBridge,
_escrowImplementation
DeployerBase(
_bridgeAddress,
address(this),
address(new L1YearnEscrow())
)
{
roleManager = RoleManager(_roleManager);

rollupManager = IPolygonRollupManager(
polygonZkEVMBridge.polygonRollupManager()
IPolygonZkEVMBridge(bridgeAddress).polygonRollupManager()
);
}

/**
* @notice Get the name of this contract.
*/
function name() external view virtual returns (string memory) {
return "L1 Stake the Bridge Deployer";
}

/*//////////////////////////////////////////////////////////////
ESCROW CREATION
//////////////////////////////////////////////////////////////*/
Expand Down Expand Up @@ -131,11 +135,11 @@ contract L1Deployer is RoleManager {
if (_l1Escrow != address(0)) revert AlreadyDeployed(_l1Escrow);

// Check if there is a current default vault.
_vault = getVault(_asset);
_vault = roleManager.getVault(_asset);

// If not, deploy one and do full setup
if (_vault == address(0)) {
_vault = _newVault(ORIGIN_NETWORK_ID, _asset);
_vault = roleManager.newVault(ORIGIN_NETWORK_ID, _asset);
}

// Deploy L1 Escrow.
Expand Down Expand Up @@ -228,7 +232,7 @@ contract L1Deployer is RoleManager {
onlyRollupAdmin(_rollupID)
returns (address _l1Escrow, address _vault)
{
_vault = _newVault(_rollupID, _asset);
_vault = roleManager.newVault(_rollupID, _asset);
_l1Escrow = _newCustomVault(_rollupID, _asset, _vault);
}

Expand All @@ -244,10 +248,8 @@ contract L1Deployer is RoleManager {
address _asset,
address _vault
) external virtual onlyRollupAdmin(_rollupID) returns (address _l1Escrow) {
// If the vault has not been registered yet.
if (!isVaultsRoleManager(_vault)) {
_addNewVault(_rollupID, _vault);
}
// Make sure the vault has been registered.
require(roleManager.isVaultsRoleManager(_vault), "!role manager");
_l1Escrow = _newCustomVault(_rollupID, _asset, _vault);
}

Expand Down Expand Up @@ -288,7 +290,7 @@ contract L1Deployer is RoleManager {
(
chainConfig_.rollupContract.admin(),
chainConfig_.escrowManager,
address(polygonZkEVMBridge),
bridgeAddress,
getL2EscrowAddress(_rollupID, _asset),
_rollupID,
_asset,
Expand All @@ -314,7 +316,7 @@ contract L1Deployer is RoleManager {
chainConfig_.escrows[_asset] = _l1Escrow;

// Send Message to Bridge for L2
polygonZkEVMBridge.bridgeMessage(
IPolygonZkEVMBridge(bridgeAddress).bridgeMessage(
_rollupID,
chainConfig_.l2Deployer,
true,
Expand Down
4 changes: 2 additions & 2 deletions src/L2Deployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ contract L2Deployer is DeployerBase {
) external payable {
// Can only be called by the bridge
require(
address(polygonZkEVMBridge) == msg.sender,
bridgeAddress == msg.sender,
"L2Deployer: Not PolygonZkEVMBridge"
);
require(
Expand Down Expand Up @@ -198,7 +198,7 @@ contract L2Deployer is DeployerBase {
L2Escrow.initialize,
(
getPositionHolder(L2_ADMIN),
address(polygonZkEVMBridge),
bridgeAddress,
_l1Escrow,
ORIGIN_NETWORK_ID,
_l1Token,
Expand Down
118 changes: 35 additions & 83 deletions src/RoleManager.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.20;

import {DeployerBase} from "./DeployerBase.sol";
import {Positions} from "./Positions.sol";
import {Roles} from "@yearn-vaults/interfaces/Roles.sol";
import {IVault} from "@yearn-vaults/interfaces/IVault.sol";
import {IAccountant} from "./interfaces/Yearn/IAccountant.sol";
Expand All @@ -11,7 +11,7 @@ import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {DebtAllocatorFactory} from "@vault-periphery/debtAllocators/DebtAllocatorFactory.sol";

/// @title Yearn Stake the Bridge Role Manager.
abstract contract RoleManager is DeployerBase {
contract RoleManager is Positions {
/// @notice Revert message for when a contract has already been deployed.
error AlreadyDeployed(address _contract);

Expand Down Expand Up @@ -42,6 +42,9 @@ abstract contract RoleManager is DeployerBase {
uint96 index;
}

/// @notice ID to use for the L1
uint32 internal constant ORIGIN_NETWORK_ID = 0;

/*//////////////////////////////////////////////////////////////
POSITION ID'S
//////////////////////////////////////////////////////////////*/
Expand Down Expand Up @@ -97,10 +100,8 @@ abstract contract RoleManager is DeployerBase {
address _emergencyAdmin,
address _keeper,
address _registry,
address _allocatorFactory,
address _polygonZkEVMBridge,
address _escrowImplementation
) DeployerBase(_polygonZkEVMBridge, address(this), _escrowImplementation) {
address _allocatorFactory
) {
chad = _governator;

// Governator gets no roles.
Expand Down Expand Up @@ -144,6 +145,33 @@ abstract contract RoleManager is DeployerBase {
VAULT CREATION
//////////////////////////////////////////////////////////////*/

/**
* @notice Deploys a new vault to the RoleManager for the default version.
* @dev This will override any existing default vault to use a new API version.
* @param _asset Address of the asset to be used.
* @return _vault Address of the new vault
*/
function newDefaultVault(
address _asset
) external virtual onlyPositionHolder(GOVERNATOR) returns (address _vault) {
_vault = _newVault(ORIGIN_NETWORK_ID, _asset);
}

/**
* @notice Permissionless creation of a new endorsed vault.
* @param _rollupID Id of the rollup to deploy for.
* @param _asset Address of the underlying asset.
* @return _vault Address of the newly created vault.
*/
function newVault(
uint32 _rollupID,
address _asset
) external virtual returns (address _vault) {
_vault = getVault(_asset, _rollupID);
if (_vault != address(0)) revert AlreadyDeployed(_vault);
_vault = _newVault(_rollupID, _asset);
}

/**
* @notice Creates a new endorsed vault.
* @param _rollupID Id of the rollup to deploy for.
Expand Down Expand Up @@ -329,82 +357,6 @@ abstract contract RoleManager is DeployerBase {
VAULT MANAGEMENT
//////////////////////////////////////////////////////////////*/

/**
* @notice Deploys a new vault to the RoleManager for the default version.
* @dev This will override any existing default vault.
* @param _asset Address of the asset to be used.
*/
function newDefaultVault(
address _asset
) external virtual onlyPositionHolder(GOVERNATOR) {
_newVault(ORIGIN_NETWORK_ID, _asset);
}

/**
* @notice Adds a new vault to the RoleManager for the default version.
* @dev This will override any existing default vault.
* @param _vault Address of the vault to be added.
*/
function addNewVault(
address _vault
) external virtual onlyPositionHolder(GOVERNATOR) {
_addNewVault(ORIGIN_NETWORK_ID, _vault);
}

/**
* @notice Adds a new vault to the RoleManager with the specified category and debt allocator.
* @dev If not already endorsed this function will endorse the vault.
* @param _rollupID rollupID for the vault to use.
* @param _vault Address of the vault to be added.
*/
function _addNewVault(uint32 _rollupID, address _vault) internal virtual {
// If not the current role manager.
if (IVault(_vault).role_manager() != address(this)) {
// Accept the position of role manager.
IVault(_vault).accept_role_manager();
}

// Deploy a new Debt Allocator.
address _debtAllocator = _deployAllocator(_vault);

// Get the current registry.
address registry = getPositionHolder(REGISTRY);

// Check if the vault has been endorsed yet in the registry.
if (!Registry(registry).isEndorsed(_vault)) {
// If not endorse it.
// NOTE: This will revert if adding a vault of an older version.
Registry(registry).endorseMultiStrategyVault(_vault);
}

// Set the roles up.
_sanctify(_vault, _debtAllocator);

// Only set an accountant if there is not one set yet.
if (IVault(_vault).accountant() == address(0)) {
_setAccountant(_vault);
}

address _asset = IVault(_vault).asset();

// Add the vault config to the mapping.
vaultConfig[_vault] = VaultConfig({
asset: _asset,
rollupID: _rollupID,
debtAllocator: _debtAllocator,
index: uint96(vaults.length)
});

// Add the vault to the mapping.
_assetToVault[_asset][_rollupID] = _vault;

// Add the vault to the array.
vaults.push(_vault);

// Emit event.
emit AddedNewVault(_vault, _debtAllocator, _rollupID);
}

/**
* @notice Update a `_vault`s debt allocator.
* @dev This will deploy a new allocator using the current
Expand Down Expand Up @@ -599,7 +551,7 @@ abstract contract RoleManager is DeployerBase {
* @notice Get the name of this contract.
*/
function name() external view virtual returns (string memory) {
return string(abi.encodePacked("L1 Stake the Bridge Deployer"));
return string(abi.encodePacked("Stake the Bridge Role Manager"));
}

/**
Expand Down
Loading

0 comments on commit 29cf9cd

Please sign in to comment.