MetaMask is asking for more MATIC

I want to request and send payment via MetaMask.
I am using the Polygon Amoy Network.
My request function is working all right, but when I try to send MATIC to that request MetaMask prompt asks for a lot more MATIC

const handlePay = async () => { 
    try {
      const payableAmount = web3.utils.toWei(requests["1"][0].toString(), "ether"); // Convert to Wei
      console.log("Payable Amount in Wei:", payableAmount);
  
      const contract = new web3.eth.Contract(ABI, contractAddress);

      const transactionObject = contract.methods.payRequest(0); // To pay the first request
      console.log("Transaction Object:", transactionObject);
  
      const data = transactionObject.encodeABI(); // Encode ABI data
      console.log("Encoded ABI Data:", data);

      // Estimate gas
      const gas = await transactionObject.estimateGas({ from: address, value: payableAmount });
      console.log("Estimated Gas:", gas);
  
      // Set the gas price
      const gasPrice = await web3.eth.getGasPrice();
      console.log("Gas Price:", gasPrice);
  
      // Convert gas price and gas limit to hexadecimal strings
      const gasPriceHex = web3.utils.toHex(gasPrice);
      const gasLimitHex = web3.utils.toHex(gas);
  
      // Send transaction
      const transactionParam = {
        from: address, // Your sender address
        to: contractAddress,
        data: data,
        value: payableAmount,
        gasPrice: gasPriceHex, // Set the gas price
        gas: gasLimitHex, // Set the gas limit
      };

      console.log("Transaction Parameters:", transactionParam);
  
      // Send transaction using MetaMask
      const result = await window.ethereum.request({
        method: "eth_sendTransaction",
        params: [transactionParam], 
      });
      console.log("Transaction Result:", result);
  
      // Ensure that the transaction is confirmed before proceeding
      const transactionReceipt = await web3.eth.getTransactionReceipt(result);
      console.log("Transaction Receipt:", transactionReceipt);
  
      // Hide modal and update data
      setPayModal(false);
      fetchData();
    } catch (error) {
      console.error("Error:", error);
    }
  };

For example, I made a request of 2 MATIC to which I am trying to send the transaction.
So this is what I am getting in the console.


which is exactly what I want to send.

But MetaMask is asking for this much
image

Also this is my payRequest function in the smart contract

function payRequest (uint256 _request) public payable { // since we had a requests mapping we need to know which request in the array (index of the request) do we have to pay


        require(_request < requests[msg.sender].length, "No, Such Request"); // the require statement checks the statement and if it passes it continues with the rest of the function, but if it doesn't the fucntion is reverted with the following message. We are checking if the request is actually in the request array for the message sender.

        request[] storage myRequests = requests[msg.sender]; // creating a request array as myRequests to get the requests from the requests mapping.

        request storage payableRequest = myRequests[_request]; //then for the specific request we want, we use the request struct as payableReq., then we look into myRequest array and call the index that we are trying to pay. 

        uint256 toPay = payableRequest.amount * 1000000000000000000; // to get the actual payable amount we get the payableReq.amount and multiply it with the decimal (1 and 18 0's) which turns the weight amount into actual native currency Matic

        require(msg.value == (toPay), "Pay Correct Amount"); // another 'require' function denoting the currency you're sending with that transaction has to be the same as the toPay var  

        payable(payableRequest.requestor).transfer(msg.value); // it takes the requestor from our payable request struct  

        addHistory(msg.sender, payableRequest.requestor, payableRequest.amount, payableRequest.message );

        myRequests[_request] = myRequests[myRequests.length-1]; // get the above request we paid
        myRequests.pop(); // delete the above request


    }

Any help would be much appreciated.

Hi this is strange. Could you tell me about this line: uint256 toPay = payableRequest.amount * 1000000000000000000; why this number?

Where are you getting estimateGas are you using MetaMask API?

// Estimate gas
      const gas = await transactionObject.estimateGas({ from: address, value: payableAmount });
      console.log("Estimated Gas:", gas);

Hi, thanks for replying.

To answer your first question “Why this number”,
uint256 toPay = payableRequest.amount * 1000000000000000000;

Purpose: Converts the amount specified in the request to Wei (or the smallest unit of Matic). Since the amount is Matic, it multiplies by 10^18 to convert it to Wei.
Polygon Standard: 1 MATIC = 10^18

To answer the second question " Where are you getting estimateGas are you using MetaMask API?"

The estimateGas method is a built-in function in the Web3.js library. It is available on transaction objects that are returned by calling a contract method.

Hi thanks for the details, could you please check this gh issue: Polygon transactions are stuck/transaction underpriced error · Issue #2828 · ethers-io/ethers.js · GitHub where you can find a solution. Could you please review it and let me know if it helps. In addition please take a look at: EIP-1559 Explained: Enhancing Gas Fees on MetaMask

Hey Thanks for the help, but unfortunately I could not find a solution to my issue in any of the links.
And as the image below shows my estimated fee I think there’s no issue with the gas fee, though I’m not sure as I am new to all this (It is my first Dapp).

image

Also, another thing I noticed in the GH issue that you linked was that most of them were using ethers.js instead of web3.js, so should I think about migrating my app from web3.js to ethers.js?

Also if you require any other parts of my code, do tell me.

Thank you for more details, is it possible for you to start a conversation and share the code with the person on the ticket: https://support.metamask.io/ please share if it’s mobile or extension there could be an issue with either one on Matic that I’m not aware of.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.