Example: Real-Time Data Fetching Oracle for Financial Models
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol";
contract ForexOracle is ChainlinkClient {
using Chainlink for Chainlink.Request;
uint256 private fee;
bytes32 private jobId;
// Mapping to store the request data
mapping(bytes32 => string) private requestCurrencyPairs;
// Initialize the Chainlink token and oracle address
constructor() {
setPublicChainlinkToken();
oracle = 0xOracleAddress; // Replace with the actual oracle address
jobId = "jobId"; // Replace with the actual job ID
fee = 0.1 * 10 ** 18; // 0.1 LINK (Varies by network and job)
}
// Function to request data from the oracle
function requestData(string memory _currencyPair) public returns (bytes32 requestId) {
Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);
request.add("currencyPair", _currencyPair);
requestId = sendChainlinkRequestTo(oracle, request, fee);
requestCurrencyPairs[requestId] = _currencyPair;
return requestId;
}
// Fulfill function to receive the data
function fulfill(bytes32 _requestId, uint256 _rate) public recordChainlinkFulfillment(_requestId) {
string memory currencyPair = requestCurrencyPairs[_requestId];
// Process the received rate
// e.g., store it or emit an event
}
}Example of Usage:
Key Features:
Last updated