0

I am trying to write a function in my ERC-20 contract that returns an array containing the tokenIDs that the caller currently holds in their wallet for an associated ERC-721 contract.

So far I have this. I am storing the array in memory, and because of that I cannot use a dynamic array. I am first iterating through all of the token IDs to check if the caller is "owner of", and then setting the array length to the total number that they own.

Then I am going through the same for loop and adding each matching ID to the array.

I cannot get it to function properly - any help would be appreciated.

function viewTokensOwned(address _contractAddress) public view returns(uint256[] memory){
        require(trustedContracts [_contractAddress], "Not a trusted contract");

        uint256 n = 0;

        for(uint256 i = 1; i < 3333; i++) {
            
            if(iSkull(_contractAddress).ownerOf(i) == msg.sender) {
                n++;
            }
            else {
            continue;
            }
        }
        
        uint256[] memory _tokensOwned = new uint256[](n);

        for(uint256 i = 1; i < 3333; i++) {
            if(iSkull(_contractAddress).ownerOf(i) == msg.sender) {
                _tokensOwned[i];
            }
            else {
            continue;
            }

        return _tokensOwned;
        }
1
  • Please share additional details on the error/unexpected behavior + remove the useless "else continue;" Nov 25 at 10:57

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.