Web3js

契約程式碼中引發的有效負載和異常的 rpc 錯誤

  • October 10, 2019

這是我在嘗試執行契約時遇到的問題!

這是我在嘗試執行契約時遇到的問題!

然後當我繼續執行它時,會彈出

在此處輸入圖像描述

這是我的 js 程式碼:

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;

這是我的穩固契約:

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);
   }
}

注意:我所有的測試都可以在 truffe 測試中正常工作 在此處輸入圖像描述

這是我的 chrome 控制台:

在此處輸入圖像描述

所以這個問題出現了,因為在將我的帳戶添加到 metamask 之後,我不得不進行遷移,truffle migrate但我沒有這樣做。所以這就是 Metamask 在連接到 Ganache 時遇到問題的原因。

新來的!所以歡迎提出建議!謝謝。

引用自:https://ethereum.stackexchange.com/questions/76571