Skip to content

Commit

Permalink
refactor: stricter naming convetions
Browse files Browse the repository at this point in the history
prefix _ only for internal/private variables
use suffix _  for param names in conflict with
other variables
  • Loading branch information
sirnicolaz committed Feb 19, 2024
1 parent 1b90493 commit 046d1f0
Showing 1 changed file with 40 additions and 42 deletions.
82 changes: 40 additions & 42 deletions contracts/Faucet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ contract Faucet is Ownable {
uint256 public constant MAX_WITHDRAWAL_AMOUNT = 5 ether;
address public contractRegistry;
mapping(address => bool) public isPoolEligible;
mapping(address => uint256) private lastWithdrawalTimes;
mapping(address => uint256) private _lastWithdrawalTimes;
event Deposited(address erc20Addr, uint256 amount);
event Withdrawn(address account, address erc20Addr, uint256 amount);

constructor(address _contractRegistry, address[] memory poolAddresses) {
contractRegistry = _contractRegistry;
constructor(address contractRegistry_, address[] memory poolAddresses) {
contractRegistry = contractRegistry_;

uint256 poolAddressesCount = poolAddresses.length;
for (uint256 i = 0; i < poolAddressesCount; i++) {
Expand All @@ -27,89 +27,87 @@ contract Faucet is Ownable {
}

/// @notice change the TCO2 contracts registry
/// @param _address the new contract registry address
/// @param contractRegistry_ the new contract registry address
function setToucanContractRegistry(
address _address
address contractRegistry_
) public virtual onlyOwner {
contractRegistry = _address;
contractRegistry = contractRegistry_;
}

/// @notice allows the owner to set the eligibility of a pool
/// @param _poolAddress the address of the pool
/// @param _isPoolEligible eligibility flag
/// @param poolAddress the address of the pool
/// @param isPoolEligible_ eligibility flag
function setPoolEligible(
address _poolAddress,
bool _isPoolEligible
address poolAddress,
bool isPoolEligible_
) public virtual onlyOwner {
isPoolEligible[_poolAddress] = _isPoolEligible;
isPoolEligible[poolAddress] = isPoolEligible_;
}

/// @notice A function to get the Faucet's balances of multiple tokens at once
/// @param _erc20Addresses An array of ERC20 contract addresses
/// @param erc20Addresses An array of ERC20 contract addresses
/// @return An array of balances
function getTokenBalances(
address[] memory _erc20Addresses
address[] memory erc20Addresses
) public view returns (uint256[] memory) {
uint256[] memory balances = new uint256[](_erc20Addresses.length);
for (uint256 i = 0; i < _erc20Addresses.length; i++) {
balances[i] = IERC20(_erc20Addresses[i]).balanceOf(address(this));
uint256[] memory balances = new uint256[](erc20Addresses.length);
for (uint256 i = 0; i < erc20Addresses.length; i++) {
balances[i] = IERC20(erc20Addresses[i]).balanceOf(address(this));
}
return balances;
}

/// @notice deposit tokens from caller to Faucet
/// @param _erc20Address ERC20 contract address to be deposited
/// @param _amount amount to be deposited
function deposit(address _erc20Address, uint256 _amount) public {
require(_checkEligible(_erc20Address), "Token rejected");
/// @param erc20Address ERC20 contract address to be deposited
/// @param amount amount to be deposited
function deposit(address erc20Address, uint256 amount) public {
require(_checkEligible(erc20Address), "Token rejected");

IERC20(_erc20Address).safeTransferFrom(
IERC20(erc20Address).safeTransferFrom(
msg.sender,
address(this),
_amount
amount
);

emit Deposited(_erc20Address, _amount);
emit Deposited(erc20Address, amount);
}

/// @notice checks if the Faucet is in a withdrawal timeout for the caller
/// @return true if in timeout, false if not
function checkIfWithdrawalTimeout() public view returns (bool) {
return
lastWithdrawalTimes[msg.sender] > block.timestamp - TIMEOUT_LIMIT;
_lastWithdrawalTimes[msg.sender] > block.timestamp - TIMEOUT_LIMIT;
}

/// @notice withdraw tokens from Faucet to caller
/// @param _erc20Address ERC20 contract address to be withdrawn
/// @param _amount amount to be withdrawn
function withdraw(address _erc20Address, uint256 _amount) public {
require(_checkEligible(_erc20Address), "Token rejected");
/// @param erc20Address ERC20 contract address to be withdrawn
/// @param amount amount to be withdrawn
function withdraw(address erc20Address, uint256 amount) public {
require(_checkEligible(erc20Address), "Token rejected");
require(!checkIfWithdrawalTimeout(), "Cannot withdraw that often");
lastWithdrawalTimes[msg.sender] = block.timestamp;
_lastWithdrawalTimes[msg.sender] = block.timestamp;

require(_amount <= MAX_WITHDRAWAL_AMOUNT, "Amount too high");
require(amount <= MAX_WITHDRAWAL_AMOUNT, "Amount too high");

IERC20(_erc20Address).safeTransfer(msg.sender, _amount);
IERC20(erc20Address).safeTransfer(msg.sender, amount);

emit Withdrawn(msg.sender, _erc20Address, _amount);
emit Withdrawn(msg.sender, erc20Address, amount);
}

/// @notice function that is only callable by owner and can withdraw as many tokens from this contract as they want
/// @param _erc20Address address of the token to be withdrawn
/// @param _amount amount of tokens to be withdrawn
/// @param erc20Address address of the token to be withdrawn
/// @param amount amount of tokens to be withdrawn
function ownerWithdraw(
address _erc20Address,
uint256 _amount
address erc20Address,
uint256 amount
) public onlyOwner {
IERC20(_erc20Address).safeTransfer(msg.sender, _amount);
IERC20(erc20Address).safeTransfer(msg.sender, amount);
}

function _checkEligible(
address _erc20Address
) internal view returns (bool) {
function _checkEligible(address erc20Address) internal view returns (bool) {
return
IToucanContractRegistry(contractRegistry).checkERC20(
_erc20Address
) || isPoolEligible[_erc20Address];
erc20Address
) || isPoolEligible[erc20Address];
}
}

0 comments on commit 046d1f0

Please sign in to comment.