Skip to content

Commit

Permalink
Add batch balance read
Browse files Browse the repository at this point in the history
  • Loading branch information
dulguun-staderlabs committed Apr 26, 2024
1 parent 61076f9 commit 5b0a5cc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
34 changes: 23 additions & 11 deletions contracts/BatchReader.sol
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.16;
pragma solidity ^0.8.16;

import "./interfaces/IPenalty.sol";
import "./interfaces/IStaderConfig.sol";
import "./interfaces/IBatchReader.sol";

/// @title Batch Reader Contract
/// @notice Provides batch operations for reading penalties and balances
contract BatchReader is IBatchReader {
IPenalty public penaltyContract;
IStaderConfig immutable staderConfig;
IPenalty public immutable PENALTY_CONTRACT;

/// @param _staderConfig Address of the StaderConfig contract
constructor(address _staderConfig) {
require(_staderConfig != address(0), "Invalid StaderConfig address");
require(_staderConfig != address(0), "BatchReader: invalid StaderConfig address");

staderConfig = IStaderConfig(_staderConfig);
penaltyContract = IPenalty(staderConfig.getPenaltyContract());
PENALTY_CONTRACT = IPenalty(IStaderConfig(_staderConfig).getPenaltyContract());
}

/// @notice Reads the total penalty amounts for a batch of validators.
function getTotalPenaltyAmounts(bytes[] calldata _pubkeys) external view override returns (uint256[] memory) {
uint256[] memory penalties = new uint256[](_pubkeys.length);
/// @notice Reads the total penalty amounts for a batch of validators
/// @param _pubkeys Public keys of the validators
/// @return penalties Array of total penalty amounts for each validator
function getTotalPenaltyAmounts(bytes[] calldata _pubkeys) external view override returns (uint256[] memory penalties) {
penalties = new uint256[](_pubkeys.length);
for (uint256 i = 0; i < _pubkeys.length; i++) {
penalties[i] = penaltyContract.totalPenaltyAmount(_pubkeys[i]);
penalties[i] = PENALTY_CONTRACT.totalPenaltyAmount(_pubkeys[i]);
}
}

/// @notice Gets balances for multiple addresses
/// @param _addresses Array of Ethereum addresses
/// @return balances Array of balances for the provided addresses
function getBalances(address[] calldata _addresses) external view override returns (uint256[] memory balances) {
balances = new uint256[](_addresses.length);
for (uint256 i = 0; i < _addresses.length; i++) {
balances[i] = _addresses[i].balance;
}
return penalties;
}
}
2 changes: 2 additions & 0 deletions contracts/interfaces/IBatchReader.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ pragma solidity 0.8.16;

interface IBatchReader {
function getTotalPenaltyAmounts(bytes[] calldata _pubkeys) external view returns (uint256[] memory);

function getBalances(address[] calldata _addresses) external view returns (uint256[] memory);
}

0 comments on commit 5b0a5cc

Please sign in to comment.