0

I have a Smart Contract that I'd like to build a web based marketplace app for. How can I list all available "Items" in the contract. Seeing as there is no upper limit to the number of minted Items, how can I implement pagination and search?

What is a best practice and scalable solution:

  • to simply return the Items[] array

  • sync the array with my database on Transfer and other events

  • other suggestions?

    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
      }
    
    }
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.