Skip to content

Commit 953ae66

Browse files
committed
Remove allocator if allowance equal 0
1 parent e9f60c9 commit 953ae66

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Allocator.sol

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,12 @@ contract Allocator is Initializable, Ownable2StepUpgradeable, UUPSUpgradeable, I
112112
revert AlreadyZero();
113113
} else if (allowanceBefore < amount) {
114114
amount = allowanceBefore;
115+
_allocators.remove(allocator);
116+
} else if (allowanceBefore == amount) {
117+
_allocators.remove(allocator);
118+
} else {
119+
_allocators.set(allocator, allowanceBefore - amount);
115120
}
116-
_allocators.set(allocator, allowanceBefore - amount);
117121
emit AllowanceChanged(allocator, allowanceBefore, allowance(allocator));
118122
}
119123

test/Allocator.t.sol

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,20 @@ contract AllocatorTest is Test {
301301
emit IAllocator.AllowanceChanged(vm.addr(1), 100, 50);
302302
allocator.decreaseAllowance(vm.addr(1), 50);
303303
}
304+
305+
function testDecreaseAllowanceRemoveAllocator() public {
306+
allocator.setAllowance(vm.addr(1), 100);
307+
allocator.setAllowance(vm.addr(2), 100);
308+
address[] memory allocators = allocator.getAllocators();
309+
assertEq(allocators[0], vm.addr(1));
310+
assertEq(allocators[1], vm.addr(2));
311+
assertEq(allocators.length, 2);
312+
allocator.decreaseAllowance(vm.addr(1), 150);
313+
allocators = allocator.getAllocators();
314+
assertEq(allocators[0], vm.addr(2));
315+
assertEq(allocators.length, 1);
316+
allocator.decreaseAllowance(vm.addr(2), 100);
317+
allocators = allocator.getAllocators();
318+
assertEq(allocators.length, 0);
319+
}
304320
}

0 commit comments

Comments
 (0)