/// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
contract BatchETHTransfer is ReentrancyGuard {
/**
* @dev Emitted when a batch ETH transfer operation is completed.
* @param sender The address initiating the batch transfer.
* @param numTransfers The number of individual transfers requested in the batch.
* @param totalAmountSent The sum of intended amounts sent by the caller.
*/
event BatchETHTransferred(
address indexed sender,
uint256 numTransfers,
uint256 totalAmountSent
);
/**
* @dev Transfers ETH to multiple recipients in a single transaction.
* @notice Caller must send the exact amount of ETH required for all transfers.
* @notice This function is not suitable for use with tokens that do not support the SafeERC20 interface.
* @notice This function limits the number of transfers to 200 to prevent block gas limits.
* @param recipients An array of addresses to send ETH to.
* @param amounts An array of amounts (in ETH) intended to send, corresponding to recipients.
*/
function batchETHTransfer(
address[] calldata recipients,
uint256[] calldata amounts
) external payable nonReentrant {
address sender = msg.sender;
uint256 numTransfers = recipients.length;
uint256 numAmounts = amounts.length;
uint256 totalAmountSent = 0;
require(numTransfers > 0, "BatchETHTransfer: empty transfer list");
require(numTransfers <= 200, "BatchETHTransfer: too many transfers");
require(
numTransfers == numAmounts,
"BatchETHTransfer: recipients and amounts length mismatch"
);
for (uint256 i = 0; i < numTransfers; i++) {
address recipient = recipients[i];
uint256 amount = amounts[i];
require(
recipient != address(0),
"BatchETHTransfer: recipient cannot be zero address"
);
require(amount > 0, "BatchETHTransfer: amount must be positive");
require(
sender != recipient,
"BatchETHTransfer: sender and recipient cannot be the same"
);
totalAmountSent += amount;
}
require(
msg.value == totalAmountSent,
"BatchETHTransfer: incorrect ETH amount sent"
);
for (uint256 i = 0; i < numTransfers; i++) {
address recipient = recipients[i];
uint256 amount = amounts[i];
payable(recipient).transfer(amount);
}
emit BatchETHTransferred(sender, numTransfers, totalAmountSent);
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/ReentrancyGuard.sol)
pragma solidity ^0.8.20;
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If EIP-1153 (transient storage) is available on the chain you're deploying at,
* consider using {ReentrancyGuardTransient} instead.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
abstract contract ReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant NOT_ENTERED = 1;
uint256 private constant ENTERED = 2;
uint256 private _status;
/**
* @dev Unauthorized reentrant call.
*/
error ReentrancyGuardReentrantCall();
constructor() {
_status = NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function _nonReentrantBefore() private {
// On the first call to nonReentrant, _status will be NOT_ENTERED
if (_status == ENTERED) {
revert ReentrancyGuardReentrantCall();
}
// Any calls to nonReentrant after this point will fail
_status = ENTERED;
}
function _nonReentrantAfter() private {
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = NOT_ENTERED;
}
/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
return _status == ENTERED;
}
}
{
"compilationTarget": {
"contracts/BatchETHTransfer.sol": "BatchETHTransfer"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[],"name":"ReentrancyGuardReentrantCall","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":false,"internalType":"uint256","name":"numTransfers","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"totalAmountSent","type":"uint256"}],"name":"BatchETHTransferred","type":"event"},{"inputs":[{"internalType":"address[]","name":"recipients","type":"address[]"},{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"name":"batchETHTransfer","outputs":[],"stateMutability":"payable","type":"function"}]