0

i make simple code, but have error "TypeError: Explicit type conversion not allowed from "uint256" to "address"." Can you help me? I even asked the gpt bot about this error, but it confused me even more)))

in the beginning there was a simple idea - 10k nft, 30 of them immediately after the deposit, the minimum price is indicated and everyone can buy without restriction

  `pragma solidity ^0.8.4;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

contract Sptzrawef is ERC721 {
  // Define the token name, symbol, and maximum supply
  uint256 public totalSupply = 10000;
  string public tokenURI;
  // Define the owner of the contract
  address public owner;

  // Define the minimum price of the token
  uint256 public minPrice;

  // Define the mapping for the token ownership
  mapping(address => mapping(uint256 => bool)) public tokenOwnership;

  // The constructor function
  constructor() ERC721("Sptzrawef", "SP") {
      tokenURI = "https://nftstorage.link/ipfs/ba.................q3oq/";

      owner = msg.sender;
      minPrice = 0.025 ether;
      totalSupply = 10000;
      // Mint 30 tokens immediately upon deployment
      for (uint256 i = 1; i <= 30; i++) {
          tokenOwnership[msg.sender][i] = true;
      }
  }

  // Function to mint new tokens
  function mint(address _to, uint256 _tokenId) public {
      require(msg.sender == owner);
      require(_tokenId <= totalSupply);
      require(tokenOwnership[_to][_tokenId] == false);
      tokenOwnership[_to][_tokenId] = true;
      totalSupply++;
  }

  // Function to buy the token
  function buy(uint256 _tokenId) public payable {
      require(msg.value >= minPrice);
      require(tokenOwnership[msg.sender][_tokenId] == false);
      require(_tokenId <= totalSupply);
      tokenOwnership[msg.sender][_tokenId] = true;
  }

  function balanceOf() public view returns (uint256) {
      return totalSupply;
  }

  function tokenOfOwnerByIndex(address _owner, uint256 _index) public view returns (uint256) {
      uint256 count = 0;
      for (uint256 i = 1; i <= totalSupply; i++) {
          if (tokenOwnership[_owner][i]) {
              if (count == _index) {
                  return i;
              }
              count++;
          }
      }
      return 0;
  }
  
  function ownerOf(uint256 _tokenId) public view override returns (address) {
      require(_tokenId <= totalSupply);
      for (uint256 i = 0; i < totalSupply; i++) {
          address wallet = address(i);                   -  error at this line
          if (tokenOwnership[wallet][_tokenId]) {
              return wallet;
          }
      }
      return address(0);
  }
}`

sofia
  • 3
  • 1

2 Answers2

-1

address is 20bytes and uint256 is 32bytes. Therefore, you need to use uint160 for the type of i to convert it to address. Or please convert i into uint160 before converting it to address

address wallet = address(uint160(i));
Satoshi Naoki
  • 353
  • 2
  • 10
-1

I think you are using ownerOf function in the wrong way. try to create a new "getWalletAddress" function, for example, and use it in this way:

  function getWalletAddress(uint256 _tokenId) public view returns (address) {
  require(_tokenId <= totalSupply);
  for (uint256 i = 0; i < totalSupply; i++) {
      address wallet = ownerOf(i);              
      if (tokenOwnership[wallet][_tokenId]) {
          return wallet;
      }
  }
  return address(0);

} }

Or if you want to override the "ownerOf" function then you should go for this code:

function ownerOf(uint256 _tokenId) public view overrides returns (address) {
  require(_tokenId <= totalSupply);
  for (uint256 i = 0; i < totalSupply; i++) {
      address wallet = _ownerOf(i);              
      if (tokenOwnership[wallet][_tokenId]) {
          return wallet;
      }
  }
  return address(0);

} }

I hope this help resolve your issue

cherry
  • 389
  • 5
  • 16
  • you idea is work!! thanks you. what you think if i use bytes20 ? its correct? – sofia Jan 30 '23 at 12:59
  • Actually, I use the bytes20 method but it always gave me a 0 address. that's why I suggested these functions. But you can give it a try – cherry Jan 30 '23 at 13:02