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
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.