[英]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事件执行以下操作。
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.