Skip to content

Commit

Permalink
build: deployer
Browse files Browse the repository at this point in the history
  • Loading branch information
Schlagonia committed Sep 5, 2024
1 parent 444219c commit 4d49917
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 66 deletions.
54 changes: 24 additions & 30 deletions contracts/Managers/Deployer.sol
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
// SPDX-License-Identifier: AGPL-3.0
pragma solidity >=0.8.18;

import {Positions} from "./Positions.sol";
import {RoleManager} from "./RoleManager.sol";
import {Registry, RegistryFactory} from "../registry/RegistryFactory.sol";
import {Roles} from "@yearn-vaults/interfaces/Roles.sol";
import {IVault} from "@yearn-vaults/interfaces/IVault.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {IVaultFactory} from "@yearn-vaults/interfaces/IVaultFactory.sol";
import {ReleaseRegistry} from "../registry/ReleaseRegistry.sol";
import {AccountantFactory} from "../accountants/AccountantFactory.sol";
import {Registry, RegistryFactory} from "../registry/RegistryFactory.sol";
import {AccountantFactory, Accountant} from "../accountants/AccountantFactory.sol";
import {DebtAllocatorFactory} from "../debtAllocators/DebtAllocatorFactory.sol";
import {IProtocolAddressProvider} from "../interfaces/IProtocolAddressProvider.sol";

// TODO:
// 1. Initiate new "project"
// 2. Can deploy using project id

contract V3Deployer is Positions {
contract V3Deployer {
event NewProject(bytes32 indexed projectId, address indexed roleManager);

struct Project {
address roleManager;
address registry;
address roleManager;
address accountant;
address debtAllocator;
}
Expand All @@ -46,58 +41,54 @@ contract V3Deployer is Positions {

address public immutable protocolAddressProvider;

/// @notice Default time until profits are fully unlocked for new vaults.
uint256 public defaultProfitMaxUnlock = 7 days;

mapping(bytes32 => Project) public projects;

constructor(address _addressProvider) {
protocolAddressProvider = _addressProvider;

// SEt keeper and debt allocator Roles
}

function getLatestFactory() public view returns (address) {
return
ReleaseRegistry(_fromAddressProvider(RELEASE_REGISTRY))
.latestFactory();
}

function _fromAddressProvider(bytes32 _id) internal view returns (address) {
return
IProtocolAddressProvider(protocolAddressProvider).getAddress(_id);
}

function newProject(address _governance) external virtual {
function newProject(
address _governance,
address _management
) external virtual returns (address _roleManager) {
bytes32 _id = getProjectId(_governance);
require(projects[_id].roleManager == address(0), "project exists");

// Deploy new Registry
address _registry = RegistryFactory(
_fromAddressProvider(REGISTRY_FACTORY)
).createNewRegistry(
string(abi.encodePacked(_prefix, " Vault Registry"))
);
).createNewRegistry(string(abi.encodePacked(" Vault Registry")));

address _accountant = AccountantFactory(
_fromAddressProvider(ACCOUNTANT_FACTORY)
).newAccountant(_roleManager, _roleManager);
).newAccountant(address(this), _roleManager);

address _debtAllocator = DebtAllocatorFactory(
_fromAddressProvider(ALLOCATOR_FACTORY)
).newDebtAllocator(_roleManager);
).newDebtAllocator(_management);

address _roleManager = address(
_roleManager = address(
new RoleManager(
_governance,
_governance,
_management,
_fromAddressProvider(KEEPER),
_registry,
_accountant,
_debtAllocator
)
);

Registry(_registry).setEndorser(_roleManager, true);
Registry(_registry).transferGovernance(_governance);

Accountant(_accountant).setVaultManager(_roleManager);
Accountant(_accountant).setFutureFeeManager(_roleManager);

projects[_id] = Project({
roleManager: _roleManager,
registry: _registry,
Expand All @@ -106,12 +97,15 @@ contract V3Deployer is Positions {
});

// Event
emit NewProject(_id, _roleManager);
}

function getProjectId(
address _governance
) public view virtual returns (bytes32) {
return
keccak256(abi.encodePacked(_governance, chain.id, block.timestamp));
keccak256(
abi.encodePacked(_governance, block.chainid, block.timestamp)
);
}
}
44 changes: 8 additions & 36 deletions contracts/Managers/RoleManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,9 @@ contract RoleManager is Positions {
) {
chad = _governance;

// Governance gets no roles.
// Governance gets all the roles.
_setPositionHolder(GOVERNANCE, _governance);

// Czar gets all of the Roles.
_setPositionHolder(MANAGEMENT, _management);
_setPositionRoles(MANAGEMENT, Roles.ALL);
_setPositionRoles(GOVERNANCE, Roles.ALL);

// Management reports, can update debt, queue, deposit limits and unlock time.
_setPositionHolder(MANAGEMENT, _management);
Expand All @@ -120,14 +117,14 @@ contract RoleManager is Positions {
_setPositionRoles(KEEPER, Roles.REPORTING_MANAGER);

// Debt allocators manage debt and also need to process reports.
_setPositionHolder(DEBT_ALLOCATOR, _debtAllocator);
_setPositionRoles(
DEBT_ALLOCATOR,
Roles.REPORTING_MANAGER | Roles.DEBT_MANAGER
);

_setPositionHolder(REGISTRY, _registry);
_setPositionHolder(ACCOUNTANT, _accountant);
_setPositionHolder(ALLOCATOR_FACTORY, _allocatorFactory);
}

/*//////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -229,9 +226,7 @@ contract RoleManager is Positions {
_assetToVault[_asset][_apiVersion][_category]
);

// Deploy a new debt allocator for the vault.
address _debtAllocator = _deployAllocator(_vault);

address _debtAllocator = getPositionHolder(DEBT_ALLOCATOR);
// Give out roles on the new vault.
_sanctify(_vault, _debtAllocator);

Expand Down Expand Up @@ -260,28 +255,6 @@ contract RoleManager is Positions {
emit AddedNewVault(_vault, _debtAllocator, _category);
}

/**
* @dev Deploys a debt allocator for the specified vault.
* @param _vault Address of the vault.
* @return _debtAllocator Address of the deployed debt allocator.
*/
function _deployAllocator(
address _vault
) internal virtual returns (address _debtAllocator) {
address factory = getPositionHolder(ALLOCATOR_FACTORY);

// If we have a factory set.
if (factory != address(0)) {
// Deploy a new debt allocator for the vault with Management as the gov.
_debtAllocator = DebtAllocatorFactory(factory).newDebtAllocator(
_vault
);
} else {
// If no factory is set we should be using one central allocator.
_debtAllocator = getPositionHolder(DEBT_ALLOCATOR);
}
}

/**
* @dev Assigns roles to the newly added vault.
*
Expand Down Expand Up @@ -387,8 +360,7 @@ contract RoleManager is Positions {
* @param _category Category associated with the vault.
*/
function addNewVault(address _vault, uint256 _category) external virtual {
address _debtAllocator = _deployAllocator(_vault);
addNewVault(_vault, _category, _debtAllocator);
addNewVault(_vault, _category, getPositionHolder(DEBT_ALLOCATOR));
}

/**
Expand Down Expand Up @@ -462,7 +434,7 @@ contract RoleManager is Positions {
function updateDebtAllocator(
address _vault
) external virtual returns (address _newDebtAllocator) {
_newDebtAllocator = _deployAllocator(_vault);
_newDebtAllocator = getPositionHolder(DEBT_ALLOCATOR);
updateDebtAllocator(_vault, _newDebtAllocator);
}

Expand Down Expand Up @@ -566,7 +538,7 @@ contract RoleManager is Positions {
address[] calldata _vaults,
address _holder,
uint256 _role
) external virtual onlyGovernance {
) external virtual onlyPositionHolder(GOVERNANCE) {
address _vault;
for (uint256 i = 0; i < _vaults.length; ++i) {
_vault = _vaults[i];
Expand Down Expand Up @@ -620,7 +592,7 @@ contract RoleManager is Positions {
*/
function setDefaultProfitMaxUnlock(
uint256 _newDefaultProfitMaxUnlock
) external virtual onlyGovernance {
) external virtual onlyPositionHolder(GOVERNANCE) {
defaultProfitMaxUnlock = _newDefaultProfitMaxUnlock;

emit UpdateDefaultProfitMaxUnlock(_newDefaultProfitMaxUnlock);
Expand Down

0 comments on commit 4d49917

Please sign in to comment.