diff --git a/core/contracts/Acre.sol b/core/contracts/Acre.sol index 34f2de84b..446703371 100644 --- a/core/contracts/Acre.sol +++ b/core/contracts/Acre.sol @@ -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); - } } diff --git a/core/contracts/Dispatcher.sol b/core/contracts/Dispatcher.sol index f2b49a28e..0997e0bdd 100644 --- a/core/contracts/Dispatcher.sol +++ b/core/contracts/Dispatcher.sol @@ -13,7 +13,6 @@ import "./Acre.sol"; contract Dispatcher is Router, Ownable { using SafeERC20 for IERC20; - error VaultAlreadyAuthorized(); error VaultUnauthorized(); @@ -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); } @@ -74,8 +71,6 @@ contract Dispatcher is Router, Ownable { } } - acre.approveVaultSharesForDispatcher(vault, 0); - emit VaultDeauthorized(vault); } @@ -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 @@ -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); @@ -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))); + } + } } diff --git a/core/test/Dispatcher.Routing.POC.test.ts b/core/test/Dispatcher.Routing.POC.test.ts index 4777355fc..5ba33db31 100644 --- a/core/test/Dispatcher.Routing.POC.test.ts +++ b/core/test/Dispatcher.Routing.POC.test.ts @@ -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) @@ -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, @@ -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 -