Promise.all已完成,但出现错误错误:Solidity函数(Metamask)的参数数无效



我目前正在进行一个使用Javascript、Async/Await和Web3(用于以太坊(的项目。我希望创建一个前端页面,能够检测用户的Metamask地址,然后在函数中使用它。我已经在前一页中使用了类似的内容,但在将其翻译到另一页时遇到了麻烦。我怀疑getCurrentAccount((没有返回任何内容,从而混淆了Promise.all期望发送的变量的数量和类型

function isInList(input) {
return new Promise(function(resolve, reject) {
myElection.areYouInList(input, function(error, response) {
if (error) {
reject(error);
} else {
resolve(response);
}
})
});
}
function hasntVotedYet(input) {
return new Promise(function(resolve, reject) {
myElection.haveYouVotedAlready(input, function(error, response) {
if (error) {
reject(error);
} else {
resolve(response);
}
})
});
}
function isNotOver() {
return new Promise(function(resolve, reject) {
myElection.isItOver(function(error, response) {
if (error) {
reject(error)
} else {
resolve(response);
}
})
});
}
async function getCurrentAccount() {
console.log("getCurrentAccount method");
web3.eth.getAccounts((err, accounts) => {
currentAccount = accounts[0]
})
return currentAccount; //KEEP IN MIND THAT YOU NEED TO ACTUALLY BE LOGGED INTO METAMASK FOR THIS TO WORK - OTHERWISE IT'LL RETURN UNDEFINED BECAUSE THERE AREN'T ANY ACCOUNTS
}

async function verify() {
console.log("Verify");
try {
-- -- > THIS LINE //
var myCurrentAccount = await getCurrentAccount();
console.log("myCurrentAccount is: " + myCurrentAccount);
data = await Promise.all([isInList(myCurrentAccount), hasntVotedYet(myCurrentAccount), isNotOver()]); //data then equals an array, containing both the strings returned from these two methods
console.log("success");
console.log(data)
if (data[0] && !data[1] && !data[2]) { //check firstly that we are allowed to vote, secondly that we have not already voted, and finally that this vote is not over yet
var x = document.getElementById("hidden");
x.style.display = "block";
} else if (!data[0]) {
alert("That address was not found in the Whitelist.");
} else if (data[1]) {
alert("You have already voted and may not vote a second time");
} else if (data[2]) {
alert("This election has already concluded. Votes can no longer validly be cast for it.");
}
} catch (error) {
console.log("Promise.all finished with error " + error)
}
}

问题来自于获取元任务帐户的方式:web3.eth.getAccounts是一个异步函数。这样的东西应该有效:

async function getCurrentAccount() {
console.log("getCurrentAccount method");
let accounts = await web3.eth.getAccounts();
return accounts[0];
}

最新更新