Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faucet redeployment and upgradeability #88

Merged
merged 1 commit into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 43 additions & 13 deletions script/Faucet.s.sol
Original file line number Diff line number Diff line change
@@ -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");
}
}
1 change: 1 addition & 0 deletions script/FaucetUpgrade.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
30 changes: 15 additions & 15 deletions src/utilities/Faucet.sol
Original file line number Diff line number Diff line change
@@ -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);
}

Expand All @@ -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 {}
Expand Down
2 changes: 1 addition & 1 deletion test/examples/DeFi/MockFlashLoan.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface IFlashLoanBorrower {
address token2,
uint256 amount2,
bytes calldata data
) external returns (bytes32);
) external returns (bool);
}

/**
Expand Down