0

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

1 Answer 1

1

when you call await contract.buy(0), you are passing 0 as the param (which is _tokenId) and not the value.

The first thing it excecute is require(msg.value == 1, 'Incorrect value'); msg.value here is not 1, so it reverts with this message.

to call the function with a value you need to use the overrides param:

 await contract.buy(0, {value: 1});

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.