Solidity

提供者沒有要使用的請求或發送方法

  • February 28, 2022

我正在做Ethereum and Solidity: The Complete Developer’s Guide,我無法解決這個錯誤。

該程式碼預計將通過

await factory.methods
.createCampaign(this.state.minimumContribution)
.send({
   from: accounts[0]
});

但它的行為不正常並給我以下錯誤:

index.js?fccc903:155 Uncaught (in promise) Error: Provider does not have a request or send method to use.
   at RequestManager.module.exports.RequestManager.send (index.js?fccc903:155)
   at sendRequest (index.js?5b050cb:617)
   at Eth.send [as getAccounts] (index.js?5b050cb:644)
   at CampaignNew._callee$ (new.js?edc8855:17)
   at tryCatch (runtime.js?0e76910:62)
   at Generator.invoke [as _invoke] (runtime.js?0e76910:296)
   at Generator.prototype.<computed> [as next] (runtime.js?0e76910:114)
   at step (asyncToGenerator.js?ae4f0b1:17)
   at asyncToGenerator.js?ae4f0b1:35
   at new Promise (<anonymous>)

我記得程式碼工作正常,幾分鐘前,但現在我不確定為什麼會發生錯誤並且元遮罩視窗沒有出現。請提出您的建議。

new.js程式碼文件是:

import React, { Component } from 'react'
import { Container, Button, Form, Input } from 'semantic-ui-react'
import Layout from '../../components/Layouts'

import factory from '../../eth/factory'
import web3 from '../../eth/web3'

class CampaignNew extends Component {
   state = {
       minimumContribution: ''
   };
   
   onSubmit = async (event)=>{
       event.preventDefault();

       const accounts = await web3.eth.getAccounts();
       await factory.methods
       .createCampaign(this.state.minimumContribution)
       .send({
           from: accounts[0]
       });
   }

   render() {
       return (
           <Container>
               <Layout>
                   <h1>Create a new Campaign page</h1>
                   <Form onSubmit={this.onSubmit}>
                       <Form.Field>
                           <label>Minimum contribution</label>
                           <Input 
                               labelPosition='right' 
                               label='wei' 
                               placeholder='0' 
                               
                               value={this.state.minimumContribution}
                               onChange={event => this.setState({minimumContribution: event.target.value})}
                           />
                       
                       </Form.Field>

                       <Button primary>Create</Button>
                   </Form>
               </Layout>

           </Container>
       );
   }
};

export default CampaignNew;

我寫的發送請求沒有問題。問題出在提供者上,我沒有正確分配。

在我的web3.js文件中,以前是這樣寫的:

web3 = new Web3(window.web3.currentProvider.enable());

我將其更改為:

await window.web3.currentProvider.enable();
web3 = new Web3(window.web3.currentProvider);

你知道我正在 Dapp 大學在 youtube 上從 Intro to Blockchain Programming 中編寫這段程式碼,並且.. 我注意到我收到了這個錯誤。為我解決的問題是我添加了

var Web3 = require('web3')

var web3 = new Web3('<the IP of your blockchain, for me it was the RPC server address in ganache (that I'm using, you could be doing something different)>')

僅適用於像我這樣的新手:<> 中的任何內容都是您必須自己辨識和輸入的內容。

我面臨的問題是提供者是空的,功能也出來了。我注意到包裹裡一定有問題。所以.. 我也做了 web3.js 的教程。我發現了這個上層程式碼。在教程中,我還表演了

npm install web3

它在我的 PC 上安裝了必要的軟體包。如果你還沒有,你也應該嘗試一下,看看會發生什麼

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