1

I am trying to create 10 random nfts with different supplies using solidity version 6 and remix ide. However I keep getting the following errors:

contracts/bullrun_cards.sol:29:16: TypeError: Type struct MyNFT.Cards memory is not implicitly convertible to expected type struct MyNFT.Cards storage ref[] storage ref. card = Cards({supply:_supply, description:_desc,card_id:newNftTokenId}); ^--------------------------------------------------------------^

contracts/bullrun_cards.sol:30:35: TypeError: Type struct MyNFT.Cards storage ref[] storage ref is not implicitly convertible to expected type struct MyNFT.Cards storage ref. allCards[newNftTokenId-1]=card;

contracts/bullrun_cards.sol:31:15: TypeError: Integer constant expected. Cards[newNftTokenId-1].push(card); ^-------------^

My code is as follows:

// SPDX-License-Identifier: MIT
pragma solidity ^0.6.12;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/solc-0.6/contracts/token/ERC721/ERC721.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/solc-0.6/contracts/utils/Counters.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/solc-0.6/contracts/access/Ownable.sol";

contract MyNFT is ERC721, Ownable {
    
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds; //Counter is a struct in the Counters library
    uint constant total_supply =7777;
    
    constructor() ERC721("MyNFT", "MNFT") public {}
    
    struct Cards{
        uint supply;
        string description;
        uint card_id;
    }
    
    Cards[] public card;
    mapping(uint =>Cards)public allCards;
    
    function createCard(uint _supply, string memory _desc) public returns(uint){
        assert(_supply<total_supply);
        _tokenIds.increment();
        uint256 newNftTokenId = _tokenIds.current();
        card = Cards({supply:_supply, description:_desc,card_id:newNftTokenId});
        allCards[newNftTokenId-1]=card;
        Cards[newNftTokenId-1].push(card);
        
    }
    
    function mintToken( uint _s, string memory _d,  string memory _tokenURI) public onlyOwner
    {
        uint newid=createCard(_s, _d);
        address _address = msg.sender;
        _safeMint(_address, newid);
        _setTokenURI(newid, _tokenURI);
        
    }
}

please kindly help me in resolving this issue

2

First, I don't see why you link a token ID with a supply value. When you transfer a token, you use the tokenId then what will happen with the supply? Will it go entirely to the new owner?

Then you declare a public (not a good idea, keep it private) Cards[] public card which you overwrite when creating a new card (card = Cards({supply:_supply, description:_desc,card_id:newNftTokenId});). Maybe use another variable name, like newCard or _card, or whatever.

Another thing is when you want to add the new card to the cards list, don't do it like Cards[newNftTokenId-1].push(card);, you need to push the new card to the card property: card.push(_newCard)

1
  • the supply is something else its not what you are thinking. I am tracking something else. – mzaidi Mar 28 at 15:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.