-
Notifications
You must be signed in to change notification settings - Fork 195
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
DiRaiks
wants to merge
7
commits into
feat/vaults
Choose a base branch
from
feat/hub-viewer
base: feat/vaults
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+568
−0
Open
Changes from all commits
Commits
Show all changes
7 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 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 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,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); | ||
} | ||
|
||
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); | ||
} |
31 changes: 31 additions & 0 deletions
31
test/0.8.25/vaults/vault-hub-viewer/contracts/CustomOwner__MockForHubViewer.sol
This file contains 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"); | ||
} | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
test/0.8.25/vaults/vault-hub-viewer/contracts/VaultHub__MockForHubViewer.sol
This file contains 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,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 | ||
} | ||
} | ||
} |
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