-
Notifications
You must be signed in to change notification settings - Fork 226
[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
Closed
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 3e13053
Merge branch 'feat/vaults-suggestions' of github.com:lidofinance/core…
DiRaiks 5694607
fix: vaultHubView codestyle
Jeday e794d76
fix: add contract check
Jeday 744cc38
fix: update isOwner method
DiRaiks e9b6c01
fix: _checkHasRole helper
Jeday 97e484e
Merge branch 'feat/hub-viewer' of github.com:lidofinance/core into fe…
Jeday 3f3318f
Merge branch 'feat/vaults' of github.com:lidofinance/core into feat/h…
Jeday 6002ca4
Merge branch 'feat/vaults' of github.com:lidofinance/core into feat/h…
Jeday 51c4ffb
feat: add pagination to vault hub viewer
Jeday 322614a
fix: bound methods
Jeday 91f22c6
feat(VaultHubViewerV1): add vaultsConnected and vaultsConnectedBound
solidovic 1be1b66
fix(VaultHubViewerV1): fix params of vaultsConnected
solidovic b80b38c
fix(tests): intermediate commit for running tests
solidovic bdbb092
feat: update vault filtering and methods, tests
DiRaiks bb460a9
Merge pull request #950 from lidofinance/fix/hub-viewer-methods-tests
solidovic 548f1c2
fix: improve pagination range validation and filtering
DiRaiks 3237186
feat: add TODO comments for vault pagination
DiRaiks 5184c98
Merge pull request #951 from lidofinance/fix/hub-viewer-filtering
DiRaiks 8b4662b
Merge pull request #948 from lidofinance/feat/hub-viewer-get-vaults-c…
solidovic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
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); | ||
} |
31 changes: 31 additions & 0 deletions
31
test/0.8.25/vaults/vault-hub-viewer/contracts/CustomOwner__MockForHubViewer.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
test/0.8.25/vaults/vault-hub-viewer/contracts/VaultHub__MockForHubViewer.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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, becauseStakingVault is IStakingVault, ..., Ownable