简体   繁体   English

Promise.all完成并出现错误错误:Solidity函数(元掩码)的参数数目无效

[英]Promise.all finished with error Error: Invalid number of arguments to Solidity function (Metamask)

I'm currently working on a project that uses Javascript, Async/Await, and Web3 (for Ethereum). 我目前正在使用Javascript,Async / Await和Web3(以太坊)的项目中工作。 I'm looking to create a frontend page that is capable of detecting a user's Metamask address, then using that within a function. 我希望创建一个能够检测用户的Metamask地址,然后在函数中使用该地址的前端页面。 I've already gotten something like this to work in a previous page, but I've run into trouble translating it onto another page. 我已经在上一页中获得了类似的功能,但是在将其转换到另一页时遇到了麻烦。 I suspect that getCurrentAccount() isn't returning anything, thus messing up the number and type of variables that Promise.all expects to have sent in. The relevant code is: 我怀疑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)
    }
}

The problem come from the way you get the metamask account : web3.eth.getAccounts is an asynchronous function. 问题出在您获取元掩码帐户的方式上: web3.eth.getAccounts是一个异步函数。 Something like that should works : 这样的事情应该工作:

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM