-
Notifications
You must be signed in to change notification settings - Fork 3
ERC4626InvestStrategy to invest in other vaults #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
94602e5
ERC4626InvestStrategy to invest in other vaults
gnarvaja 9a29a6f
Add more tolerance to the test
gnarvaja f4d5653
Merge branch 'main' of github.com:ensuro/vaults into add-erc4626-stra…
gnarvaja 4021611
Dummy change to force GitHub sync
gnarvaja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| pragma solidity ^0.8.0; | ||
|
|
||
| import {IERC20} from "@openzeppelin/contracts/interfaces/IERC20.sol"; | ||
| import {IERC4626} from "@openzeppelin/contracts/interfaces/IERC4626.sol"; | ||
| import {IInvestStrategy} from "./interfaces/IInvestStrategy.sol"; | ||
| import {InvestStrategyClient} from "./InvestStrategyClient.sol"; | ||
|
|
||
| /** | ||
| * @title AaveV3InvestStrategy | ||
| * | ||
| * @dev Strategy that invests/deinvests into a 4626 vault | ||
| * | ||
| * @custom:security-contact [email protected] | ||
| * @author Ensuro | ||
| */ | ||
| contract ERC4626InvestStrategy is IInvestStrategy { | ||
| address private immutable __self = address(this); | ||
| bytes32 public immutable storageSlot = InvestStrategyClient.makeStorageSlot(this); | ||
|
|
||
| IERC4626 internal immutable _vault; | ||
| IERC20 internal immutable _asset; | ||
|
|
||
| error CanBeCalledOnlyThroughDelegateCall(); | ||
| error CannotDisconnectWithAssets(); | ||
| error NoExtraDataAllowed(); | ||
|
|
||
| modifier onlyDelegCall() { | ||
| if (address(this) == __self) revert CanBeCalledOnlyThroughDelegateCall(); | ||
| _; | ||
| } | ||
|
|
||
| constructor(IERC4626 vault_) { | ||
| _vault = vault_; | ||
| _asset = IERC20(vault_.asset()); | ||
| } | ||
|
|
||
| /// @inheritdoc IInvestStrategy | ||
| function connect(bytes memory initData) external virtual override onlyDelegCall { | ||
| if (initData.length != 0) revert NoExtraDataAllowed(); | ||
| } | ||
|
|
||
| /// @inheritdoc IInvestStrategy | ||
| function disconnect(bool force) external virtual override onlyDelegCall { | ||
| // Here I check _vault.balanceOf() instead of totalAssets(). In an extreme cases, when the vault lost all its | ||
| // value these can differ, but on those cases I think it's safer to block the disconnection unless forced | ||
| if (!force && _vault.balanceOf(address(this)) != 0) revert CannotDisconnectWithAssets(); | ||
| } | ||
|
|
||
| /// @inheritdoc IInvestStrategy | ||
| function maxWithdraw(address contract_) public view virtual override returns (uint256) { | ||
| return _vault.maxWithdraw(contract_); | ||
| } | ||
|
|
||
| /// @inheritdoc IInvestStrategy | ||
| function maxDeposit(address contract_) public view virtual override returns (uint256) { | ||
| return _vault.maxDeposit(contract_); | ||
| } | ||
|
|
||
| /// @inheritdoc IInvestStrategy | ||
| function asset(address) public view virtual override returns (address) { | ||
| return address(_asset); | ||
| } | ||
|
|
||
| /** | ||
| * @dev Returns the ERC4626 where this strategy invests the funds | ||
| */ | ||
| function investVault() public view returns (IERC4626) { | ||
| return _vault; | ||
| } | ||
|
|
||
| /// @inheritdoc IInvestStrategy | ||
| function totalAssets(address contract_) public view virtual override returns (uint256 assets) { | ||
| return _vault.convertToAssets(_vault.balanceOf(contract_)); | ||
| } | ||
|
|
||
| /// @inheritdoc IInvestStrategy | ||
| function withdraw(uint256 assets) external virtual override onlyDelegCall { | ||
| _vault.withdraw(assets, address(this), address(this)); | ||
| } | ||
|
|
||
| /// @inheritdoc IInvestStrategy | ||
| function deposit(uint256 assets) external virtual override onlyDelegCall { | ||
| _asset.approve(address(_vault), assets); | ||
| _vault.deposit(assets, address(this)); | ||
| } | ||
|
|
||
| /// @inheritdoc IInvestStrategy | ||
| function forwardEntryPoint(uint8, bytes memory) external view onlyDelegCall returns (bytes memory) { | ||
| // solhint-disable-next-line gas-custom-errors,reason-string | ||
| revert(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
En este caso no es mejor implementar un custom error
NotImplemented()en lugar de un revert a secas?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
En varios lugares lo tenemos así, como un revert sin motivo. NotImplemented no aportaría demasiado