Skip to content

Commit

Permalink
Fix balanceHeld (#74)
Browse files Browse the repository at this point in the history
* fix `tick()` not updating `balanceHeld`

* DRY the dangerous code path
  • Loading branch information
juliangruber authored Feb 1, 2024
1 parent aeee747 commit 545c2eb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
3 changes: 1 addition & 2 deletions src/ImpactEvaluator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ contract ImpactEvaluator is AccessControl, Balances {
if (block.number >= currentRoundEndBlockNumber) {
advanceRound();
}
transferScheduled();
balanceHeld -= transferScheduled();
}

function addMeasurements(
Expand Down Expand Up @@ -151,7 +151,6 @@ contract ImpactEvaluator is AccessControl, Balances {

function releaseRewards() public onlyAdmin {
_releaseRewards();
balanceHeld -= transferScheduled();
}

function availableBalance() public view returns (uint) {
Expand Down
19 changes: 16 additions & 3 deletions test/ImpactEvaluator.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ contract ImpactEvaluatorTest is Test {
impactEvaluator.setScores(1, addresses, scores);
assertEq(vm.addr(1).balance, 0);
impactEvaluator.releaseRewards();
assertEq(vm.addr(1).balance, 0);
impactEvaluator.tick();
assertEq(vm.addr(1).balance, 100 ether);

impactEvaluator.revokeRole(
Expand Down Expand Up @@ -390,6 +392,7 @@ contract ImpactEvaluatorTest is Test {
impactEvaluator.releaseRewards();
vm.expectRevert("Scheduled transfers still pending");
impactEvaluator.releaseRewards();
impactEvaluator.tick();

assertEq(impactEvaluator.participantCountReadyForTransfer(), 0);
for (uint i = 0; i < 10; i++) {
Expand Down Expand Up @@ -456,15 +459,18 @@ contract ImpactEvaluatorTest is Test {

function test_BalanceHeld() public {
ImpactEvaluator impactEvaluator = new ImpactEvaluator(address(this));
impactEvaluator.setMaxTransfersPerTx(1);
vm.deal(payable(address(impactEvaluator)), 100 ether);
assertEq(impactEvaluator.balanceHeld(), 0);

impactEvaluator.adminAdvanceRound();
impactEvaluator.adminAdvanceRound();
address payable[] memory addresses = new address payable[](1);
address payable[] memory addresses = new address payable[](2);
addresses[0] = payable(vm.addr(1));
uint[] memory scores = new uint[](1);
scores[0] = impactEvaluator.MAX_SCORE();
addresses[1] = payable(vm.addr(2));
uint[] memory scores = new uint[](2);
scores[0] = impactEvaluator.MAX_SCORE() / 2;
scores[1] = impactEvaluator.MAX_SCORE() / 2;
impactEvaluator.setScores(1, addresses, scores);
assertEq(
impactEvaluator.balanceHeld(),
Expand All @@ -480,6 +486,10 @@ contract ImpactEvaluatorTest is Test {
);

impactEvaluator.releaseRewards();
assertEq(impactEvaluator.balanceHeld(), 100 ether);
impactEvaluator.tick();
assertEq(impactEvaluator.balanceHeld(), 50 ether);
impactEvaluator.tick();
assertEq(impactEvaluator.balanceHeld(), 0);
}

Expand Down Expand Up @@ -532,6 +542,7 @@ contract ImpactEvaluatorTest is Test {
impactEvaluator.adminAdvanceRound();
impactEvaluator.setScores(2, addresses, scores);
impactEvaluator.releaseRewards();
impactEvaluator.tick();
assertEq(vm.addr(1).balance, 0.8 ether);

assertEq(impactEvaluator.minBalanceForTransfer(), 0.5 ether);
Expand All @@ -551,12 +562,14 @@ contract ImpactEvaluatorTest is Test {
scores[0] = impactEvaluator.MAX_SCORE() / 2;
impactEvaluator.setScores(1, addresses, scores);
impactEvaluator.releaseRewards();
impactEvaluator.tick();
assertEq(vm.addr(1).balance, 50 ether, "round 1");

impactEvaluator.adminAdvanceRound();

impactEvaluator.setScores(2, addresses, scores);
impactEvaluator.releaseRewards();
impactEvaluator.tick();
assertEq(vm.addr(1).balance, 100 ether, "round 2");
}

Expand Down

0 comments on commit 545c2eb

Please sign in to comment.