Last active
November 29, 2022 10:27
-
-
Save Infrablok/428b05c9bb0efe4f9e23a9330c357942 to your computer and use it in GitHub Desktop.
A sample code how to use aviationstack.com rest end points in your solidity smart contract
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'; | |
import '@chainlink/contracts/src/v0.8/ConfirmedOwner.sol'; | |
contract UseAviationApi is ChainlinkClient, ConfirmedOwner { | |
using Chainlink for Chainlink.Request; | |
string public flightStatus; | |
bytes32 private jobId; | |
uint256 private fee; | |
event RequestFulfilled(bytes32 indexed requestId, string indexed data); | |
/** | |
* Goerli Testnet details: | |
* Link Token: 0x326C977E6efc84E512bB9C30f76E30c160eD06FB | |
* Oracle: 0xCC79157eb46F5624204f47AB42b3906cAA40eaB7 | |
* jobId: 7d80a6386ef543a3abb52817f6707e3b | |
*/ | |
constructor() ConfirmedOwner(msg.sender) { | |
setChainlinkToken(0x326C977E6efc84E512bB9C30f76E30c160eD06FB); | |
setChainlinkOracle(0xCC79157eb46F5624204f47AB42b3906cAA40eaB7); | |
jobId = '7d80a6386ef543a3abb52817f6707e3b'; | |
fee = (1 * LINK_DIVISIBILITY) / 10; // 0,1 * 10**18 (Varies by network and job) | |
} | |
/** | |
* @notice Request variable flight status in string from the oracle | |
*/ | |
function requestFlightStatus() public { | |
Chainlink.Request memory req = buildChainlinkRequest(jobId, address(this), this.fulfill.selector); | |
req.add( | |
'get', | |
'http://api.aviationstack.com/v1/flights? | |
access_key=a6656fabf7fbe4fde6e4061f5be7042a& | |
flight_number=611&airline_name=Qantas&limit=1' //aviationstack url,use your own access key from aviation stack.com | |
); | |
req.add('path', 'data,0,flight_status'); // parse the json tree | |
sendChainlinkRequest(req, fee); | |
} | |
/** | |
* @notice Fulfillment function for flight status | |
* @dev This is called by the oracle. recordChainlinkFulfillment must be used. | |
*/ | |
function fulfill(bytes32 requestId, string memory data) public recordChainlinkFulfillment(requestId) { | |
emit RequestFulfilled(requestId, data); | |
flightStatus = data; | |
} | |
/** | |
* Allow withdraw of Link tokens from the contract | |
*/ | |
function withdrawLink() public onlyOwner { | |
LinkTokenInterface link = LinkTokenInterface(chainlinkTokenAddress()); | |
require(link.transfer(msg.sender, link.balanceOf(address(this))), 'Unable to transfer'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment