首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >什么是bytes calldata _data?

什么是bytes calldata _data?
EN

Stack Overflow用户
提问于 2021-08-31 11:14:32
回答 1查看 811关注 0票数 0

在这个约定函数中,bytes calldata _data的功能是什么?如何使用它?

代码语言:javascript
复制
  /**
    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);
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-01 00:01:09

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

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

至于bytes:它只是一个变量类型,可以保存从1到32的字节序列。

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

注意:

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

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

有关calldata的更多详细信息,请访问here

有关bytes的更多详细信息,请访问here

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68997666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档