A Comprehensive Guide to Chainlink Functions

Razvan Mihailescu
5 min readMay 8, 2023

--

Chainlink is a decentralized oracle network that allows smart contracts to securely access off-chain data and interact with external systems. Chainlink functions are an important component of this network, enabling developers to easily access and utilize a wide range of data sources, services, and APIs in their smart contracts. In this comprehensive guide, we will explore everything you need to know about Chainlink functions, including their purpose, types, usage, and examples.

What are Chainlink Functions?

Chainlink functions are essentially a type of smart contract that provides a standardized interface for accessing external data and services. These functions are deployed on the Chainlink network, where they act as oracles that can be used by other smart contracts to interact with external systems. Chainlink functions are designed to be modular and flexible, allowing developers to easily customize them to suit their specific needs.

Types of Chainlink Functions

Chainlink functions can be classified into two main types based on their purpose:

Data Functions:

These functions provide access to off-chain data, which can be used by smart contracts for various purposes such as triggering certain actions, making decisions, or performing calculations. Some examples of data functions include:

  • HTTPGet: This function enables a smart contract to access external APIs and retrieve data from them using HTTP requests.
  • Randomness: This function generates random numbers that can be used for various purposes such as selecting winners in a lottery or choosing a random word in a game.
  • Price Feed: This function provides real-time price data for various assets, which can be used by smart contracts for trading, arbitrage, or other financial applications.

Service Functions:

These functions provide access to external services, which can be used by smart contracts to perform various actions such as sending emails, making payments, or accessing file storage. Some examples of service functions include:

  • SendGrid: This function enables a smart contract to send emails using the SendGrid API.
  • PayPal: This function enables a smart contract to make payments using the PayPal API.
  • IPFS: This function enables a smart contract to store and retrieve data on the InterPlanetary File System (IPFS).

Usage of Chainlink Functions

To use Chainlink functions in your smart contracts, you need to follow these steps:

  1. Choose the appropriate Chainlink function for your use case.
  2. Deploy the Chainlink function on the Chainlink network.
  3. Create a smart contract that uses the Chainlink function as an oracle.
  4. Call the Chainlink function from your smart contract to retrieve the required data or perform the desired action.
  5. Use the retrieved data or perform the action in your smart contract.

Let’s take a look at an example of how to use the HTTPGet function to retrieve data from an external API in a smart contract. In this example, we will create a smart contract that retrieves the current temperature of a city from an external API and stores it in a variable.

Choose the appropriate Chainlink function for your use case.

In this case, we will use the HTTPGet function to retrieve data from an external API.

Deploy the Chainlink function on the Chainlink network

.The HTTPGet function is already deployed on the Chainlink network, so we don’t need to deploy it ourselves.

Create a smart contract that uses the Chainlink function as an oracle.

Here is an example of a smart contract that uses the HTTPGet function as an oracle:

pragma solidity ^0.8.0;

import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol";

contract TemperatureContract is ChainlinkClient {

uint256 public temperature;

constructor() public {
setPublicChainlinkToken();
}

function getTemperature(string memory _city) public {
Chainlink.Request memory request = buildChainlinkRequest(
"c0f6d82985c74a9ba84e9c6345b5d5d5",
address(this),
this.fulfillTemperature.selector
);
string memory url = string(abi.encodePacked("https://api.openweathermap.org/data/2.5/weather?q=", _city, "&appid=YOUR_APP_ID"));
request.add("get", url);
request.add("path", "main.temp");
sendChainlinkRequestTo(0x01BE23585060835E02B77ef475b0Cc51aA1e0709, request, 0.1 ether);
}

function fulfillTemperature(bytes32 _requestId, uint256 _temperature) public recordChainlinkFulfillment(_requestId) {
temperature = _temperature;
}

}

This contract imports the ChainlinkClient library and inherits from it. It also defines a public variable called temperature, which will store the retrieved temperature value.

The getTemperature function takes a string parameter _city, which is the name of the city whose temperature we want to retrieve. Inside this function, we create a Chainlink request using the buildChainlinkRequest function, which takes the following parameters:

  • The job ID of the HTTPGet function on the Chainlink network.
  • The address of the contract that will receive the response.
  • The function selector of the fulfillTemperature function, which will handle the response.

We also create a URL string using the OpenWeatherMap API and add it to the request using the add function. Finally, we send the request to the Chainlink network using the sendChainlinkRequestTo function.

The fulfillTemperature function is called when the response is received from the Chainlink network. It takes two parameters: _requestId, which identifies the Chainlink request, and _temperature, which is the retrieved temperature value. This function simply stores the temperature value in the temperature variable.

Call the Chainlink function from your smart contract to retrieve the required data.

We can now call the getTemperature function from our smart contract to retrieve the current temperature of a city. For example:

TemperatureContract contract = new TemperatureContract();
contract.getTemperature("New York");

Use the retrieved data in your smart contract.

After calling the getTemperature function, we can retrieve the temperature value from the temperature variable:

uint256 currentTemperature = contract.temperature();

Conclusion

Chainlink functions are a powerful tool for developers looking to build smart contracts that interact with external systems and data sources. With a wide range of data and service functions available, developers can easily customize and integrate Chainlink functions into their smart contracts to perform a variety of tasks. From accessing price data for financial applications to sending emails and making payments, Chainlink functions provide a flexible and secure way to interact with the outside world.

As the adoption of blockchain technology continues to grow, Chainlink functions are likely to play an increasingly important role in the development of decentralized applications. By enabling secure and reliable access to off-chain data and services, Chainlink functions are helping to bridge the gap between blockchain technology and the real world.

We hope this comprehensive guide to Chainlink functions has been helpful and informative. Whether you’re a seasoned blockchain developer or just getting started, understanding the basics of Chainlink functions is an important step towards building powerful and innovative decentralized applications.

--

--

Responses (1)