10 - Oracles Revisited

Oracles in DeFi

Oracles are an essential part of DeFi since finance cannot exist in a vacuum without information. Oracles are bridges between a blockchain and the real world. They are used as queriable on-chain APIs to get information into smart contracts. The data could be anything from price information, weather reports, random numbers or more. Oracles can also be bi-directional and can “send” data out to the real world. They are further described here.

Many top DeFi applications by total value locked use oracles in some manner. Some of the most popular use cases are collecting pricing data, event-driven decentralized execution, and random number generation.

Some popular oracle sources are Chainlink and Tellor. Chainlink is described here. Protocols like Uniswap and Maker can also act like oracles, since they can provide data from their feeds. In fact Compound Finance uses Uniswap v2 data in conjuction with Chainlink.

Oracles in DeFi are used for a wealth of different reasons:

  1. To get the value of underlying collateral (such as Aave, Synthetix, Compound)
  2. To generate a random lottery winner (such as PoolTogether
  3. Margin Trading (such as SushiSwap
  4. Execute decentralized event-driven tasks, so that your contracts react to events! The oracle can execute your contracts if a certain event happens.
  5. Verify a stablecoin’s collateral
  6. Fetch Cryptocurrency prices
  7. And more.

For many of these DeFi services to work and not be exploited, they need to be connected to the real world to operate correctly. As a result, oracles are used within the popular lending or swaps protocol infrastructure—for example, Bancor uses Chainlink to mitigate impermanent loss.

Oracle Attacks in DeFi

When looking to get the price of assets, many DeFi users mistake using their own liquidity pools to price an asset or using a poor oracle source. An architecture where a protocol relies on itself to get the value of an underlying asset is a target for attack. Attackers can use flash loans to manipulate the price in their liquidity pools, and therefore ruin any computation using the liquidity pools for pricing. Flash loans are discussed later in the DeFi Lending lesson.

Decentralized exchange data can be manipulated, so they are not good decentralized data sources and don’t work as decentralized oracles. However, there are exceptions. For example, Uniswap takes the average price across a time period of around 30 minutes and uses that as the price. This is known as getting the TWAP (Time Weighted Average Price).

Using a more robust decentralized solution like Chainlink will protect you from these oracle manipulation / flash loan attacks.

A further look into Oracles

You can create your own oracles. To understand how to use oracles in general, here is a good starter tutorial. Check out Chainlink’s tutorial for a deeper dive and their video walk through.

Creating a highly robust oracle requires much thought because oracle data inputs directly determine the consuming smart contracts’ outputs. This leads to the Oracle Problem. Hence, oracles need to be as decentralized, secure and reliable as the blockchain networks that run them. If not, it’s pointless to use a blockchain network. Single centralized nodes are subject to single points of failure if a node becomes corrupted or goes offline. Decentralization and [having multiple data providers is essential since it’s difficult to know if a provider is trustworthy, introducing risk. The data source could go offline or deliver bad data. You can get a pretty comprehensive look into decentralized oracles here.

Indexers

As mentioned, Oracles can provide data about the real world. Yet smart contracts can create data themselves, which may be important to a protocol. With all this internal chain data, how do you organize and find it? We use Indexers, which are decentralized methods to collect, organize and query information. Think of them as what Google does for information, but for on-chain data. This allows for the easy querying of data.

Ethereum is a networked decentralized database. Yet to query data, we have to look at event logs. Wouldn’t it be better if we had a language we could query data with easier? We can now use TheGraph Protocol, a decentralized protocol for indexing and querying blockchain data that uses GraphQL as query language. Think of it as the “Google of blockchains” since indexing and sorting are what search engines do. TheGraph solves the problem of querying on-chain data as dapps grow in size. TheGraph Protocol is explained very well here.

[GraphQL] allows for an easier way of querying of data compared to REST APIs. Instead of getting the whole dataset, GraphQL allows for tailoring. GraphQL enables you to send queries to get precisely the data you’re looking for in one request instead of working with strict server-defined endpoints. Luckily, it builds upon knowledge of REST, and the basics picked up quickly.

You can learn how to use TheGraph protocol via their developer docs and developer academy.

Indexers and Oracles

You can use TheGraph and ChainLink described Chainlink’s description, EthGlobal’s TheGraph Protocol workshop and TheGraph’s blog.

Additional Resources