lut*_*dle 4 ethereum solidity openzeppelin
我正在尝试从打开的 zeppelin 导入一些合约文件,以便我的 Solidity 智能合约可以继承它们的功能,当尝试编写在编译时在我的智能合约上运行的 chai 测试时,我在 chai 测试中遇到错误。
3 passing (2s)
1 failing
1) Contract: Color
minting
creates a new token:
TypeError: contract.totalSupply is not a function
Run Code Online (Sandbox Code Playgroud)
我的合同导入了 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;
}
}
Run Code Online (Sandbox Code Playgroud)
最后是我的测试文件(我已缩短它以仅显示给我错误的测试)
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)
})
})
})
Run Code Online (Sandbox Code Playgroud)
另外,如果我们查看包含该函数的文件,totalSupply()
它的作用域是公共的,因此它应该通过导入在函数外部可见
我做了一些挖掘并从 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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我们就可以调用totalSupply()
方法了
归档时间: |
|
查看次数: |
4566 次 |
最近记录: |