0xD1x0n
0xD1x0n

Reputation: 653

Error encountered during contract execution [stack underflow (0 <=> 2)]

I'm developing an ERC721 contract. Whenever I call my mint function, I get this error:

Error encountered during contract execution [stack underflow (0 <=> 2)]

My function contains nothing but a simple require and return statement.

I then downgraded to Sol 0.6.6 But i keep getting the same issue. Even meta mask shows this message when I'm about to confirm the transaction:

enter image description here

This is my Mint function :

function Mint() notBroke external payable returns (uint256) {  

    require(msg.value >= 0.05 ether, 'You didnt pay enough Eth Buddy :/'); 

    return 1;
}

This is My notBroke Modifier :

   modifier notBroke() {
    require(address(this).balance > 0 ether,"You  Broke");
    _;
}

And this is how I am committing the transaction from the Front end (using meta mask) :

        const transactionParameters = {
              to: Contract.address, 
              from: props.account, // must match user's active address.
              value: parseInt(Web3.utils.toWei("0.05", 'ether')).toString(16), 
              data: web3.eth.abi.encodeFunctionCall(    
                {
                    "inputs": [],
                    "name": "Mint",
                    "outputs": [
                      {
                        "internalType": "uint256",
                        "name": "",
                        "type": "uint256"
                      }
                    ],
                    "stateMutability": "payable",
                    "type": "function"
                  },[]
            ),
              chainId: `0x${parseInt(props.network)}`, // Used to prevent transaction reuse across blockchains. Auto-filled by MetaMask.
            };


            await window.ethereum.request({
              method: 'eth_sendTransaction',
              params: [transactionParameters],
            }).then( (result) => {

                 resolve(result) 

            }).catch((error)=> resolve(error))

And this is whats shown on Etherscan:

enter image description here

Upvotes: 1

Views: 2172

Answers (2)

Petr Hejda
Petr Hejda

Reputation: 43491

The modifier throws an exception (effectively reverting the transaction) with message "You Broke" if the contract address (not the sender address) has 0 balance.


If you want to update the modifier to revert only if the sender address has 0 balance, you need to use msg.sender instead of address(this):

require(msg.sender.balance > 0 ether,"You  Broke");

Upvotes: 2

MuzammalRamzan
MuzammalRamzan

Reputation: 11

Here you have added modifier Which means your contract should have balance greater then 0, so at first time your contract will have 0 ether so it will revert transaction, please try to check balance of your contract and I think so you should check balance user who want to mint token

Upvotes: 1

Related Questions