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

Open
wants to merge 7 commits into
base: feat/vaults
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions contracts/0.8.25/vaults/VaultHubViewerV1.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// 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";

interface IDashboardACL {
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 {
bytes32 constant strictTrue = keccak256(hex"0000000000000000000000000000000000000000000000000000000000000001");

IVaultHub public immutable vaultHub;
bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;

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

/// @notice Checks if a given address is a contract
/// @param account The address to check
/// @return True if the address is a contract, false otherwise
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 vaultOwner = vault.owner();
if (vaultOwner == _owner) {
return true;
}

return _checkHasRole(vaultOwner, _owner, DEFAULT_ADMIN_ROLE);
}

/// @notice Checks if a given address has a given role on a vault owner contract
/// @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 hasRole(IVault vault, address _member, bytes32 _role) public view returns (bool) {
address vaultOwner = vault.owner();
if (vaultOwner == address(0)) {
return false;
}

return _checkHasRole(vaultOwner, _member, _role);
}

/// @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
uint256 valid = 0;

// Populate the array with the owner's vaults
for (uint256 i = 0; i < count; i++) {
IVault vaultInstance = IVault(vaultHub.vault(i));
if (isOwner(vaultInstance, _owner)) {
vaults[valid] = vaultHub.vault(i);
valid++;
}
}

return _filterNonZeroVaults(vaults, valid);
}

/// @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);

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

return _filterNonZeroVaults(vaults, valid);
}

/// @notice safely returns if role member has given role
/// @param _contract that can have ACL or not
/// @param _member addrress to check for role
/// @return _role ACL role bytes
function _checkHasRole(address _contract, address _member, bytes32 _role) internal view returns (bool) {
if (!isContract(_contract)) return false;

bytes memory payload = abi.encodeWithSignature("hasRole(bytes32,address)", _role, _member);
(bool success, bytes memory result) = _contract.staticcall(payload);

if (success && keccak256(result) == strictTrue) {
return true;
} else {
return false;
}
}

/// @notice Filters out zero address vaults from an array
/// @param _vaults Array of vaults to filter
/// @param _validCount number of non-zero vaults
/// @return filtered An array of non-zero vaults
function _filterNonZeroVaults(
IVault[] memory _vaults,
uint256 _validCount
) internal pure returns (IVault[] memory filtered) {
filtered = new IVault[](_validCount);
for (uint256 i = 0; i < _validCount; i++) {
filtered[i] = _vaults[i];
}
}

/// @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,31 @@
// SPDX-License-Identifier: UNLICENSED
// for testing purposes only

pragma solidity 0.8.25;

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

//
// This mock represents custom Vault owner contract, that does not have ACL e.g. Safe
//
contract CustomOwner__MockForHubViewer {
bool public shouldRevertFallback;

constructor() {
shouldRevertFallback = true;
}

function setShouldRevertFallback(bool _shouldRevertFallback) external {
shouldRevertFallback = _shouldRevertFallback;
}

fallback() external {
if (shouldRevertFallback) revert("fallback");
}

receive() external payable {
if (shouldRevertFallback) {
revert("fallback");
}
}
}
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