简体   繁体   English

使用Web3和Go-ethereum在私有以太坊区块链上进行交易时未捕获(承诺)错误

[英]Uncaught (in promise) Error while opening and doing transaction on private ethereum blockchain using web3 and go-ethereum

I am trying to integrate a react app with go-ethereum using web3js. 我正在尝试使用web3js将React应用程序与以太坊集成。

an event Onsubmit will do the following: 1. Create an account. 事件Onsubmit将执行以下操作:1.创建一个帐户。 2. Open the account. 2.打开帐户。 3. Approve a smart contract with the created account. 3.使用创建的帐户批准智能合约。

Here is my code 这是我的代码

import React, { Component } from 'react';
import './App.css';
import web3 from './web3';

.... ....

class App extends Component {

  constructor(props){
    super(props);

    this.state = {requester : '', receiver:'', balance: '', message:''};
  }

 async  componentDidMount(){
    const requester = await auth.methods.requester().call();
    const receiver = await auth.methods.receiver().call();
    const approvers = await auth.methods.approvers(0).call();
    const balance = await web3.eth.getBalance(auth.options.address);

    this.setState({requester,receiver,balance});

  }

  onSubmit = async (event)=>{
      event.preventDefault();
      console.log('Approving the smart contract ..... Mining in process ! ');

      var pass = "xxxxxxx"

      var newaccount = web3.eth.personal.newAccount(pass);

      var promise1 = Promise.resolve(newaccount);


      promise1.then(function(value) {
        var accountnumber = value;
        console.log(accountnumber);
        web3.eth.personal.unlockAccount(accountnumber,pass, 1500);    
        auth.methods.approve().send({gas: '1000000',from: accountnumber});

        console.log('Smart Contract approved ! ');


            });
  };

The account is getting created but while doing the transaction I am receiving the following error. 正在创建帐户,但是在进行交易时,我收到以下错误。

Approving the smart contract ..... Mining in process ! 批准智能合约.....正在开采! App.js:57 App.js:57

0x98f76b2673d545F55c0ff1e961f15EF0a7DfBaD3 0x98f76b2673d545F55c0ff1e961f15EF0a7DfBaD3

App.js:71 Smart Contract App.js:71个智能合约

approved ! 批准! errors.js:29 Uncaught (in promise) Error: Returned error: authentication needed: password or unlock at Object.ErrorResponse (errors.js:29) at index.js:125 at XMLHttpRequest.request.onreadystatechange (index.js:103) at XMLHttpRequestEventTarget.dispatchEvent (xml-http-request-event-target.js:50) at XMLHttpRequest._setReadyState (xml-http-request.js:288) at XMLHttpRequest._onHttpResponseEnd (xml-http-request.js:459) at push../node_modules/stream-http/lib/response.js.exports.IncomingMessage. errors.js:29未捕获(承诺)错误:返回错误:需要身份验证:密码或在XMLHttpRequest.request.onreadystatechange(index.js:103)的index.js:125在Object.ErrorResponse(errors.js:29)解锁)XMLHttpRequest._onHttpResponseEnd(xml-http-request.js:459)的XMLHttpRequest._setReadyState(xml-http-request.js:288)的XMLHttpRequestEventTarget.dispatchEvent(xml-http-request-event-target.js:50)在push ../ node_modules / stream-http / lib / response.js.exports.IncomingMessage中。 (xml-http-request.js:413) at push../node_modules/stream-http/lib/response.js.exports.IncomingMessage.emit (events.js:139) at endReadableNT (_stream_readable.js:1030) at afterTickTwo (index.js:31) at Item.push../node_modules/process/browser.js.Item.run (browser.js:167) at drainQueue (browser.js:131) ErrorResponse @ errors.js:29 (anonymous) @ index.js:125 request.onreadystatechange @ index.js:103 XMLHttpRequestEventTarget.dispatchEvent @ xml-http-request-event-target.js:50 XMLHttpRequest._setReadyState @ xml-http-request.js:288 XMLHttpRequest._onHttpResponseEnd @ xml-http-request.js:459 (anonymous) @ xml-http-request.js:413 emit @ events.js:139 endReadableNT @ _stream_readable.js:1030 afterTickTwo @ index.js:31 push../node_modules/process/browser.js.Item.run @ browser.js:167 drainQueue @ browser.js:131 setTimeout (async) _fireError @ index.js:72 sendTxCallback @ index.js:465 (anonymous) @ index.js:125 request.onreadystatechange @ index.js:103 XMLHttpRequestEventTarget.dispatchEvent @ xml (xml-http-request.js:413),位于push ../ node_modules / stream-http / lib / response.js.exports.IncomingMessage.emit(events.js:139),位于endReadableNT(_stream_read.js:1030), afterTickTwo(index.js:31)位于Item.push ../ node_modules / process / browser.js.Item.run(browser.js:167)在排水队列(browser.js:131)ErrorResponse @ errors.js:29(匿名)@ index.js:125 request.onreadystatechange @ index.js:103 XMLHttpRequestEventTarget.dispatchEvent @ xml-http-request-event-target.js:50 XMLHttpRequest._setReadyState @ xml-http-request.js:288 XMLHttpRequest._onHttpResponseEnd @ xml-http-request.js:459(匿名)@ xml-http-request.js:413发出@ events.js:139 endReadableNT @ _stream_visible.js:1030 afterTickTwo @ index.js:31 push ../ node_modules /进程/浏览器.js.Item.run @浏览器.js:167排水队列@浏览器.js:131 setTimeout(异步)_fireError @ index.js:72 sendTxCallback @ index.js:465(匿名)@ index.js:125请求.onreadystatechange @ index.js:103 XMLHttpRequestEventTarget.dispatchEvent @ xml -http-request-event-target.js:50 XMLHttpRequest._setReadyState @ xml-http-request.js:288 XMLHttpRequest._onHttpResponseEnd @ xml-http-request.js:459 (anonymous) @ xml-http-request.js:413 emit @ events.js:139 endReadableNT @ _stream_readable.js:1030 afterTickTwo @ index.js:31 push../node_modules/process/browser.js.Item.run @ browser.js:167 drainQueue @ browser.js:131 setTimeout (async) runTimeout @ browser.js:43 push../node_modules/process/browser.js.process.nextTick @ browser.js:156 nextTick @ index.js:30 maybeReadMore @ _stream_readable.js:521 addChunk @ _stream_readable.js:300 readableAddChunk @ _stream_readable.js:278 push../node_modules/readable-stream/lib/_stream_readable.js.Readable.push @ _stream_readable.js:242 (anonymous) @ response.js:47 write @ response.js:44 -http-request-event-target.js:50 XMLHttpRequest._setReadyState @ xml-http-request.js:288 XMLHttpRequest._onHttpResponseEnd @ xml-http-request.js:459(匿名)@ xml-http-request.js: 413发出@ events.js:139 endReadableNT @ _stream_read.js:1030 afterTickTwo @ index.js:31 push ../ node_modules / process / browser.js.Item.run @ browser.js:167排水队列@ browser.js:131 setTimeout(异步)runTimeout @ browser.js:43 push ../ node_modules / process / browser.js.process.nextTick @ browser.js:156 nextTick @ index.js:30也许ReadMore @ _stream_visible.js:521 addChunk @ _stream_可读。 js:300可读AddChunk @ _stream_read.js:278 push ../ node_modules / ready-stream / lib / _stream_visible.js.Readable.push @ _stream_visible.js:242(匿名)@ response.js:47写@ response.js: 44

Edit: Changed the code to catch the errors 编辑:更改代码以捕获错误

web3.eth.personal.unlockAccount(accountnumber,pass, 1500, function(err, result){
          if(err){
            alert("Error"+ err);
            return;}
          alert("Account Opening: "+ result);});

..... .....

auth.methods.approve().send({gas: '1000000',from: accountnumber}, function(err, result){
      if(err){
        alert("Error"+ err);
        return;}
      alert("Account address: "+ result);
      console.log('Smart Contract approved ! ');});

The web3.eth.personal.unlockAccount is returning "true" but the still the auth.methods.approve is giving me the error. web3.eth.personal.unlockAccount返回“ true”,但是auth.methods.approve仍然给我错误。

So, after some major changes to the code, I am able to do the following from an onSubmit event on a react app. 因此,在对代码进行了一些重大更改之后,我能够通过react应用程序上的onSubmit事件执行以下操作。

  1. Create an account. 创建一个帐户。
  2. Transfer some gas to it. 向其中转移一些气体。
  3. Unlock the account. 解锁帐户。
  4. Sign a contract with the account. 与该帐户签订合同。

Here is the code 这是代码

onSubmit = async (event)=>{
      event.preventDefault();
      console.log('Approving the smart contract ..... Mining in process ! ');

      var pass = "passsword1"

      var newaccount = web3.eth.personal.newAccount(pass);

      var promise1 = Promise.resolve(newaccount);


      promise1.then(function(value) {
        var accountnumber = value;
        console.log(accountnumber);
        web3.eth.personal.unlockAccount('0x197022acd263e8be0f6b65b10d1e5cdbaa244c17',"*****", 1500, function(err, result){
          if(err){
            alert("Error"+ err);
            return;
          }else {
          alert("Parent Opening: "+ result);
          web3.eth.sendTransaction({
          from: "0x197022acd263e8be0f6b65b10d1e5cdbaa244c17",
          to: accountnumber, 
          value: '100000000000000000', 
          }, function(err, transactionHash) {
          if (err) { 
              console.log(err); 
          } else {
            web3.eth.personal.unlockAccount(accountnumber,pass, 1500, function(err, result){
              if(err){
                alert("Error"+ err);
                return;
              }else{
                console.log(web3.eth.getBalance(accountnumber));
                alert("Child Opening: "+ result);
                auth.methods.approve().send({gas: '20000000',from: accountnumber}, function(err, result){
                if(err){
                  alert("Error"+ err);
                  return;
                }else{
                  console.log("Account address: "+ result);
                  console.log('Smart Contract approved ! ');

                }
                });
              }
            });
          }
          });
       }

      });
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MetaMask Web3 以太坊未定义 - MetaMask Web3 ethereum not defined Web3 元掩码连接无法读取未定义的属性(读取“以太坊”) - Web3 metamask connection Cannot read properties of undefined (reading 'ethereum') 未捕获(承诺)错误:checkIfTransactionsExists 处没有以太坊 object (TransactionContext.jsx:105:13) - Uncaught (in promise) Error: No ethereum object at checkIfTransactionsExists (TransactionContext.jsx:105:13) 错误:在调用 Multicall Contract ethereum 的聚合 function 时发送交易需要签名者 - Error: sending a transaction requires a signer while ccalling aggregate function of Multicall Contract ethereum 从Chrome扩展执行Web3事务时出错 - Getting error while performing web3 transaction from chrome extention 如何在以太坊交易中使用 useState 设置交易哈希 - How to set the transaction hash with useState in an Ethereum transaction 我已经下载了在 react、solidity、truffle 中构建的以太坊区块链项目 - I have downloaded the ethereum blockchain project build in react,solidity,truffle 在 React 中导入 web3 时无法修复错误 - I can't fix error while importing web3 in React 当我将数据提交到数据库表时,我在浏览器控制台中遇到此错误:未捕获(承诺)错误:请求失败,状态代码为 404 - While I go to submit data into Database table I face this error in browser console: Uncaught (in promise) Error: Request failed with status code 404 错误:未捕获类型错误:无法解析模块说明符“web3”。 相对引用必须以“/”、“./”或“../”开头 - Error: Uncaught TypeError: Failed to resolve module specifier "web3". Relative references must start with either "/", "./", or "../"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM