0

Im using a course, and I got this error:

ReferenceError: totalSupply is not defined

and Idk what it means by that, cause I never got this error before. can somebody help me fix this error? thanks and this is my code:

const { assert } = require("chai");

const KryptoBird = artifacts.require("KryptoBird");

// check for chai
require('chai')
.use(require('chai-as-promised'))
.should()

contract('KryptoBird', (accounts) => {

    let contract
    before( async () => {
    contract = await KryptoBird.deployed()
    })
    
    
    
    describe('deployment', async() => {
        it("deploys successfully", async () => {
            const address = contract.address;
            assert.notEqual(address, '')
            assert.notEqual(address, null)
            assert.notEqual(address, undefined)
            assert.notEqual(address, 0x0)
        })
        it('has a name', async() => {
            const name = await contract.name()
            assert.equal(name, 'KryptoBird')
        }) 

        it('has a symbol', async() => {
            const symbol = await contract.symbol()
            assert.equal(symbol, 'KBIRDZ')
        })

    }) 

    describe('minting', async ()=> {
        it('creates a new token', async ()=> {
            const result = await contract.mint('https...1')
            const totalSupply = await contract.totalSupply();
            assert.equal(totalSupply, 1)
            const event = result.logs[0].args
            assert.equal(event._from, '0x0000000000000000000000000000000000000000', 'from is the contract')
            assert.equal(event._to, accounts[0], 'to is msg.sender')

            await contract.mint('https...1').should.be.rejected
        })
    })

    describe('indexing', async()=> {
        it('lists KryptoBirdz', async()=> {            
        // Mint three new tokens
        await contract.mint('https...2')
        await contract.mint('https...3')
        await contract.mint('https...4')
        const totalSupply = await contract.totalSupply()
        })

        let result = []
        let KryptoBird
        for(i = 1; i <= totalSupply; i++) {
            KryptoBird = await contract.kryptoBirdz(i)
            result.push(KryptoBird)
    }

    let expected = ['https...1','https...2','https...3','https...4']
    assert.equal(result.join(','), expected.join(','))

})

})

if u need another contract, just tell me, and if u could help me, thanks so much, because I need to finish this today. I've been trying to fix this for 2 hours, it didnt work. so please, if u could help me, it would mean a LOT to me.

here another code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import './libraries/ERC721.sol';
import './libraries/IERC721Enumerable.sol';


contract ERC721Enumerable is IERC721Enumerable, ERC721 {

    uint256[] private _allTokens;

    // mapping from tokenId to position in _allTokens array
        mapping(uint256 => uint256) private _allTokensIndex;

    // mapping of owner to list of all owner token ids
        mapping(address => uint256[]) private _ownedTokens;

    // mapping from token ID to index of the owner tokens list 
        mapping(uint256 => uint256) private _ownedTokensIndex;

        
constructor() {
        _registerInterface(bytes4(keccak256('totalSupply(bytes4)')^
        keccak256('tokenByIndex(bytes4)')^keccak256('tokenOfOwnerByIndex(bytes4)')));
    }


    function _mint(address to, uint256 tokenId) internal override(ERC721) {
        super._mint(to, tokenId);

        _addTokensToAllTokenEnumeration(tokenId); 
        _addTokensToOwnerEnumeration(to, tokenId);
    }


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

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

    // two functions - one that returns tokenByIndex and 
    // another one that returns tokenOfOwnerByIndex
    function tokenByIndex(uint256 index) public override view returns(uint256) {
        // make sure that the index is not out of bounds of the total supply 
        require(index < totalSupply(), 'global index is out of bounds!');
        return _allTokens[index];
    }

    function tokenOfOwnerByIndex(address owner, uint index) public override view returns(uint256) {
        require(index < balanceOf(owner),'owner index is out of bounds!');
        return _ownedTokens[owner][index];
    }

    // return the total supply of the _allTokens array
    function totalSupply() public override  view returns(uint256) {
        return _allTokens.length;
    }

}

1 Answer 1

0

Without your contract code, I can only assume that a possible issue could be your contract inherits from ERC721 instead of ERC721Enumerable, which contains the totalSupply() function, while ERC721 does not.

6
  • which contract code do u need?
    – Korumaq1xa
    Commented Sep 2, 2022 at 2:40
  • As much as you can provide
    – kev1n
    Commented Sep 2, 2022 at 2:43
  • I sent the ERC721Enumerable code, is that what u need?
    – Korumaq1xa
    Commented Sep 2, 2022 at 2:46
  • No now i'm just confused
    – kev1n
    Commented Sep 2, 2022 at 2:51
  • what contract do u need to be specific? like the name
    – Korumaq1xa
    Commented Sep 2, 2022 at 2:55

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.