异步/等待+ Promisify(JavaScript,Web3,以太坊)

时间:2018-07-12 20:32:25

标签: javascript async-await ethereum web3

很抱歉,如果这是一个业余问题,或者背叛了缺乏的了解,但是我一直在为我的项目开发前端,但是遇到了一个问题。我一直在使用Async / Await来获取我的(HTML + Javascript)前端,以等待我对Contract的回调完成,以便它们可以与我的前端的各个部分一起使用。但是,在我的最新页面中,我的代码“挂起”一直存在问题,实际上并没有返回结果。完整的代码如下,突出显示了最相关的部分:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>

        <link rel="stylesheet" type="text/css" href="main.css">

        <script src="./node_modules/web3/dist/web3.min.js"></script>

    </head>
    <body>
        <div class="container">

        <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
        <label for="contractManager">Contract Manager</label>
        <select id="Contracts">
        </select>
        <div class="container">
        <label for="codeInput" class="col-lg-2 control-label">Enter Your Address (For Now, only working with Test Contract - in future, will be able to enter the Contract's ID too)</label>
        <input id="codeInput">
        <button id="checkContract">Prove that you own this contract</button>
        <div id="hidden">

        </div>

    <script>
    const promisify = (inner) =>
        new Promise((resolve, reject) =>
            inner((err, res) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(res);
                }
            })
        );
    //so, this promise ought to return err if the async call returned an error, return res otherwise
         if (typeof web3 !== 'undefined') {
                web3 = new Web3(web3.currentProvider);
                console.log("Metamask logged into successfully");
            } else {
                console.log("Metamask NOT logged into successfully");
                // set the provider you want from Web3.providers
                web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/rxskoaj8487I8J426Zid"));
                //note that I don't intend for this part to actually be used - if you're not logged into Metamask, things are pretty bad and we shouldn't go ahead. This is merely to prevent crashing.
            }   

            //now we need the ABI for RegionalContract2
                     web3.eth.defaultAccount = web3.eth.accounts[0];
          var RegionalContract2 = web3.eth.contract(
        [
        {
            "constant": false,
            "inputs": [
                {
                    "name": "desc",
                    "type": "string"
                }
            ],
            "name": "addProposal",
            "outputs": [],
            "payable": false,
            "stateMutability": "nonpayable",
            "type": "function"
        },
        {
            "constant": false,
            "inputs": [
                {
                    "name": "voterID",
                    "type": "address"
                },
                {
                    "name": "proposal",
                    "type": "string"
                }
            ],
            "name": "castVote",
            "outputs": [],
            "payable": false,
            "stateMutability": "nonpayable",
            "type": "function"
        },
        {
            "constant": false,
            "inputs": [
                {
                    "name": "region",
                    "type": "string"
                },
                {
                    "name": "contractAddress",
                    "type": "address"
                }
            ],
            "name": "getListFromMain",
            "outputs": [],
            "payable": false,
            "stateMutability": "nonpayable",
            "type": "function"
        },
        {
            "constant": false,
            "inputs": [],
            "name": "setOver",
            "outputs": [
                {
                    "name": "",
                    "type": "string"
                }
            ],
            "payable": false,
            "stateMutability": "nonpayable",
            "type": "function"
        },
        {
            "inputs": [],
            "payable": false,
            "stateMutability": "nonpayable",
            "type": "constructor"
        },
        {
            "constant": true,
            "inputs": [
                {
                    "name": "id",
                    "type": "address"
                }
            ],
            "name": "areYouInList",
            "outputs": [
                {
                    "name": "",
                    "type": "bool"
                }
            ],
            "payable": false,
            "stateMutability": "view",
            "type": "function"
        },
        {
            "constant": true,
            "inputs": [],
            "name": "getFounder",
            "outputs": [
                {
                    "name": "",
                    "type": "address"
                }
            ],
            "payable": false,
            "stateMutability": "view",
            "type": "function"
        },
        {
            "constant": true,
            "inputs": [
                {
                    "name": "key",
                    "type": "uint8"
                }
            ],
            "name": "getProposalName",
            "outputs": [
                {
                    "name": "",
                    "type": "string"
                }
            ],
            "payable": false,
            "stateMutability": "view",
            "type": "function"
        },
        {
            "constant": true,
            "inputs": [
                {
                    "name": "key",
                    "type": "uint8"
                }
            ],
            "name": "getProposalVotes",
            "outputs": [
                {
                    "name": "",
                    "type": "uint256"
                }
            ],
            "payable": false,
            "stateMutability": "view",
            "type": "function"
        },
        {
            "constant": true,
            "inputs": [
                {
                    "name": "id",
                    "type": "address"
                }
            ],
            "name": "haveYouVotedAlready",
            "outputs": [
                {
                    "name": "",
                    "type": "bool"
                }
            ],
            "payable": false,
            "stateMutability": "view",
            "type": "function"
        },
        {
            "constant": true,
            "inputs": [],
            "name": "howManyProposals",
            "outputs": [
                {
                    "name": "",
                    "type": "uint256"
                }
            ],
            "payable": false,
            "stateMutability": "view",
            "type": "function"
        },
        {
            "constant": true,
            "inputs": [],
            "name": "isItOver",
            "outputs": [
                {
                    "name": "",
                    "type": "bool"
                }
            ],
            "payable": false,
            "stateMutability": "view",
            "type": "function"
        },
        {
            "constant": true,
            "inputs": [],
            "name": "showWhiteList",
            "outputs": [
                {
                    "name": "",
                    "type": "address[]"
                }
            ],
            "payable": false,
            "stateMutability": "view",
            "type": "function"
        },
        {
            "constant": true,
            "inputs": [],
            "name": "tallyVotes",
            "outputs": [
                {
                    "name": "",
                    "type": "string"
                }
            ],
            "payable": false,
            "stateMutability": "view",
            "type": "function"
        }
    ]);

    var yourAddress;
    var isThisContractYours;
    var whoIsFounder;

    try{
    var RegionalContract2 = RegionalContract2.at("0x2C7a8bACc5757d63669baE4a5457F6aE8F6d49ef", function (error, res){
    if(!error) {
    console.log("The Contract is " + RegionalContract2)
    console.log("Everything's ship shape! Contract instantiated asynchronously!")}
    });
    }catch (err) {
    console.log(err);
    }

    $("#checkContract").click(function() {

  ***Relevant section 1***

    async function getFounder() { //use try/catch to avoid error
        console.log("getFounder method");
            try{
             whoIsFounder = await promisify(RegionalContract2.getFounder);
            }catch(err) {
            console.log(err)
            }

                console.log("This Election's Founder is : " + whoIsFounder);

            return whoIsFounder;


        }

     ***Relevant Section 2***

    async function isItYours(inputAddress){
       try{
        console.log("Inside the isItYours method");
        founder = await promisify(getFounder);//according to someone online, need to pass a function to promisify, not the result of a function, so using '.getFounder' instead of '.getFounder()'
            console.log("founder is " + founder);//also, I 
          if (founder != null){
                if (founder === inputAddress) {//if the Founder is the same as the value entered
                    //for now we'll stick with popping up an alert. In final version, should un-hide a piece of the screen
                alert("You are in fact the owner of this Election.");
                }
                else {
                alert("You are NOT the owner of this Election.");
                }
          }
         }catch (err) {
            console.log(err)
         }
    }
    console.log("The inputted value is " + codeInput.value);
    isItYours(codeInput.value);

    })
    </script>

再次,对于这是一个显而易见的新手问题,我深表歉意,但是在我之前的页面中使用了与您在上面看到的代码非常相似的代码,并且在那里工作正常(并且仍然可以正常工作),所以我我不确定问题究竟来自何方。

谢谢。

0 个答案:

没有答案