From 9c420ee7073af06a78ce10f937a1f634c944d111 Mon Sep 17 00:00:00 2001 From: Daniel Simon Date: Thu, 11 Apr 2024 16:58:51 +0700 Subject: [PATCH] feat: make WETH an ERC20 faucet This will let us print free WETH for manual testing & demos. --- contracts/src/deployment.sol | 10 +++-- .../src/test/TestContracts/ERC20Faucet.sol | 40 +++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 contracts/src/test/TestContracts/ERC20Faucet.sol diff --git a/contracts/src/deployment.sol b/contracts/src/deployment.sol index a25a52dc..f11ebd62 100644 --- a/contracts/src/deployment.sol +++ b/contracts/src/deployment.sol @@ -2,8 +2,6 @@ pragma solidity 0.8.18; -import "openzeppelin-contracts/contracts/token/ERC20/ERC20.sol"; - import "./ActivePool.sol"; import "./BoldToken.sol"; import "./BorrowerOperations.sol"; @@ -17,6 +15,7 @@ import "./StabilityPool.sol"; import "./TroveManager.sol"; import "./MockInterestRouter.sol"; import "./test/TestContracts/PriceFeedTestnet.sol"; +import {ERC20Faucet} from "./test/TestContracts/ERC20Faucet.sol"; struct LiquityContracts { IActivePool activePool; @@ -34,7 +33,12 @@ struct LiquityContracts { } function _deployAndConnectContracts() returns (LiquityContracts memory contracts) { - contracts.WETH = new ERC20("Wrapped ETH", "WETH"); + contracts.WETH = new ERC20Faucet( + "Wrapped ETH", // _name + "WETH", // _symbol + 10 ether, // _tapAmount + 1 days // _tapPeriod + ); // TODO: optimize deployment order & constructor args & connector functions diff --git a/contracts/src/test/TestContracts/ERC20Faucet.sol b/contracts/src/test/TestContracts/ERC20Faucet.sol new file mode 100644 index 00000000..23f2c64e --- /dev/null +++ b/contracts/src/test/TestContracts/ERC20Faucet.sol @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: GPL-3.0 +pragma solidity 0.8.18; + +import "openzeppelin-contracts/contracts/token/ERC20/ERC20.sol"; +import "openzeppelin-contracts/contracts/access/Ownable.sol"; + +contract ERC20Faucet is ERC20, Ownable { + uint256 public immutable tapAmount; + uint256 public immutable tapPeriod; + + mapping(address => uint256) public lastTapped; + + constructor(string memory _name, string memory _symbol, uint256 _tapAmount, uint256 _tapPeriod) + ERC20(_name, _symbol) + { + tapAmount = _tapAmount; + tapPeriod = _tapPeriod; + } + + function mint(address _to, uint256 _amount) external onlyOwner { + _mint(_to, _amount); + } + + function tapTo(address receiver) public { + uint256 timeNow = _requireNotRecentlyTapped(); + + _mint(receiver, tapAmount); + lastTapped[receiver] = timeNow; + } + + function tap() external { + tapTo(msg.sender); + } + + function _requireNotRecentlyTapped() internal view returns (uint256 timeNow) { + timeNow = block.timestamp; + + require(timeNow >= lastTapped[msg.sender] + tapPeriod, "ERC20Faucet: must wait before tapping again"); + } +}