//SPDX-License-Identifier: AGPL-v3.0
pragma solidity ^0.8.0;
library AllocationLogger {
/**
* @param allocationUid: a uid generated by the sturdy subnet in response to your allocation request
* @param minerUid: the uid of the miner whose allocation you use
* @param userAddress: the address of the eoa account/contract which holds the assets that need to be allocated
* @param allocatedPools: the addresses of the underlying "pools" in which the assets will be distributed across
* @param allocationAmounts: the amounts allocated across the aforementioned pools (in the same order)
*/
event AllocationEvent(
bytes32 indexed allocationUid,
uint256 indexed minerUid,
address indexed userAddress,
address[] allocatedPools,
uint256[] allocationAmounts
);
error MismatchedArrays();
function logAllocation(
bytes32 allocationUid,
uint256 minerUid,
address userAddress,
address[] memory allocatedPools,
uint256[] memory allocationAmounts
) internal {
if(!(allocatedPools.length == allocationAmounts.length)) {
revert MismatchedArrays();
}
// Emit the event
emit AllocationEvent(allocationUid, minerUid, userAddress, allocatedPools, allocationAmounts);
}
}
interface IDebtManager {
struct StrategyAllocation {
address strategy;
uint256 debt;
}
function manualAllocation( StrategyAllocation[] memory _newPositions) external payable;
}
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor() {
_transferOwnership(_msgSender());
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby disabling any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
contract SturdyAllocator is Ownable {
mapping(address => bool) allocators;
constructor() {
allocators[msg.sender] = true;
}
function setAllocator(address allocator, bool value) public onlyOwner {
allocators[allocator] = value;
}
function _isAllocator(address toCheck) public view returns (bool) {
return allocators[toCheck];
}
modifier onlyAllocator() {
require(_isAllocator(msg.sender));
_;
}
function allocate(
bytes32 allocationUid,
uint256 minerUid,
address userAddress,
address debtManager,
address[] memory silos, // NOTE: the silos must be available to the addresses pointed to by DebtManager(debtManager).vault()
uint256[] memory allocationAmounts
) public onlyAllocator {
if (!(silos.length == allocationAmounts.length)) {
revert AllocationLogger.MismatchedArrays();
}
// rebalance pools
IDebtManager.StrategyAllocation[]
memory allocs = new IDebtManager.StrategyAllocation[](silos.length);
for (uint256 i = 0; i < silos.length; i++) {
allocs[i] = IDebtManager.StrategyAllocation(
silos[i],
allocationAmounts[i]
);
}
IDebtManager(debtManager).manualAllocation(allocs);
// Emit the event
// AllocationLogger.logAllocation(allocationUid, minerUid, userAddress, silos, allocationAmounts);
emit AllocationLogger.AllocationEvent(
allocationUid,
minerUid,
userAddress,
silos,
allocationAmounts
);
}
}
{
"compilationTarget": {
"SturdyAllocator.sol": "SturdyAllocator"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 1660
},
"remappings": []
}
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"MismatchedArrays","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"allocationUid","type":"bytes32"},{"indexed":true,"internalType":"uint256","name":"minerUid","type":"uint256"},{"indexed":true,"internalType":"address","name":"userAddress","type":"address"},{"indexed":false,"internalType":"address[]","name":"allocatedPools","type":"address[]"},{"indexed":false,"internalType":"uint256[]","name":"allocationAmounts","type":"uint256[]"}],"name":"AllocationEvent","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"inputs":[{"internalType":"address","name":"toCheck","type":"address"}],"name":"_isAllocator","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"allocationUid","type":"bytes32"},{"internalType":"uint256","name":"minerUid","type":"uint256"},{"internalType":"address","name":"userAddress","type":"address"},{"internalType":"address","name":"debtManager","type":"address"},{"internalType":"address[]","name":"silos","type":"address[]"},{"internalType":"uint256[]","name":"allocationAmounts","type":"uint256[]"}],"name":"allocate","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"allocator","type":"address"},{"internalType":"bool","name":"value","type":"bool"}],"name":"setAllocator","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"}]