Skip to content

Commit

Permalink
add setMinBalanceForTransfer, addBalances (#73)
Browse files Browse the repository at this point in the history
* add `setMinBalanceForTransfer`, `addBalances`

* Update src/ImpactEvaluator.sol

Co-authored-by: Miroslav Bajtoš <oss@bajtos.net>

* fix test

* fix `balanceHeld`

---------

Co-authored-by: Miroslav Bajtoš <oss@bajtos.net>
  • Loading branch information
juliangruber and bajtos authored Jan 10, 2024
1 parent 3387f01 commit 5a17940
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ will be called and the round finishes.

### `.setMaxTransfersPerTx(uint max)`

### `setMinBalanceForTransfer(uint min)`

### `addBalances(address[] addresses, uint[] balances)`

### `.releaseRewards()`

## Getters / Views
Expand Down
6 changes: 5 additions & 1 deletion src/Balances.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ contract Balances {
address payable[] public readyForTransfer;
address payable[] public scheduledForTransfer;
uint public maxTransfersPerTx = 10;
uint public constant minBalanceForTransfer = 0.5 ether;
uint public minBalanceForTransfer = 0.5 ether;

event Transfer(address indexed to, uint256 amount);
event TransferFailed(address indexed to, uint256 amount);
Expand Down Expand Up @@ -79,4 +79,8 @@ contract Balances {
function _setMaxTransfersPerTx(uint _maxTransfersPerTx) internal {
maxTransfersPerTx = _maxTransfersPerTx;
}

function _setMinBalanceForTransfer(uint _minBalanceForTransfer) internal {
minBalanceForTransfer = _minBalanceForTransfer;
}
}
24 changes: 24 additions & 0 deletions src/ImpactEvaluator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,30 @@ contract ImpactEvaluator is AccessControl, Balances {
_setMaxTransfersPerTx(_maxTransfersPerTx);
}

function setMinBalanceForTransfer(
uint _minBalanceForTransfer
) public onlyAdmin {
_setMinBalanceForTransfer(_minBalanceForTransfer);
}

function addBalances(
address payable[] calldata addresses,
uint[] calldata _balances
) public payable onlyAdmin {
uint totalAmount = 0;
for (uint i = 0; i < _balances.length; i++) {
totalAmount += _balances[i];
}
require(
msg.value == totalAmount,
"Sum of balances must match msg.value"
);
for (uint i = 0; i < addresses.length; i++) {
increaseParticipantBalance(addresses[i], _balances[i]);
}
balanceHeld += totalAmount;
}

function tick() public {
if (block.number >= currentRoundEndBlockNumber) {
advanceRound();
Expand Down
42 changes: 42 additions & 0 deletions test/ImpactEvaluator.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -565,4 +565,46 @@ contract ImpactEvaluatorTest is Test {
impactEvaluator.setScores(1, addresses, scores);
}
}

function test_SetMinBalanceForTransfer() public {
ImpactEvaluator impactEvaluator = new ImpactEvaluator(address(this));
assertEq(impactEvaluator.minBalanceForTransfer(), 0.5 ether);
impactEvaluator.setMinBalanceForTransfer(1 ether);
assertEq(impactEvaluator.minBalanceForTransfer(), 1 ether);
impactEvaluator.setMinBalanceForTransfer(0);
assertEq(impactEvaluator.minBalanceForTransfer(), 0);
}

function test_SetMinBalanceForTransferNotAdmin() public {
ImpactEvaluator impactEvaluator = new ImpactEvaluator(address(0x1));
vm.expectRevert("Not an admin");
impactEvaluator.setMinBalanceForTransfer(1 ether);
}

function test_AddBalances() public {
ImpactEvaluator impactEvaluator = new ImpactEvaluator(address(this));

address payable[] memory addresses = new address payable[](2);
addresses[0] = payable(vm.addr(1));
addresses[1] = payable(vm.addr(2));
uint[] memory balances = new uint[](2);
balances[0] = 50 ether;
balances[1] = 50 ether;

vm.expectRevert("Sum of balances must match msg.value");
impactEvaluator.addBalances{ value: 0 }(addresses, balances);

impactEvaluator.addBalances{ value: 100 ether }(addresses, balances);
assertEq(
impactEvaluator.rewardsScheduledFor(addresses[0]),
50 ether,
"addresses[0] balance"
);
assertEq(
impactEvaluator.rewardsScheduledFor(addresses[1]),
50 ether,
"addresses[1] balance"
);
assertEq(impactEvaluator.balanceHeld(), 100 ether);
}
}

0 comments on commit 5a17940

Please sign in to comment.