0

Getting this error when I execute my smart contract function "purchase" using nodejs, it's a very simple smart contract so i'm stuck as to why i keep getting this error.

Solidity

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/Ownable.sol";

contract ProductPage is Ownable {
    
    event PayDay(uint256 date);
    
    constructor() {}
    
    function purchase() external payable {}
    
    function contractBalance() public view returns(uint256) {
                return address(this).balance;
    }
    
    function withdraw() public onlyOwner {
        if (address(this).balance != 0) {
            (bool success,) = msg.sender.call{value: address(this).balance}("");
            require(success, "Failed to send Ether");
        }
        
        emit PayDay(block.timestamp);
    }
}

React

        const pay = async (e) => {
                try {
                        const tx = contract
                                .methods
                                .purchase()
                                .send({from: accounts[0], value:web3.utils.toWei("1", "ether")})
                                .encodeABI();
                        window.ethereum
                                .request({
                                        method: 'eth_sendTransaction',
                                        params: [
                                                {
                                                        from: accounts[0],
                                                        to: contractAddress,
                                                        data: tx,
                                                }
                                        ]
                                })
                } catch(err) {
                        console.error(err);
                }
                await setOpen(false);
        }

Metamask

enter image description here

0

My fault, forgot to edit the deploy migrations file(i.e 2_deploy_contracts.js)

1
  • Accept your own answer and provide more details if you think would be helpful to any other user Jun 22 at 3:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy