1

I'm trying to execute a payable function in my contract, although the price was fixed at 0.001 ether, Metamask is asking me for more than 2 ethers apart from gas fees. I think the problem is in the value I'm sending in the transaction request, but I'm not sure.

Why Metamask is asking me for an amount of ether other than the price was fixed in my contract?

Contract.sol:

function mintNFTs(uint _count) public payable {
    uint totalMinted = _tokenIdCounter.current();
    
    require(totalMinted.add(_count) <= MAX_SUPPLY, "Not enough NFTs left!");
    require(_count >0 && _count <= MAX_PER_MINT, "Cannot mint specified number of NFTs.");
    require(msg.value >= PRICE.mul(_count), "Not enough ether to purchase NFTs.");
    for (uint i = 0; i < _count; i++) {
        _mintSingleNFT();
        }
    }
        
function _mintSingleNFT() private {
    uint newTokenID = _tokenIdCounter.current();
        _safeMint(msg.sender, newTokenID);
        _tokenIdCounter.increment();
}

App.js

const tx = {
    'from': currentAccount,
    'to': contractAddress,
    'nonce': nonce.toString(),
    'gas': "500000",
    'value': web3.utils.toWei('0.002', 'ether'),
    'data': nftContract.methods.mintNFTs(1).encodeABI(),
};

1 Answer 1

0

I found the answer. You have to send a hex number to the value parameter and not wei, so, you have to convert from decimal to wei to hex. It works for me.

'value': web3.utils.toHex(web3.utils.toWei(nftPrice.toString(), 'ether')),

Source: How to Send Ethereum with Node and Web3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.