- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从 open zeppelin 导入一些合约文件,以便我的 Solidity 智能合约可以继承它们的功能,当尝试编写在编译时在我的智能合约上运行的 chai 测试时,我在 chai 测试中遇到错误。
3 passing (2s)
1 failing
1) Contract: Color
minting
creates a new token:
TypeError: contract.totalSupply is not a function
我的契约(Contract)导入 openzeppelin 契约(Contract)
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()
的文件它是公共(public)范围的,因此它应该通过导入在函数外部可见
最佳答案
我们必须扩展 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()
方法
关于ethereum - totalsupply() 不是函数 openzeppelin 契约(Contract),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68810515/
当我点击 remix.ethereum.org 下的创建时,我不明白智能合约的实际去向。例如,如果我选择 Injected Web3,这应该会将合约发布到 ropsten 测试网上,对吧?但是我怎样才
我通过下面的推荐安装了ganache-cli npm install -g ganache-cli 我已经下载了文件 ganache-1.1.0-beta.0-x86_64.AppImage 当我点
如果我在 ETH 和 RSK 网络中导入相同的私钥,则帐户相同但大小写不同。为什么是这样?这个可以吗? 最佳答案 大写作为校验和,涉及以下标准 SLIP-0044 EIP-155 EIP-1191 有
我们落后于代理。代理已设置。 早些时候,我们运行以下命令并成功安装了以太坊。 sudo apt-get install software-properties-common sudo add-apt-
请找到我的 golang 规范 GOOS=linux GOARCH=amd64 go build github.com/ethereum/go-ethereum/crypto/secp256k1: b
我正在尝试使用 Puppeth(如 Péter Szilágyi demoed in Ethereum devcon three 2017)设置私有(private)以太坊测试网络。我在 macboo
我是一步一步做的: Git 克隆 go-ethereum 项目到我的本地 PC。它位于D:\GOPATH\src\github.com\ethereum\go-ethereum。这是源代码去以太坊 注
我正在尝试生成 personal_sign在 Golang 中就像在 ethers.js 中实现的那样。 Similar question但最终使用了常规 sign过个人sign_implementa
我正在尝试安装 Istanbul-tools 以运行 IBFT 以太坊网络,如本教程所示 https://medium.com/getamis/istanbul-bft-ibft-c2758b7fe6
我想降低我的 ERC-721A 合约的部署成本。 一般来说; 扁平化智能合约是否有助于降低其成本? 目前我正在使用 ethers.js's contractFactory.deploy method带
我有契约(Contract)要求输入 bytes32,所以我需要将地址转换为 bytes32,但在 web3 中没有看到这个方法。 最佳答案 以太坊地址为 20 个字节,因此您将十六进制地址转换为字节
我想与将在调用时部署其他契约(Contract)的功能签订契约(Contract)。可能吗?有什么想法吗? 最佳答案 这是部署合约的合约示例。 https://ethereum.stackexchan
由于与以太坊创建智能合约涉及以太币用户,因此重新填充所有用户的以太币对公司而言成本高昂。因此,假设一家公司决定通过以太坊网络发行一种代币,而该代币代表一种新货币。每次用户向其他人发送代币时,代币的原始
我想知道提交智能合约调用的确切效果。例如,如果我在 uniswap 上将 USDC 兑换成 ETH。 对于交易,我们有 eth_call,对于合约有类似的东西吗?这需要适用于任意复杂的交互,基本上是我
如果我有这样的功能: function sendToAuthor (uint tokenId) public payable{ //here I want to get some ether fr
我正在试验以太坊。我已经按照网站上的说明成功设置了一个私有(private)测试网。但是,我无法添加来自不同机器的对等点。在我创建的任何节点上,admin.nodeInfo.NodeUrl 参数都是未
我在 Remix 上做了一笔交易,花费了 199093 gas。在撰写本文时,天然气价格为 7.2 Gwei(0.031 美元)from here ethgas station 如果我想计算我应该为这
根据 web3.eth.sendTransaction 的文档和 eth_sendTransaction 的文档: 交易对象可以包含一个可选的data参数,它应该是一个String,包含: 要么是包含
是否有任何方法可以在 Solidity 中实现一个集合,以检查元素是否存在,平均 O(1)?我一直在考虑使用没有值的映射对象,这是否比使用数组并迭代以查找元素更快? 最佳答案 是的,就速度而言,您最好
在OpenZeppelin ERC20实现中,有一个_transfer方法: function _transfer( address sender, address recipient
我是一名优秀的程序员,十分优秀!