0

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'.I have added the code in app.js file, but the query won't get executed.I tried to execute the functions using truffle console and it worked well.
The following is the contract code :-

pragma solidity ^0.4.0;

import "./usingOraclize.sol";

contract WeatherApiCall is usingOraclize {

   string public weathercondition;
   function WeatherApiCall() {

           OAR = OraclizeAddrResolverI(0x3df0db5bda9d685e41cb4a8834c44f8028957417);
   }

   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 for the corresponding smartcontract code:-

var accounts;
var account;
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';[![enter image description here][1]][1]
    } 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.update(to ,datetime, {from: account}).then(function(value) {
         console.log("Testing");
         console.log(value);
         console.log(value.valueOf());
   }).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 loaded the dapp and provided the inputs and printed the values correctly at javascript console.But the call to update function won't get executed. enter image description here

1

I can't speak to your use-case but I think I see a missing step.

This all-important line:

weatherapicall.update(to ,datetime, {from: account}).then(function(value) {

You're going to get a transaction hash that indicates the transaction was submitted to the blockchain. If I'm not mistaken, this is what is appearing in the console log. It doesn't contain the result of the transaction because that result isn't knowable until the transaction is mined.

Should look approximately like this:

weatherapicall.update(to ,datetime, {from: account}).then(function(txn) {
    console.log("transaction submited", txn);
    return getTransactionReceiptMined(txn); // wait for mined result
})
.then(function(receipt) {
    console.log("transaction mined", receipt);
    // explore the receipt to find your data, or return a call() to get it

I've had good luck with getTransactionReceiptMined() here:

https://gist.github.com/xavierlepretre/88682e871f4ad07be4534ae560692ee6

Hope it helps.

  • @ Rob Hitchens I have updated my app.js file but it returns the result of 'transaction mined' as 'undefined'. I have added a call to oraclize in my solidity function.It won't get executed(Because Ethereum-bridge won't get any query request).Isn't this is the reason for 'undefined'? – Crissi Mariam Robert Feb 8 '17 at 10:37
  • @criss I would interpret 'undefined' as 'weatherapicall.update()' threw a JUMP or gas-out for some reason. Problems with the bridge sounds like a candidate for problems with the update(). :-) As for the call after, I see the callback sets weathercondition (string public). If your update() transaction is verified (mined) then that should be updated and you can fetch it from your contract, after .then(function(receipt) { ... – Rob Hitchens - B9lab Feb 8 '17 at 11:23
  • I have tried to call the update function in solidity contract using truffle console. "" contract.update('chennai','2017-02-08 18:00:00').then(console.log) "" It returned the transaction hash and ethereum-bridge returns the result correctly for the given oraclize query.But the call to update won't get executed through app.js. – Crissi Mariam Robert Feb 8 '17 at 11:38
  • @ Rob Hitchens Any idea regarding the issue??? – Crissi Mariam Robert Feb 9 '17 at 4:04
  • Nothing obvious and I don't see you overlooking things I would suggest. If I understand the plot correctly, the original issue is addressed but doing so has revealed another mystery. Question of why something works in the console but not in the app (what's the difference?); might require knowledge of Oraclize/Ethereum-Bridge to spot some sort of oversight. I would consider posting a new question with the code, and a picture of your steps in truffle console to prove that side works as expected. May even give a hint to something missing from the app. Why does A work and B doesn't? I'm curious! – Rob Hitchens - B9lab Feb 9 '17 at 5:04

Your Answer

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

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