Since Blockchain ecosystems are decentralized, on-chain smart contracts cannot access off-chain data natively. However, Chainlink furnishes a platform for blockchain oracles, which are nodes on the network that serves as a bridge between on-chain & off-chain data. Oracles enable smart contracts to retrieve external data.   

Each oracle node can be configured to perform a vast range of chores relying on the adapters it supports. Some of these adapters include HTTP POST, HTTP GET, JSON Parse, Multiply, etc.  

In this blog, we will explain how one can call Aviationstack.com API from Ethereum smart contracts using Chainlink’s oracle. 

4 Steps To Call Aviationstack.com API From Ethereum Smart Contracts Using Chainlink’s Oracle

Using Chainlink’s Oracles, the latest data can be retrieved by using any REST API call from a smart contract in real-time. The four simple steps stated below will help you know how to call the REST endpoint provided by Aviation Data in Solidity to get real-time flight status, i.e., scheduled, delayed, landed, etc. 

Step 1: Add A Testnet To MetaMask Wallet 

We will use the goerli TestNet for this example.    

  • Open Metamask and then click on the Networks and choose Goerli test network.   
  • In the remix, select Injected provider Metamask to connect to goerli test network.   
  • Before starting development, add some test coins from the test faucet from here. 

Also, get Link tokens from Chainlink’s test faucet.

Chainlink’s test faucet

Step 2: Find The Oracle Address For Your Chain   

From https://docs.chain.link/any-api/testnet-oracles/ get the list of different test net addresses of oracles.  

We are using goerli test net oracle with the address
0xcc79157eb46f5624204f47ab42b3906caa40eab7  

with jobId —7d80a6386ef543a3abb52817f6707e3b 

Whenever we request something through the Chainlink oracle, we request that through Chainlink jobs. Simple jobs define when, how, why, etc., that need to be done via Chainlink nodes. This job id (7d80a6386ef543a3abb52817f6707e3b) describes to the oracle that we need to return the response in a String of other jobs. It returns a response in bool, int256, unit 256, etc. 

Step 3: Add The Code To Your Solidity Smart Contract 

Integrate the following code into your smart contract: 

//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 bytes 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 

); 

req.add('path', 'data,0,flight_status'); // parse the json tree 

sendChainlinkRequest(req, fee);  

} 

 

/** 

* @notice Fulfillment function for variable bytes 

* @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'); 

} 

}  

Open this code in Gist    

The breakdown of each component of this contract is as follows –   

  1. Constructor: Sets up the contract with the Oracle address, Job ID, and LINK fee that oracle charges for the job  
  2. requestFlightStatus function: Builds and sends a request. The request includes fulfillment functions selector – to the oracle. Notice how it adds the get, path, and parameters. These are read by the tasks in the job to perform correctly. ‘get’ is used by HTTP, and ‘path’ is used by JSON Parse.
  3. fulfill function: This is where the result is sent upon the completion of Oracle’s Job.
  4. WithdrawLink Function: This method is used to withdraw the user’s link token from their smart contract.   

Note: The calling contract should own enough LINK to pay the fee, which by default is 0.1 LINK. 

Step 4: Deploy And Test   

  • Now choose Injected Web3 as your Environment in Remix and deploy your contract to the goerli TestNet.  
  • Next, choose the requestFlightStatus() function, it will then pay the transaction fee, and when the transaction becomes successful, it calls the flightStatus, and the result is displayed as shown below.

Deploy and Test 

Ending Words

Well, if you want to connect APIs from Ethereum smart contracts using chainlink oracles in your existing or new Blockchain-based projects, get in touch with one of the excellent Blockchain development companies (Infrablok). It will help you get a professional who can handle your project from scratch. 

Leave a Reply

Your email address will not be published. Required fields are marked *