0

I am new to the smart contract and please let me know why I have the error shown below.

  • I compiled with the command, "truffle compile".
  • I did not make any particular edits to the original contract on the github.

The error is: /Users/gemcutter/demo/contracts/nftokenEnum.sol:40:25: DeclarationError: Undeclared identifier. require(index < balanceOf(owner), "ERC721Enumerable: owner index out of bounds");

The contract is :

pragma solidity ^0.5.2;

interface IERC165 {
    function supportsInterface(bytes4 interfaceID) external view returns (bool);
}

interface IERC721 {
    event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId);
    event Approval(address indexed _owner, address indexed _approved, uint256 indexed _tokenId);
    event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved);

function balanceOf(address owner) external view returns (uint256 balance);
function ownerOf(uint256 tokenId) external view returns (address owner);
function safeTransferFrom(address from, address to, uint256 tokenId) external;
function transferFrom(address from, address to, uint256 tokenId) external;
function approve(address to, uint256 tokenId) external;
function getApproved(uint256 tokenId) external view returns (address operator);
function setApprovalForAll(address operator, bool _approved) external;
function isApprovedForAll(address owner, address operator) external view returns (bool);
function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;
}

interface IERC721Enumerable {
    function totalSupply() external view returns (uint256);
    function tokenOfOwnerByIndex(address owner, uint256 index) external view returns (uint256 tokenId);
    function tokenByIndex(uint256 index) external view returns (uint256);
}

contract ERC721Enum is IERC165, IERC721, IERC721Enumerable {
// ERC721Enumerable Variables
mapping(address => uint256[]) private _ownedTokens;
mapping(uint256 => uint256) private _ownedTokensIndex;
uint256[] private _allTokens;
mapping(uint256 => uint256) private _allTokensIndex;

bytes4 private constant _INTERFACE_ID_ERC721_ENUMERABLE = 0x780e9d63;

function tokenOfOwnerByIndex(address owner, uint256 index) public view returns (uint256) {
    require(index < balanceOf(owner), "ERC721Enumerable: owner index out of bounds");
    return _ownedTokens[owner][index];
}

function totalSupply() public view returns (uint256) {
    return _allTokens.length;
}

function tokenByIndex(uint256 index) public view returns (uint256) {
    require(index < totalSupply(), "ERC721Enumerable: global index out of bounds");
    return _allTokens[index];
}


function _transferFrom(address from, address to, uint256 tokenId) internal {
    super._transferFrom(from, to, tokenId);

    _removeTokenFromOwnerEnumeration(from, tokenId);
    _addTokenToOwnerEnumeration(to, tokenId);
}

function _tokensOfOwner(address owner) internal view returns (uint256[] storage) {
    return _ownedTokens[owner];
}

function _addTokenToOwnerEnumeration(address to, uint256 tokenId) private {
    _ownedTokensIndex[tokenId] = _ownedTokens[to].length;
    _ownedTokens[to].push(tokenId);
}

function _addTokenToAllTokensEnumeration(uint256 tokenId) private {
    _allTokensIndex[tokenId] = _allTokens.length;
    _allTokens.push(tokenId);
}

function _removeTokenFromOwnerEnumeration(address from, uint256 tokenId) private {
    uint256 lastTokenIndex = _ownedTokens[from].length.sub(1);
    uint256 tokenIndex = _ownedTokensIndex[tokenId];

    if (tokenIndex != lastTokenIndex) {
        uint256 lastTokenId = _ownedTokens[from][lastTokenIndex];

        _ownedTokens[from][tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
        _ownedTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index
    }

    _ownedTokens[from].length--;
}

function _removeTokenFromAllTokensEnumeration(uint256 tokenId) private {

    uint256 lastTokenIndex = _allTokens.length.sub(1);
    uint256 tokenIndex = _allTokensIndex[tokenId];
    uint256 lastTokenId = _allTokens[lastTokenIndex];

    _allTokens[tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
    _allTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index

    _allTokens.length--;
    _allTokensIndex[tokenId] = 0;
}
}
1

The error is Undeclared identifier:

browser/NFT.sol:39:21: DeclarationError: Undeclared identifier.
require(index < balanceOf(owner), "ERC721Enumerable: owner index out of bounds");
                ^-------^

In the contract as shown there isn't an implementation for the balanceOf function, so I assume that your contract is missing some code. I suggest you check the original source of the code you are using.


I recommend having a look at OpenZeppelin documentation on ERC721.

OpenZeppelin is a library for secure smart contract development. It provides implementations of standards like ERC20 and ERC721 which you can deploy as-is or extend to suit your needs, as well as Solidity components to build custom contracts and more complex decentralized systems.

If you have more questions about OpenZeppelin you can always ask in the community forum

  • Thank you for answering my question. Maybe I do not understand exactly what defines "implementation". I do not have "Undeclared identifier" errors for the other two functions: totalSupply() and tokenByIndex(uint index), although I do not "implement" them. What makes a difference between them here? – Gem Cutter Jul 10 at 7:55
  • By the way I saw the site, ethereum.stackexchange.com/questions/34805/interface-function Contract "implementation" is "interface" {}, is the way to define the "implementation"? I am confused. – Gem Cutter Jul 10 at 8:11
  • For information on Interfaces, please see the Solidity documentation: solidity.readthedocs.io/en/latest/contracts.html#interfaces Your contract has implemented totalSupply() and tokenByIndex(uint index). – abcoathup Jul 16 at 10:19

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.