Skip to main content

DebtAllocator.sol

Git Source

Author: yearn.finance

This Debt Allocator is meant to be used alongside a Yearn V3 vault to provide the needed triggers for a keeper to perform automated debt updates for the vaults strategies. Each allocator contract will serve one Vault and each strategy that should be managed by this allocator will need to be added manually by setting a targetRatio and maxRatio. The allocator aims to allocate debt between the strategies based on their set target ratios. Which are denominated in basis points and represent the percent of total assets that specific strategy should hold. The trigger will attempt to allocate up to the maxRatio when the strategy has minimumChange amount less than the targetRatio. And will pull funds from the strategy when it has minimumChange more than its maxRatio.

State Variables

MAX_BPS

uint256 internal constant MAX_BPS = 10_000;

factory

Address to get permissioned roles from.

address public immutable factory;

vault

Address of the vault this serves as allocator for.

address public vault;

minimumWait

Time to wait between debt updates in seconds.

uint256 public minimumWait;

minimumChange

The minimum amount denominated in asset that will

uint256 public minimumChange;

totalDebtRatio

Total debt ratio currently allocated in basis points.

uint256 public totalDebtRatio;

maxDebtUpdateLoss

Max loss to accept on debt updates in basis points.

uint256 public maxDebtUpdateLoss;

managers

Mapping of addresses that are allowed to update debt ratios.

mapping(address => bool) public managers;

_configs

Mapping of strategy => its config.

mapping(address => Config) internal _configs;

Functions

onlyGovernance

Make sure the caller is governance.

modifier onlyGovernance();

onlyManagers

Make sure the caller is governance or a manager.

modifier onlyManagers();

onlyKeepers

Make sure the caller is a keeper

modifier onlyKeepers();

_isGovernance

Check the Factories governance address.

function _isGovernance() internal view virtual;

_isManager

Check is either factories governance or local manager.

function _isManager() internal view virtual;

_isKeeper

Check is one of the allowed keepers.

function _isKeeper() internal view virtual;

constructor

constructor();

initialize

Initializes the debt allocator.

Should be called atomically after cloning.

function initialize(address _vault, uint256 _minimumChange) public virtual;

Parameters

NameTypeDescription
_vaultaddressAddress of the vault this allocates debt for.
_minimumChangeuint256The minimum in asset that must be moved.

update_debt

Debt update wrapper for the vault.

This can be used if a minimum time between debt updates is desired to be used for the trigger and to enforce a max loss. This contract must have the DEBT_MANAGER role assigned to them. The function signature matches the vault so no update to the call data is required. This will also run checks on losses realized during debt updates to assure decreases did not realize profits outside of the allowed range.

function update_debt(address _strategy, uint256 _targetDebt) public virtual onlyKeepers;

shouldUpdateDebt

Check if a strategy's debt should be updated.

This should be called by a keeper to decide if a strategies debt should be updated and if so by how much.

function shouldUpdateDebt(address _strategy) public view virtual returns (bool, bytes memory);

Parameters

NameTypeDescription
_strategyaddressAddress of the strategy to check.

Returns

NameTypeDescription
<none>bool. Bool representing if the debt should be updated.
<none>bytes. Calldata if true or reason if false.

increaseStrategyDebtRatio

Increase a strategies target debt ratio.

setStrategyDebtRatio functions will do all needed checks.

function increaseStrategyDebtRatio(address _strategy, uint256 _increase) external virtual;

Parameters

NameTypeDescription
_strategyaddressThe address of the strategy to increase the debt ratio for.
_increaseuint256The amount in Basis Points to increase it.

decreaseStrategyDebtRatio

Decrease a strategies target debt ratio.

function decreaseStrategyDebtRatio(address _strategy, uint256 _decrease) external virtual;

Parameters

NameTypeDescription
_strategyaddressThe address of the strategy to decrease the debt ratio for.
_decreaseuint256The amount in Basis Points to decrease it.

setStrategyDebtRatio

Sets a new target debt ratio for a strategy.

This will default to a 20% increase for max debt.

function setStrategyDebtRatio(address _strategy, uint256 _targetRatio) public virtual;

Parameters

NameTypeDescription
_strategyaddressAddress of the strategy to set.
_targetRatiouint256Amount in Basis points to allocate.

setStrategyDebtRatio

Sets a new target debt ratio for a strategy.

A minimumChange for that strategy must be set first. This is to prevent debt from being updated too frequently.

function setStrategyDebtRatio(address _strategy, uint256 _targetRatio, uint256 _maxRatio) public virtual onlyManagers;

Parameters

NameTypeDescription
_strategyaddressAddress of the strategy to set.
_targetRatiouint256Amount in Basis points to allocate.
_maxRatiouint256Max ratio to give on debt increases.

removeStrategy

Remove a strategy from this debt allocator.

Will delete the full config for the strategy

function removeStrategy(address _strategy) external virtual onlyManagers;

Parameters

NameTypeDescription
_strategyaddressAddress of the address ro remove.

setMinimumChange

Set the minimum change variable for a strategy.

This is the minimum amount of debt to be added or pulled for it to trigger an update.

function setMinimumChange(uint256 _minimumChange) external virtual onlyGovernance;

Parameters

NameTypeDescription
_minimumChangeuint256The new minimum to set for the strategy.

setMaxDebtUpdateLoss

Set the max loss in Basis points to allow on debt updates.

Withdrawing during debt updates use redeem which allows for 100% loss. This can be used to assure a loss is not realized on redeem outside the tolerance.

function setMaxDebtUpdateLoss(uint256 _maxDebtUpdateLoss) external virtual onlyGovernance;

Parameters

NameTypeDescription
_maxDebtUpdateLossuint256The max loss to accept on debt updates.

setMinimumWait

Set the minimum time to wait before re-updating a strategies debt.

This is only enforced per strategy.

function setMinimumWait(uint256 _minimumWait) external virtual onlyGovernance;

Parameters

NameTypeDescription
_minimumWaituint256The minimum time in seconds to wait.

setManager

Set if a manager can update ratios.

function setManager(address _address, bool _allowed) external virtual onlyGovernance;

Parameters

NameTypeDescription
_addressaddressThe address to set mapping for.
_allowedboolIf the address can call update_debt.

getConfig

Get a strategies full config.

Used for customizations by inheriting the contract.

function getConfig(address _strategy) public view virtual returns (Config memory);

Parameters

NameTypeDescription
_strategyaddressAddress of the strategy.

Returns

NameTypeDescription
<none>ConfigThe strategies current Config.

getStrategyTargetRatio

Get a strategies target debt ratio.

function getStrategyTargetRatio(address _strategy) external view virtual returns (uint256);

Parameters

NameTypeDescription
_strategyaddressAddress of the strategy.

Returns

NameTypeDescription
<none>uint256The strategies current targetRatio.

getStrategyMaxRatio

Get a strategies max debt ratio.

function getStrategyMaxRatio(address _strategy) external view virtual returns (uint256);

Parameters

NameTypeDescription
_strategyaddressAddress of the strategy.

Returns

NameTypeDescription
<none>uint256The strategies current maxRatio.

Events

UpdateStrategyDebtRatio

An event emitted when a strategies debt ratios are Updated.

event UpdateStrategyDebtRatio(
address indexed strategy, uint256 newTargetRatio, uint256 newMaxRatio, uint256 newTotalDebtRatio
);

StrategyChanged

An event emitted when a strategy is added or removed.

event StrategyChanged(address indexed strategy, Status status);

UpdateMinimumWait

An event emitted when the minimum time to wait is updated.

event UpdateMinimumWait(uint256 newMinimumWait);

UpdateMinimumChange

An event emitted when the minimum change is updated.

event UpdateMinimumChange(uint256 newMinimumChange);

UpdateManager

An event emitted when a keeper is added or removed.

event UpdateManager(address indexed manager, bool allowed);

UpdateMaxDebtUpdateLoss

An event emitted when the max debt update loss is updated.

event UpdateMaxDebtUpdateLoss(uint256 newMaxDebtUpdateLoss);

Structs

Config

Struct for each strategies info.

struct Config {
bool added;
uint16 targetRatio;
uint16 maxRatio;
uint96 lastUpdate;
uint120 open;
}

Enums

Status

Status when a strategy is added or removed from the allocator.

enum Status {
NULL,
ADDED,
REMOVED
}