Skip to content

Commit

Permalink
test: update for factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Schlagonia committed Jan 8, 2024
1 parent cee748e commit 92b7ea8
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 265 deletions.
54 changes: 27 additions & 27 deletions contracts/debtAllocators/GenericDebtAllocatorFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity 0.8.18;

import {Clonable} from "@periphery/utils/Clonable.sol";
import {RoleManager} from "../Managers/RoleManager.sol";
import {Governance} from "@periphery/utils/Governance.sol";
import {GenericDebtAllocator} from "./GenericDebtAllocator.sol";

Expand All @@ -16,12 +17,15 @@ contract GenericDebtAllocatorFactory is Governance, Clonable {
/// @notice Revert message for when a debt allocator already exists.
error AlreadyDeployed(address _allocator);

/// @notice An event emitted when a new debt allocator is added or deployed.
event NewDebtAllocator(address indexed allocator, address indexed vault);
/// @notice An even emitted when a new `roleManager` is set.
event UpdateRoleManager(address indexed roleManager);

/// @notice An event emitted when a keeper is added or removed.
event UpdateKeeper(address indexed keeper, bool allowed);

/// @notice An event emitted when a new debt allocator is added or deployed.
event NewDebtAllocator(address indexed allocator, address indexed vault);

/// @notice Only allow `governance` or the `roleManager`.
modifier onlyAuthorized() {
_isAuthorized();
Expand All @@ -42,19 +46,20 @@ contract GenericDebtAllocatorFactory is Governance, Clonable {
/// @notice Mapping of addresses that are allowed to update debt.
mapping(address => bool) public keepers;

/// @notice Mapping of vault => allocator.
mapping(address => address) public allocators;

constructor(
address _governance,
address _roleManager
) Governance(_governance) {
// Deploy a dummy allocator as the original.
original = address(new GenericDebtAllocator(address(1), 0));

// Set the initial role manager.
roleManager = _roleManager;
emit UpdateRoleManager(_roleManager);

// Default to allow governance to be a keeper.
keepers[_governance] = true;
emit UpdateKeeper(_governance, true);
}

/**
Expand All @@ -80,20 +85,13 @@ contract GenericDebtAllocatorFactory is Governance, Clonable {
function newGenericDebtAllocator(
address _vault,
uint256 _minimumChange
) public virtual onlyAuthorized returns (address newAllocator) {
// Make sure their is not already an allocator deployed for the vault.
if (allocators[_vault] != address(0))
revert AlreadyDeployed(allocators[_vault]);

) public virtual returns (address newAllocator) {
// Clone new allocator off the original.
newAllocator = _clone();

// Initialize the new allocator.
GenericDebtAllocator(newAllocator).initialize(_vault, _minimumChange);

// Add it to the
allocators[_vault] = newAllocator;

// Emit event.
emit NewDebtAllocator(newAllocator, _vault);
}
Expand All @@ -113,26 +111,28 @@ contract GenericDebtAllocatorFactory is Governance, Clonable {
address _strategy
) public view virtual returns (bool, bytes memory) {
return
GenericDebtAllocator(allocators[_vault]).shouldUpdateDebt(
_strategy
);
GenericDebtAllocator(allocator(_vault)).shouldUpdateDebt(_strategy);
}

/**
* @notice Set a specific allocator for a specific vault.
* @dev This will override any previously deployed versions and should
* be done with care.
*
* @param _vault Address of the vault
* @param _allocator Address of the debtAllocator.
* @notice Helper function to easily get a vaults debt allocator from the role manager.
* @param _vault Address of the vault to get the allocator for.
* @return Address of the vaults debt allocator if one exists.
*/
function setAllocator(
address _vault,
address _allocator
function allocator(address _vault) public view virtual returns (address) {
return RoleManager(roleManager).getDebtAllocator(_vault);
}

/**
* @notice Update the Role Manager address.
* @param _roleManager New role manager address.
*/
function setRoleManager(
address _roleManager
) external virtual onlyGovernance {
allocators[_vault] = _allocator;
roleManager = _roleManager;

emit NewDebtAllocator(_allocator, _vault);
emit UpdateRoleManager(_roleManager);
}

/**
Expand Down
14 changes: 8 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
from ape import accounts, project, networks
from utils.constants import MAX_INT, WEEK, ROLES
from utils.constants import MAX_INT, WEEK, ROLES, ZERO_ADDRESS
from web3 import Web3, HTTPProvider
from hexbytes import HexBytes
import os
Expand Down Expand Up @@ -395,9 +395,11 @@ def address_provider(deploy_address_provider):


@pytest.fixture(scope="session")
def deploy_generic_debt_allocator_factory(project, daddy):
def deploy_generic_debt_allocator_factory(project, daddy, brain):
def deploy_generic_debt_allocator_factory(gov=daddy):
generic_debt_allocator_factory = gov.deploy(project.GenericDebtAllocatorFactory)
generic_debt_allocator_factory = gov.deploy(
project.GenericDebtAllocatorFactory, brain, ZERO_ADDRESS
)

return generic_debt_allocator_factory

Expand All @@ -413,9 +415,7 @@ def generic_debt_allocator_factory(deploy_generic_debt_allocator_factory):

@pytest.fixture(scope="session")
def generic_debt_allocator(generic_debt_allocator_factory, project, vault, daddy):
tx = generic_debt_allocator_factory.newGenericDebtAllocator(
vault, daddy, sender=daddy
)
tx = generic_debt_allocator_factory.newGenericDebtAllocator(vault, sender=daddy)

event = list(tx.decode_logs(generic_debt_allocator_factory.NewDebtAllocator))[0]

Expand Down Expand Up @@ -446,6 +446,7 @@ def deploy_role_manager(
def role_manager(
deploy_role_manager,
daddy,
brain,
healthcheck_accountant,
generic_debt_allocator_factory,
registry,
Expand All @@ -459,6 +460,7 @@ def role_manager(
role_manager.setPositionHolder(
role_manager.ALLOCATOR_FACTORY(), generic_debt_allocator_factory, sender=daddy
)
generic_debt_allocator_factory.setRoleManager(role_manager, sender=brain)

return role_manager

Expand Down
Loading

0 comments on commit 92b7ea8

Please sign in to comment.