我的问题有两部分,
我正在尝试使用带有元数据的 Openzeppelin 合约创建一个 ERC721 代币,这是可枚举的。我的理解是在 openzeppelin v4.0.0 之后,他们删除了包含元数据和可枚举的 ERC721Full.sol 合同。我想使用solidity 0.8.0,所以那些旧合同不起作用,对吗?将 ERC721Enumerable.sol 导入继承到 ERC721.sol 合约时,得到 TypeError: Definition of base has to precede definition of derived contract
我尝试在自己的合同中导入 ERC721Enumerable.sol,但仍然出错。我还尝试导入旧的 ERC721Full.sol 合约并将所有 pragma 0.5.0 更改为 pragma 0.8.0,但它像其他十几个合约一样继承并且更改所有这些合约似乎并不明智。我用 IERC721Enumerable.sol 尝试了同样的方法,仍然错误。有任何想法吗?任何帮助都会很棒!
第二部分。 ERC__ 和 IERC__ 有什么区别? IERC 合同的目的是什么?
谢谢!!
这是我的合同(我正在学习教程)。我导入常规的 ERC721 合约,继承它。当我测试并调用 totalSupply 函数时,它给了我一个错误,因为没有 totalSupply 函数:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract Color is ERC721 {
string[] public colors;
mapping(string => bool) _colorExists;
constructor() ERC721("Color", "COLOR") {
}
function mint(string memory _color) public {
colors.push(_color);
uint _id = colors.length;
_mint(msg.sender, _id);
_colorExists[_color] = true;
}
}
我的测试脚本:
const Color = artifacts.require('./Color.sol')
require('chai')
.use(require('chai-as-promised'))
.should()
contract('Color', (accounts) => {
let contract
before(async () => {
contract = await Color.deployed()
})
describe('deployment', async () => {
it('deploys successfully', async () => {
contract = await Color.deployed()
const address = contract.address
console.log(address)
assert.notEqual(address, 0x0)
assert.notEqual(address,'')
assert.notEqual(address, null)
assert.notEqual(address, undefined)
})
it('has a name', async () => {
const name = await contract.name()
assert.equal(name, 'Color')
})
it('has a symbol', async () => {
const symbol = await contract.symbol()
assert.equal(symbol, 'COLOR')
})
})
describe('minting', async () => {
it('creates a new token', async () => {
const result = await contract.mint('#00CD22')
const totalSupply = await contract.totalSupply()
// SUCCESS
asert.equal(totalSupply, 1)
})
})
})
this is my error without the enumerable contract/without totalSupply
如果您愿意,我可以粘贴 openzeppelin 合同,或者链接它们here
I also tried this, importing ERC721Enumerable
告诉我您需要更多信息! 提前致谢
答案 0 :(得分:1)
默认情况下,ERC721 没有 totalSupply
方法,这就是您收到错误的原因。 totalSupply
方法来自 IERC721Enumerable,它是标准 ERC721 的可选扩展,如 the documentation states。如果您希望您的 ERC721 是可枚举的,只需从 openzeppelin 实现为您的派生合约导入可枚举扩展,如下所示:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract Color is ERC721Enumerable {
string[] public colors;
mapping(string => bool) _colorExists;
constructor() ERC721("Color", "COLOR") {
}
function mint(string memory _color) public {
colors.push(_color);
uint _id = colors.length;
_mint(msg.sender, _id);
_colorExists[_color] = true;
}
}
在尝试导入 ERC721Enumerable 时编译器给您错误的原因是您尝试在 Openzeppelin ERC721 实现中导入它,但该合约必须在 ERC721Enumberable 之前存在。换句话说,继承链是
ERC721 <-- ERC721Enumerable
你想做的是
ERC721 <-- ERC721Enumerable
|_____________↑
这会造成无法满足的循环依赖。
ERC 合约就像每种 OOP 编程语言中的抽象类(首先想到的,也许最相关的是 Java 和 C++),而 IERC 是接口;这意味着虽然两者都不能直接实例化(它们都需要孩子来实现某些东西),但 ERC 合约为相应的 IERC 方法提供了标准实现。这就是你经常看到合约实现 ERC 合约而不是 IERC 合约的原因。
答案 1 :(得分:1)
要使用 ERC271Enumerable 扩展,您需要实现它并覆盖 ERC271、_beforeTokenTransfer
和 supportsInterface
的一些功能。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
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", "COLORS") {}
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;
_mint(msg.sender, _id);
_colorExists[_color] = true;
}
}
ERC__ 和 IERC__ 有什么区别? IERC 合同的目的是什么?
重要的是确保 Contract 实现具有正确的方法,以及正确的可见性、参数和返回值。