文件 1 的 1:BatchTransfer.sol
pragma solidity ^0.8.0;
interface IERC20 {
function transfer(address recipient, uint256 amount) external;
function transferFrom(address sender, address recipient, uint256 amount) external;
}
contract BatchTransfer {
function batchTransferEther(address payable[] calldata recipients, uint256[] calldata amounts) external payable {
require(recipients.length == amounts.length, "Recipients and amounts arrays must have the same length");
for (uint256 i = 0; i < recipients.length; i++) {
recipients[i].transfer(amounts[i]);
}
uint256 remainingBalance = address(this).balance;
if (remainingBalance > 0) {
payable(msg.sender).transfer(remainingBalance);
}
}
function batchTransferToken(IERC20 token, address[] calldata recipients, uint256[] calldata amounts) external {
require(recipients.length == amounts.length, "Recipients and amounts arrays must have the same length");
uint256 totalTokens = 0;
for (uint256 i = 0; i < recipients.length; i++) {
totalTokens += amounts[i];
}
token.transferFrom(msg.sender, address(this), totalTokens);
for (uint256 i = 0; i < recipients.length; i++) {
token.transfer(recipients[i], amounts[i]);
}
}
function batchTransferTokenSimple(IERC20 token, address[] calldata recipients, uint256[] calldata amounts)
external
{
require(recipients.length == amounts.length, "Recipients and amounts arrays must have the same length");
for (uint256 i = 0; i < recipients.length; i++) {
token.transferFrom(msg.sender, recipients[i], amounts[i]);
}
}
}
{
"compilationTarget": {
"BatchTransfer.sol": "BatchTransfer"
},
"evmVersion": "shanghai",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": false,
"runs": 200
},
"remappings": []
}