Solidity

TypeError:在 uint256 中進行參數相關查找後,成員“toString”未找到或不可見

  • March 7, 2022

我在嘗試解決這個問題時遇到了很大的麻煩。也許這裡的任何人都可以幫我找到它。

這是 .sol 契約,我有這個錯誤:


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))
    |                                                   ^^^^^^^^^^^^^^^^^

也許有人可以在這裡幫助我。

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

toString()在 Solidity 中通常不是一個函式。然而,OpenZeppelin 中有這個庫:

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

可以接受 auint256作為參數並返回 a string,我認為這就是您要尋找的。因此,只需導入該庫,您的程式碼就可以正常工作。

所以這裡是需要改變的:不確定如何更好地格式化它,但你只需要添加導入行,並將返回行替換為我在下面添加的行。

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)) : ""; 

你也可以使用這個:

using Strings for uint256;

引用自:https://ethereum.stackexchange.com/questions/119294