How does Indexed Data is brought from The Graph to Smart Contracts with Chainlink Oracles?

Hasnat Sajid
8 min readOct 26, 2020

With the increasing popularity of smart contracts, Sergey Nazarov and Steve Ellis observed that such contracts can only rely on data that is on-chain and there’s no secure way to connect them to external sources of information. To overcome this limitation of smart contracts, they founded the Chainlink project which relied on a network of decentralized oracles to feed accurate data to smart contracts in a secure way from external sources.

The Graph is excited to announce an integration with Chainlink, the market-leading decentralized oracle network. The integration will allow indexed data from The Graph’s APIs, called subgraphs, to be relayed to smart contracts via Chainlink oracles. This opens up opportunities for developers to use indexed data sets to build powerful, truly decentralized applications.

Below are three use cases where developers can use Chainlink oracles to access subgraphs on-chain to augment the value of their decentralized application:

  • Using a subgraph to catalog on-chain liquidity to calculate and compare slippage of potential trades made on a DEX and other Defi applications
  • Using a subgraph to catalog the average gas consumed per block for each transaction to calculate recommended gas fees

Synthetix is an example of a Defi app that combines the best of middleware protocols: using Chainlink oracles for price feeds and powering their exchange UI using a subgraph. This integration is the start of an expansion in middleware protocols that seamlessly integrate, providing the necessary infrastructure to build truly serverless and decentralized applications.

Smart contracts

Let’s take a step back and try to understand what smart contracts are. These are decentralized infrastructures, such as a blockchain that is obviously run by computer applications. Instead of being legally enforceable, such as a regular contract, a smart contract enforces the relationship through a cryptographic code. In a decentralized network, there is no way of manipulating it or disrupting its execution. Hence, the element of trust that is required for a regular contract is taken out of the picture with smart contracts. However, smart contracts are unable to interact with external resources such as banking systems, APIs, and data feed; therefore it is necessary that middleware knew as “Oracle” is used. Chainlink develops a secure, fully decentralized Oracle network based on blockchain technology and combines intelligent contracts with off-chain resources.

The Graph: Building Open APIs for Web3

An open API, also known as a public API, is an application programming interface. It enables the owner of service accessible to the network to grant users of this service, for example, developers, universal access. An API is a software intermediary that allows application programs to interact with one another and share data.

Indexing is a way of structuring and categorizing data so it can be easily searched for and queried by users. Google is a famous example of indexing web data, substantially decreasing the time it takes for users to find and use the information on the Internet. Without data indexing, users would need to manually scroll through unorganized data stores filled with raw and unmapped data to find the information they need.

It is often a REST implementation that provides a specific service or software function while protecting the rest of the application. Open APIs are published on the internet and freely shared. For example, a startup software company could publish a number of APIs. With the aim of encouraging third-party developers in vertical industries to innovate and find new ways to use the startup’s software product. In theory, it’s a win-win business deal.

Blockchains are increasingly becoming data-rich environments, both through data generation activities on-chain (smart contract events and calls) and through dApps bringing external data on-chain via oracles. All apps that show blockchain data in their UIs require indexed data to reference the information they need and create good user experiences.

The Graph, developers had to write their own indexing code and maintain servers and databases on centralized infrastructure. This introduces a central point of failure; the indexed data can be maliciously or mistakenly manipulated by the dApp or the single server can go offline. Furthermore, developers have to spend extra time and money to run the infrastructure themselves. Deploying a subgraph is the most efficient way for developers to index blockchain data.

The Graph solves the problem of centralized data indexing by building a decentralized protocol for indexing blockchain data. Today subgraphs can be deployed to The Graph’s hosted service and a decentralized network will be launching later this year. This will ensure that data remains open and that dApps continue to run no matter what.

With the decentralized network, users will no longer have to trust teams to operate servers and developers can deploy to the reliable public infrastructure they don’t have to manage. The Graph uses GraphQL, an open-source query language widely used by the web developer community. It provides a powerful API to get exactly the data users to need in a single request, seamlessly traversing and combining data sources. GraphQL makes it easy for Web2 developers to build in Web3.

Chainlink: Bridging Indexed Data with Smart Contracts

Blockchain activity alone produces several valuable on-chain data sets that are increasingly attractive for use in decentralized applications. While the raw data may not be as useful for a dApp, the indexed and compiled data can be. Most data presented in a dApp UI (eg. user balances, digital art, votes) is blockchain data indexed by The Graph. Useful indexed data includes gas data to determine the recommended gas fee, block times to determine the average block time, a DEX’s on-chain liquidity to calculate slippage, and much more.

Broadcasting off-chain data and storing it on-chain is another important area where data indexing becomes useful. For example, deploying a subgraph that includes the schema and manifests for consuming blockchain data can be useful for analyzing historical data, such as determining the quality of an oracle node based on past response time to a price data query, the accuracy of those responses, the number of jobs completed, and more.

The next important feature is making data indexed by The Graph available to smart contracts via secure and reliable oracles. Chainlink is a framework for building decentralized oracle networks that is critical to data sharing between decentralized applications. It allows users to decentralize both the node operator and data source of an oracle network, as well as incorporate advanced features such as trusted execution environments and verifiable randomness. Being the most widely used oracle network across an increasing number of different blockchains, Chainlink oracles make for a natural combination with The Graph for apps across blockchain environments to index data and deliver that data to smart contracts.

All subgraphs are potential data sources for Chainlink oracles that can feed into a smart contract to trigger execution or feed into off-chain computation protocols. Access to subgraphs enables more data-rich oracles. Smart contracts can also use Chainlink oracles to trigger the indexing of data on The Graph.

As part of the integration, The Graph will index DEX liquidity data that Chainlink oracles can bridge to DEX UIs as reference data for calculating slippage based on a user’s trade size. The Graph will also create an index that catalogs gas prices paid per transaction. dApps can reference this Gas Station API via Chainlink to calculate the recommended gas price. Finally, there are plans to develop a universal API to allow smart contracts access to any subgraph using a Chainlink decentralized oracle network. Thus, developers can index real-world data and use it in their smart contracts, such as gaming results, user profiles, and more.

Some initial work on integrating Chainlink and The Graph was also done by a team at the ETHBerlin Zwei hackathon. The Graph Oracle allows smart contracts to run queries on The Graph and have the results posted back to the smart contracts.

Seeding Middleware Infrastructure for Accelerated Smart Contract Development

We have already seen the creation of several stand-alone applications that either function completely on-chain, or use oracles to enhance their ability to produce value for users. In our opinion, smart contract technology has only scratched the surface of its potential due to a lack of decentralized middleware infrastructure. This is rapidly changing, as the tools available to developers outside of the base layer are growing, including The Graph for indexing and querying data, Chainlink for oracles, Ceramic for storing data, LivePeer for video streaming, Unstoppable Domains for web hosting, and many more. Together, these middleware protocols will make developing a smart contract substantially easier, while providing access to more functionality.

“We’re thrilled to collaborate with Chainlink to bring a vast amount of new blockchain datasets to developers, augmenting the value of their decentralized applications. The Graph, developers can start cataloging important blockchain data, such as gas prices and slippage, and securely relay them to their dApps using Chainlink oracles. This brings end-to-end decentralization of data indexing and data retrieval, a critical step in abstracting complexity to allow for more advanced dApp development.” — Yaniv Tal, Co-founder at The Graph

About Chainlink

If you’re a developer and want to connect your smart contract to existing data and infrastructure outside the underlying blockchain, reach out to them here. Your smart contracts connected to real-world data, events, and payments. They can help you quickly and securely launch your data-enabled application or Chainlink Price Reference Data Contract on manner today. You can also visit the developer documentation or join the technical discussion on Discord. Learn more by visiting the Chainlink website or follow them on Twitter or Reddit.

ChainLink is a decentralized oracle network that provides real-world data to smart contracts on the blockchain. To understand the benefits of ChainLink and how it functions, you need to understand some fundamental, interconnected concepts. Let’s start with smart contracts.

This is where oracles come into play. An oracle is a software known as ‘middleware’ that acts as an intermediary, translating data from the real world to smart contracts on the blockchain and back again.

The Graph

The Graph Network decentralizes the query and API layer of Web3, removing a tradeoff dApp developers struggle with today: whether to build an application that is performant or to build an app that is truly decentralized.

Today, developers can run a Graph Node on their own infrastructure, or they can build on our hosted service. Developers build and deploy subgraphs, which describe how to ingest and index data from Web3 data sources. Many leading Ethereum projects have already built subgraphs including Uniswap, ENS, DAOstack, Synthetix, Moloch, and more. In The Graph Network, any Indexer will be able to stake Graph Tokens (GRT) to participate in the network and earn fees as well as inflation rewards for serving queries.

Consumers will be able to use this growing set of Indexers by paying for their metered usage, proving a model where the laws of supply and demand sustain the services provided by the protocol.

--

--