[英]ERC-721 Smart contract is minting 2 NFTs at a time
我有這個來自 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);
}
}
您在mint()
function 中的for
循環中存在邏輯錯誤。
for (uint256 i = 0; i <= _mintAmount; i++)
示例: _mintAmount
為1
(與從構造函數傳遞的相同)。
第一次迭代:
i
是0
,小於或等於 1
=> 執行迭代並執行_safeMint()
第二次迭代:
i
是1
,小於或等於 1
=> 它仍然執行迭代並執行_safeMint()
通過將條件更改為i < _mintAmoun
( i 小於) 來修復它。 然后它只會執行一次(對於mintAmount
值 1)。
for (uint256 i = 1; i <= _mintAmount; i++)
上述方式的工作方式相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.