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

Update Faucet contract to use bridging on drip function #10621

Merged
merged 10 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 18 additions & 5 deletions packages/contracts-bedrock/src/periphery/faucet/Faucet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ contract SafeSend {
}
}

interface IL1StandardBridge {
// Declare the depositETHTo function as it is defined in L1StandardBridge
function depositETHTo(address to, uint32 minGasLimit, bytes calldata extraData) external payable;
tarunkhasnavis marked this conversation as resolved.
Show resolved Hide resolved
}

/// @title Faucet
/// @notice Faucet contract that drips ETH to users.
contract Faucet {
Expand All @@ -20,7 +25,8 @@ contract Faucet {
/// @param userId The id of the user that requested the drip.
/// @param amount The amount of funds sent.
/// @param recipient The recipient of the drip.
event Drip(string indexed authModule, bytes32 indexed userId, uint256 amount, address indexed recipient);
/// @param bridge The destination chain's bridge of the drip.
event Drip(string indexed authModule, bytes32 indexed userId, uint256 amount, address indexed recipient, address bridge);

/// @notice Parameters for a drip.
struct DripParameters {
Expand Down Expand Up @@ -88,7 +94,8 @@ contract Faucet {
/// @notice Drips ETH to a recipient account.
/// @param _params Drip parameters.
/// @param _auth Authentication parameters.
function drip(DripParameters memory _params, AuthParameters memory _auth) public {
/// @param _bridge Bridge address.
function drip(DripParameters memory _params, AuthParameters memory _auth, address _bridge) public {
tarunkhasnavis marked this conversation as resolved.
Show resolved Hide resolved
// Grab the module config once.
ModuleConfig memory config = modules[_auth.module];

Expand Down Expand Up @@ -119,10 +126,16 @@ contract Faucet {
// Mark the nonce as used.
nonces[_auth.id][_params.nonce] = true;

// Execute a safe transfer of ETH to the recipient account.
new SafeSend{ value: config.amount }(_params.recipient);
if (_bridge == address(0)) {
// Use SafeSend if no bridge address is provided
new SafeSend{ value: config.amount }(_params.recipient);
} else {
// Execute a bridging of ETH to the recipient account.
IL1StandardBridge l1Bridge = IL1StandardBridge(_bridge);
l1Bridge.depositETHTo{value: config.amount}(_params.recipient, 200000, "");
}

emit Drip(config.name, _auth.id, config.amount, _params.recipient);
emit Drip(config.name, _auth.id, config.amount, _params.recipient, _bridge);
}

/// @notice Returns the enable value of a given auth module.
Expand Down
41 changes: 28 additions & 13 deletions packages/contracts-bedrock/test/periphery/faucet/Faucet.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ contract Faucet_Initializer is Test {
address internal faucetAuthAdmin;
address internal nonAdmin;
address internal fundsReceiver;
address internal bridgeAddress;
uint256 internal faucetAuthAdminKey;
uint256 internal nonAdminKey;
uint256 internal startingTimestamp = 1000;
Expand All @@ -31,6 +32,7 @@ contract Faucet_Initializer is Test {
vm.warp(startingTimestamp);
faucetContractAdmin = makeAddr("faucetContractAdmin");
fundsReceiver = makeAddr("fundsReceiver");
bridgeAddress = makeAddr("bridgeAddr");

faucetAuthAdminKey = 0xB0B0B0B0;
faucetAuthAdmin = vm.addr(faucetAuthAdminKey);
Expand Down Expand Up @@ -120,7 +122,8 @@ contract FaucetTest is Faucet_Initializer {
vm.prank(nonAdmin);
faucet.drip(
Faucet.DripParameters(payable(fundsReceiver), nonce),
Faucet.AuthParameters(optimistNftFam, keccak256(abi.encodePacked(fundsReceiver)), signature)
Faucet.AuthParameters(optimistNftFam, keccak256(abi.encodePacked(fundsReceiver)), signature),
bridgeAddress
);
}

Expand All @@ -142,7 +145,8 @@ contract FaucetTest is Faucet_Initializer {
vm.expectRevert("Faucet: drip parameters could not be verified by security module");
faucet.drip(
Faucet.DripParameters(payable(fundsReceiver), nonce),
Faucet.AuthParameters(optimistNftFam, keccak256(abi.encodePacked(fundsReceiver)), signature)
Faucet.AuthParameters(optimistNftFam, keccak256(abi.encodePacked(fundsReceiver)), signature),
bridgeAddress
);
}

Expand All @@ -164,7 +168,8 @@ contract FaucetTest is Faucet_Initializer {
vm.prank(nonAdmin);
faucet.drip(
Faucet.DripParameters(payable(fundsReceiver), nonce),
Faucet.AuthParameters(optimistNftFam, keccak256(abi.encodePacked(fundsReceiver)), signature)
Faucet.AuthParameters(optimistNftFam, keccak256(abi.encodePacked(fundsReceiver)), signature),
bridgeAddress
);
uint256 recipientBalanceAfter = address(fundsReceiver).balance;
assertEq(recipientBalanceAfter - recipientBalanceBefore, 1 ether, "expect increase of 1 ether");
Expand All @@ -188,7 +193,8 @@ contract FaucetTest is Faucet_Initializer {
vm.prank(nonAdmin);
faucet.drip(
Faucet.DripParameters(payable(fundsReceiver), nonce),
Faucet.AuthParameters(githubFam, keccak256(abi.encodePacked(fundsReceiver)), signature)
Faucet.AuthParameters(githubFam, keccak256(abi.encodePacked(fundsReceiver)), signature),
bridgeAddress
);
uint256 recipientBalanceAfter = address(fundsReceiver).balance;
assertEq(recipientBalanceAfter - recipientBalanceBefore, 0.05 ether, "expect increase of .05 ether");
Expand All @@ -214,7 +220,8 @@ contract FaucetTest is Faucet_Initializer {
vm.prank(nonAdmin);
faucet.drip(
Faucet.DripParameters(payable(fundsReceiver), nonce),
Faucet.AuthParameters(githubFam, keccak256(abi.encodePacked(fundsReceiver)), signature)
Faucet.AuthParameters(githubFam, keccak256(abi.encodePacked(fundsReceiver)), signature),
bridgeAddress
);
}

Expand All @@ -235,15 +242,17 @@ contract FaucetTest is Faucet_Initializer {
vm.startPrank(faucetContractAdmin);
faucet.drip(
Faucet.DripParameters(payable(fundsReceiver), nonce),
Faucet.AuthParameters(githubFam, keccak256(abi.encodePacked(fundsReceiver)), signature)
Faucet.AuthParameters(githubFam, keccak256(abi.encodePacked(fundsReceiver)), signature),
bridgeAddress
);

faucet.configure(githubFam, Faucet.ModuleConfig("GithubModule", false, 1 days, 0.05 ether));

vm.expectRevert("Faucet: provided auth module is not supported by this faucet");
faucet.drip(
Faucet.DripParameters(payable(fundsReceiver), nonce),
Faucet.AuthParameters(githubFam, keccak256(abi.encodePacked(fundsReceiver)), signature)
Faucet.AuthParameters(githubFam, keccak256(abi.encodePacked(fundsReceiver)), signature),
bridgeAddress
);
vm.stopPrank();
}
Expand All @@ -265,13 +274,15 @@ contract FaucetTest is Faucet_Initializer {
vm.startPrank(faucetContractAdmin);
faucet.drip(
Faucet.DripParameters(payable(fundsReceiver), nonce),
Faucet.AuthParameters(githubFam, keccak256(abi.encodePacked(fundsReceiver)), signature)
Faucet.AuthParameters(githubFam, keccak256(abi.encodePacked(fundsReceiver)), signature),
bridgeAddress
);

vm.expectRevert("Faucet: nonce has already been used");
faucet.drip(
Faucet.DripParameters(payable(fundsReceiver), nonce),
Faucet.AuthParameters(githubFam, keccak256(abi.encodePacked(fundsReceiver)), signature)
Faucet.AuthParameters(githubFam, keccak256(abi.encodePacked(fundsReceiver)), signature),
bridgeAddress
);
vm.stopPrank();
}
Expand All @@ -293,7 +304,8 @@ contract FaucetTest is Faucet_Initializer {
vm.startPrank(faucetContractAdmin);
faucet.drip(
Faucet.DripParameters(payable(fundsReceiver), nonce0),
Faucet.AuthParameters(githubFam, keccak256(abi.encodePacked(fundsReceiver)), signature0)
Faucet.AuthParameters(githubFam, keccak256(abi.encodePacked(fundsReceiver)), signature0),
bridgeAddress
);

bytes32 nonce1 = faucetHelper.consumeNonce();
Expand All @@ -311,7 +323,8 @@ contract FaucetTest is Faucet_Initializer {
vm.expectRevert("Faucet: auth cannot be used yet because timeout has not elapsed");
faucet.drip(
Faucet.DripParameters(payable(fundsReceiver), nonce1),
Faucet.AuthParameters(githubFam, keccak256(abi.encodePacked(fundsReceiver)), signature1)
Faucet.AuthParameters(githubFam, keccak256(abi.encodePacked(fundsReceiver)), signature1),
bridgeAddress
);
vm.stopPrank();
}
Expand All @@ -333,7 +346,8 @@ contract FaucetTest is Faucet_Initializer {
vm.startPrank(faucetContractAdmin);
faucet.drip(
Faucet.DripParameters(payable(fundsReceiver), nonce0),
Faucet.AuthParameters(githubFam, keccak256(abi.encodePacked(fundsReceiver)), signature0)
Faucet.AuthParameters(githubFam, keccak256(abi.encodePacked(fundsReceiver)), signature0),
bridgeAddress
);

bytes32 nonce1 = faucetHelper.consumeNonce();
Expand All @@ -351,7 +365,8 @@ contract FaucetTest is Faucet_Initializer {
vm.warp(startingTimestamp + 1 days + 1 seconds);
faucet.drip(
Faucet.DripParameters(payable(fundsReceiver), nonce1),
Faucet.AuthParameters(githubFam, keccak256(abi.encodePacked(fundsReceiver)), signature1)
Faucet.AuthParameters(githubFam, keccak256(abi.encodePacked(fundsReceiver)), signature1),
bridgeAddress
);
vm.stopPrank();
}
Expand Down