solidity实现批量转账

直接贴代码:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract BatchTransfer {
    event Transfer(address indexed from, address indexed to, uint256 value);

    constructor() payable {
    }

    function batchTransfer(address payable[] memory _recipients, uint256 _amount) public payable {
        for(uint256 i = 0; i < _recipients.length; i++) {
            _transfer(msg.sender, _recipients[i], _amount);
        }
    }

    function _transfer(address _from, address payable _to, uint256 _amount) internal {
        (bool success, ) = _to.call{value: _amount}("");
        require(success, "Transfer failed");

        emit Transfer(_from, _to, _amount);
    }
}

实现原理:先往合约中转账指定的金额,然后通过合约转账给接收者

合约部署:

转账支持:

如果转账失败,可以直接debug:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章