rpc error with payload and exception thrown in contract code

by vteja711   Last Updated October 09, 2019 06:28 AM

This is the problem I get when trying to run my contract!

This is the problem I get when trying to run my contract!

And then when I go ahead and run it, this pops up

enter image description here

This is my js code:

import React, { Component } from 'react';
import Web3 from 'web3';
import logo from '../logo.png';
import './App.css';
import Navbar from './Navbar';
import Main from './Main';
import Marketplace from '../abis/Marketplace.json';

class App extends Component {

  async componentWillMount()
  {
    await this.loadWeb3()
    await this.loadBlockChainData()
  }


  async loadWeb3()
  {
    if(window.ethereum)
    {
      window.web3 = new Web3(window.ethereum)
      await window.ethereum.enable()
    }
    else if (window.web3)
    {
      window.web3 = new Web3(window.web3.currentProvider)
    }
    else{
      window.alert("Non ethereum browser dude!!!!")
    }
  }

  async loadBlockChainData()
  {
    const web3 = window.web3
    //load Account
    const accounts = await web3.eth.getAccounts()
    this.setState({account: accounts[0]})
    const networkId = await web3.eth.net.getId()
    const networkData = Marketplace.networks[networkId]
    if(networkData) 
    {
      const marketplace = web3.eth.Contract(Marketplace.abi, networkData.address)
      //console.log(marketplace)
      this.setState({marketplace})
      //const productCount = await marketplace.methods.productCount().call()
      const productCount = await marketplace.methods.productCount().call()
      //console.log(productCount.toString())
      this.setState({loading: false})
    } 
    else 
    {
      window.alert('Marketplace contract not deployed to detected network.')
    }
  }



  constructor(props)
  {
    super(props)
    this.state =
    {
      account: '',
      productCount: 0,
      products: [], 
      loading: true
    }
    this.createProduct=this.createProduct.bind(this)
  }

  createProduct(name,price)
  {
    this.setState({loading : true})
    this.state.marketplace.methods.createProduct(name, price).send({from: this.state.account })
    .once('receipt', (receipt) => {
      this.setState({loading: false})
    })
  }

  render() {
    return (
      <div>
        <Navbar account={this.state.account} />
        <div className="Container-fluid mt-5">
          <div className="row">
            <main role="main" className="col-lg-12 d-flex">
              <Main createProduct={this.createProduct}/>
            </main>
          </div>
        </div>
      </div>
    );
  }
}

export default App;

This is my solidity contract:

pragma solidity ^0.5.0;

contract Marketplace {
    string public name;
    uint public productCount = 0;
    mapping(uint => Product) public products;

    struct Product {
        uint id;
        string name;
        uint price;
        address payable owner;
        bool purchased;
    }

    event ProductCreated(
        uint id,
        string name,
        uint price,
        address payable owner,
        bool purchased
    );

    event ProductPurchased(
        uint id,
        string name,
        uint price,
        address payable owner,
        bool purchased
    );

    constructor() public {
        name = "Dapp University Marketplace";
    }

    function createProduct(string memory _name, uint _price) public {
        // Require a valid name
        require(bytes(_name).length > 0);
        // Require a valid price
        require(_price > 0);
        // Increment product count
        productCount ++;
        // Create the product
        products[productCount] = Product(productCount, _name, _price, msg.sender, false);
        // Trigger an event
        emit ProductCreated(productCount, _name, _price, msg.sender, false);
    }

    function purchaseProduct(uint _id) public payable {
        // Fetch the product
        Product memory _product = products[_id];
        // Fetch the owner
        address payable _seller = _product.owner;
        // Make sure the product has a valid id
        require(_product.id > 0 && _product.id <= productCount);
        // Require that there is enough Ether in the transaction
        require(msg.value >= _product.price);
        // Require that the product has not been purchased already
        require(!_product.purchased);
        // Require that the buyer is not the seller
        require(_seller != msg.sender);
        // Transfer ownership to the buyer
        _product.owner = msg.sender;
        // Mark as purchased
        _product.purchased = true;
        // Update the product
        products[_id] = _product;
        // Pay the seller by sending them Ether
        address(_seller).transfer(msg.value);
        // Trigger an event
        emit ProductPurchased(productCount, _product.name, _product.price, msg.sender, true);
    }
}

Note: All my tests are working fine with truffe test enter image description here

This is my chrome console:

enter image description here



Related Questions


Updated May 20, 2019 17:28 PM

Updated February 07, 2019 15:28 PM

Updated May 25, 2018 09:28 AM