Skip to content

Commit 7e43ba0

Browse files
authored
refactor: faucet drip updates (#36)
* update drip to be onlyOwner and allow caller to provide rate limiting lookup key Signed-off-by: Aaron Sutula <[email protected]> * support multiple keys in drip Signed-off-by: Aaron Sutula <[email protected]> * longer rate limit time Signed-off-by: Aaron Sutula <[email protected]> * update test for longer rate limit time Signed-off-by: Aaron Sutula <[email protected]> * update rate limit interval and drip amount Signed-off-by: Aaron Sutula <[email protected]> --------- Signed-off-by: Aaron Sutula <[email protected]> Co-authored-by: Aaron Sutula <[email protected]>
1 parent 2b0b3dd commit 7e43ba0

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

src/token/Faucet.sol

+16-10
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ 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
30-
uint256 internal _dripAmount = 100;
30+
uint256 internal _dripAmount = 18;
3131

3232
/// @dev Initializes the Faucet contract
3333
/// @dev Sets the contract deployer as the initial owner
@@ -58,16 +58,22 @@ 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) {
63-
revert TryLater();
61+
/// @param keys Array of keys to identify the recipient used in the _nextRequestAt mapping
62+
function drip(address payable recipient, string[] calldata keys) external onlyOwner {
63+
uint256 keysLength = keys.length;
64+
uint256 amount = _dripAmount;
65+
for (uint256 i = 0; i < keysLength; i++) {
66+
if (_nextRequestAt[keys[i]] > block.timestamp) {
67+
revert TryLater();
68+
}
6469
}
65-
if (address(this).balance < _dripAmount) {
70+
if (address(this).balance < amount) {
6671
revert FaucetEmpty();
6772
}
68-
69-
_nextRequestAt[recipient] = block.timestamp + (5 minutes);
70-
recipient.transfer(_dripAmount);
73+
for (uint256 i = 0; i < keysLength; i++) {
74+
_nextRequestAt[keys[i]] = block.timestamp + (12 hours);
75+
}
76+
recipient.transfer(amount);
7177
}
7278

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

test/Faucet.t.sol

+14-6
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,48 @@ contract FaucetTest is Test {
1414
Faucet internal faucet;
1515
Vm.Wallet internal wallet;
1616
Vm.Wallet internal chain;
17+
address internal owner;
1718
address internal constant TESTER = address(0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38);
1819
uint256 internal constant MINT_AMOUNT = 1000 * 10 ** 18;
20+
string[] internal keys = ["test1", "test2"];
1921

2022
function setUp() public virtual {
2123
chain = vm.createWallet("chain");
2224
vm.deal(chain.addr, MINT_AMOUNT);
2325
wallet = vm.createWallet("user");
2426
FaucetDeployer faucetDeployer = new FaucetDeployer();
2527
faucet = faucetDeployer.run(MINT_AMOUNT / 2);
28+
owner = faucet.owner();
2629
assertEq(faucet.supply(), MINT_AMOUNT / 2);
2730
}
2831

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

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

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

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

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

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

48-
vm.warp(block.timestamp + (5 minutes));
55+
vm.warp(block.timestamp + (12 hours));
4956

50-
faucet.drip(payable(wallet.addr));
57+
faucet.drip(payable(wallet.addr), keys);
58+
vm.stopPrank();
5159

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

0 commit comments

Comments
 (0)