Verified contractExactMIT
BatchTransfer
Address0x0b883db3ba6ba2f7c3f1b4fc8e65f6876e94b6ad
- Balance
0 ETH
- Transactions
13
- Compiler
solc 0.8.28
- Same code
3 more contracts
Details
- Compiler
- solc 0.8.28+commit.7893614a
- Language
- solidity
- Optimization
- Yes, 1,000,000 runs
- EVM version
- cancun
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 25,702,151
- Creation transaction
- 0x985fe3f08d…0a999f2938
src/BatchTransfer.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
/// @title BatchTransfer
/// @notice 单笔交易内批量转出 ERC20 或原生币,兼容 USDT 等不返回 bool 的非标准实现。
/// @dev 无 owner、无存储、不跨交易持币。ERC20 路径资金始终从调用者直达收款人,
/// 合约永不成为授权的持有方,最坏损失上限即调用者授予本合约的 allowance。
/// 原生币路径在单次调用内短暂持币,收尾时余额归零。建议 ERC20 按单批总额
/// 精确授权,不要授权 type(uint256).max。
contract BatchTransfer {
/// @dev 收款人与金额数组长度不等
error LengthMismatch();
/// @dev 收款人数组为空
error EmptyRecipients();
/// @dev token 地址上没有代码(EOA 会让底层 call 静默成功)
error NotAContract();
/// @dev 第 index 个收款人是零地址或本合约,两者都会导致资金不可挽回
error InvalidRecipient(uint256 index);
/// @dev 第 index 笔转账失败,附带被调用方的原始 revert 数据
error TransferFailed(uint256 index, bytes reason);
/// @dev 严格模式下第 index 个收款人实际到账 received,与要求的 expected 不符
error AmountMismatch(uint256 index, uint256 expected, uint256 received);
/// @dev 严格模式下付款方实际被扣 debited,与批次总额 expected 不符
error TotalMismatch(uint256 expected, uint256 debited);
/// @dev balanceOf 调用失败或返回值非 32 字节,无法校验到账额
error BalanceQueryFailed();
/// @dev 原生币批量转账时 msg.value 不足以覆盖全部收款人的总额
error InsufficientValue(uint256 required, uint256 provided);
/// @notice 批量转出,每个收款人金额独立
/// @param token ERC20 代币地址
/// @param to 收款人列表
/// @param amount 与 to 一一对应的金额列表
function batchTransfer(address token, address[] calldata to, uint256[] calldata amount)
external
{
uint256 n = to.length;
if (n != amount.length) revert LengthMismatch();
if (n == 0) revert EmptyRecipients();
if (token.code.length == 0) revert NotAContract();
for (uint256 i; i < n;) {
_check(to[i], i);
_transferFrom(token, to[i], amount[i], i);
unchecked {
++i;
}
}
}
/// @notice 批量转出等额代币,calldata 比 batchTransfer 更小
/// @param token ERC20 代币地址
/// @param to 收款人列表
/// @param amount 每个收款人收到的金额
function batchTransferSame(address token, address[] calldata to, uint256 amount) external {
uint256 n = to.length;
if (n == 0) revert EmptyRecipients();
if (token.code.length == 0) revert NotAContract();
for (uint256 i; i < n;) {
_check(to[i], i);
_transferFrom(token, to[i], amount, i);
unchecked {
++i;
}
}
}
/// @notice 严格模式批量转出:逐笔用 balanceOf 差值校验实际到账额,
/// 任一笔到账不足或超出即整批回滚。
/// @dev 适用于对到账额敏感的场景。可防住 USDT 手续费开关被打开、
/// 收取转账费的代币、以及任何到账额与传入额不一致的实现。
/// 额外成本约每人 5k gas(两次 balanceOf)。
/// 收款人不得为付款方自己,否则余额差值无意义,会以 AmountMismatch 回滚。
/// @param token ERC20 代币地址
/// @param to 收款人列表
/// @param amount 与 to 一一对应的金额,必须精确到账
function batchTransferExact(address token, address[] calldata to, uint256[] calldata amount)
external
{
uint256 n = to.length;
if (n != amount.length) revert LengthMismatch();
if (n == 0) revert EmptyRecipients();
if (token.code.length == 0) revert NotAContract();
uint256 total;
uint256 senderBefore = _balanceOf(token, msg.sender);
for (uint256 i; i < n;) {
_check(to[i], i);
_transferExact(token, to[i], amount[i], i);
total += amount[i]; // 必须 checked:回绕会让 _checkDebit 失去意义
unchecked {
++i;
}
}
_checkDebit(token, senderBefore, total);
}
/// @notice 严格模式等额批量转出,校验方式同 batchTransferExact
/// @param token ERC20 代币地址
/// @param to 收款人列表
/// @param amount 每个收款人必须精确收到的金额
function batchTransferSameExact(address token, address[] calldata to, uint256 amount)
external
{
uint256 n = to.length;
if (n == 0) revert EmptyRecipients();
if (token.code.length == 0) revert NotAContract();
uint256 senderBefore = _balanceOf(token, msg.sender);
for (uint256 i; i < n;) {
_check(to[i], i);
_transferExact(token, to[i], amount, i);
unchecked {
++i;
}
}
_checkDebit(token, senderBefore, amount * n);
}
/// @dev 拦掉两种资金不可挽回的收款人:
/// 零地址(USDT 不做零地址检查,转过去等于销毁)、
/// 本合约(无 owner 无提取函数,转进来永久锁死)
function _check(address to, uint256 index) private view {
if (to == address(0) || to == address(this)) revert InvalidRecipient(index);
}
/// @dev 以低层 call 发起 transferFrom,按三种情况判定成功:
/// - returndata 为空:USDT / 老 BNB 这类非标准实现,视为成功
/// - returndata 为 32 字节:按标准 ERC20 解码 bool
/// - 其他长度:无法判定,一律失败
/// 调用失败时向上冒泡被调用方的 revert 数据,便于定位黑名单、
/// 余额不足、allowance 不足等具体原因。
function _transferFrom(address token, address to, uint256 amount, uint256 index) private {
(bool ok, bytes memory ret) = token.call(
abi.encodeWithSelector(0x23b872dd, msg.sender, to, amount) // transferFrom
);
if (!ok) revert TransferFailed(index, ret);
if (ret.length == 0) return;
if (ret.length != 32 || !abi.decode(ret, (bool))) revert TransferFailed(index, ret);
}
/// @dev 转一笔并校验收款人余额实际增加了 amount,多一分少一分都回滚
function _transferExact(address token, address to, uint256 amount, uint256 index) private {
uint256 before = _balanceOf(token, to);
_transferFrom(token, to, amount, index);
uint256 received = _balanceOf(token, to) - before;
if (received != amount) revert AmountMismatch(index, amount, received);
}
/// @dev 校验付款方实际被扣额等于批次总额,防住只对付款方额外收费的实现
function _checkDebit(address token, uint256 senderBefore, uint256 total) private view {
uint256 debited = senderBefore - _balanceOf(token, msg.sender);
if (debited != total) revert TotalMismatch(total, debited);
}
/// @dev balanceOf 的安全读取:失败或返回值长度异常时明确回滚,
/// 不允许把无法校验的情况当成校验通过
function _balanceOf(address token, address who) private view returns (uint256) {
(bool ok, bytes memory ret) =
token.staticcall(abi.encodeWithSelector(0x70a08231, who)); // balanceOf
if (!ok || ret.length != 32) revert BalanceQueryFailed();
return abi.decode(ret, (uint256));
}
/// @notice 批量转出原生币,每个收款人金额独立
/// @dev 合约在单次调用内短暂持币。收尾时余额必须归零,保证不跨交易持币。
/// 使用 call 而非 transfer:后者 2300 gas 固定补贴会让 Gnosis Safe
/// 等智能合约钱包的 receive() 因 gas 不足而失败。任一笔失败整批回滚。
/// @param to 收款人列表
/// @param amount 与 to 一一对应的金额列表
function batchTransferETH(address[] calldata to, uint256[] calldata amount)
external
payable
{
uint256 n = to.length;
if (n != amount.length) revert LengthMismatch();
if (n == 0) revert EmptyRecipients();
// 先把总额加完再校验 msg.value,而不是边转边算:
// 钱花光后再失败会报成"第 k 笔转账失败",掩盖真实原因(少给了钱)。
// 这一遍循环只做加法,不外呼,重入无从插入。
uint256 total;
for (uint256 i; i < n;) {
total += amount[i]; // 必须 checked:回绕会让下面的校验失效
unchecked {
++i;
}
}
if (msg.value < total) revert InsufficientValue(total, msg.value);
for (uint256 i; i < n;) {
_check(to[i], i);
_sendETH(to[i], amount[i], i);
unchecked {
++i;
}
}
// 多付的退回,收尾时合约余额归零 —— "不跨交易持币"的保证
uint256 refund = msg.value - total;
if (refund > 0) _sendETH(msg.sender, refund, type(uint256).max);
}
/// @notice 批量转出等额原生币,calldata 比 batchTransferETH 更小
/// @param to 收款人列表
/// @param amount 每个收款人收到的金额
function batchTransferETHSame(address[] calldata to, uint256 amount) external payable {
uint256 n = to.length;
if (n == 0) revert EmptyRecipients();
// 与 batchTransferETH 同理:先校验再转账,失败时报的是真实原因。
// amount * n 必须 checked,回绕会让校验失去意义。
uint256 total = amount * n;
if (msg.value < total) revert InsufficientValue(total, msg.value);
for (uint256 i; i < n;) {
_check(to[i], i);
_sendETH(to[i], amount, i);
unchecked {
++i;
}
}
uint256 refund = msg.value - total;
if (refund > 0) _sendETH(msg.sender, refund, type(uint256).max);
}
/// @dev 发送原生币。用 call 而非 transfer:后者 2300 gas 固定补贴会让
/// 带 receive 逻辑的智能合约钱包收不到钱。失败时向上冒泡被调用方
/// 的原始 revert 数据,便于定位 out-of-gas、拒收等具体原因。
function _sendETH(address to, uint256 amount, uint256 index) private {
(bool ok, bytes memory ret) = to.call{value: amount}("");
if (!ok) revert TransferFailed(index, ret);
}
}
Compiler settingsShow
{
"viaIR": false,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"libraries": {},
"optimizer": {
"runs": 1000000,
"enabled": true
},
"evmVersion": "cancun",
"remappings": [
"forge-std/=lib/forge-std/src/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.