Skip to content

Commit d6bba75

Browse files
authored
Fix getAccounts (#565)
fix getAccounts
1 parent bd54c96 commit d6bba75

File tree

2 files changed

+59
-8
lines changed

2 files changed

+59
-8
lines changed

contracts/prebuilts/account/utils/BaseAccountFactory.sol

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,16 @@ abstract contract BaseAccountFactory is IAccountFactory, Multicall {
119119

120120
/// @notice Returns all accounts between the given indices.
121121
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-
}
122+
require(
123+
_start < _end && _end <= _baseAccountFactoryStorage().allAccounts.length(),
124+
"BaseAccountFactory: invalid indices"
125+
);
128126

127+
uint256 len = _end - _start;
129128
accounts = new address[](_end - _start);
130129

131-
for (uint256 i = _start; i < _end; i += 1) {
132-
accounts[i] = _baseAccountFactoryStorage().allAccounts.at(i);
130+
for (uint256 i = 0; i < len; i += 1) {
131+
accounts[i] = _baseAccountFactoryStorage().allAccounts.at(i + _start);
133132
}
134133
}
135134

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,14 @@ contract SimpleAccountTest is BaseTest {
322322

323323
/// @dev Create more than one accounts with the same admin.
324324
function test_state_createAccount_viaEntrypoint_multipleAccountSameAdmin() public {
325+
uint256 start = 0;
326+
uint256 end = 0;
327+
328+
assertEq(accountFactory.totalAccounts(), 0);
329+
330+
vm.expectRevert("BaseAccountFactory: invalid indices");
331+
address[] memory accs = accountFactory.getAccounts(start, end);
332+
325333
uint256 amount = 100;
326334

327335
for (uint256 i = 0; i < amount; i += 1) {
@@ -365,6 +373,50 @@ contract SimpleAccountTest is BaseTest {
365373
)
366374
);
367375
}
376+
377+
start = 25;
378+
end = 75;
379+
380+
address[] memory accountsPaginatedOne = accountFactory.getAccounts(start, end);
381+
382+
for (uint256 i = 0; i < (end - start); i += 1) {
383+
assertEq(
384+
accountsPaginatedOne[i],
385+
Clones.predictDeterministicAddress(
386+
accountFactory.accountImplementation(),
387+
_generateSalt(accountAdmin, bytes(abi.encode(start + i))),
388+
address(accountFactory)
389+
)
390+
);
391+
}
392+
393+
start = 0;
394+
end = amount;
395+
396+
address[] memory accountsPaginatedTwo = accountFactory.getAccounts(start, end);
397+
398+
for (uint256 i = 0; i < (end - start); i += 1) {
399+
assertEq(
400+
accountsPaginatedTwo[i],
401+
Clones.predictDeterministicAddress(
402+
accountFactory.accountImplementation(),
403+
_generateSalt(accountAdmin, bytes(abi.encode(start + i))),
404+
address(accountFactory)
405+
)
406+
);
407+
}
408+
409+
start = 75;
410+
end = 25;
411+
412+
vm.expectRevert("BaseAccountFactory: invalid indices");
413+
accs = accountFactory.getAccounts(start, end);
414+
415+
start = 25;
416+
end = amount + 1;
417+
418+
vm.expectRevert("BaseAccountFactory: invalid indices");
419+
accs = accountFactory.getAccounts(start, end);
368420
}
369421

370422
/*///////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)