Skip to content

Commit

Permalink
Merge pull request #48 from symbioticfi/update-tokenized
Browse files Browse the repository at this point in the history
Simplify tokenized vault
  • Loading branch information
1kresh authored Sep 27, 2024
2 parents ac27562 + dc57c33 commit 1c8dc70
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 367 deletions.
3 changes: 1 addition & 2 deletions src/contracts/common/MigratableEntity.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
pragma solidity 0.8.25;

import {IMigratableEntity} from "../../interfaces/common/IMigratableEntity.sol";
import {IMigratablesFactory} from "../../interfaces/common/IMigratablesFactory.sol";

import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
Expand Down Expand Up @@ -78,7 +77,7 @@ abstract contract MigratableEntity is
_migrate(oldVersion, newVersion, data);
}

function _initialize(uint64, address, bytes calldata) internal virtual {}
function _initialize(uint64, address, bytes memory) internal virtual {}

function _migrate(uint64, uint64, bytes calldata) internal virtual {}
}
4 changes: 2 additions & 2 deletions src/contracts/vault/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ contract Vault is VaultStorage, MigratableEntity, AccessControlUpgradeable, IVau
function deposit(
address onBehalfOf,
uint256 amount
) external virtual nonReentrant returns (uint256 depositedAmount, uint256 mintedShares) {
) public virtual nonReentrant returns (uint256 depositedAmount, uint256 mintedShares) {
if (onBehalfOf == address(0)) {
revert InvalidOnBehalfOf();
}
Expand Down Expand Up @@ -427,7 +427,7 @@ contract Vault is VaultStorage, MigratableEntity, AccessControlUpgradeable, IVau
isWithdrawalsClaimed[epoch][msg.sender] = true;
}

function _initialize(uint64, address, bytes calldata data) internal virtual override {
function _initialize(uint64, address, bytes memory data) internal virtual override {
(InitParams memory params) = abi.decode(data, (InitParams));

if (params.collateral == address(0)) {
Expand Down
107 changes: 8 additions & 99 deletions src/contracts/vault/VaultTokenized.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,55 +53,20 @@ contract VaultTokenized is Vault, ERC20Upgradeable, IVaultTokenized {
function deposit(
address onBehalfOf,
uint256 amount
) external override(Vault, IVault) nonReentrant returns (uint256 depositedAmount, uint256 mintedShares) {
if (onBehalfOf == address(0)) {
revert InvalidOnBehalfOf();
}

if (depositWhitelist && !isDepositorWhitelisted[msg.sender]) {
revert NotWhitelistedDepositor();
}

uint256 balanceBefore = IERC20(collateral).balanceOf(address(this));
IERC20(collateral).safeTransferFrom(msg.sender, address(this), amount);
depositedAmount = IERC20(collateral).balanceOf(address(this)) - balanceBefore;

if (depositedAmount == 0) {
revert InsufficientDeposit();
}

if (isDepositLimit && totalStake() + depositedAmount > depositLimit) {
revert DepositLimitReached();
}

uint256 activeStake_ = activeStake();
uint256 activeShares_ = activeShares();
) public override(Vault, IVault) returns (uint256 depositedAmount, uint256 mintedShares) {
(depositedAmount, mintedShares) = super.deposit(onBehalfOf, amount);

mintedShares = ERC4626Math.previewDeposit(depositedAmount, activeShares_, activeStake_);

_activeStake.push(Time.timestamp(), activeStake_ + depositedAmount);
_mint(onBehalfOf, mintedShares);

emit Deposit(msg.sender, onBehalfOf, depositedAmount, mintedShares);
emit Transfer(address(0), onBehalfOf, mintedShares);
}

function _withdraw(
address claimer,
uint256 withdrawnAssets,
uint256 burnedShares
) internal override returns (uint256 mintedShares) {
_burn(msg.sender, burnedShares);
_activeStake.push(Time.timestamp(), activeStake() - withdrawnAssets);

uint256 epoch = currentEpoch() + 1;
uint256 withdrawals_ = withdrawals[epoch];
uint256 withdrawalsShares_ = withdrawalShares[epoch];

mintedShares = ERC4626Math.previewDeposit(withdrawnAssets, withdrawalsShares_, withdrawals_);
mintedShares = super._withdraw(claimer, withdrawnAssets, burnedShares);

withdrawals[epoch] = withdrawals_ + withdrawnAssets;
withdrawalShares[epoch] = withdrawalsShares_ + mintedShares;
withdrawalSharesOf[epoch][claimer] += mintedShares;
emit Transfer(msg.sender, address(0), burnedShares);
}

/**
Expand Down Expand Up @@ -137,67 +102,11 @@ contract VaultTokenized is Vault, ERC20Upgradeable, IVaultTokenized {
emit Transfer(from, to, value);
}

function _initialize(uint64 initialVersion, address owner_, bytes calldata data) internal override {
function _initialize(uint64 initialVersion, address owner_, bytes memory data) internal override {
(InitParamsTokenized memory params) = abi.decode(data, (InitParamsTokenized));

__ERC20_init(params.name, params.symbol);

if (params.collateral == address(0)) {
revert InvalidCollateral();
}

if (params.epochDuration == 0) {
revert InvalidEpochDuration();
}

if (params.defaultAdminRoleHolder == address(0)) {
if (params.depositWhitelistSetRoleHolder == address(0)) {
if (params.depositWhitelist) {
if (params.depositorWhitelistRoleHolder == address(0)) {
revert MissingRoles();
}
} else if (params.depositorWhitelistRoleHolder != address(0)) {
revert MissingRoles();
}
}

if (params.isDepositLimitSetRoleHolder == address(0)) {
if (params.isDepositLimit) {
if (params.depositLimit == 0 && params.depositLimitSetRoleHolder == address(0)) {
revert MissingRoles();
}
} else if (params.depositLimit != 0 || params.depositLimitSetRoleHolder != address(0)) {
revert MissingRoles();
}
}
}

collateral = params.collateral;

burner = params.burner;

epochDurationInit = Time.timestamp();
epochDuration = params.epochDuration;
super._initialize(initialVersion, owner_, abi.encode(params.baseParams));

depositWhitelist = params.depositWhitelist;

isDepositLimit = params.isDepositLimit;
depositLimit = params.depositLimit;

if (params.defaultAdminRoleHolder != address(0)) {
_grantRole(DEFAULT_ADMIN_ROLE, params.defaultAdminRoleHolder);
}
if (params.depositWhitelistSetRoleHolder != address(0)) {
_grantRole(DEPOSIT_WHITELIST_SET_ROLE, params.depositWhitelistSetRoleHolder);
}
if (params.depositorWhitelistRoleHolder != address(0)) {
_grantRole(DEPOSITOR_WHITELIST_ROLE, params.depositorWhitelistRoleHolder);
}
if (params.isDepositLimitSetRoleHolder != address(0)) {
_grantRole(IS_DEPOSIT_LIMIT_SET_ROLE, params.isDepositLimitSetRoleHolder);
}
if (params.depositLimitSetRoleHolder != address(0)) {
_grantRole(DEPOSIT_LIMIT_SET_ROLE, params.depositLimitSetRoleHolder);
}
__ERC20_init(params.name, params.symbol);
}
}
24 changes: 2 additions & 22 deletions src/interfaces/vault/IVaultTokenized.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,12 @@ import {IVault} from "./IVault.sol";
interface IVaultTokenized is IVault {
/**
* @notice Initial parameters needed for a tokenized vault deployment.
* @param collateral vault's underlying collateral
* @param burner vault's burner to issue debt to (e.g., 0xdEaD or some unwrapper contract)
* @param epochDuration duration of the vault epoch (it determines sync points for withdrawals)
* @param depositWhitelist if enabling deposit whitelist
* @param isDepositLimit if enabling deposit limit
* @param depositLimit deposit limit (maximum amount of the collateral that can be in the vault simultaneously)
* @param defaultAdminRoleHolder address of the initial DEFAULT_ADMIN_ROLE holder
* @param depositWhitelistSetRoleHolder address of the initial DEPOSIT_WHITELIST_SET_ROLE holder
* @param depositorWhitelistRoleHolder address of the initial DEPOSITOR_WHITELIST_ROLE holder
* @param isDepositLimitSetRoleHolder address of the initial IS_DEPOSIT_LIMIT_SET_ROLE holder
* @param depositLimitSetRoleHolder address of the initial DEPOSIT_LIMIT_SET_ROLE holder
* @param baseParams initial parameters needed for a vault deployment (InitParams)
* @param name name for the ERC20 tokenized vault
* @param symbol symbol for the ERC20 tokenized vault
*/
struct InitParamsTokenized {
address collateral;
address burner;
uint48 epochDuration;
bool depositWhitelist;
bool isDepositLimit;
uint256 depositLimit;
address defaultAdminRoleHolder;
address depositWhitelistSetRoleHolder;
address depositorWhitelistRoleHolder;
address isDepositLimitSetRoleHolder;
address depositLimitSetRoleHolder;
InitParams baseParams;
string name;
string symbol;
}
Expand Down
Loading

0 comments on commit 1c8dc70

Please sign in to comment.