// File: contracts/CoinBatchTransfer.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;

import "@openzeppelin/contracts/security/ReentrancyGuard.sol";

contract CoinBatchTransfer is ReentrancyGuard {
    event TransfersSucceeded(address indexed sender, uint256 amount);
    event RefundSucceeded(address indexed recipient, uint256 amount);
    event TransferSucceeded(address indexed recipient, uint256 amount);

    error CoinBatchTransfer_TransferFailed(address recipient, uint256 amount);
    error CoinBatchTransfer_RefundFailed(address recipient, uint256 amount);
    error CoinBatchTransfer_InvalidRecipient();
    error CoinBatchTransfer_InvalidInputParams(
        uint256 recipientsLength,
        uint256 amountsLength
    );
    error CoinBatchTransfer_InvalidValue();

    /**
     * @notice Transfer the specified amounts to the specified recipients.
     * @param recipients_ array of recipients of coin (ETH)
     * @param amounts_ array of amounts of coin (ETH) to transfer
     */
    function batchTransfer(
        address[] calldata recipients_,
        uint256[] calldata amounts_
    ) external payable nonReentrant {
        uint256 _recipientsLen = recipients_.length;
        uint256 _amountsLen = amounts_.length;
        if (_recipientsLen != _amountsLen || _recipientsLen == 0) {
            revert CoinBatchTransfer_InvalidInputParams(
                _recipientsLen,
                _amountsLen
            );
        }
        uint256 _total = msg.value;
        for (uint256 i; i < _recipientsLen; ++i) {
            address _recipient = recipients_[i];
            uint256 _amount = amounts_[i];
            if (_amount == 0) {
                continue;
            }

            if (_total < _amount) {
                revert CoinBatchTransfer_InvalidValue();
            }

            if (_recipient == address(0)) {
                revert CoinBatchTransfer_InvalidRecipient();
            }
            _total -= _amount;
            (bool _transferSuccess, ) = _recipient.call{value: _amount}("");
            if (!_transferSuccess) {
                revert CoinBatchTransfer_TransferFailed(_recipient, _amount);
            } else {
                emit TransferSucceeded(_recipient, _amount);
            }
        }
        if (_total > 0) {
            _executeRefund(_total);
        }
        emit TransfersSucceeded(msg.sender, msg.value - _total);
    }

    /**
     * @notice Refund the remaining balance to msg.sender
     */
    function _executeRefund(uint256 refund_) private {
        address _recipient = msg.sender;
        (bool _refundSuccess, ) = _recipient.call{value: refund_}("");
        if (!_refundSuccess) {
            revert CoinBatchTransfer_RefundFailed(_recipient, refund_);
        }
        emit RefundSucceeded(_recipient, refund_);
    }
}

// File: @openzeppelin/contracts/security/ReentrancyGuard.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (security/ReentrancyGuard.sol)

pragma solidity ^0.8.0;

/**
 * @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 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;

    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
        require(_status != _ENTERED, "ReentrancyGuard: reentrant call");

        // 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;
    }
}