📌Crypto Price Feed Contract

Introduction

The Crypto Price Feed Oracle is a decentralized smart contract designed to fetch real-time cryptocurrency price 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 securely and transparently access real-world cryptocurrency price information.

Technical Description

The Crypto Price Feed Oracle is implemented as a smart contract on a blockchain platform such as Ethereum. It listens for cryptocurrency price data requests, fetches real-time price information 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 _symbol_crypto (e.g., "ETH").

  2. The contract makes an external API request to fetch the latest price of the specified cryptocurrency.

  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 cryptocurrency price data.

Key Features

  • Secure and tamper-proof storage of cryptocurrency price data.

  • Transparent and publicly accessible data.

  • Pay-per-use model for requesting cryptocurrency price information.

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

Usage

To interact with the Crypto Price Feed Oracle, follow these steps:

1. Deploy the Contract

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

0x380fAbAB6DbcCB27E0030B5C4aD1272aF10D5546

2. Request Cryptocurrency Price Data

Call the function:

requestCryptoData(_symbol_crypto, _sender_address)
  • _symbol_crypto: The ticker symbol of the cryptocurrency (e.g., "ETH", "BTC").

  • _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 price data.

  • On successful execution, the function returns a unique request ID.

3. Retrieve Cryptocurrency Price Data

Once the data is available, call:

retrieveData(id)
  • id: The request ID received from the previous function.

  • This function retrieves the latest cryptocurrency price data.

Smart Contract Implementation

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

interface CryptoPriceFeedOracle {
    function add(string memory symbol, address payable sender) external payable returns (string memory, address, int);
    function getCryptoData(uint _id) external view returns (string memory);
}

contract CryptoPriceFeedContract {
    address public cryptoOracleAddress;
    
    constructor(address _oracleAddress) {
        cryptoOracleAddress = _oracleAddress;
    }

    function requestCryptoData(string memory _symbol_crypto, address payable _sender_address) 
        public payable returns (string memory, address, int) 
    {
        return CryptoPriceFeedOracle(cryptoOracleAddress).add{value: msg.value}(_symbol_crypto, _sender_address);
    }

    function retrieveData(uint id) public view returns (string memory) {
        return CryptoPriceFeedOracle(cryptoOracleAddress).getCryptoData(id);
    }
}

Contract Functions

1. Constructor

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

2. Request Cryptocurrency Price Data

function requestCryptoData(string memory _symbol_crypto, address payable _sender_address) public payable returns (string memory, address, int)
  • Allows users to request price data for a specific cryptocurrency.

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

  • Takes in _symbol_crypto (cryptocurrency ticker symbol) and _sender_address (user's address).

  • Calls the add function on the Oracle contract to store the requested data and generate an ID.

  • Returns:

    • symbol_crypto: The requested cryptocurrency symbol.

    • sender: Address of the requester.

    • id: Unique request ID.

3. Retrieve Cryptocurrency Price Data

function retrieveData(uint id) public view returns (string memory)
  • Fetches stored cryptocurrency price data for a given request ID.

  • Calls getCryptoData function on the Oracle contract.

  • Returns:

    • price: The latest price of the requested cryptocurrency.

Last updated