Skip to content

Commit

Permalink
Merge pull request #9 from sirnicolaz/miotto/lila-6086-update-faucet-…
Browse files Browse the repository at this point in the history
…to-support-any-kind-of-pool-token-simplify

[LILA-6086]: reduce complexity + deploy
  • Loading branch information
0xmichalis authored Feb 22, 2024
2 parents 51985f1 + ffb4c78 commit 94bc3d6
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 504 deletions.
48 changes: 1 addition & 47 deletions contracts/Faucet.sol
Original file line number Diff line number Diff line change
@@ -1,48 +1,16 @@
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

import {console} from "hardhat/console.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {IToucanContractRegistry} from "./interfaces/IToucanContractRegistry.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";

contract Faucet is Ownable {
using SafeERC20 for IERC20;

uint256 public constant TIMEOUT_LIMIT = 30;
uint256 public constant MAX_WITHDRAWAL_AMOUNT = 5 ether;
address public contractRegistry;
mapping(address => bool) public isPoolEligible;
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_;

uint256 poolAddressesCount = poolAddresses.length;
for (uint256 i = 0; i < poolAddressesCount; i++) {
setPoolEligible(poolAddresses[i], true);
}
}

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

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

/// @notice A function to get the Faucet's balances of multiple tokens at once
/// @param erc20Addresses An array of ERC20 contract addresses
Expand All @@ -61,15 +29,11 @@ contract Faucet is Ownable {
/// @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(
msg.sender,
address(this),
amount
);

emit Deposited(erc20Address, amount);
}

/// @notice checks if the Faucet is in a withdrawal timeout for the caller
Expand All @@ -83,15 +47,12 @@ contract Faucet is Ownable {
/// @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;

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

IERC20(erc20Address).safeTransfer(msg.sender, 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
Expand All @@ -103,11 +64,4 @@ contract Faucet is Ownable {
) public onlyOwner {
IERC20(erc20Address).safeTransfer(msg.sender, amount);
}

function _checkEligible(address erc20Address) internal view returns (bool) {
return
IToucanContractRegistry(contractRegistry).checkERC20(
erc20Address
) || isPoolEligible[erc20Address];
}
}
6 changes: 0 additions & 6 deletions contracts/interfaces/IToucanContractRegistry.sol

This file was deleted.

9 changes: 0 additions & 9 deletions deploy/00_deploy_faucet.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import { DeployFunction } from "hardhat-deploy/types";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { deploymentAddresses } from "../utils/constants";

const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const { CONTRACT_REGISTRY_ADDRESS, BCT_ADDRESS, NCT_ADDRESS } =
deploymentAddresses[hre.network.name];

if (!CONTRACT_REGISTRY_ADDRESS || !BCT_ADDRESS || !NCT_ADDRESS) {
throw new Error(`Missing deployment addresses for ${hre.network.name}`);
}

const { deployments, getNamedAccounts } = hre;
const { deploy } = deployments;
const { deployer } = await getNamedAccounts();
Expand All @@ -20,7 +12,6 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {

await deploy("Faucet", {
from: deployer,
args: [CONTRACT_REGISTRY_ADDRESS, [BCT_ADDRESS, NCT_ADDRESS]],
log: true,
autoMine: true, // speed up deployment on local network (ganache, hardhat), no effect on live networks
});
Expand Down
219 changes: 46 additions & 173 deletions deployments/alfajores/Faucet.json

Large diffs are not rendered by default.

Loading

0 comments on commit 94bc3d6

Please sign in to comment.