2

I am developing a dapp using 'testrpc' and 'truffle'.I have also used ethereum-bridge for Oraclize.I have written a smart contract function which calls a url using 'OraclizeQuery'.

The following is the contract code :-

pragma solidity ^0.4.0;

import "./usingOraclize.sol";
contract WeatherApiCall is usingOraclize {
       string public weathercondition;
       uint public personscount=2;
       function WeatherApiCall() { 
            OAR = OraclizeAddrResolverI(0xec6487ae821eb3c874c197067ad134faa99c3c11);//contractaddress retrieved on to deploying the Oraclize OAR and Connector from ethereum-bridge
       }

      function __callback(bytes32 myid, string result) {
                 if (msg.sender != oraclize_cbAddress()) throw;
                 weathercondition=result;
     }
     function update(string to,string datetime) payable returns(bool sufficient) {
               oraclize_query("URL", strConcat("json(http://api.openweathermap.org/data/2.5/forecast?q='", to ,"'&mode=json&APPID=d2e8279188c8649c17540f798c9cc972).list[?(@.dt_txt='", datetime, "')].weather[0].main"));
              return true;
       }
}

The following is the index.html file :-

 <!DOCTYPE html>
 <html>
 <head>
        <title>Sample Truffle App</title>
        <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
       <link href="./app.css" rel='stylesheet' type='text/css'>
       <script src="./app.js"></script>
</head>
<body>
        <h1>Airline</h1>
        <h2>Example Truffle Dapp</h2>
        <br>
        <h1>FORM</h1>
              <br><label for="from">From:</label><input type="text" id="from" placeholder="--Residing Place--"></input>
              <br><label for="to">To:</label><input type="text" id="to" placeholder="--Travelling Place--"></input>
              <br><label for="persons">No. of Persons:</label><input type="text" id="persons" placeholder="--No. of Persons--"></input>
              <br><label for="date">Travel Date:</label><input type="text" id="date" placeholder="--Date of travel yyyy-MM-dd--"></input>
              <br><label for="time">Destination Time:</label><input type="text" id="time" placeholder="--HH:mm:ss--"></input>
              <br><br><button id="send" onclick="sendRequest()">Generate Quote</button> 
             <br><br>
 </body>
 </html>

The following is the app.js file.

    var accounts;
    var account;

    function getTransactionReceiptMined(){

              web3.eth.getTransactionReceiptMined = function (txnHash, interval) {
                       var transactionReceiptAsync;
                       interval = interval ? interval : 500;
                       transactionReceiptAsync = function(txnHash, resolve, reject) {
                       try {
                                   var receipt = web3.eth.getTransactionReceipt(txnHash);
                                  if (receipt == null) {
                                               setTimeout(function () {
                                                 transactionReceiptAsync(txnHash, resolve, reject);
                                                }, interval);
                                  } else {
                                                 resolve(receipt);
                                   }
                    } catch(e) {
                           reject(e);
                    }
     };

    if (Array.isArray(txnHash)) {
                  var promises = [];
                  txnHash.forEach(function (oneTxHash) {
                               promises.push(web3.eth.getTransactionReceiptMined(oneTxHash, interval));
                   });
                      return Promise.all(promises);
                   } else {
                              return new Promise(function (resolve, reject) {
                                       transactionReceiptAsync(txnHash, resolve, reject);
                            });
                  }
     };

 }

 function sendRequest() {
       var weatherapicall = WeatherApiCall.deployed();
       var fromdata = document.getElementById("from").value;
       var to = document.getElementById("to").value;
       var persons = parseInt(document.getElementById("persons").value);
       var date = document.getElementById("date").value;
       var time = document.getElementById("time").value;
       //verifying at javascript console
        console.log(fromdata);
        console.log(to);
        console.log(persons);
        console.log(date);
        console.log(time);
        var timeperiod;     
        if ((time >= '00:00:00') && (time < '03:00:00')) {
                    timeperiod = '00:00:00';
        } else if ((time >= '03:00:00') && (time < '06:00:00')) {
                    timeperiod = '03:00:00';
        } else if ((time >= '06:00:00') && (time < '09:00:00')) {
                    timeperiod = '06:00:00';
        } else if ((time >= '09:00:00') && (time < '12:00:00')) {
                    timeperiod = '09:00:00';
        } else if ((time >= '12:00:00') && (time < '15:00:00')) {
                   timeperiod = '12:00:00';
        } else if ((time >= '15:00:00') && (time < '18:00:00')) {
                  timeperiod = '15:00:00';
        } else if ((time >= '18:00:00') && (time < '21:00:00')) {
                  timeperiod = '18:00:00';
       } else if ((time >= '21:00:00') && (time < '24:00:00')) {
                  timeperiod = '21:00:00';
       }
      datetime = date + ' ' + timeperiod;
      console.log(datetime);    
      WeatherApiCall.deployed().update(to ,datetime, {from: account}).then(function(txn) {
                   console.log("Testing");
                   console.log("transaction submited", txn);
                   return getTransactionReceiptMined(txn); // wait for mined result    
      })
      .then(function(receipt) {
                   console.log("transaction mined", receipt);
      })
     .catch(function(e) {
                   console.log(e);     
     });
 };
  window.onload = function() {
           web3.eth.getAccounts(function(err, accs) {
           if (err != null) {
                  alert("There was an error fetching your accounts.");
                  return;
           }
           if (accs.length == 0) {
                  alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");
                  return;
           }
           accounts = accs;
           account = accounts[0];         
         });
 }

I have used 'testrpc' as client.

I have used ethereum-bridge for oraclize.

enter image description here

Based on the OAR address i have updated my contract OAR address.

I have used the following commands to check the functions in solidity contract. enter image description here

The contract.update function call the url given in the oraclizequery and fetch the result.It can be shown in the terminal window for ethereum-bridge. In the ethereum-bridge terminal window,contract.update function call creates a query and fetches the result based on dynamic input. enter image description here

I have tried the dapp in my localhost:8080. enter image description here

From the output i have infered that the transaction is submitted correctly but won't get mined.While checking in my ethereum-bridge terminal no query is created on clicking the button 'Generate Quote'.

I have added the code in github account for your reference. Github Reference Link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.