I am trying to call buy function which is a payable function using metamask signer method. I am getting error for sending incorrect ether value. Here I have set the price as 1 which is 0x01 in hex value. Here is the solidity code for buy function:
function buy(uint256 _tokenId) external payable {
require(msg.value == 1, 'Incorrect value');
_transfer(ownerOf(_tokenId), msg.sender, _tokenId);
payable(ownerOf(_tokenId)).transfer(msg.value); // send the ETH to the seller
}
Now I want to call this function using metamask. Here is the code:
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contract = new ethers.Contract(contractAddress, abi, signer);
async function buyNFT()
{
let params =[{
from:"Address of the buyer",
to: "Address of token owner",
gas: '0x76c0',
gasPrice: '0x9184e72a000',
value: '0x01',
data: await contract.buy(0)
}]
ethereum.request({ method: 'eth_sendTransaction', params,}).then((result) => {
console.log("This is the result : ",result)
}).catch((error) => {console.log("This is the error message:", error)});
But I am getting this error stating that- message: 'execution reverted: Incorrect value