Created
February 5, 2022 13:25
-
-
Save simonvc/866217032b3fa2889e05a9278c725753 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.7; | |
import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol"; | |
contract APIConsumer is ChainlinkClient { | |
using Chainlink for Chainlink.Request; | |
uint256 public airquality; | |
address private oracle; | |
bytes32 private jobId; | |
uint256 private fee; | |
event recordedAirQuality( | |
uint, //timestamp | |
uint256 //airquality | |
); | |
/** | |
* Network: Kovan | |
* Oracle: 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8 (Chainlink Devrel | |
* Node) | |
* Job ID: d5270d1c311941d0b08bead21fea7747 | |
* Fee: 0.1 LINK | |
* Oracle 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8 | |
* HTttpGetJsonParseMultiplyEthInt256 83ba9ddc927946198fbd0bf1bd8a8c25 | |
**/ | |
// Kovan Link oracle address 0xa36085F69e2889c224210F603D836748e7dC0088 | |
constructor() { | |
setPublicChainlinkToken(); | |
oracle = 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8; | |
jobId = "d5270d1c311941d0b08bead21fea7747"; | |
fee = 0.1 * 10 ** 18; // (Varies by network and job) | |
} | |
/** | |
* Create a Chainlink request to retrieve API response, find the target | |
* data, then multiply by 1000000000000000000 (to remove decimal places from data). | |
*/ | |
function requestAirQualityData() public returns (bytes32 requestId) | |
{ | |
Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfill.selector); | |
// Set the URL to perform the GET request on | |
request.add("get", "https://data.waag.org/api/holu/holukit/calibrated/recent"); | |
request.add("path", "$[?(@.id=='115')].pm25_mean"); //this PATH will find the data for De Ceuval | |
// Multiply the result by 1000 to remove decimals | |
int timesAmount = 1000; | |
request.addInt("times", timesAmount); | |
// Sends the request | |
return sendChainlinkRequestTo(oracle, request, fee); | |
} | |
/** | |
* Receive the response in the form of uint256 | |
*/ | |
function fulfill(bytes32 _requestId, uint256 _airquality) public recordChainlinkFulfillment(_requestId) | |
{ | |
airquality = _airquality; | |
// now we emit an event to ensure this is recorded to the blockchain | |
emit recordedAirQuality( | |
block.timestamp, // timestamp | |
airquality //the value | |
); | |
} | |
function getDeCeuvelAirQuality() external view returns (uint256) | |
{ | |
// This function allows other smart contracts to get the current air quality at DeCeuvel | |
return airquality; | |
} | |
// function withdrawLink() external {} - Implement a withdraw function to avoid locking your LINK in the contract | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment