什么是字节 calldata _data?

Vic*_*dez 1 solidity

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);
  }
Run Code Online (Sandbox Code Playgroud)

MrF*_*oid 8

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

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

现在来说bytes:它只是一个变量类型,保存从 1 到 32 的字节序列。

至于实际参数,以及它在合同中的含义,我找到了你所指的合同,以及它的附加数据,没有指定格式,它似乎也是一个可选参数。

笔记:

在版本 0.6.9 之前,引用类型参数的数据位置仅限于calldata外部函数、memory公共函数以及内部函数memorystorage私有函数中。现在memorycalldata可以在所有函数中使用,无论其可见性如何。

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

有关更多详细信息calldata,请访问此处

有关更多详细信息bytes,请访问此处

有关实际合同的更多详细信息,请访问此处