Skip to content

[SI] VaultHubViewer contract #905

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

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
136feba
feat: add VaultHubViewer contract, add tests
DiRaiks Dec 23, 2024
3e13053
Merge branch 'feat/vaults-suggestions' of github.com:lidofinance/core…
DiRaiks Dec 24, 2024
5694607
fix: vaultHubView codestyle
Jeday Dec 24, 2024
e794d76
fix: add contract check
Jeday Dec 24, 2024
744cc38
fix: update isOwner method
DiRaiks Dec 25, 2024
e9b6c01
fix: _checkHasRole helper
Jeday Dec 25, 2024
97e484e
Merge branch 'feat/hub-viewer' of github.com:lidofinance/core into fe…
Jeday Dec 25, 2024
3f3318f
Merge branch 'feat/vaults' of github.com:lidofinance/core into feat/h…
Jeday Jan 15, 2025
6002ca4
Merge branch 'feat/vaults' of github.com:lidofinance/core into feat/h…
Jeday Jan 23, 2025
51c4ffb
feat: add pagination to vault hub viewer
Jeday Jan 24, 2025
322614a
fix: bound methods
Jeday Jan 28, 2025
91f22c6
feat(VaultHubViewerV1): add vaultsConnected and vaultsConnectedBound
solidovic Feb 16, 2025
1be1b66
fix(VaultHubViewerV1): fix params of vaultsConnected
solidovic Feb 17, 2025
b80b38c
fix(tests): intermediate commit for running tests
solidovic Feb 18, 2025
bdbb092
feat: update vault filtering and methods, tests
DiRaiks Feb 18, 2025
bb460a9
Merge pull request #950 from lidofinance/fix/hub-viewer-methods-tests
solidovic Feb 18, 2025
548f1c2
fix: improve pagination range validation and filtering
DiRaiks Feb 18, 2025
3237186
feat: add TODO comments for vault pagination
DiRaiks Feb 18, 2025
5184c98
Merge pull request #951 from lidofinance/fix/hub-viewer-filtering
DiRaiks Feb 18, 2025
8b4662b
Merge pull request #948 from lidofinance/feat/hub-viewer-get-vaults-c…
solidovic Feb 18, 2025
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
252 changes: 252 additions & 0 deletions contracts/0.8.25/vaults/VaultHubViewerV1.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
// 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 {VaultHub} from "./VaultHub.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

}

contract VaultHubViewerV1 {
bytes32 constant strictTrue = keccak256(hex"0000000000000000000000000000000000000000000000000000000000000001");

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

constructor(address _vaultHubAddress) {
if (_vaultHubAddress == address(0)) revert ZeroArgument("_vaultHubAddress");
vaultHub = VaultHub(_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) {
(IVault[] memory vaults, uint256 valid) = _vaultsByOwner(_owner);

return _filterNonZeroVaults(vaults, 0, valid);
}

/// @notice Returns all vaults owned by a given address
/// @param _owner Address of the owner
/// @param _from Index to start from inclisive
/// @param _to Index to end at non-inculsive
/// @return array of vaults owned by the given address
/// @return number of leftover vaults in range
function vaultsByOwnerBound(
address _owner,
uint256 _from,
uint256 _to
) public view returns (IVault[] memory, uint256) {
(IVault[] memory vaults, uint256 valid) = _vaultsByOwner(_owner);

uint256 count = valid > _to ? _to : valid;
uint256 leftover = valid > _to ? valid - _to : 0;

return (_filterNonZeroVaults(vaults, _from, count), leftover);
}

/// @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) {
(IVault[] memory vaults, uint256 valid) = _vaultsByRole(_role, _member);

return _filterNonZeroVaults(vaults, 0, valid);
}

/// @notice Returns all vaults with a given role on a given address
/// @param _role Role to check
/// @param _member Address to check
/// @param _from Index to start from inclisive
/// @param _to Index to end at non-inculsive
/// @return array of vaults in range with the given role on the given address
/// @return number of leftover vaults
function vaultsByRoleBound(
bytes32 _role,
address _member,
uint256 _from,
uint256 _to
) public view returns (IVault[] memory, uint256) {
(IVault[] memory vaults, uint256 valid) = _vaultsByRole(_role, _member);

uint256 count = valid > _to ? _to : valid;
uint256 leftover = valid > _to ? valid - _to : 0;

return (_filterNonZeroVaults(vaults, _from, count), leftover);
}

/// @notice Returns all connected vaults
/// @return array of connected vaults
function vaultsConnected() public view returns (IVault[] memory) {
(IVault[] memory vaults, uint256 valid) = _vaultsConnected();

return _filterNonZeroVaults(vaults, 0, valid);
}

/// @notice Returns all connected vaults within a range
/// @param _from Index to start from inclisive
/// @param _to Index to end at non-inculsive
/// @return array of connected vaults
/// @return number of leftover connected vaults
function vaultsConnectedBound(
uint256 _from,
uint256 _to
) public view returns (IVault[] memory, uint256) {
(IVault[] memory vaults, uint256 valid) = _vaultsConnected();

uint256 count = valid > _to ? _to : valid;
uint256 leftover = valid > _to ? valid - _to : 0;

return (_filterNonZeroVaults(vaults, _from, count), leftover);
}

// ==================== Internal Functions ====================

/// @dev common logic for vaultsConnected and vaultsConnectedBound
function _vaultsConnected() internal view returns (IVault[] memory, uint256) {
// TODO: get vaults by pages, not all vaults
uint256 count = vaultHub.vaultsCount();
IVault[] memory vaults = new IVault[](count);

uint256 valid = 0;
for (uint256 i = 0; i < count; i++) {
if (!vaultHub.vaultSocket(i).isDisconnected) {
vaults[valid] = IVault(vaultHub.vault(i));
valid++;
}
}

return (vaults, valid);
}

/// @dev common logic for vaultsByRole and vaultsByRoleBound
function _vaultsByRole(bytes32 _role, address _member) internal view returns (IVault[] memory, uint256) {
// TODO: get vaults by pages, not all vaults
uint256 count = vaultHub.vaultsCount();
IVault[] memory vaults = new IVault[](count);

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

return (vaults, valid);
}

/// @dev common logic for vaultsByOwner and vaultsByOwnerBound
function _vaultsByOwner(address _owner) internal view returns (IVault[] memory, uint256) {
// TODO: get vaults by pages, not all vaults
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] = IVault(vaultHub.vault(i));
valid++;
}
}
return (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
/// @return filtered An array of non-zero vaults
function _filterNonZeroVaults(
IVault[] memory _vaults,
uint256 _from,
uint256 _to
) internal pure returns (IVault[] memory filtered) {
if (_to < _from) revert WrongPaginationRange(_from, _to);

uint256 count = _to - _from;
filtered = new IVault[](count);
for (uint256 i = 0; i < count; i++) {
filtered[i] = _vaults[_from + i];
}
}

// ==================== Errors ====================

/// @notice Error for zero address arguments
/// @param argName Name of the argument that is zero
error ZeroArgument(string argName);

/// @notice Error for wrong pagination range
/// @param _from Start of the range
/// @param _to End of the range
error WrongPaginationRange(uint256 _from, uint256 _to);
}
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,105 @@
// 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 vaultSocket(uint256 _index) external view returns (VaultHub.VaultSocket memory) {
return _getVaultHubStorage().sockets[_index];
}

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