diff --git a/src/Faucet.sol b/src/Faucet.sol index cecdeaf..d45a308 100644 --- a/src/Faucet.sol +++ b/src/Faucet.sol @@ -25,5 +25,8 @@ contract Faucet { emit Withdrawal(msg.sender, withdraw_amount); } + /** + * @notice Receive ether, people can send ether that they are not gonna use to this contract + */ receive() external payable {} } diff --git a/test/Faucet.t.sol b/test/Faucet.t.sol index c4b1646..ff74527 100644 --- a/test/Faucet.t.sol +++ b/test/Faucet.t.sol @@ -3,12 +3,43 @@ pragma solidity ^0.8.24; import {Test} from "forge-std/Test.sol"; import {Faucet} from "src/Faucet.sol"; +import {Deploy} from "script/Deploy.s.sol"; contract FaucetTest is Test { + Faucet faucet; - function test_withdraw() public { - Faucet faucet = new Faucet(); - faucet.deposit{value: 1 ether}(); + address testUser = makeAddr("testUser"); + + function setup() public { + Deploy deploy = new Deploy(); + faucet = deploy.run(); + + //transfer some funds to the faucet + //put it here, so for the production, we can fund the faucet manually, for security reasons. + vm.startBroadcast(); + vm.deal(address(faucet), 100 ether); + vm.stopBroadcast(); + } + + + function test_withdrawTooLarge() public { + vm.prank(testUser); + vm.expectRevert(Faucet.Faucet__amountTooLarge); + faucet.withdraw(10 ether); + } + + function test_withdraw(uint amount) public { + vm.prank(testUser); + uint _amount = amount % 0.1 ether; + faucet.withdraw(_amount); + assertEq(address(testUser).balance, _amount); + } + + function test_rateLimit() public { + vm.prank(testUser); + faucet.withdraw(0.01 ether); + vm.expectRevert(Faucet.Faucet__rateLimt); + faucet.withdraw(0.01 ether); } } \ No newline at end of file