Reputation: 11
I’m having some trouble deploying my smart contract on the Berlin VM local host to test it. I’ve followed the presale and presale debug tutorials exactly, and I’ve also tried deploying a smart contract that has worked previously. Both times I get the following error message:
VM error revert. Revert the transaction has been reverted to the initial state. Note: the called function should be payable if you send value and the value you send should be less than your current balance.
I’ve tried it with different values in the deploy section on Remix so I don’t think it’s that there are insufficient funds. I've also tried deploying on the Rinkby test network and I get the error
Gas estimation failed. Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? execution reverted
Can anyone help? My code is:
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
contract NFTNAME is ERC721Enumerable, Ownable {
using Strings for uint256;
string public baseURI;
string public baseExtension = ".json";
uint256 public cost = 77 ether;
uint256 public maxSupply = 6666;
uint256 public maxMintAmount = 20;
uint256 public nftPerAddressLimit = 7;
bool public paused = false;
bool public onlyWhitelisted = true;
address[] public whitelistedAddresses;
mapping(address => uint256) public addressMintedBalance;
constructor(
string memory _name,
string memory _symbol,
string memory _initBaseURI
) ERC721(_name, _symbol) {
setBaseURI(_initBaseURI);
mint(90);
}
// internal
function _baseURI() internal view virtual override returns (string memory) {
return baseURI;
}
// public
function mint(uint256 _mintAmount) public payable {
require(!paused);
uint256 supply = totalSupply();
require(_mintAmount > 0);
require(_mintAmount <= maxMintAmount);
require(supply + _mintAmount <= maxSupply);
if (msg.sender != owner()) {
if(onlyWhitelisted == true) {
require(isWhitelisted(msg.sender), "user is not whitelisted");
uint256 ownerMintedCount = addressMintedBalance[msg.sender];
require(ownerMintedCount + _mintAmount <= nftPerAddressLimit, "max NFT per address exceeded");
}
require(msg.value >= cost * _mintAmount);
}
for (uint256 i = 1; i <= _mintAmount; i++) {
addressMintedBalance[msg.sender]++;
_safeMint(msg.sender, supply + i);
}
}
function isWhitelisted(address _user) public view returns (bool) {
for (uint i = 0; i < whitelistedAddresses.length; i++) {
if (whitelistedAddresses[i] == _user) {
return true;
}
}
return false;
}
function walletOfOwner(address _owner)
public
view
returns (uint256[] memory)
{
uint256 ownerTokenCount = balanceOf(_owner);
uint256[] memory tokenIds = new uint256[](ownerTokenCount);
for (uint256 i; i < ownerTokenCount; i++) {
tokenIds[i] = tokenOfOwnerByIndex(_owner, i);
}
return tokenIds;
}
function tokenURI(uint256 tokenId)
public
view
virtual
override
returns (string memory)
{
require(
_exists(tokenId),
"ERC721Metadata: URI query for nonexistent token"
);
string memory currentBaseURI = _baseURI();
return bytes(currentBaseURI).length > 0
? string(abi.encodePacked(currentBaseURI, tokenId.toString(), baseExtension))
: "";
}
//only owner
function setNftPerAddressLimit(uint256 _limit) public onlyOwner() {
nftPerAddressLimit = _limit;
}
function setCost(uint256 _newCost) public onlyOwner() {
cost = _newCost;
}
function setmaxMintAmount(uint256 _newmaxMintAmount) public onlyOwner() {
maxMintAmount = _newmaxMintAmount;
}
function setBaseURI(string memory _newBaseURI) public onlyOwner {
baseURI = _newBaseURI;
}
function setBaseExtension(string memory _newBaseExtension) public onlyOwner {
baseExtension = _newBaseExtension;
}
function pause(bool _state) public onlyOwner {
paused = _state;
}
function setOnlyWhitelisted(bool _state) public onlyOwner {
onlyWhitelisted = _state;
}
function whitelistUsers(address[] calldata _users) public onlyOwner {
delete whitelistedAddresses;
whitelistedAddresses = _users;
}
function withdraw() public payable onlyOwner {
(bool success, ) = payable(msg.sender).call{value: address(this).balance}("");
require(success);
}
}
Upvotes: 1
Views: 3436
Reputation: 43481
The constructor
calls the mint()
function passing it 90
as the _mintAmount
value.
The mint()
function contains a require()
condition that throws an exception (and effectively reverts the transaction) if the condition is not met:
require(_mintAmount <= maxMintAmount);
When you look at the maxMintAmount
definition, you can see its value is 20
.
So this condition require(90 <= 20)
fails, effectively causing the deployment transaction to revert.
Solution: Either decrease the value passed to the mint()
function from the constructor so that it meets the <= 20
condition, or increase the maxMintAmount
value so it's higher than 90
.
Upvotes: 2