Skip to content

Commit

Permalink
Add functions for batch deposit and mint to Eth vaults
Browse files Browse the repository at this point in the history
  • Loading branch information
tsudmi committed Jun 27, 2024
1 parent 4b544b0 commit 7e54778
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 15 deletions.
25 changes: 25 additions & 0 deletions contracts/interfaces/IEthErc20Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

pragma solidity ^0.8.22;

import {IKeeperRewards} from './IKeeperRewards.sol';
import {IVaultAdmin} from './IVaultAdmin.sol';
import {IVaultVersion} from './IVaultVersion.sol';
import {IVaultFee} from './IVaultFee.sol';
Expand Down Expand Up @@ -53,4 +54,28 @@ interface IEthErc20Vault is
* @param params The encoded parameters for initializing the EthErc20Vault contract
*/
function initialize(bytes calldata params) external payable;

/**
* @notice Deposits assets to the vault and mints OsToken shares to the receiver
* @param receiver The address to receive the OsToken
* @param referrer The address of the referrer
* @return osTokenShares The amount of OsToken shares minted
*/
function depositAndMintOsToken(
address receiver,
address referrer
) external payable returns (uint256 osTokenShares);

/**
* @notice Updates the state, deposits assets to the vault and mints OsToken shares to the receiver
* @param receiver The address to receive the OsToken
* @param referrer The address of the referrer
* @param harvestParams The parameters for the harvest
* @return osTokenShares The amount of OsToken shares minted
*/
function updateStateAndDepositAndMintOsToken(
address receiver,
address referrer,
IKeeperRewards.HarvestParams calldata harvestParams
) external payable returns (uint256 osTokenShares);
}
25 changes: 25 additions & 0 deletions contracts/interfaces/IEthVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

pragma solidity ^0.8.22;

import {IKeeperRewards} from './IKeeperRewards.sol';
import {IVaultAdmin} from './IVaultAdmin.sol';
import {IVaultVersion} from './IVaultVersion.sol';
import {IVaultFee} from './IVaultFee.sol';
Expand Down Expand Up @@ -47,4 +48,28 @@ interface IEthVault is
* @param params The encoded parameters for initializing the EthVault contract
*/
function initialize(bytes calldata params) external payable;

/**
* @notice Deposits assets to the vault and mints OsToken shares to the receiver
* @param receiver The address to receive the OsToken
* @param referrer The address of the referrer
* @return osTokenShares The amount of OsToken shares minted
*/
function depositAndMintOsToken(
address receiver,
address referrer
) external payable returns (uint256 osTokenShares);

/**
* @notice Updates the state, deposits assets to the vault and mints OsToken shares to the receiver
* @param receiver The address to receive the OsToken
* @param referrer The address of the referrer
* @param harvestParams The parameters for the harvest
* @return osTokenShares The amount of OsToken shares minted
*/
function updateStateAndDepositAndMintOsToken(
address receiver,
address referrer,
IKeeperRewards.HarvestParams calldata harvestParams
) external payable returns (uint256 osTokenShares);
}
23 changes: 22 additions & 1 deletion contracts/vaults/ethereum/EthErc20Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
pragma solidity ^0.8.22;

import {Initializable} from '@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol';
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import {IEthErc20Vault} from '../../interfaces/IEthErc20Vault.sol';
import {IEthVaultFactory} from '../../interfaces/IEthVaultFactory.sol';
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import {IKeeperRewards} from '../../interfaces/IKeeperRewards.sol';
import {Multicall} from '../../base/Multicall.sol';
import {ERC20Upgradeable} from '../../base/ERC20Upgradeable.sol';
import {VaultValidators} from '../modules/VaultValidators.sol';
Expand Down Expand Up @@ -93,6 +94,26 @@ contract EthErc20Vault is
);
}

/// @inheritdoc IEthErc20Vault
function depositAndMintOsToken(
address receiver,
address referrer
) public payable override returns (uint256 osTokenShares) {
deposit(msg.sender, referrer);
osTokenShares = _calcMaxOsTokenShares(msg.value);
mintOsToken(receiver, osTokenShares, referrer);
}

/// @inheritdoc IEthErc20Vault
function updateStateAndDepositAndMintOsToken(
address receiver,
address referrer,
IKeeperRewards.HarvestParams calldata harvestParams
) external payable override returns (uint256 osTokenShares) {
updateState(harvestParams);
return depositAndMintOsToken(receiver, referrer);
}

/// @inheritdoc IERC20
function transfer(
address to,
Expand Down
21 changes: 21 additions & 0 deletions contracts/vaults/ethereum/EthVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pragma solidity ^0.8.22;
import {Initializable} from '@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol';
import {IEthVault} from '../../interfaces/IEthVault.sol';
import {IEthVaultFactory} from '../../interfaces/IEthVaultFactory.sol';
import {IKeeperRewards} from '../../interfaces/IKeeperRewards.sol';
import {Multicall} from '../../base/Multicall.sol';
import {VaultValidators} from '../modules/VaultValidators.sol';
import {VaultAdmin} from '../modules/VaultAdmin.sol';
Expand Down Expand Up @@ -89,6 +90,26 @@ contract EthVault is
);
}

/// @inheritdoc IEthVault
function depositAndMintOsToken(
address receiver,
address referrer
) public payable override returns (uint256 osTokenShares) {
deposit(msg.sender, referrer);
osTokenShares = _calcMaxOsTokenShares(msg.value);
mintOsToken(receiver, osTokenShares, referrer);
}

/// @inheritdoc IEthVault
function updateStateAndDepositAndMintOsToken(
address receiver,
address referrer,
IKeeperRewards.HarvestParams calldata harvestParams
) external payable override returns (uint256 osTokenShares) {
updateState(harvestParams);
return depositAndMintOsToken(receiver, referrer);
}

/// @inheritdoc IVaultEnterExit
function enterExitQueue(
uint256 shares,
Expand Down
30 changes: 16 additions & 14 deletions contracts/vaults/modules/VaultOsToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,7 @@ abstract contract VaultOsToken is VaultImmutables, VaultState, VaultEnterExit, I
position.shares += SafeCast.toUint128(osTokenShares);

// calculate and validate LTV
if (
Math.mulDiv(
convertToAssets(_balances[msg.sender]),
_osTokenConfig.getConfig(address(this)).ltvPercent,
_maxPercent
) < _osTokenVaultController.convertToAssets(position.shares)
) {
if (_calcMaxOsTokenShares(convertToAssets(_balances[msg.sender])) < position.shares) {
revert Errors.LowLtv();
}

Expand Down Expand Up @@ -275,17 +269,25 @@ abstract contract VaultOsToken is VaultImmutables, VaultState, VaultEnterExit, I
_syncPositionFee(position);

// calculate and validate position LTV
if (
Math.mulDiv(
convertToAssets(_balances[user]),
_osTokenConfig.getConfig(address(this)).ltvPercent,
_maxPercent
) < _osTokenVaultController.convertToAssets(position.shares)
) {
if (_calcMaxOsTokenShares(convertToAssets(_balances[user])) < position.shares) {
revert Errors.LowLtv();
}
}

/**
* @dev Internal function for calculating the maximum amount of osToken shares that can be minted
* @param assets The amount of assets to convert to osToken shares
* @return maxOsTokenShares The maximum amount of osToken shares that can be minted
*/
function _calcMaxOsTokenShares(uint256 assets) internal view returns (uint256) {
uint256 maxOsTokenAssets = Math.mulDiv(
assets,
_osTokenConfig.getConfig(address(this)).ltvPercent,
_maxPercent
);
return _osTokenVaultController.convertToShares(maxOsTokenAssets);
}

/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
Expand Down

0 comments on commit 7e54778

Please sign in to comment.