首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ReferenceError:未定义totalSupply

ReferenceError:未定义totalSupply
EN

Ethereum用户
提问于 2022-09-02 02:11:08
回答 1查看 103关注 0票数 0

我正在使用一门课程,我得到了一个错误:

代码语言:javascript
复制
ReferenceError: totalSupply is not defined

我不知道这意味着什么,因为我以前从来没有犯过这个错误。有人能帮我解决这个错误吗?谢谢,这是我的密码:

代码语言:javascript
复制
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(','))

})

})

如果你需要另一份合同,就告诉我,如果你能帮我的话,非常感谢,因为我今天要完成这个任务。我已经试着修了两个小时了,它没有用。所以,如果你能帮我,这对我来说意义重大。

这里还有另一个代码:

代码语言:javascript
复制
// 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;
    }

}
EN

回答 1

Ethereum用户

发布于 2022-09-02 02:36:42

如果没有契约代码,我只能假设一个可能的问题是您从ERC721继承的合同,而不是包含totalSupply()函数的ERC721Enumerable,而ERC721没有。

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/134840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档