Skip to content

Commit

Permalink
Improve the Dispatcher contact
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuba committed Dec 14, 2023
1 parent 2c08d1d commit a815d10
Showing 1 changed file with 68 additions and 62 deletions.
130 changes: 68 additions & 62 deletions core/contracts/Dispatcher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ contract Dispatcher is Router, Ownable {
Acre public acre;
IERC20 public tbtc;

uint16 public vaultsTotalWeight = 1000;

/// @notice Authorized Yield Vaults that implement ERC4626 standard. These
/// vaults deposit assets to yield strategies, e.g. Uniswap V3
/// WBTC/TBTC pool. Vault can be a part of Acre ecosystem or can be
Expand All @@ -46,6 +44,12 @@ contract Dispatcher is Router, Ownable {
IVault[] public vaults;
mapping(IVault => VaultInfo) public vaultsInfo;

/// @dev Vaults weights defined in VaultInfo structure should sum up to
/// vaultsTotalWeight. If vaults weight defined in the VaultInfo
/// is less than vaultsTotalWeight the rest of funds will be kept
/// as a buffer in the Acre contract.
uint16 public vaultsTotalWeight = 1000;

event VaultAuthorized(address indexed vault);
event VaultDeauthorized(address indexed vault);
event VaultWeightUpdated(
Expand Down Expand Up @@ -120,66 +124,6 @@ contract Dispatcher is Router, Ownable {
return vaults;
}

function sharesOwner() public returns (address) {
return address(this);
}

// TODO: Add access restriction
function depositToVault(
IVault vault,
uint256 amount,
uint256 minSharesOut
) public returns (uint256 sharesOut) {
if (!isVaultAuthorized(vault)) {
revert VaultUnauthorized();
}

require(vault.asset() == address(tbtc), "vault asset is not tbtc");

IERC20(tbtc).safeTransferFrom(address(acre), address(this), amount);
IERC20(tbtc).approve(address(vault), amount);

return Router.deposit(vault, sharesOwner(), amount, minSharesOut);
}

// TODO: Add access restriction
function withdrawFromVault(
IVault vault,
uint256 amount,
uint256 maxSharesOut
) public returns (uint256 sharesOut) {
uint256 shares = vault.previewWithdraw(amount);

IERC20(vault).approve(address(vault), shares);

return Router.withdraw(vault, address(acre), amount, maxSharesOut);
}

// TODO: Add access restriction
function redeemFromVault(
IVault vault,
uint256 shares,
uint256 minAmountOut
) public returns (uint256 amountOut) {
IERC20(vault).approve(address(vault), shares);

Router.redeem(vault, address(acre), shares, minAmountOut);
}

// TODO: Add function to withdrawMax

// TODO: Check possibilities of Dispatcher upgrades and shares migration.
function migrateShares(IVault[] calldata _vaults) public onlyOwner {
address newDispatcher = address(acre.dispatcher());

for (uint i = 0; i < _vaults.length; i++) {
_vaults[i].transfer(
newDispatcher,
_vaults[i].balanceOf(address(this))
);
}
}

function vaultsWeight() internal view returns (uint16 totalWeight) {
for (uint256 i = 0; i < vaults.length; i++) {
totalWeight += vaultsInfo[vaults[i]].weight;
Expand Down Expand Up @@ -246,4 +190,66 @@ contract Dispatcher is Router, Ownable {
depositToVault(vault, depositAmount, minSharesOut);
}
}

function sharesOwner() public returns (address) {
return address(this);
}

// TODO: This function has to be internal.
function depositToVault(
IVault vault,
uint256 amount,
uint256 minSharesOut
) public returns (uint256 sharesOut) {
if (!isVaultAuthorized(vault)) {
revert VaultUnauthorized();
}

require(vault.asset() == address(tbtc), "vault asset is not tbtc");

IERC20(tbtc).safeTransferFrom(address(acre), address(this), amount);
IERC20(tbtc).approve(address(vault), amount);

return Router.deposit(vault, sharesOwner(), amount, minSharesOut);
}

// TODO: This function has to be internal.
function withdrawFromVault(
IVault vault,
uint256 amount,
uint256 maxSharesOut
) public returns (uint256 sharesOut) {
uint256 shares = vault.previewWithdraw(amount);

IERC20(vault).approve(address(vault), shares);

return Router.withdraw(vault, address(acre), amount, maxSharesOut);
}

// TODO: This function has to be internal.
function redeemFromVault(
IVault vault,
uint256 shares,
uint256 minAmountOut
) public returns (uint256 amountOut) {
IERC20(vault).approve(address(vault), shares);

Router.redeem(vault, address(acre), shares, minAmountOut);
}

// TODO: Add function to withdrawMax

// TODO: Check possibilities of Dispatcher upgrades and shares migration.
function migrateShares(IVault[] calldata _vaults) public onlyOwner {
address newDispatcher = address(acre.dispatcher());

require(newDispatcher != address(0), "new dispatcher cannot be address zero");

for (uint i = 0; i < _vaults.length; i++) {
_vaults[i].transfer(
newDispatcher,
_vaults[i].balanceOf(address(this))
);
}
}
}

0 comments on commit a815d10

Please sign in to comment.