# Air Quality Feed Contract

### Introduction

The **Air Quality Feed Oracle** is a decentralized smart contract solution designed to fetch real-time air quality data and provide it to other smart contracts. Acting as a bridge between blockchain applications and external data sources, this oracle enables smart contracts to access and utilize air quality information trustless and transparently.

### Technical Description

The **Air Quality Feed Oracle** is implemented as a smart contract on a blockchain platform such as Ethereum. It listens for air quality data requests, fetches real-time data from an external 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_id`.
2. The contract makes an external API request to fetch air quality 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 air quality data.

#### Key Features

* Secure and tamper-proof storage of air quality data.
* Transparent and publicly accessible data.
* Pay-per-use model for requesting air quality data.
* Event-driven updates that notify subscribers when new data is available.

### Usage

To interact with the **Air Quality Feed Oracle**, follow these steps:

#### 1. Deploy the Contract

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

```
0x67373CD605E1cEAb60f28fd0455C70115472D5Ba
```

#### 2. Request Air Quality Data

Call the function:

```
requestAirData(_location_id, _sender_address)
```

* `_location_id`: The ID of the location for which air quality data is requested.
* `_sender_address`: The address of the user making the request.
* This is a **payable function**, meaning the user must pay a certain amount of Ether to access the air quality data.
* On successful execution, the function returns a unique request ID.

#### 3. Retrieve Air Quality Data

Once the data is available, call:

```
retrieveData(id)
```

* `id`: The request ID received from the previous function.
* This function retrieves the latest air quality data for the specified request.

### Smart Contract Implementation

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

interface AirQualityOracle {
    function add(uint cityID, address payable sender) external payable returns (uint, address, int);
    function getAirData(uint _id) external view returns (string memory);
}

contract UserContract {
    address public AirQualityOracleAddress;

    constructor(address _oracleAddress) {
        AirQualityOracleAddress = _oracleAddress;
    }

    uint public location_id;
    address public sender_address;
    int public request_id;

    function requestAirData(uint _location_id, address payable _sender_address)
        public payable returns (uint, address, int)
    {
        location_id = _location_id;
        (uint loc_id, address sender, int id) = AirQualityOracle(AirQualityOracleAddress).add{value: msg.value}(_location_id, _sender_address);
        return (loc_id, sender, id);
    }

    function retrieveData(uint id) public view returns (string memory) {
        return AirQualityOracle(AirQualityOracleAddress).getAirData(id);
    }
}
```

### Contract Functions

#### 1. Constructor

```
constructor(address _oracleAddress)
```

* Initializes the contract with the address of the **AirQualityOracle** smart contract.

#### 2. Request Air Quality Data

```
function requestAirData(uint _location_id, address payable _sender_address)
    public payable returns (uint, address, int)
```

* Allows users to request air quality data for a specific location.
* Requires a **payable transaction** where users must send Ether to access the data.
* Takes in `_location_id` (location identifier) and `_sender_address` (user's address).
* Returns:
  * `loc_id`: Location ID.
  * `sender`: Address of the requester.
  * `id`: Unique request ID.

#### 3. Retrieve Air Quality Data

```
function retrieveData(uint id) public view returns (string memory)
```

* Fetches the stored air quality data for a given request ID.
* Returns air quality data as a **string**.
