Skip to content

Instantly share code, notes, and snippets.

@codetit4n
Last active September 2, 2023 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codetit4n/8cc430a9f2bbe7cc17a4ba9f9bfcb392 to your computer and use it in GitHub Desktop.
Save codetit4n/8cc430a9f2bbe7cc17a4ba9f9bfcb392 to your computer and use it in GitHub Desktop.
Simple solidity faucet for funding wallets
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;
/// @title Solidity Faucet
/// @author Lokesh Kumar (https://github.com/codeTIT4N)
/// @notice This is a simple faucet contract
/// @dev Has a cooldown period for every wallet of 24 hrs
/// @dev Keeps a reserve in case the fund sender(authorized address) runs out of gas while funding wallets
contract Faucet {
address public authorizedAddress;
// If the authorized address's balance goes below this value, the gas reserve funds will be sent to that address
uint256 public immutable MIN_BAL_AUTHORIZED;
uint256 public immutable GAS_RESERVE;
uint256 public maxWithdrawPerDay = 10 ether;
mapping (address => uint256) public lastFundedTimestamp;
constructor(address _authorized) payable {
authorizedAddress = _authorized;
MIN_BAL_AUTHORIZED = 0.2 ether;
GAS_RESERVE = 1 ether;
require(msg.value == GAS_RESERVE, "Faucet must have gas reserve funds!");
}
modifier onlyAuthorized(){
require(msg.sender == authorizedAddress, "Not authorized!");
_;
}
function setAuthorized(address _newAddress) onlyAuthorized external{
require(_newAddress != address(0), "Authorized address can't be null!");
authorizedAddress = _newAddress;
}
function setMaxWithdrawPerDay(uint256 _newMax) onlyAuthorized external{
require(_newMax > 0, "Max withdraw per day can't be zero!");
maxWithdrawPerDay = _newMax;
}
function fundAddress(address _to) external onlyAuthorized {
require(address(this).balance >= maxWithdrawPerDay + GAS_RESERVE, "Not enugh funds in faucet!");
if(lastFundedTimestamp[_to] > 0){
require(block.timestamp - lastFundedTimestamp[_to] >= 1 days, "Cooldown period!");
}
// in case authorized address runs out of funds for paying gas
if ((authorizedAddress).balance <= MIN_BAL_AUTHORIZED) {
payable(authorizedAddress).transfer(GAS_RESERVE);
}
lastFundedTimestamp[_to] = block.timestamp;
payable(_to).transfer(maxWithdrawPerDay);
}
function depositFunds() external payable {
require(msg.value > 0, "Can't deposit zero funds!");
}
receive() external payable { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment