Skip to content

Commit 86ca669

Browse files
committed
update drip to be onlyOwner and allow caller to provide rate limiting lookup key
Signed-off-by: Aaron Sutula <[email protected]>
1 parent ac3edbc commit 86ca669

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/Faucet.sol

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ event Funding(address indexed from, uint256 value);
2323
/// @title Faucet Contract
2424
/// @dev A simple faucet contract for distributing tokens
2525
contract Faucet is Ownable {
26-
/// @dev Mapping to store the next allowed request time for each address
27-
mapping(address => uint256) internal _nextRequestAt;
26+
/// @dev Mapping to store the next allowed request time for each key
27+
mapping(string => uint256) internal _nextRequestAt;
2828

2929
/// @dev Amount of tokens to drip per request
3030
uint256 internal _dripAmount = 100;
@@ -58,16 +58,18 @@ contract Faucet is Ownable {
5858
/// @dev Distributes tokens to the specified recipient
5959
/// @dev Reverts if the recipient has requested tokens too recently
6060
/// @param recipient The address to receive the tokens
61-
function drip(address payable recipient) external {
62-
if (_nextRequestAt[recipient] > block.timestamp) {
61+
/// @param key The key to identify the recipient used in the _nextRequestAt mapping
62+
function drip(address payable recipient, string calldata key) external onlyOwner {
63+
uint256 nextRequestAt = _nextRequestAt[key];
64+
if (nextRequestAt > block.timestamp) {
6365
revert TryLater();
6466
}
65-
if (address(this).balance < _dripAmount) {
67+
uint256 amount = _dripAmount;
68+
if (address(this).balance < amount) {
6669
revert FaucetEmpty();
6770
}
68-
69-
_nextRequestAt[recipient] = block.timestamp + (5 minutes);
70-
recipient.transfer(_dripAmount);
71+
_nextRequestAt[key] = block.timestamp + (5 minutes);
72+
recipient.transfer(amount);
7173
}
7274

7375
/// @dev Sets the amount of tokens to distribute per request

test/Faucet.t.sol

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ contract FaucetTest is Test {
1414
Faucet internal faucet;
1515
Vm.Wallet internal wallet;
1616
Vm.Wallet internal chain;
17+
address owner;
1718
address constant tester = address(0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38);
19+
string constant key = "test";
1820
uint256 constant mintAmount = 1000 * 10 ** 18;
1921

2022
function setUp() public virtual {
@@ -23,31 +25,37 @@ contract FaucetTest is Test {
2325
wallet = vm.createWallet("user");
2426
FaucetDeployer faucetDeployer = new FaucetDeployer();
2527
faucet = faucetDeployer.run("local", mintAmount / 2);
28+
owner = faucet.owner();
2629
assertEq(faucet.supply(), mintAmount / 2);
2730
}
2831

2932
function test_DripTransfer() public {
3033
assertEq(wallet.addr.balance, 0);
3134

32-
faucet.drip(payable(wallet.addr));
35+
vm.prank(owner);
36+
faucet.drip(payable(wallet.addr), key);
3337

3438
assertEq(faucet.supply(), mintAmount / 2 - faucet.dripAmount());
3539
assertEq(wallet.addr.balance, faucet.dripAmount());
3640
}
3741

3842
function test_DripTransferNoDelayFail() public {
39-
faucet.drip(payable(wallet.addr));
43+
vm.prank(owner);
44+
faucet.drip(payable(wallet.addr), key);
4045

4146
vm.expectRevert(TryLater.selector);
42-
faucet.drip(payable(wallet.addr));
47+
vm.prank(owner);
48+
faucet.drip(payable(wallet.addr), key);
4349
}
4450

4551
function test_DripTransferDelay() public {
46-
faucet.drip(payable(wallet.addr));
52+
vm.startPrank(owner);
53+
faucet.drip(payable(wallet.addr), key);
4754

4855
vm.warp(block.timestamp + (5 minutes));
49-
50-
faucet.drip(payable(wallet.addr));
56+
57+
faucet.drip(payable(wallet.addr), key);
58+
vm.stopPrank();
5159

5260
assertEq(wallet.addr.balance, 2 * faucet.dripAmount());
5361
}

0 commit comments

Comments
 (0)