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

Add paginated method to get all accounts: getAccounts and totalAccounts #564

Merged
merged 1 commit into from
Oct 30, 2023
Merged
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
21 changes: 21 additions & 0 deletions contracts/prebuilts/account/utils/BaseAccountFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,27 @@ abstract contract BaseAccountFactory is IAccountFactory, Multicall {
return _baseAccountFactoryStorage().allAccounts.contains(_account);
}

/// @notice Returns the total number of accounts.
function totalAccounts() external view returns (uint256) {
return _baseAccountFactoryStorage().allAccounts.length();
}

/// @notice Returns all accounts between the given indices.
function getAccounts(uint256 _start, uint256 _end) external view returns (address[] memory accounts) {
uint256 len = _baseAccountFactoryStorage().allAccounts.length();
require(_start < _end && _end <= len, "BaseAccountFactory: invalid indices");

if (len == 0) {
return new address[](0);
}

accounts = new address[](_end - _start);

for (uint256 i = _start; i < _end; i += 1) {
accounts[i] = _baseAccountFactoryStorage().allAccounts.at(i);
}
}

/// @notice Returns all accounts created on the factory.
function getAllAccounts() external view returns (address[] memory) {
return _baseAccountFactoryStorage().allAccounts.values();
Expand Down
1 change: 1 addition & 0 deletions src/test/smart-wallet/Account.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ contract SimpleAccountTest is BaseTest {

address[] memory allAccounts = accountFactory.getAllAccounts();
assertEq(allAccounts.length, amount);
assertEq(accountFactory.totalAccounts(), amount);

for (uint256 i = 0; i < amount; i += 1) {
assertEq(
Expand Down