Skip to content

Commit

Permalink
Make Dispatcher the shares owner
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuba committed Dec 13, 2023
1 parent 4e211f8 commit 03b5784
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
8 changes: 0 additions & 8 deletions core/contracts/Acre.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,10 @@ contract Acre is ERC4626, Ownable {
function upgradeDispatcher(Dispatcher _newDispatcher) public onlyOwner {
if (address(dispatcher) != address(0)) {
IERC20(asset()).approve(address(dispatcher), 0);
// TODO: Remove dispatcher's approvals for the vaults.
}

dispatcher = _newDispatcher;

IERC20(asset()).approve(address(dispatcher), type(uint256).max);
}

function approveVaultSharesForDispatcher(address vault, uint256 amount) external {
if (msg.sender != address(dispatcher)) revert CallerNotDispatcher();

// TODO: Emit event
IERC20(vault).safeIncreaseAllowance(address(dispatcher), amount);
}
}
18 changes: 10 additions & 8 deletions core/contracts/Dispatcher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import "./Acre.sol";
contract Dispatcher is Router, Ownable {
using SafeERC20 for IERC20;


error VaultAlreadyAuthorized();
error VaultUnauthorized();

Expand Down Expand Up @@ -51,8 +50,6 @@ contract Dispatcher is Router, Ownable {
vaults.push(vault);
vaultsInfo[vault].authorized = true;

acre.approveVaultSharesForDispatcher(vault, type(uint256).max);

emit VaultAuthorized(vault);
}

Expand All @@ -74,8 +71,6 @@ contract Dispatcher is Router, Ownable {
}
}

acre.approveVaultSharesForDispatcher(vault, 0);

emit VaultDeauthorized(vault);
}

Expand Down Expand Up @@ -103,7 +98,7 @@ contract Dispatcher is Router, Ownable {
IERC20(tbtc).safeTransferFrom(address(acre), address(this), amount);
IERC20(tbtc).approve(address(vault), amount);

Router.deposit(vault, address(acre), amount, minSharesOut);
Router.deposit(vault, address(this), amount, minSharesOut);
}

// TODO: Add access restriction
Expand All @@ -114,7 +109,6 @@ contract Dispatcher is Router, Ownable {
) public returns (uint256 sharesOut) {
uint256 shares = vault.previewWithdraw(amount);

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

Router.withdraw(vault, address(acre), amount, maxSharesOut);
Expand All @@ -126,11 +120,19 @@ contract Dispatcher is Router, Ownable {
uint256 shares,
uint256 minAmountOut
) public returns (uint256 amountOut) {
IERC20(vault).safeTransferFrom(address(acre), address(this), shares);
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(IERC4626[] 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)));
}
}
}
12 changes: 6 additions & 6 deletions core/test/Dispatcher.Routing.POC.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ describe.only("Dispatcher", () => {
vaultDepositAmount,
)

expect(await vault.balanceOf(await acre.getAddress())).to.be.equal(
expect(await vault.balanceOf(await acre.getAddress())).to.be.equal(0)
expect(await vault.balanceOf(await dispatcher.getAddress())).to.be.equal(
expectedSharesDeposit,
)
expect(await vault.balanceOf(await dispatcher.getAddress())).to.be.equal(0)

// Simulate Vault generating yield.
const yieldAmount = to1e18(300)
Expand All @@ -102,10 +102,10 @@ describe.only("Dispatcher", () => {
expectedSharesWithdraw,
)

expect(await vault.balanceOf(await dispatcher.getAddress())).to.be.equal(0)
expect(await vault.balanceOf(await acre.getAddress())).to.be.equal(
expect(await vault.balanceOf(await dispatcher.getAddress())).to.be.equal(
expectedSharesDeposit - expectedSharesWithdraw,
)
expect(await vault.balanceOf(await acre.getAddress())).to.be.equal(0)

expect(await tbtc.balanceOf(await acre.getAddress())).to.be.equal(
staker1Amount - vaultDepositAmount + amountToWithdraw1,
Expand All @@ -125,10 +125,10 @@ describe.only("Dispatcher", () => {
expectedAmountRedeem,
)

expect(await vault.balanceOf(await dispatcher.getAddress())).to.be.equal(0)
expect(await vault.balanceOf(await acre.getAddress())).to.be.equal(
expect(await vault.balanceOf(await dispatcher.getAddress())).to.be.equal(
expectedSharesDeposit - expectedSharesWithdraw - sharesToRedeem,
)
expect(await vault.balanceOf(await acre.getAddress())).to.be.equal(0)

expect(await tbtc.balanceOf(await acre.getAddress())).to.be.equal(
staker1Amount -
Expand Down

0 comments on commit 03b5784

Please sign in to comment.