代币"/>
批量转账erc777代币
// contracts/GLDToken.sol 用0.6.12版本 加载的openzeppelin 全改成0.6.12 版本
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC777/IERC777.sol";
import "@openzeppelin/contracts/token/ERC777/IERC777Recipient.sol";
import "@openzeppelin/contracts/utils/introspection/IERC1820Registry.sol";contract TransferToken is IERC777Recipient {//如果是自己的链,就用自己的erc1820 合约IERC1820Registry internal constant _ERC1820_REGISTRY =IERC1820Registry(0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24);mapping(address => bool) internal _trustedContracts;address internal _manager;modifier onlyManager {require(msg.sender == _manager, "Sender is not manager");_;}constructor(address[] memory trusted_contracts) public {for (uint256 i = 0; i < trusted_contracts.length; i++) {_trustedContracts[trusted_contracts[i]] = true;}_manager = msg.sender;_ERC1820_REGISTRY.setInterfaceImplementer(address(this),keccak256(abi.encodePacked("ERC777TokensRecipient")),address(this));}function changeManager(address newManager) public onlyManager {_manager = newManager;}function isTrustedContract(address c) public view returns (bool) {return _trustedContracts[c];}function setTrustedContractState(address c, bool state) public onlyManager {// require(msg.sender == _manager);_trustedContracts[c] = state;}function retrieveToken(address tokenContract, uint256 value)publiconlyManager{// require balance > value// address implementer = _ERC1820_REGISTRY.getInterfaceImplementer(msg.sender, keccak256("ERC777Token"));// require (implementer != address(0), "Token contract should be an erc777 contract");require(IERC20(tokenContract).balanceOf(address(this)) >= value,"Transfer value is greater than contract balance");IERC20(tokenContract).transfer(_manager, value);}function distributeCfx(bytes memory userData) public payable {(address [] memory tos, uint256[] memory vals) =abi.decode(userData, (address[], uint256[]));uint256 length = tos.length;require(tos.length == vals.length, "tos and vals length not match");// 检查 amount 相同uint256 sum = 0;for (uint256 i = 0; i < length; ++i) {sum += vals[i];}require(sum == msg.value, "Amount should equal to the sum of transfer");for (uint256 i = 0; i < length; ++i) {address payable recipient = payable(address(uint160(tos[i])));recipient.transfer(vals[i]);}}// @notice called when someone attempts to transfer ERC-777 tokens to this address. If this function were to throw or doesn't exist, then the token transfer would fail.function tokensReceived(address operator,address from,address to,uint256 amount,bytes calldata userData,bytes calldata operatorData) external override {require(to == address(this), "should transfer to this contract");// 有可能是不必要的,但暂时保留require(_trustedContracts[msg.sender],"The ERC777 Token is not registered in routing contract");(address[] memory tos, uint256[] memory vals) =abi.decode(userData, (address[], uint256[]));uint256 length = tos.length;require(tos.length == vals.length, "tos and vals length not match");// 检查 amount 相同uint256 sum = 0;for (uint256 i = 0; i < length; ++i) {sum += vals[i];}require(sum == amount, "Amount should equal to the sum of transfer");// 本合约中不会有 ERC777 Token// 这里合约只会调用 ERC777 合约的转账接口address implementer =_ERC1820_REGISTRY.getInterfaceImplementer(msg.sender,keccak256("ERC777Token"));require(implementer != address(0),"Message sender should be an erc777 contract");for (uint256 i = 0; i < length; ++i) {IERC777(implementer).send(tos[i], vals[i], "");}}
}
本合约是erc777合约的回调方法。发布合约时需要填erc777合约地址。然后调用erc777合约的send方法,实现批量转账
更多推荐
批量转账erc777代币
发布评论