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,在公共函数中被限制为memory,在内部和私有函数中被限制为memorystorage。现在memorycalldata可以在所有函数中使用,不管它们是否可见。

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

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

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

有关实际合同的更多细节,请点击此处。

最新更新