2

My problem : I want to show transaction details like block hash , block no and input parameters. but it was showing that metamask does not support callback. our function is not waiting till transaction got mined and it enters into else part. how to solve it?

<!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="../script/main.css">

        <script src="../node_modules/web3/dist/web3.min.js"></script>  
        <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
        <script src="../script/app.js"></script>

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

        <h1>INSTAFER</h1>
        <h3>APPROVE</h3>


        <p id="result"></p>

        <label for="address" class="col-lg-2 control-label">Address to be approved </label>
        <input id="address" type="text">

        <label for="amount" class="col-lg-2 control-label"> Maximum Amount</label>
        <input id="amount" type="number">

       <!-- <label for="name" class="col-lg-2 control-label">Instructor Age</label>
        <input id="age" type="text"> -->

        <button id="approve" style="height:50px;width:200px">Approve now</button>
        <input type="button" style="height:50px;width:200px" id="homebutton" value="Home" onClick="location.href='../index.html'" >


    </div>
<script>
     $("#approve").click(function() {
            console.log($("#address").val());
            console.log($("#amount").val());
            INCTokenC.approve($("#address").val(),$("#amount").val(),function(error, result){
                if(!error)
                    {

                        tx.then(function(res){console.log(res);});
                        console.log(tx);
                        if (tx != null) {
                            while(tx.blockNumber==null){
                                var tx = getTx(result);
                        }
                        var txResult = "TxHash : " + tx.hash +"\n TxBlockNumber : " + tx.blockNumber;
                        $("#result").html(txResult);
                        console.log(result);
                        }
                        else {
                            $("#result").html(' No Transaction Happen');
                        }
                    }
                else
                    console.error(error);
            });
        });
</script>



</body>
</html>

1 Answer 1

0

It is highly recommended to use the Truffle Framework's JavaScript way of calling functions instead of jQuery, although Truffle's PetSShop tutorial shows otherwise.

If you do truffle unbox webpack and check the app/app.js, you will see the advantage in that: The functions use promises, which only resolve, if/when a transaction got mined. As a result of that, you will get a transaction ID you can use to perform web3 functions on to receive the information you desire. But for that it's exactly the other way round: MetaMask forces you to use callbacks, synchronous function calls are not permitted.

E.g.

 YourCountract.deployed().then(function(instance){
    theContract = instance;
    return theContract.yourFunction({from: yourAccount})
    }).then(function(theTransactionID){
// you get here only if the transaction was successful, but you still have to check for it's status - was the function run successfully?
    web3.getTransactionReceipt(theTransactionID, function(error, result){


    [handle error and result here]

    })
     })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.