I want to send requests to 3 Weather APIs and compare their results like if the rainfall at a specific day were the same London to avoid the single point of failure.

I already learned how I can send multiple requests to APIs. What is the best way to compare their results in Solidity? Is there already a mechanism from oraclize itself?


Comparing the results of an API call will be the same as comparing any other data in Solidity.

In your example, you are comparing weather, which is likely a uint256. You will likely also need the date you are comparing, which would be a timestamp (also a uint256). Each of these data sources would need a mapping of timestamp to temperature. The mappings would look as follows:

mapping (uint256 => uint256) public sourceOneWeather;
mapping (uint256 => uint256) public sourceTwoWeather;
mapping (uint256 => uint256) public sourceThreeWeather;

Comparing them would simply be to put them into a conditional statement and returning the output of whatever comparison you desire.

Add function example, per comments

An example comparison function would be:

function compareResults(uint256 _timestamp) public returns (uint256){
    if (sourceOneWeather[_timestamp] > sourceTwoWeather[_timestamp]):
        if (sourceOneWeather[_timestamp] > sourceThreeWeather[_timestamp]):
            return sourceOneWeather[_timestamp];
            return sourceThreeWeather[_timestamp];
    else if (sourceTwoWeather[_timestamp] > sourceThreeWeather[_timestamp]):
        return sourceTwoWeather[_timestamp];
    return sourceThreeWeather[_timestamp];
  • 1
    Thanks for your fast answer. Saving them is no problem. But how i do now, that all 3 Requests gave me an answer so i can call a method to compare the results? Nov 16 '18 at 22:36
  • 1
    That simply depends on what you are comparing. You would have to create a function that compares the results, yes. I will add a small example to the code. Nov 16 '18 at 22:39
  • 1
    Thanks for the example of code. But i think you did not understand my question. As example i want to know, if it really rained in London at the 10th of November 2018. To avoid single point of failure, i want to request at least 3 APIs. Requesting these three APIs is no problem. And comparing their results is no problem too. My question is: How can i know, that all 3 Responses are received so i can compare their results? Nov 16 '18 at 22:45
  • 1
    Set an isReceived flag after each Oracle call return. Nov 16 '18 at 22:47
  • Ok had that solution in my head. Thought there would be another possiblity. Thank you @shane ! Nov 16 '18 at 22:50

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.