📌Traffic Feed Contract

Introduction

The Smart Contract Traffic Feed Oracle is a decentralized solution designed to fetch real-time traffic data and provide it to smart contracts. Acting as a bridge between blockchain-based contracts and external data sources, this oracle enables smart contracts to seamlessly access and utilize live traffic information.


Technical Description

The Smart Contract Traffic Feed Oracle is implemented as a blockchain smart contract (e.g., on Ethereum). It listens for traffic data events, stores the data in a decentralized manner, and makes it accessible across the network.

How It Works

The contract accepts coordinates (longitude & latitude) as input. It sends a request to an external traffic API and retrieves the latest data. The contract updates its storage with real-time traffic metrics:

  • Free Flow Speed

  • Current Travel Time

  • Free Flow Travel Time

An event is triggered that other smart contracts can monitor.

Users interact with the contract by calling its functions and providing the necessary inputs. The stored traffic data is secure, transparent, and tamper-proof, ensuring a reliable data source for decentralized applications.


🔗 Usage

To utilize the Smart Contract Traffic Feed Oracle, follow these steps:

1️⃣ Deploy the Contract

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

0x7D9C78e1B944669f78E9571Ca1783BC479Aa47e7

2️⃣ Request Traffic Data

Call the function:

requestTrafficData(first, second, address)
  • This function accepts longitude and latitude as input.

  • It is a payable function, requiring a small amount of ETH for access.

  • Upon completion, it returns an ID for tracking the data request.

3️⃣ Retrieve Traffic Data

Call the function:

retrieveData(_id)
  • Pass the ID obtained from the previous step to fetch the latest traffic data.


📜 Contract Code

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

interface TrafficDataOracle {
    function add(string memory first, string memory second, address payable sender) payable external returns (string memory, string memory, address, int);
    function getTrafficData(uint _id) view external returns (uint, uint, uint);
}

contract Contract2 {
    address TrafficoracleAddress;

    constructor(address _c1) {
        TrafficoracleAddress = _c1;
    }

    int public _id;

    function requestTrafficData(string memory _first, string memory _second, address payable _sender_address) public payable returns (string memory, string memory, address, int) {
        (string memory first, string memory sec, address sender, int id) = TrafficDataOracle(TrafficoracleAddress).add{value: msg.value}(_first, _second, _sender_address);
        return (first, sec, sender, id);
    }

    function retrieveData(uint id) public view returns (uint, uint, uint) {
        return TrafficDataOracle(TrafficoracleAddress).getTrafficData(id);
    }
}

Contract Functions

🔹 constructor(address _c1)

  • Accepts the TrafficDataOracle contract address.

  • Sets up the reference to the external Oracle contract.

🔹 requestTrafficData

function requestTrafficData(string memory _first, string memory _second, address payable _sender_address) public payable returns (string memory, string memory, address, int);
  • Accepts starting location and destination as string inputs.

  • Requires a payment in Ethereum for data access.

  • Returns:

    • Starting location

    • Destination

    • Sender address

    • Request ID

🔹 retrieveData

function retrieveData(uint id) public view returns (uint, uint, uint);
  • Fetches traffic data based on a given request ID.

  • Returns:

    • Travel time

    • Distance

    • Timestamp

Last updated