Add ERC1155 BatchTranfer to array of address


I’m pretty new to blockchain development and especially ERC1155 and now I’m trying to figure out how to use the BatchTransfer function in an ERC1155 contract.
I want the same function as below but I also want to address _to to be an array, the ID and amount will be a fixed amount.
I’d be most grateful if anyone can help me or point me in the right direction!

:computer: Environment
Remix solidity ^0.5.11,

:1234: Code to reproduce

  function safeBatchTransferFrom(address _from, address _to, uint256[] memory _ids, uint256[] memory _amounts, bytes memory _data)
    // Requirements
    require((msg.sender == _from) || isApprovedForAll(_from, msg.sender), "ERC1155#safeBatchTransferFrom: INVALID_OPERATOR");
    require(_to != address(0), "ERC1155#safeBatchTransferFrom: INVALID_RECIPIENT");

    _safeBatchTransferFrom(_from, _to, _ids, _amounts);
    _callonERC1155BatchReceived(_from, _to, _ids, _amounts, _data);


  function _safeBatchTransferFrom(address _from, address _to, uint256[] memory _ids, uint256[] memory _amounts)
    require(_ids.length == _amounts.length, "ERC1155#_safeBatchTransferFrom: INVALID_ARRAYS_LENGTH");

    // Number of transfer to execute
    uint256 nTransfer = _ids.length;

    // Executing all transfers
    for (uint256 i = 0; i < nTransfer; i++) {
      // Update storage balance of previous bin
      balances[_from][_ids[i]] = balances[_from][_ids[i]].sub(_amounts[i]);
      balances[_to][_ids[i]] = balances[_to][_ids[i]].add(_amounts[i]);

    // Emit event
    emit TransferBatch(msg.sender, _from, _to, _ids, _amounts);
1 Like

Hi @Dazzy,

Welcome to the community :wave:

ERC1155 offers a BatchTransfer fof multiple token IDs and values but not recipients.

You can add your own BatchTransfer but you will need to comply with the EIP:

See scenario 9 and Implementation specific transfer API rule in the EIP on Safe Transfer rules:

You will need to emit a TransferSingle for each transfer.

Your code will essentially need to loop through calls to safeTransferFrom ( or equivalent.

1 Like