📌Weather Feed Contract

Introduction

The Weather Feed Oracle is a decentralized smart contract designed to fetch real-time weather data and provide it to other smart contracts. Acting as a bridge between blockchain applications and external weather data sources, this oracle enables smart contracts to access and utilize real-world weather information securely and transparently.

Technical Description

The Weather Feed Oracle is implemented as a smart contract on a blockchain platform such as Ethereum. It listens for weather data requests, fetches real-time weather information from an external weather API, and stores the data in a decentralized manner. This ensures accessibility, security, and immutability.

Workflow

  1. A user invokes the contract by providing a _location (city name).

  2. The contract makes an external API request to fetch weather data for the specified location.

  3. The retrieved data is stored in the contract’s storage and an event is triggered.

  4. Other smart contracts or users can monitor this event and retrieve the stored weather data.

Key Features

  • Secure and tamper-proof storage of weather data.

  • Transparent and publicly accessible data.

  • Pay-per-use model for requesting weather information.

  • Event-driven updates that notify subscribers when new data is available.

Usage

To interact with the Weather Feed Oracle, follow these steps:

1. Deploy the Contract

Deploy the contract to the blockchain using the following Oracle Contract address:

0x9259A1758adf242eA6c9A4b2F0146f2f68c4d34A

2. Request Weather Data

Call the function:

requestWeatherData(_location)
  • _location: The city name for which weather data is requested.

  • This is a payable function, meaning the user must pay a certain amount of Ether to access the data.

  • On successful execution, the function returns a success message.

3. Retrieve Weather Data

Once the data is available, call:

retrieveWeather(_location)
  • _location: The city name for which data was previously requested.

  • This function retrieves the latest weather data, including temperature and weather description.

Smart Contract Implementation

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;

interface Oracle {
    function requestWeather(string memory _location) external payable returns (string memory);
    function getWeather(string memory _location) external view returns (string memory, string memory);
}

contract WeatherFeedContract {
    address public oracleAddress;

    constructor(address _oracleAddress) {
        oracleAddress = _oracleAddress;
    }

    function requestWeatherData(string memory _location) 
        public payable returns (string memory) 
    {
        return Oracle(oracleAddress).requestWeather{value: msg.value}(_location);
    }

    function retrieveWeather(string memory _location) public view returns (string memory, string memory) {
        return Oracle(oracleAddress).getWeather(_location);
    }
}

Contract Functions

1. Constructor

constructor(address _oracleAddress)
  • Initializes the contract with the address of the Oracle smart contract.

2. Request Weather Data

function requestWeatherData(string memory _location) public payable returns (string memory)
  • Allows users to request weather data for a specific location.

  • Requires a payable transaction where users must send Ether to access the data.

  • Takes in _location (city name) as a parameter.

  • Calls the requestWeather function on the Oracle contract to fetch the data.

  • Returns a success message if the request is processed correctly.

3. Retrieve Weather Data

function retrieveWeather(string memory _location) public view returns (string memory, string memory)
  • Fetches stored weather data for a given location.

  • Calls getWeather function on the Oracle contract.

  • Returns:

    • temperature: The current temperature.

    • weather_description: The weather condition (e.g., sunny, rainy, cloudy).

Last updated