0

I have the following contract in Solidity:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Strings.sol";

contract MyContract is ERC1155, Ownable {
    string public name;
    string public symbol;
    uint256 public mintPrice;
    uint256 public totalSupply;
    uint256 public maxSupply;
    address public withdrawWallet;

    constructor(string memory _name, string memory _symbol) payable ERC1155("https://ipfs.io/ipfs/QmQq3kLMG8fkikYqjrBST2inxTk9sYpcAn274e14sdfgj/") {
        name = _name;
        symbol = _symbol;
        mintPrice = 0.002 ether;
        totalSupply = 0;
        maxSupply = 10000;
        withdrawWallet = msg.sender;
    }

    function mintOwner(uint256 _amount) public onlyOwner {
        require(totalSupply + _amount <= maxSupply, 'Sold out');
        _mint(msg.sender, 0, _amount, "");
        totalSupply = totalSupply + _amount;
    }

    function mint(uint256 _amount) public payable {
        require(msg.value == _amount * mintPrice, 'wrong mint value');
        require(totalSupply + _amount <= maxSupply, 'Sold out');
        _mint(msg.sender, 0, _amount, "");
        totalSupply = totalSupply + _amount;
    }
    
    function setURI(string memory newuri) public onlyOwner {
        _setURI(newuri);
    }

    function withdraw() external onlyOwner {
        (bool success,) = withdrawWallet.call{value : address(this).balance}('');
        require(success, 'withdraw failed');
    }
}

It works fine and I can deploy it until I add the mint() function with price requirement:

function mint(uint256 _amount) public payable {
        require(msg.value == _amount * mintPrice, 'wrong mint value');
        require(totalSupply + _amount <= maxSupply, 'Sold out');
        _mint(msg.sender, 0, _amount, "");
        totalSupply = totalSupply + _amount;
    }

After that I get the following error message:

Error: cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ] (reason="execution reverted: wrong mint value", method="estimateGas", transaction={"from":"0x0aaa289E4DBeecD5E59856d67C775202932Bb411","to":"0xB9C2....fF","data":"0xa0...2710","accessList":null}, error={"name":"ProviderError","code":3,"_isProviderError":true,"data":"0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001077726f6e67206d696e742076616c756500000000000000000000000000000000"}, code=UNPREDICTABLE_GAS_LIMIT, version=providers/5.6.6)

Not quite sure why I am getting this error, any help would be greatly appreciated.

Thanks ahead of time.

Alex K
  • 11
  • 1
  • 1

3 Answers3

0
require(msg.value == _amount * mintPrice, 'wrong mint value');

This condition in the mint() function is not met, which effectively fails to estimate gas needed to run the transaction because the transaction would revert.

Solution: Pass a correct amount of ETH (value param of the transaction) that will pass the condition.

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
0

I was having same issue yesterday, what you can do is add a gas limit manually to the transection, in your case when calling the mint function in the hardhat script, you can add manual gasLimit to it, for example.

YourInstanceName.mint(10, { gasLimit: 1 * 10 ** 6 })

btw I found another similar stackoverflow question link you can check it out.

Muhammad Hassan
  • 424
  • 3
  • 5
0

Same problem.

I solved it in this way.

1)) Create a new directory totally empty: /blabla/createsmartaccount_dir.

2)) Create these 2 files inside it.

/blabla/createsmartaccount_dir/index.js

/blabla/createsmartaccount_dir/package.json

3)) Run the commands

npm install

node ./index.js

##################################

#/blabla/createsmartaccount_dir/index.js

const { ethers } = require( 'ethers')
const { EthersAdapter } = require(  '@safe-global/protocol-kit')
const Safe = require(  '@safe-global/protocol-kit')
const { SafeFactory } = require(  '@safe-global/protocol-kit')
const { SafeAccountConfig } = require(  '@safe-global/protocol-kit')
const { ContractNetworksConfig } = require(  '@safe-global/protocol-kit')

const execute = async () => {
    const RPC_URL='https://rpc2.sepolia.org'
    const provider = new ethers.providers.JsonRpcProvider(RPC_URL)
    
    
    
    // Initialize signers
    //metamask wallet and privatekey
    //0x967xxxxxxxxxxxxxxxxxxxxxxxxxxxx
    const signerWallet = 
new ethers.Wallet("ca46xxxxxxxxxxxxxxx", provider)

    const ethAdapter = new EthersAdapter({ethers, signerOrProvider: signerWallet})
    
    const chainId = await ethAdapter.getChainId()
    console.log(`ChainId: ${chainId}`)

    
  
    
    const safeVersion = '1.3.0'
    const isL1SafeMasterCopy = false
    const safeFactory = await SafeFactory.create({ ethAdapter: ethAdapter })



    const safeAccountConfig = {
        threshold: 1, // Have to be >= 1 && <= totalOwners
        owners: ["0x967xxxxxxxxxxxxxxxxxxxxxxxxxxxx"],
        eth_estimateGas:30000,
    }
    console.log("Start Deploying Safe");



    
    
  
    const safeSdkOwner1 = await safeFactory.deploySafe({ safeAccountConfig })

    const safeAddress = safeSdkOwner1.getAddress()

    console.log(`Safe deployed at: ${safeAddress}`)  
}
execute();

##################################

#/blabla/createsmartaccount_dir/package.json

{
  "dependencies": {
    "@safe-global/api-kit": "^1.0.1",
    "@safe-global/protocol-kit": "^0.1.1",
    "@safe-global/safe-core-sdk-types": "^1.10.1",
    "dotenv": "^16.0.3",
    "ethers": "5.7.2"
  }
}

https://ethereum.stackexchange.com/questions/150579/periodic-failure-when-attempting-to-create-gnosis-safe-via-safe-global-protocol/150771#150771

quine9997
  • 685
  • 7
  • 13