Skip to content
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

[SI] VaultHubViewer contract #905

Draft
wants to merge 9 commits into
base: feat/vaults
Choose a base branch
from
137 changes: 137 additions & 0 deletions contracts/0.8.25/vaults/VaultHubViewerV1.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// SPDX-FileCopyrightText: 2024 Lido <[email protected]>
// SPDX-License-Identifier: GPL-3.0

// See contracts/COMPILERS.md
pragma solidity 0.8.25;
import {IStakingVault} from "./interfaces/IStakingVault.sol";
import {OwnableUpgradeable} from "contracts/openzeppelin/5.0.2/upgradeable/access/OwnableUpgradeable.sol";
Jeday marked this conversation as resolved.
Show resolved Hide resolved


interface IDashboard {
tamtamchik marked this conversation as resolved.
Show resolved Hide resolved
function getRoleMember(bytes32 role, uint256 index) external view returns (address);
function hasRole(bytes32 role, address account) external view returns (bool);
}

interface IVault is IStakingVault {
function owner() external view returns (address);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be worth moving inside IStakingVault. Looks like may be used often.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

owner here conflicts when moving to IStakingVault, because StakingVault is IStakingVault, ..., Ownable

}

interface IVaultHub {
tamtamchik marked this conversation as resolved.
Show resolved Hide resolved
function vaultsCount() external view returns (uint256);
function vault(uint256 _index) external view returns (IVault);
}

contract VaultHubViewerV1 {
IVaultHub public immutable vaultHub;
bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;

constructor(address _vaultHubAddress) {
if (_vaultHubAddress == address(0)) revert ZeroArgument("_vaultHubAddress");
vaultHub = IVaultHub(_vaultHubAddress);
}

function isContract(address account) public view returns (bool) {
uint256 size;
assembly {
size := extcodesize(account)
}
return size > 0;
}


/// @notice Checks if a given address is the owner of a vault
/// @param vault The vault to check
/// @param _owner The address to check
/// @return True if the address is the owner, false otherwise
function isOwner(IVault vault, address _owner) public view returns (bool) {
address currentOwner = vault.owner();
if (currentOwner == _owner) {
return true;
}
if (isContract(currentOwner)) {
try IDashboard(currentOwner).hasRole(DEFAULT_ADMIN_ROLE, _owner) returns (bool hasRole) {
return hasRole;
Jeday marked this conversation as resolved.
Show resolved Hide resolved
} catch {
return false;
}
}
return false;
}

/// @notice Checks if a given address has a given role on a vault
/// @param vault The vault to check
/// @param _member The address to check
/// @param _role The role to check
/// @return True if the address has the role, false otherwise
function isHasRole(IVault vault, address _member, bytes32 _role) public view returns (bool) {
Jeday marked this conversation as resolved.
Show resolved Hide resolved
address owner = vault.owner();
if (owner == address(0)) {
return false;
}

try IDashboard(owner).hasRole(_role, _member) returns (bool hasRole) {
return hasRole;
Jeday marked this conversation as resolved.
Show resolved Hide resolved
} catch {
return false;
}
}

/// @notice Returns all vaults owned by a given address
/// @param _owner Address of the owner
/// @return An array of vaults owned by the given address
function vaultsByOwner(address _owner) public view returns (IVault[] memory) {
uint256 count = vaultHub.vaultsCount();
IVault[] memory vaults = new IVault[](count);

// Populate the array with the owner's vaults
for (uint256 i = 0; i < count; i++) {
if (isOwner(vaultHub.vault(i), _owner)) {
vaults[i] = vaultHub.vault(i);
}
}
Jeday marked this conversation as resolved.
Show resolved Hide resolved

return _filterNonZeroVaults(vaults);
}

/// @notice Returns all vaults with a given role on a given address
/// @param _role Role to check
/// @param _member Address to check
/// @return An array of vaults with the given role on the given address
function vaultsByRole(bytes32 _role, address _member) public view returns (IVault[] memory) {
uint256 count = vaultHub.vaultsCount();
IVault[] memory vaults = new IVault[](count);

for (uint256 i = 0; i < count; i++) {
if (isHasRole(vaultHub.vault(i), _member, _role)) {
vaults[i] = vaultHub.vault(i);
}
}

return _filterNonZeroVaults(vaults);
}

/// @notice Filters out zero address vaults from an array
/// @param vaults Array of vaults to filter
/// @return An array of non-zero vaults
function _filterNonZeroVaults(IVault[] memory vaults) internal pure returns (IVault[] memory) {
uint256 nonZeroLength = 0;
for (uint256 i = 0; i < vaults.length; i++) {
if (address(vaults[i]) != address(0)) {
nonZeroLength++;
}
}
IVault[] memory nonZeroVaults = new IVault[](nonZeroLength);
uint256 index = 0;
for (uint256 i = 0; i < vaults.length; i++) {
if (address(vaults[i]) != address(0)) {
nonZeroVaults[index] = vaults[i];
index++;
}
}
return nonZeroVaults;
}
Jeday marked this conversation as resolved.
Show resolved Hide resolved

/// @notice Error for zero address arguments
/// @param argName Name of the argument that is zero
error ZeroArgument(string argName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// SPDX-License-Identifier: UNLICENSED
// for testing purposes only

pragma solidity 0.8.25;

import {VaultHub} from "contracts/0.8.25/vaults/VaultHub.sol";
import {IStakingVault} from "contracts/0.8.25/vaults/interfaces/IStakingVault.sol";

contract IStETH {
function mintExternalShares(address _receiver, uint256 _amountOfShares) external {}

function burnExternalShares(uint256 _amountOfShares) external {}
}

contract VaultHub__MockForHubViewer {
uint256 internal constant BPS_BASE = 100_00;
IStETH public immutable steth;
// keccak256(abi.encode(uint256(keccak256("VaultHub")) - 1)) & ~bytes32(uint256(0xff))
bytes32 private constant VAULT_HUB_STORAGE_LOCATION =
0xb158a1a9015c52036ff69e7937a7bb424e82a8c4cbec5c5309994af06d825300;

constructor(IStETH _steth) {
steth = _steth;
}

event Mock__VaultDisconnected(address vault);
event Mock__Rebalanced(uint256 amount);

mapping(address => VaultHub.VaultSocket) public vaultSockets;

function mock__setVaultSocket(address vault, VaultHub.VaultSocket memory socket) external {
vaultSockets[vault] = socket;
}

function mock_vaultLock(address vault, uint256 amount) external {
IStakingVault(vault).lock(amount);
}

function vaultSocket(address vault) external view returns (VaultHub.VaultSocket memory) {
return vaultSockets[vault];
}

function vaultSocketIndex(address vault) public view returns (uint256) {
return _getVaultHubStorage().vaultIndex[vault];
}

function vaultsCount() public view returns (uint256) {
return _getVaultHubStorage().sockets.length;
}

function vault(uint256 _index) public view returns (address) {
return _getVaultHubStorage().sockets[_index].vault;
}

function mock_vaultSocket() public view returns (VaultHub.VaultSocket[] memory) {
return _getVaultHubStorage().sockets;
}

function disconnectVault(address vault) external {
emit Mock__VaultDisconnected(vault);
}

function mintSharesBackedByVault(address /* vault */, address recipient, uint256 amount) external {
steth.mintExternalShares(recipient, amount);
}

function burnSharesBackedByVault(address /* vault */, uint256 amount) external {
steth.burnExternalShares(amount);
}

function voluntaryDisconnect(address _vault) external {
emit Mock__VaultDisconnected(_vault);
}

function rebalance() external payable {
emit Mock__Rebalanced(msg.value);
}

function mock_connectVault(address _vault) external {
VaultHub.VaultHubStorage storage $ = _getVaultHubStorage();

VaultHub.VaultSocket memory vr = VaultHub.VaultSocket(
_vault,
0, // sharesMinted
uint96(0),
uint16(0),
uint16(0),
uint16(0),
false // isDisconnected
);

$.vaultIndex[_vault] = $.sockets.length;
$.sockets.push(vr);
}

function _getVaultHubStorage() private pure returns (VaultHub.VaultHubStorage storage $) {
assembly {
$.slot := VAULT_HUB_STORAGE_LOCATION
}
}
}
Loading
Loading