什么是 bytes calldata _data?

What is bytes calldata _data?

bytes calldata _data在这个合约函数中的作用是什么,如何使用?

  /**
    Mint a batch of tokens into existence and send them to the `_recipient`
    address. In order to mint an item, its item group must first have been
    created. Minting an item must obey both the fungibility and size cap of its
    group.

    @param _recipient The address to receive all NFTs within the newly-minted
      group.
    @param _ids The item IDs for the new items to create.
    @param _amounts The amount of each corresponding item ID to create.
    @param _data Any associated data to use on items minted in this transaction.
  */
  function mintBatch(address _recipient, uint256[] calldata _ids,
    uint256[] calldata _amounts, bytes calldata _data)
    external virtual {
    require(_recipient != address(0),
      "ERC1155: mint to the zero address");
    require(_ids.length == _amounts.length,
      "ERC1155: ids and amounts length mismatch");

    // Validate and perform the mint.
    address operator = _msgSender();
    _beforeTokenTransfer(operator, address(0), _recipient, _ids, _amounts,
      _data);

    // Loop through each of the batched IDs to update storage of special
    // balances and circulation balances.
    for (uint256 i = 0; i < _ids.length; i++) {
      require(_hasItemRight(_ids[i], MINT),
        "Super1155: you do not have the right to mint that item");

      // Retrieve the group ID from the given item `_id` and check mint.
      uint256 shiftedGroupId = (_ids[i] & GROUP_MASK);
      uint256 groupId = shiftedGroupId >> 128;
      uint256 mintedItemId = _mintChecker(_ids[i], _amounts[i]);

      // Update storage of special balances and circulating values.
      balances[mintedItemId][_recipient] = balances[mintedItemId][_recipient]
        .add(_amounts[i]);
      groupBalances[groupId][_recipient] = groupBalances[groupId][_recipient]
        .add(_amounts[i]);
      totalBalances[_recipient] = totalBalances[_recipient].add(_amounts[i]);
      mintCount[mintedItemId] = mintCount[mintedItemId].add(_amounts[i]);
      circulatingSupply[mintedItemId] = circulatingSupply[mintedItemId]
        .add(_amounts[i]);
      itemGroups[groupId].mintCount = itemGroups[groupId].mintCount
        .add(_amounts[i]);
      itemGroups[groupId].circulatingSupply =
        itemGroups[groupId].circulatingSupply.add(_amounts[i]);
    }

    // Emit event and handle the safety check.
    emit TransferBatch(operator, address(0), _recipient, _ids, _amounts);
    _doSafeBatchTransferAcceptanceCheck(operator, address(0), _recipient, _ids,
      _amounts, _data);
  }

calldata是包含函数参数的特殊数据位置,仅适用于外部函数调用参数。 Calldata 是存储函数参数的不可修改、非持久区域,其行为主要类似于 memory.

如果可以,请尝试使用calldata作为数据位置,因为它可以避免复制并确保数据无法被修改。具有 calldata 数据位置的数组和结构也可以从函数返回,但无法分配此类类型。

现在至于bytes:它只是一个变量类型,包含从 1 到最多 32 的字节序列。

还有关于实参,在合同中的意思,我找到你说的合同,它的附加数据没有指定格式,好像也是可选参数。

注:

在版本 0.6.9 之前,引用类型参数的数据位置在外部函数中限制为 calldata,在 public 函数中限制为 memory,并且 memorystorage 在内部和私人的。现在 memorycalldata 可以在所有函数中使用,无论它们的可见性如何。

在 0.5.0 版本之前,数据位置可以省略,并且会根据变量的种类、函数类型等默认到不同的位置,但现在所有复杂类型都必须给出明确的数据位置。

有关 calldata 的更多详细信息,请转到 here

有关 bytes 的更多详细信息,请转到 here

有关实际合同的更多详细信息,请转到 here