0

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

  • "Trying to run my contract" - what does that even mean? Trying to deploy your contract? Trying to execute a specific function (if yes, then which one is it)? BTW, you most definitely need to change products[productCount] = Product(...) to products[productCount] = new Product(...). – goodvibration Oct 9 '19 at 5:50
  • And when your tests run successfully under Truffle + Ganache but not elsewhere, it is likely because Ganache automatically unlocks some 10 accounts for you (which are then passed to Truffle as input), while other environments don't. – goodvibration Oct 9 '19 at 5:53
  • The function is createProduct and I have my metamask connected to my local RPC(Ganache). – vteja711 Oct 9 '19 at 5:55
  • Even after adding new it still has the same problem – vteja711 Oct 9 '19 at 5:58
0

So this issue was coming up because after adding my accounts to metamask I had to make migrations with truffle migrate and I did not do that. So that was the reason why Metamask had problems connecting to Ganache.

New here! So suggestions are welcome! Thanks.

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.