-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Yield-earning Warp Routes with ERC4626 (#3076)
### Description Integrates ERC4626 compatible vaults. This PR assumes that the vault `asset` is the same as `wrappedToken` - `_transferFromSender()` deposits into the vault - `_transferTo()` withdraws from the vault - `sweep()` redeems excess shares for the vault asset - Uses internal `assetDeposited` store to keep track of assets deposited. Used to calculate excess shares to be withdrawn by owner - Makes minor changes to existing test files to allow new tests to inherit ### Drive-by changes <!-- Are there any minor or drive-by changes also included? --> ### Related issues - Implements #2450 ### Backward compatibility Yes ### Testing Manual/Unit Tests --------- Co-authored-by: Yorke Rhodes <[email protected]>
- Loading branch information
Showing
8 changed files
with
358 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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,13 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity >=0.8.0; | ||
import "@openzeppelin/contracts/token/ERC20/extensions/ERC4626.sol"; | ||
import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
import "@openzeppelin/contracts/interfaces/IERC20.sol"; | ||
|
||
contract ERC4626Test is ERC4626 { | ||
constructor( | ||
address _asset, | ||
string memory _name, | ||
string memory _symbol | ||
) ERC4626(IERC20(_asset)) ERC20(_name, _symbol) {} | ||
} |
This file contains 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
82 changes: 82 additions & 0 deletions
82
solidity/contracts/token/HypERC20CollateralVaultDeposit.sol
This file contains 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,82 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity >=0.8.0; | ||
import "@openzeppelin/contracts/token/ERC20/extensions/ERC4626.sol"; | ||
import {HypERC20Collateral} from "./HypERC20Collateral.sol"; | ||
|
||
/** | ||
* @title Hyperlane ERC20 Token Collateral with deposits collateral to a vault | ||
* @author ltyu | ||
*/ | ||
contract HypERC20CollateralVaultDeposit is HypERC20Collateral { | ||
// Address of the ERC4626 compatible vault | ||
ERC4626 public immutable vault; | ||
|
||
// Internal balance of total asset deposited | ||
uint256 public assetDeposited; | ||
|
||
event ExcessSharesSwept(uint256 amount, uint256 assetsRedeemed); | ||
|
||
constructor( | ||
ERC4626 _vault, | ||
address _mailbox | ||
) HypERC20Collateral(_vault.asset(), _mailbox) { | ||
vault = _vault; | ||
wrappedToken.approve(address(vault), type(uint256).max); | ||
} | ||
|
||
/** | ||
* @dev Transfers `_amount` of `wrappedToken` from `msg.sender` to this contract, and deposit into vault | ||
* @inheritdoc HypERC20Collateral | ||
*/ | ||
function _transferFromSender( | ||
uint256 _amount | ||
) internal override returns (bytes memory metadata) { | ||
metadata = super._transferFromSender(_amount); | ||
_depositIntoVault(_amount); | ||
} | ||
|
||
/** | ||
* @dev Deposits into the vault and increment assetDeposited | ||
* @param _amount amount to deposit into vault | ||
*/ | ||
function _depositIntoVault(uint256 _amount) internal { | ||
assetDeposited += _amount; | ||
vault.deposit(_amount, address(this)); | ||
} | ||
|
||
/** | ||
* @dev Transfers `_amount` of `wrappedToken` from this contract to `_recipient`, and withdraws from vault | ||
* @inheritdoc HypERC20Collateral | ||
*/ | ||
function _transferTo( | ||
address _recipient, | ||
uint256 _amount, | ||
bytes calldata | ||
) internal virtual override { | ||
_withdrawFromVault(_amount, _recipient); | ||
} | ||
|
||
/** | ||
* @dev Withdraws from the vault and decrement assetDeposited | ||
* @param _amount amount to withdraw from vault | ||
* @param _recipient address to deposit withdrawn underlying to | ||
*/ | ||
function _withdrawFromVault(uint256 _amount, address _recipient) internal { | ||
assetDeposited -= _amount; | ||
vault.withdraw(_amount, _recipient, address(this)); | ||
} | ||
|
||
/** | ||
* @notice Allows the owner to redeem excess shares | ||
*/ | ||
function sweep() external onlyOwner { | ||
uint256 excessShares = vault.maxRedeem(address(this)) - | ||
vault.convertToShares(assetDeposited); | ||
uint256 assetsRedeemed = vault.redeem( | ||
excessShares, | ||
owner(), | ||
address(this) | ||
); | ||
emit ExcessSharesSwept(excessShares, assetsRedeemed); | ||
} | ||
} |
This file contains 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 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.