0

Hi everyone hope I will be clear enough with the explanation because I couldn't find a proper solution. I am just trying to checkout an item with my buy function but it seems like I am stuck.Here is my code.

Here is my contract:

pragma solidity ^0.5.0;

contract Buy {
    address[] public buyers;
    uint256 public minAmount;

    struct Buyer {
    uint256 amountPayed;
    uint256 itemSelected;
    }
    mapping(address => Buyer) public buyerInfo;

    function buy(uint256 itemSelected) public payable returns (uint amount){
    require(itemSelected >= 1 && itemSelected <= 10);
    require(msg.value >= minAmount);

    buyerInfo[msg.sender].amountPayed = msg.value;
    buyerInfo[msg.sender].itemSelected = itemSelected;

    buyers.push(msg.sender);

        return amount;
  }
}

And here is my handleBuy function in React

handleBuy(e){
        e.preventDefault();
        this.state.simpleBuy.setProvider(this.state.web3.currentProvider);
        const account = this.state.account;
        var simpleBuyInstance;

        this.state.simpleBuy.deployed().then((instance) => {
            simpleBuyInstance = instance;

            this.setState(prevState => ({
                ...prevState,
                account,
                simpleBuyInstance
            }));


              return simpleBuyInstance.buy(2, {
                 from: account,
                 gas: 300000,
                 value: this.state.web3.utils.toWei(this.state.value.toString(), 'ether')
             })
        }).then((result) => {
            console.log(result);
        })
    }

this function above was working fine before implementing a payable. I am having an error after the transaction saying : inpage.js:1 MetaMask - RPC Error: Error: Error: [ethjs-rpc] rpc error with payload. Any help would be appreciated.

  • Is this on the main net? – Niklas Feurstein Aug 20 at 12:56
  • what exactly do you mean? sorry I did not understand. – Ekin Alcar Aug 20 at 13:03
  • Do you deploy this on public Ethereum or a testnet like Ropsten? – Niklas Feurstein Aug 20 at 13:06
  • it is actually on localhost port 7545 – Ekin Alcar Aug 20 at 13:08
  • set a gas price for the transaction – Niklas Feurstein Aug 20 at 13:10

Your Answer

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

Browse other questions tagged or ask your own question.