-
Notifications
You must be signed in to change notification settings - Fork 56
/
challenge2.sol
33 lines (26 loc) · 1.06 KB
/
challenge2.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract BatchTransfer {
mapping(address => uint256) public pendingWithdrawals;
function batchTransfer(address payable[] calldata recipients, uint256[] calldata amounts) external payable {
require(recipients.length == amounts.length, "Recipients and amounts length mismatch");
uint256 totalAmount = 0;
uint256 i;
for (i = 0; i < amounts.length; i++) {
totalAmount += amounts[i];
}
require(totalAmount == msg.value, "Incorrect total amount");
for (i = 0; i < recipients.length; i++) {
(bool success, ) = recipients[i].call{value: amounts[i]}("");
if (!success) {
pendingWithdrawals[recipients[i]] += amounts[i];
}
}
}
function withdrawPending() external {
uint256 amount = pendingWithdrawals[msg.sender];
pendingWithdrawals[msg.sender] = 0;
(bool success, ) = payable(msg.sender).call{value: amount}("");
require(success, "Withdrawal failed");
}
}