2

im having a big trouble trying to resolve this problem. Maybe anyone here can help me find it.

This is the .sol contract and im having this error:


TypeError: Member "toString" not found or not visible after argument-dependent lookup in uint256.
    --> VasikTest_flat.sol:1347:51:
     |
1347 |         ? string(abi.encodePacked(currentBaseURI, _tokenId.toString(), uriSuffix))
     |                                                   ^^^^^^^^^^^^^^^^^

Maybe anyone can help me here.

pragma solidity >=0.7.0 <0.9.0;


contract VasikTest is ERC721Enumerable, Ownable {  
    using Address for address;

    string public uriPrefix = "";
    string public uriSuffix = ".json";
    string public hiddenMetadataUri;
    
    // Starting and stopping sale, presale and whitelist
    bool public saleActive = false;
    bool public whitelistActive = false;
    bool public presaleActive = false;
    bool public revealed = false;


    // Reserved for the team, customs, giveaways, collabs and so on.
    uint256 public reserved = 2;

    // Price of each token
    uint256 public initial_price = 0.001 ether;
    uint256 public price;

    // Maximum limit of tokens that can ever exist
    uint256 public constant MAX_SUPPLY = 10;
    uint256 public constant MAX_PRESALE_SUPPLY = 5;
    uint256 public constant MAX_MINT_PER_TX = 2;


    // Team addresses for withdrawals
    address public a1;
    address public a2;
    address public a3;

    // List of addresses that have a number of reserved tokens for whitelist
    mapping (address => uint256) public whitelistReserved;


    constructor () ERC721 ("Vasik One mint", "OneMintVasik") {
        price = initial_price;
        setHiddenMetadataUri("ipfs://QmVntyJMSn8FxYVHtWig3qtfFed5aH1mr7EeXx1nespWpu/hidden.json");
    }

    function tokenURI(uint256 _tokenId)
    public
    view
    virtual
    override
    returns (string memory)
  {
    require(
      _exists(_tokenId),
      "ERC721Metadata: URI query for nonexistent token"
    );

    if (revealed == false) {
      return hiddenMetadataUri;
    }
    string memory currentBaseURI = _baseURI();
    return bytes(currentBaseURI).length > 0
        ? string(abi.encodePacked(currentBaseURI, _tokenId.toString(), uriSuffix))
        : "";
  }

    // See which address owns which tokens
    function tokensOfOwner(address addr) public view returns(uint256[] memory) {
        uint256 tokenCount = balanceOf(addr);
        uint256[] memory tokensId = new uint256[](tokenCount);
        for(uint256 i; i < tokenCount; i++){
            tokensId[i] = tokenOfOwnerByIndex(addr, i);
        }
        return tokensId;
    }

    // Exclusive whitelist minting
    function mintWhitelist(uint256 _amount) public payable {
        uint256 supply = totalSupply();
        uint256 reservedAmt = whitelistReserved[msg.sender];
        require( whitelistActive,                   "Whitelist isn't active" );
        require( reservedAmt > 0,                   "No tokens reserved for your address" );
        require( _amount <= reservedAmt,            "Can't mint more than reserved" );
        require( supply + _amount <= MAX_SUPPLY,    "Can't mint more than max supply" );
        require( msg.value == price * _amount,      "Wrong amount of ETH sent" );
        whitelistReserved[msg.sender] = reservedAmt - _amount;
        for(uint256 i; i < _amount; i++){
            _safeMint( msg.sender, supply + i );
        }
    }

    // Presale minting
    function mintPresale(uint256 _amount) public payable {
        uint256 supply = totalSupply();
        require( presaleActive,                             "Sale isn't active" );
        require( _amount > 0 && _amount <= MAX_MINT_PER_TX, "Can only mint between 1 and 20 tokens at once" );
        require( supply + _amount <= MAX_PRESALE_SUPPLY,    "Can't mint more than max supply" );
        require( msg.value == price * _amount,              "Wrong amount of ETH sent" );
        for(uint256 i; i < _amount; i++){
            _safeMint( msg.sender, supply + i );
        }
    }

    // Standard mint function
    function mintToken(uint256 _amount) public payable {
        uint256 supply = totalSupply();
        require( saleActive,                                "Sale isn't active" );
        require( _amount > 0 && _amount <= MAX_MINT_PER_TX, "Can only mint between 1 and 10 tokens at once" );
        require( supply + _amount <= MAX_SUPPLY,            "Can't mint more than max supply" );
        require( msg.value == price * _amount,              "Wrong amount of ETH sent" );
        for(uint256 i; i < _amount; i++){
            _safeMint( msg.sender, supply + i );
        }
    }

    // Admin minting function to reserve tokens for the team, collabs, customs and giveaways
    function mintReserved(uint256 _amount) public onlyOwner {
        // Limited to a publicly set amount
        require( _amount <= reserved, "Can't reserve more than set amount" );
        reserved -= _amount;
        uint256 supply = totalSupply();
        for(uint256 i; i < _amount; i++){
            _safeMint( msg.sender, supply + i );
        }
    }
    
    // Edit reserved whitelist spots
    function editWhitelistReserved(address[] memory _a, uint256[] memory _amount) public onlyOwner {
        for(uint256 i; i < _a.length; i++){
            whitelistReserved[_a[i]] = _amount[i];
        }
    }

    

    // Start and stop whitelist
    function setWhitelistActive(bool val) public onlyOwner {
        whitelistActive = val;
    }

    // Start and stop presale
    function setPresaleActive(bool val) public onlyOwner {
        presaleActive = val;
    }

    // Start and stop sale
    function setSaleActive(bool val) public onlyOwner {
        saleActive = val;
    }

      function setHiddenMetadataUri(string memory _hiddenMetadataUri) public onlyOwner {
    hiddenMetadataUri = _hiddenMetadataUri;
  }

  function setUriPrefix(string memory _uriPrefix) public onlyOwner {
    uriPrefix = _uriPrefix;
  }

  function setUriSuffix(string memory _uriSuffix) public onlyOwner {
    uriSuffix = _uriSuffix;
  }

    function setRevealed(bool _state) public onlyOwner {
    revealed = _state;
    }

    // Set a different price in case ETH changes drastically
    function setPrice(uint256 newPrice) public onlyOwner {
        price = newPrice;
    }

    // Set team addresses
    function setAddresses(address[] memory _a) public onlyOwner {
        a1 = _a[0];
        a2 = _a[1];
        a3 = _a[2];
    }

    // Withdraw funds from contract for the team
    function withdrawTeam(uint256 amount) public payable onlyOwner {
        uint256 percent = amount / 100;
        require(payable(a1).send(percent * 40));
        require(payable(a2).send(percent * 40));
        require(payable(a3).send(percent * 20));
    }

    function _baseURI() internal view virtual override returns (string memory) {
    return uriPrefix;
  }
}

2 Answers 2

2

toString() simply is not normally a function in Solidity. There is however this library in OpenZeppelin:

https://github.com/OpenZeppelin/openzeppelin-contracts/blob/538b6d21b15733601f9193af5b9f662b94f16ea1/contracts/utils/Strings.sol

Which can accept a uint256 as an argument and return a string, which I think is what you're looking for. So just import that library and your code should work.

So here's what needs to be changed: Not sure how to format it better but you just have to add the import line, and replace the return line with the one I've added below.

pragma solidity >=0.7.0 <0.9.0; 

import "@openzeppelin/contracts/utils/Strings.sol"; 

return bytes(currentBaseURI).length > 0 ? 
string(abi.encodePacked(currentBaseURI, Strings.toString(_tokenId), uriSuffix)) : ""; 
10
  • I try importing the strings.sol file and still the error remains...
    – vasik
    Commented Jan 19, 2022 at 4:36
  • You need to call it through the library, using Strings.toString().
    – Meriadoc
    Commented Jan 19, 2022 at 4:51
  • Can you send me that part of the code with your correction? Because I think I am doing it the wrong way.
    – vasik
    Commented Jan 19, 2022 at 5:10
  • I edited it into my answer, you just need the import statement and to replace _tokenId.toString with Strings.toString(_tokenId), hope that helps
    – Meriadoc
    Commented Jan 19, 2022 at 6:02
  • Work perfectly but now when i mint the first NFT, it gives me the first with an IDTOKEN 0, and must be IDTOKEN 1. Did you know how to solve that? sorry to bother you
    – vasik
    Commented Jan 19, 2022 at 17:51
2

You can also use this:

using Strings for uint256;
1
  • 1
    That will work if you imported the correct file with the library definition.
    – Ismael
    Commented Mar 7, 2022 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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