Skip to content

Commit

Permalink
feat: doa multicall support
Browse files Browse the repository at this point in the history
  • Loading branch information
fp-crypto committed Jun 20, 2024
1 parent 03f3cdc commit e509743
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 10 deletions.
25 changes: 19 additions & 6 deletions contracts/debtAllocators/DebtOptimizerApplicator.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
// SPDX-License-Identifier: GNU AGPLv3
pragma solidity >=0.8.18;

import {DebtAllocator, DebtAllocatorFactory} from "./DebtAllocator.sol";
import {Multicall} from "@openzeppelin/contracts/utils/Multicall.sol";
import {IDebtAllocatorFactory} from "./DebtAllocator.sol";

contract DebtOptimizerApplicator {
interface IDebtAllocator {
function setStrategyDebtRatio(
address _strategy,
uint256 _targetRatio
) external;
function setStrategyDebtRatio(
address _strategy,
uint256 _targetRatio,
uint256 _maxRatio
) external;
}

contract DebtOptimizerApplicator is Multicall {
/// @notice An event emitted when a keeper is added or removed.
event UpdateManager(address indexed manager, bool allowed);

Expand All @@ -30,7 +43,7 @@ contract DebtOptimizerApplicator {
function _isGovernance() internal view virtual {
require(
msg.sender ==
DebtAllocatorFactory(debtAllocatorFactory).governance(),
IDebtAllocatorFactory(debtAllocatorFactory).governance(),
"!governance"
);
}
Expand All @@ -40,7 +53,7 @@ contract DebtOptimizerApplicator {
require(
managers[msg.sender] ||
msg.sender ==
DebtAllocatorFactory(debtAllocatorFactory).governance(),
IDebtAllocatorFactory(debtAllocatorFactory).governance(),
"!manager"
);
}
Expand Down Expand Up @@ -75,12 +88,12 @@ contract DebtOptimizerApplicator {
) public onlyManagers {
for (uint8 i; i < _strategyDebtRatios.length; ++i) {
if (_strategyDebtRatios[i].maxRatio == 0) {
DebtAllocator(_debtAllocator).setStrategyDebtRatio(
IDebtAllocator(_debtAllocator).setStrategyDebtRatio(
_strategyDebtRatios[i].strategy,
_strategyDebtRatios[i].targetRatio
);
} else {
DebtAllocator(_debtAllocator).setStrategyDebtRatio(
IDebtAllocator(_debtAllocator).setStrategyDebtRatio(
_strategyDebtRatios[i].strategy,
_strategyDebtRatios[i].targetRatio,
_strategyDebtRatios[i].maxRatio
Expand Down
14 changes: 10 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,13 +404,19 @@ def debt_allocator_factory(deploy_debt_allocator_factory):


@pytest.fixture(scope="session")
def debt_allocator(debt_allocator_factory, project, vault, daddy):
tx = debt_allocator_factory.newDebtAllocator(vault, sender=daddy)
def create_debt_allocator(debt_allocator_factory, daddy):
def create_debt_allocator(vault):
tx = debt_allocator_factory.newDebtAllocator(vault, sender=daddy)
event = list(tx.decode_logs(debt_allocator_factory.NewDebtAllocator))[0]
debt_allocator = project.DebtAllocator.at(event.allocator)
return debt_allocator

event = list(tx.decode_logs(debt_allocator_factory.NewDebtAllocator))[0]
yield create_debt_allocator

debt_allocator = project.DebtAllocator.at(event.allocator)

@pytest.fixture(scope="session")
def debt_allocator(create_debt_allocator, vault):
debt_allocator = create_debt_allocator(vault)
yield debt_allocator


Expand Down
59 changes: 59 additions & 0 deletions tests/debtAllocators/test_debt_optimizer_applicator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ape
from ape import chain, project
from utils.constants import MAX_INT, ROLES
import itertools


def test_setup(debt_optimizer_applicator, debt_allocator_factory, brain):
Expand Down Expand Up @@ -111,3 +112,61 @@ def test_set_ratios(
assert debt_allocator.totalDebtRatio() == 10_000
assert debt_allocator.getConfig(strategy) == (True, 8_000, 9_000, 0, 0)
assert debt_allocator.getConfig(new_strategy) == (True, 2_000, 2_000 * 1.2, 0, 0)


def test_set_ratios_multicall(
debt_optimizer_applicator,
debt_allocator,
brain,
daddy,
asset,
create_vault,
create_strategy,
create_debt_allocator,
user,
):
debt_allocators: dict[str, list[str]] = {}
for _ in range(2):
vault = create_vault(asset)
debt_allocator = create_debt_allocator(vault)
debt_allocator.setManager(debt_optimizer_applicator, True, sender=brain)
debt_allocator.setMinimumChange(1, sender=brain)
debt_allocators[debt_allocator.address] = []
for _ in range(2):
debt_allocators[debt_allocator.address].append(create_strategy(asset))

calldata = [
debt_optimizer_applicator.setStrategyDebtRatios.encode_input(
allocator, [(strategy, int(5_000), int(0)) for strategy in strategies]
)
for allocator, strategies in debt_allocators.items()
]

with ape.reverts("!manager"):
debt_optimizer_applicator.multicall(calldata, sender=user)

tx = debt_optimizer_applicator.multicall(
calldata,
sender=brain,
)

events = list(tx.decode_logs(debt_allocator.UpdateStrategyDebtRatio))
strategies = list(itertools.chain(*debt_allocators.values()))

assert len(events) == 4
for event in events:
assert event.strategy in strategies
assert event.newTargetRatio == 5_000
assert event.newMaxRatio == int(5_000 * 1.2)

for debt_allocator_addr, strategies in debt_allocators.items():
debt_allocator = project.DebtAllocator.at(debt_allocator_addr)
assert debt_allocator.totalDebtRatio() == 10_000
for strategy in strategies:
assert debt_allocator.getConfig(strategy) == (
True,
5_000,
5_000 * 1.2,
0,
0,
)

0 comments on commit e509743

Please sign in to comment.