【问题标题】:Keep track of all minted tokens on ERC721 Smart Contract跟踪 ERC721 智能合约上的所有铸造代币
【发布时间】:2021-04-10 16:32:09
【问题描述】:

我有一个Smart Contract,我想为它构建一个基于网络的市场应用程序。如何在合同中列出所有可用的“项目”。鉴于铸造的Item数量没有上限,如何实现分页和搜索?

什么是最佳实践和可扩展的解决方案:

  • 简单地返回 Items[] 数组

  • 在传输和其他事件上将数组与我的数据库同步

  • 其他建议?

    pragma solidity ^0.4.24;
    
    contract Item is ERC721{
    
    struct Item{
        string name; // Name of the Item
        uint level; // Item Level
        uint rarityLevel;  // 1 = normal, 2 = rare, 3 = epic, 4 = legendary
    }
    
    Item[] public items; // First Item has Index 0
    address public owner;
    
    function Item() public {
        owner = msg.sender; // The Sender is the Owner; Ethereum Address of the Owner
    }
    
    function createItem(string _name, address _to) public{
        require(owner == msg.sender); // Only the Owner can create Items
        uint id = items.length; // Item ID = Length of the Array Items
        items.push(Item(_name,5,1)) // Item ("Sword",5,1)
        _mint(_to,id); // Assigns the Token to the Ethereum Address that is specified
    }
    
    }
    

【问题讨论】:

    标签: ethereum solidity smartcontracts


    【解决方案1】:

    您可以创建一个 itemsCount 公共属性来保存现有项目的当前数量,并在每个 items.push() 之后增加它。


    没有分页和搜索:

    想要读取您的数据的链下系统,可以简单地从items[0] 循环到items[itemsCount],因为它只是一个读取操作,它不需要事务(即它是免费的)。


    带分页和搜索:

    您可以创建一个view 函数:

    1. 将page 和query 作为参数
    2. 循环遍历现有的items,如果项目符合条件(name 包含query),则将其添加到结果数组中
    3. 返回结果数组

    注意:在 Solidity 中,步骤 2 实际上会稍微复杂一些,因为您不能将 push 放入内存中的动态数组。所以你需要:

    1. 遍历项目并找到符合条件的数量(不超过页数限制)
    2. 创建一个固定长度的内存数组results
    3. 现在您可以再次遍历项目并用值填充固定长度的results 数组

    【讨论】:

      【解决方案2】:

      映射比数组更有效。您可以在智能合约中有一个 uint 来保存每个 Item 的计数,并使用 getter 函数从要从项目计数中减去的任何数字中获取每个项目。

      contract Item is ERC721{
      
      struct Item{
          string name; // Name of the Item
          uint level; // Item Level
          uint rarityLevel;  // 1 = normal, 2 = rare, 3 = epic, 4 = legendary
          uint id;
      }
      
      mapping(uint => Item) items; // First Item has Index 0
      uint count;
      address public owner;
      
      function Item() public {
          owner = msg.sender; // The Sender is the Owner; Ethereum Address of the Owner
      }
      
      function createItem(string memory _name, address _to, uint level, uint rarityLevel
                          uint id) public{
          require(owner == msg.sender); // Only the Owner can create Items
              uint num = count++;
              items[num].name = _name;
              items[num].level = level;
              items[num].rarityLevel = rarityLevel;
              items[num].id = num;
              count++;
      }
      
      function getItem(uint item) public view returns(string memory _name, uint level, 
                       uint rarityLevel, uint id){
              uint level = items[item].level;
              uint rarityLevel = items[item].rarityLevel;
              uint id = items[item].id;
              string memory _name = items[item].name
              return(level, rarityLevel, id, _name)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-07-01
        • 1970-01-01
        • 2021-06-26
        • 2019-02-17
        • 2022-07-21
        • 2022-12-12
        • 1970-01-01
        • 2017-08-03
        相关资源
        最近更新 更多