简体   繁体   English

为什么ERC721的铸币function有权限控制?

[英]Why does the minting function of ERC721 have an access control?

Most of the ERC721 examples using Open Zeppelin I see require the mint function to have an access control where only the owner of the contract is allowed to call the function.我看到的大多数使用 Open Zeppelin 的 ERC721 示例都要求 mint function 具有访问控制,其中只有合约的所有者被允许调用 function。 For example , 例如

function mint(address to) public virtual {
    require(hasRole(MINTER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have minter role to mint");

    _mint(to, _tokenIdTracker.current());
    _tokenIdTracker.increment();
}

or the following using the Ownable library.或以下使用Ownable库。

function mint(address receiver) external onlyOwner returns (uint256) {
    _tokenIds.increment();

    uint256 newTokenId = _tokenIds.current();
    _mint(receiver, newTokenId);

    return newTokenId;
}

Does this mean a new contract has to be deployed each time a new token is minted?这是否意味着每次铸造新代币时都必须部署新合约? This seems not only excessive in terms of the gas fee, but also the ERC721 contract has properties for mapping different owners and tokens:这似乎不仅在 gas 费用方面过高,而且 ERC721 合约还具有映射不同所有者和代币的属性:

// Mapping from token ID to owner address
mapping (uint256 => address) private _owners;

// Mapping owner address to token count
mapping (address => uint256) private _balances;

which wouldn't make sense if minting is restricted to the contract owner.如果铸币仅限于合约所有者,这将毫无意义。

It makes more sense to me that you deploy a single ERC721 contract (and its dependencies) and have the users call the mint function.对我来说,部署单个ERC721 合约(及其依赖项)并让用户调用 mint function 更有意义。 What is the best practice for the mint function of ERC721? ERC721的薄荷function的最佳实践是什么?

The ERC-721 standard does not define a "best" or "correct" way to mint new tokens (such as whether it should be open or restricted) and it's up to each contract developer to implement or omit the minting feature in a way that reflects their needs.ERC-721标准没有定义铸造新代币的“最佳”或“正确”方式(例如是否应该开放或限制),并且由每个合约开发人员以一种方式实现或省略铸造功能反映了他们的需求。

Creating of NFTs ("minting") and destruction NFTs ("burning") is not included in the specification.规范中不包括创建 NFT(“铸造”)和销毁 NFT(“燃烧”)。 Your contract may implement these by other means.您的合同可以通过其他方式实施这些。 Please see the event documentation for your responsibilities when creating or destroying NFTs.在创建或销毁 NFT 时,请参阅事件文档以了解您的职责。

But having a whitelist of addresses that are authorized to mint new tokens (eg MINTER_ROLE or onlyOwner ) seems to be more common than allowing anyone to freely mint new tokens.但是拥有一个授权铸造新代币的地址白名单(例如MINTER_ROLEonlyOwner )似乎比允许任何人自由铸造新代币更常见。


Even though it's theoretically possible to deploy new contract each time a new token is minted, it's not a standard approach (and I personally haven't seen any contract that does it).尽管理论上可以在每次铸造新代币时部署新合约,但这不是标准方法(我个人还没有看到任何这样做的合约)。 In most cases the minting process "just" creates a new ID, stores a new string/URL value associated with the ID, associates this new token with an owner address (of the token, not a contract owner), plus updates some metadata such as amount of tokens owned by an address (see example below).在大多数情况下,铸币过程“只是”创建一个新 ID,存储与该 ID 关联的新字符串/URL 值,将这个新令牌与所有者地址(令牌的,而不是合约所有者)相关联,并更新一些元数据,例如作为地址拥有的代币数量(参见下面的示例)。

The token owner can then transfer their tokens, give anyone control over their tokens, and do other stuff depending on the contract implementation.然后,代币所有者可以转移他们的代币,让任何人控制他们的代币,并根据合约实施做其他事情。

The mappings that you point out in your question ( _owners and _balances ) suggest that they store token owner (not contract owner) addresses as well as amount of tokens held by each address.您在问题中指出的映射( _owners_balances )表明它们存储代币所有者(而非合约所有者)地址以及每个地址持有的代币数量。

Example:例子:

  1. Contract owner mints token ID 1 to address 0x123 .合约所有者将令牌 ID 1铸造到地址0x123

    • Value of _owners[1] is 0x123 (was 0, the default value) _owners[1]的值为0x123 (为 0,默认值)

    • Value of _balances[0x123] becomes 1 (was 0, the default value) _balances[0x123]的值变为1 (原为 0,默认值)

  2. Contract owner mints token ID 2 to address 0x123 .合约所有者铸造代币 ID 2到地址0x123

    • Value of _owners[1] is still 0x123 _owners[1]的值仍然是0x123

    • Value of _owners[2] is now 0x123 (was 0, the default value) _owners[2]的值现在是0x123 (原为 0,默认值)

    • Value of _balances[0x123] becomes 2 (because they now own 2 tokens) _balances[0x123]的值变为2 (因为他们现在拥有 2 个令牌)

Replying to comments: As stated in example contract owner (markeplace owner) will mint token then this owner has to bear gas fee.回复评论:如示例合约所有者(市场所有者)将铸造代币,然后该所有者必须承担汽油费。 What if i want address 0x123 token owner to pay gas fees?..thanks.如果我希望地址 0x123 代币所有者支付 gas 费用怎么办?..谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM