totalsupply() 不是函数 openzeppelin contracts

totalsupply() is not a function openzeppelin contracts

我正在尝试从 open zeppelin 导入一些合约文件,这样我的 solidity 智能合约就可以继承它们的功能,当我在编译时尝试在我的智能合约上编写 运行 的 chai 测试时,我收到一个错误在我的柴测试中。

  3 passing (2s)
  1 failing

  1) Contract: Color
       minting
         creates a new token:
     TypeError: contract.totalSupply is not a function

我的合约导入了 openzeppelin 合约

pragma solidity 0.8.7;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; //import base functionality
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol"; //import totalsupply()

contract color is ERC721 {
    string[] public colors;
    mapping(string => bool) _colorExists; //mappings are like json objects where value a is searched and its value is returned
    constructor() ERC721("Color", "COLOR") {
    }

    function mint(string memory _color) public{
      colors.push(_color);
      uint _id = colors.length -1;

      _mint(msg.sender,_id);
      _colorExists[_color] = true;
 
    }
}

最后是我的测试文件(我已将其缩短以仅显示给我错误的测试)

const { assert } = require('chai')
const Color = artifacts.require('./Color.sol')

require('chai')
.use(require('chai-as-promised'))
.should()

contract('Color', (accounts) =>{
    let FormControlStatic

    before(async ()=>{
        contract = 
        await Color.deployed()
    })
    
    describe('minting', async ()=>{
        
        it('creates a new token', async ()=>{
            const result = await contract.mint('#EC058E')
            console.log(result)
            const totalSupply = await contract.totalSupply()

            assert.equal(totalSupply,1)
            console.log(result)
        })
    })
})

另外,如果我们查看包含函数 totalSupply() 的文件,它是公开范围的,因此它应该通过 import

在函数外部可见

我做了一些挖掘并从 openzeppelin 导入了实际函数所在的文件,但是似乎我仍然遇到相同的错误,我尝试单独编译以查看更改后重新编译是否可以解决,但没有

不确定最近是否有其他人经历过这个问题或者可能有解决方案 我也在这里导入当前版本 https://www.npmjs.com/package/@openzeppelin/contracts

谢谢!

我们必须扩展 IERC721Enumerable 合约,并实现其虚拟功能。

contract Color is ERC721, IERC721Enumerable { // We must extends IERC721Enumerable 
    string[] public colors;
    mapping(string => bool) _colorExists;

    constructor() ERC721("Color", "COLOR") {}

    function mint(string memory _color) public {
        colors.push(_color);
        uint256 _id = colors.length - 1;

        // _mint(msg.sender,_id);
        _colorExists[_color] = true;
    }
    

    // And must override below three functions

    function tokenOfOwnerByIndex(address owner, uint256 index) public view override returns (uint256) {
        // You need update this logic.
        // ...
        return 3;
    }

    function totalSupply() external  view override returns (uint256) {
      // You need update this logic.
      // ...
      return 1;
    }

    function tokenByIndex(uint256 index) external view  override returns (uint256) {
      // You need update this logic.
      // ...
      return 5;
    }
}

然后,我们可以调用totalSupply()方法

下面是智能合约的完整实现。

正如您在问题中提到的,它使用比 tutorial:

更新的版本
  • 可靠性 (0.8.0)
  • 打开 Zeppelin (4.3.2)
pragma solidity ^0.8.0; // Note that this is using a newer version than in 

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


contract Color is ERC721, ERC721Enumerable {
    string[] public colors;
    mapping(string => bool) _colorExists;

    constructor() ERC721("Color", "COLOR") public {
    }

    function _beforeTokenTransfer(address from, address to, uint256 tokenId)
    internal
    override(ERC721, ERC721Enumerable)
    {
        super._beforeTokenTransfer(from, to, tokenId);
    }

    function supportsInterface(bytes4 interfaceId)
    public
    view
    override(ERC721, ERC721Enumerable)
    returns (bool)
    {
        return super.supportsInterface(interfaceId);
    }


    function mint(string memory _color) public {
        colors.push(_color);
        uint _id = colors.length - 1;
        _mint(msg.sender, _id);
        _colorExists[_color] = true;
    }
}


您还可以查看 OpenZeppelin 关于 Extending Contracts 的部分以了解 overridessuper

您可以按照以下步骤继续教程。

  • 首先用ERC721Enumerable创建一个新的合约文件:
 ./node_modules/.bin/truffle-flattener ./node_modules/@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol > contracts/ERC721Enumerable.sol
  • 第二次用新的 ERC721Enumerable:
  • 替换旧的 ERC721
import "./ERC721Enumerable.sol";
     
contract Color is ERC721Enumerable {
         // 
     }
  • 删除合同文件夹下的旧 ERC721Full.sol 文件。