From 9cfa529593da88707df54338e0e8b95ec7c096f3 Mon Sep 17 00:00:00 2001 From: TokenTitan Date: Sat, 26 Oct 2024 22:32:19 +0530 Subject: [PATCH] refactor: faucet deployment and upgradeability --- script/Faucet.s.sol | 56 +++++++++++++++++++++------- script/FaucetUpgrade.s.sol | 1 + src/utilities/Faucet.sol | 30 +++++++-------- test/examples/DeFi/MockFlashLoan.sol | 2 +- 4 files changed, 60 insertions(+), 29 deletions(-) diff --git a/script/Faucet.s.sol b/script/Faucet.s.sol index 5afb7d9..ecf92f4 100644 --- a/script/Faucet.s.sol +++ b/script/Faucet.s.sol @@ -1,24 +1,54 @@ -// SPDX-License-Identifier: MIT +// SPDX-License-Identifier: GPL-3.0 + pragma solidity 0.8.26; import "forge-std/Script.sol"; import "src/utilities/Faucet.sol"; -import {Upgrades} from "openzeppelin-foundry-upgrades/Upgrades.sol"; +import {BaseDeployer} from "./BaseDeployer.s.sol"; + +/* solhint-disable no-console*/ +import {console2} from "forge-std/console2.sol"; + +contract DeployFaucet is Script, BaseDeployer { + /// @dev Compute the CREATE2 address for Faucet contract. + /// @param salt The salt for the Faucet contract. + modifier computeCreate2(bytes32 salt) { + _create2addr = computeCreate2Address(salt, hashInitCode(type(Faucet).creationCode)); + + _; + } -contract DeployFaucet is Script { - function run() external returns (address, address) { - uint256 deployerPrivateKey = vm.envUint("DEPLOYER_KEY"); - vm.startBroadcast(deployerPrivateKey); + /// @dev Helper to iterate over chains and select fork. + /// @param deployForks The chains to deploy to. + /// @return address of the deployed contract + function createDeployMultichain(Chains[] memory deployForks) + internal + override + computeCreate2(_salt) + returns (address) + { + console2.log("Faucet create2 address:", _create2addr, "\n"); - // Deploy the upgradeable contract - address _proxyAddress = - Upgrades.deployTransparentProxy("Faucet.sol", msg.sender, abi.encodeCall(Faucet.initialize, ())); + for (uint256 i; i < deployForks.length;) { + console2.log("Deploying Faucet to chain: ", uint256(deployForks[i]), "\n"); + + createSelectFork(deployForks[i]); + + _chainDeployCallBreaker(); + + unchecked { + ++i; + } + } + return _create2addr; + } - // Get the implementation address - address implementationAddress = Upgrades.getImplementationAddress(_proxyAddress); + /// @dev Function to perform actual deployment. + function _chainDeployCallBreaker() private broadcast(_deployerPrivateKey) { + Faucet faucet = new Faucet{salt: _salt}(); - vm.stopBroadcast(); + require(_create2addr == address(faucet), "Address mismatch Faucet"); - return (implementationAddress, _proxyAddress); + console2.log("Faucet deployed at address:", address(faucet), "\n"); } } diff --git a/script/FaucetUpgrade.s.sol b/script/FaucetUpgrade.s.sol index a75a5eb..b1c4955 100644 --- a/script/FaucetUpgrade.s.sol +++ b/script/FaucetUpgrade.s.sol @@ -4,6 +4,7 @@ pragma solidity 0.8.26; import "forge-std/Script.sol"; import {Upgrades} from "openzeppelin-foundry-upgrades/Upgrades.sol"; +/// @notice not being used since faucet was not deployed using a proxy contract UpgradeFaucet is Script { function run() external returns (address, address) { uint256 deployerPrivateKey = vm.envUint("DEPLOYER_KEY"); diff --git a/src/utilities/Faucet.sol b/src/utilities/Faucet.sol index 327b7c2..a44205d 100644 --- a/src/utilities/Faucet.sol +++ b/src/utilities/Faucet.sol @@ -1,40 +1,40 @@ // SPDX-License-Identifier: GPL-3.0 pragma solidity 0.8.26; -import "openzeppelin-upgradeable/access/AccessControlUpgradeable.sol"; +import "openzeppelin-contracts/contracts/access/AccessControl.sol"; -contract Faucet is AccessControlUpgradeable { +contract Faucet is AccessControl { bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE"); uint256 public dripAmount; /// @notice disable lock features for the initial version of the faucet - /// uint256 public lockDuration; + /// @dev defaults to zero on deployment + uint256 public lockDuration; /// @notice used to block users of faucet for 24 hours after taking every call - /// @dev mapping to store address and blocktime + 1 day - // mapping(address => uint256) public lockTime; + /// @dev mapping to store address and blocktime + lock duration + mapping(address => uint256) public lockTime; + event LockDurationUpdated(uint256 lockDuration); event DripAmountUpdated(uint256 amount); - // event LockDurationUpdated(uint256 lockDuration); event FundsTransferred(address indexed requestor, uint256 dripAmount); event FundsGranted(address indexed receiver, uint256 amount); - function initialize() external initializer { + constructor() { _grantRole(DEFAULT_ADMIN_ROLE, _msgSender()); _grantRole(ADMIN_ROLE, _msgSender()); _setDripAmount(5 ether); - // lockDuration = 1 days; } /// @notice call to get funds from the faucet function requestFunds(address payable _requestor) external payable { // check if funds were transferred recently - // require(block.timestamp > lockTime[_requestor], "Faucet: Please try later"); + require(block.timestamp > lockTime[_requestor], "Faucet: Please try later"); // check if there is enough balance require(address(this).balance > dripAmount, "Faucet: Not enough funds"); _requestor.transfer(dripAmount); - // lockTime[_requestor] = block.timestamp + lockDuration; + lockTime[_requestor] = block.timestamp + lockDuration; emit FundsTransferred(_requestor, dripAmount); } @@ -53,11 +53,11 @@ contract Faucet is AccessControlUpgradeable { } /// @notice commented lock feature for the initial version of the faucet - // function changeLockDuration(uint256 newLockDuration) external onlyRole(ADMIN_ROLE) { - // require(newLockDuration > 0, "Faucet: Invalid duration value"); - // lockDuration = newLockDuration; - // emit LockDurationUpdated(newLockDuration); - // } + function changeLockDuration(uint256 newLockDuration) external onlyRole(ADMIN_ROLE) { + require(newLockDuration > 0, "Faucet: Invalid duration value"); + lockDuration = newLockDuration; + emit LockDurationUpdated(newLockDuration); + } // function to add funds to the smart contract receive() external payable {} diff --git a/test/examples/DeFi/MockFlashLoan.sol b/test/examples/DeFi/MockFlashLoan.sol index c2817a6..bf4364d 100644 --- a/test/examples/DeFi/MockFlashLoan.sol +++ b/test/examples/DeFi/MockFlashLoan.sol @@ -11,7 +11,7 @@ interface IFlashLoanBorrower { address token2, uint256 amount2, bytes calldata data - ) external returns (bytes32); + ) external returns (bool); } /**