ethereum - ERC-721 智能合约一次铸造 2 个 NFT
问题描述
我有这个来自 Hash Lip 的 github 的智能合约,据我所知,应该一次铸造 1 个,但每次都铸造 2 个。代码如下:
设置代码:
// SPDX-License-Identifier: GPL-3.0
// Created by HashLips
// The Nerdy Coder Clones
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract TestBoxes is ERC721Enumerable, Ownable {
using Strings for uint256;
string public baseURI;
string public baseExtension = ".json";
uint256 public cost = 0.01 ether;
uint256 public maxSupply = 3; //there should only be 3 minted (I have 3 image files to test)
uint256 public maxMintAmount = 3;
bool public paused = false;
mapping(address => bool) public whitelisted;
然后合约铸币的部分如下。正如您在上面看到的,我将最大值设置为 3,在接下来的部分中,构造函数执行后,它会为所有者铸造 1 个 NFT。
constructor(
string memory _name,
string memory _symbol,
string memory _initBaseURI
) ERC721(_name, _symbol) {
setBaseURI(_initBaseURI);
mint(msg.sender, 1); //should mint 1 at deployment but mints 2...
}
// internal
function _baseURI() internal view virtual override returns (string memory) {
return baseURI;
}
// public
function mint(address _to, uint256 _mintAmount) public payable {
uint256 supply = totalSupply();
require(!paused);
require(_mintAmount > 0);
require(_mintAmount <= maxMintAmount);
require(supply + _mintAmount <= maxSupply);
if (msg.sender != owner()) {
if(whitelisted[msg.sender] != true) {
require(msg.value >= cost * _mintAmount);
}
}
for (uint256 i = 0; i <= _mintAmount; i++) { //start the index at 0
_safeMint(_to, supply + i);
}
}
解决方案
您在函数for
内的循环中有逻辑错误。mint()
for (uint256 i = 0; i <= _mintAmount; i++)
示例:_mintAmount
is 1
(与从构造函数传递相同)。
第一次迭代:
i
是0
,小于或等于 1=> 执行迭代并执行
_safeMint()
第二次迭代:
i
是1
,小于或等于 1=> 它仍然执行迭代并执行
_safeMint()
i < _mintAmoun
通过将条件更改为( i 小于)来修复它。然后它只会执行一次(对于mintAmount
值 1)。
推荐阅读
- angular8 - 如果在角度 8 的 kendo-treeview 中没有子级,如何隐藏该行?
- javascript - React-为什么heroku无法识别我的“模型”文件夹?
- java - 无法连接到 Docker postgres 容器 - Scala
- go - 如何使用 go template 和 cassandraDB 在前端处理大量数据?
- javascript - CocoaPods 找不到 pod "ReactCommon/jscallinvoker" 的兼容版本
- python - Pandas 使用选定的值作为索引进行旋转
- java - 可选的泛型类型参数
- python - 带有弹出窗口的破折号数据表保存到 excel 或 csv
- python - Python:是否有仅计算日期的解决方案(pivot 或 groupby)
- amp-html - 加载后立即取消静音 amp-story 视频,以确保默认启用声音