Skip to content

Commit bd54c96

Browse files
authored
Add paginated method to get all accounts: getAccounts and totalAccounts (#564)
Add getAccounts and totalAccounts
1 parent c778b3c commit bd54c96

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

contracts/prebuilts/account/utils/BaseAccountFactory.sol

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,27 @@ abstract contract BaseAccountFactory is IAccountFactory, Multicall {
112112
return _baseAccountFactoryStorage().allAccounts.contains(_account);
113113
}
114114

115+
/// @notice Returns the total number of accounts.
116+
function totalAccounts() external view returns (uint256) {
117+
return _baseAccountFactoryStorage().allAccounts.length();
118+
}
119+
120+
/// @notice Returns all accounts between the given indices.
121+
function getAccounts(uint256 _start, uint256 _end) external view returns (address[] memory accounts) {
122+
uint256 len = _baseAccountFactoryStorage().allAccounts.length();
123+
require(_start < _end && _end <= len, "BaseAccountFactory: invalid indices");
124+
125+
if (len == 0) {
126+
return new address[](0);
127+
}
128+
129+
accounts = new address[](_end - _start);
130+
131+
for (uint256 i = _start; i < _end; i += 1) {
132+
accounts[i] = _baseAccountFactoryStorage().allAccounts.at(i);
133+
}
134+
}
135+
115136
/// @notice Returns all accounts created on the factory.
116137
function getAllAccounts() external view returns (address[] memory) {
117138
return _baseAccountFactoryStorage().allAccounts.values();

src/test/smart-wallet/Account.t.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ contract SimpleAccountTest is BaseTest {
353353

354354
address[] memory allAccounts = accountFactory.getAllAccounts();
355355
assertEq(allAccounts.length, amount);
356+
assertEq(accountFactory.totalAccounts(), amount);
356357

357358
for (uint256 i = 0; i < amount; i += 1) {
358359
assertEq(

0 commit comments

Comments
 (0)