Oracle Price Feed on StarkEx
dYdX has recently launched their perpetual contracts platform, powered by the StarkEx scalability engine in ZK-Rollup mode (i.e. with on-chain data). StarkEx provides scalability and lower gas costs for users, but it also enables building a better system: implementing new business logic, including new features that were too expensive without a layer-2 solution.
In this blog post we will discuss how StarkEx uses oracle prices in a more effective and secure manner.
Oracles on StarkEx
Oracles are an essential part of any permissionless blockchain, as they allow dApps to rely on data that is external to the blockchain. dYdX is no different: it uses price feeds for the various assets traded on it to keep track of traders’ collateralization ratio.
However, updating oracles is quite expensive. A reliable update is an update that was aggregated from many sources, which means many signature verifications and storage operations on L1. Typically, the cost of such an update could reach millions of gas — a considerable cost that is eventually borne by users.
Costly updates mean less frequent updates, which translate into a less efficient dApp. It is impractically expensive to update the USD-ETH price on-chain every block — which means that dApps that depend on such an update will be slower to respond to price changes. This problem is intensified in times when we need frequent updates the most, namely during periods of rapid price movement. In such times, people are most likely to want to interact with L1, and require up-to-date prices, thus increasing congestion on the blockchain. Congestion means that price updates will unfortunately be less frequent (as happened on Black Thursday). Even though the dYdX StarkEx system has been live for only two weeks, we were fortunate enough to already experience such a stress test: on the morning of April 17, 2021 cryptocurrency prices dropped by as much as 20% — StarkEx smoothly handled over 1,000 liquidations in less than an hour, for a bit over 6K gas/liquidation.
With StarkEx, dYdX could dramatically increase the frequency and number of price oracles, thus better protecting the feed from price manipulations. We’re currently using Chainlink’s oracle network to power secure price feeds for our Layer 2 protocol. Chainlink has integrated with Starkware to extend the Chainlink Network’s oracle functions to high-performance Layer 2 solutions. In a short time, other oracle providers, such as MakerDAO, may be used for some markets. These affordable, frequent and reliable price updates are what allows dYdX to offer higher leverage for traders.
How Does it Work?
First, the details of a list of parties responsible for supplying off-chain price feeds to dYdX (alongside the required quorum) are hashed and stored in dYdX’s smart contract. Traders will soon be able to see the preimage on dYdX’s website and decide whether they trust these entities and quorum or not — giving the same transparency as an L1 smart contract.
Let’s take a look at how we enable frequent updates in a cost-effective way. First, eligible parties submit — off-chain! — signed prices. dYdX aggregates these inputs and calculates the median.
This entire set of signatures, as well as the median, are then sent to StarkEx, where the dYdX Cairo program checks that the following holds:
- Input validity: Signatures are valid, and all public keys belong to eligible parties in the list that appears on-chain
- Timestamps are sufficiently fresh
- There is a quorum of valid public key signatures
- The median price was calculated correctly
Assuming the above properties do indeed hold, a STARK-proof for the correct execution of the Cairo program is generated. The STARK proof is sent on-chain, where — if it is verified — a single number is stored (the average between the minimal and maximal price used in the batch). Worth noting: the single price stored per asset per batch, is independent of the number of off-chain providers feeding it, or the frequency of their reporting during the batch.
Now the price can be used immediately in subsequent off-chain transactions, even before the proof is verified on-chain. Important to note that the correctness of the price tick is guaranteed by the STARK proof, meaning that if the price is invalid, the batch that includes it and all transactions that rely on this invalid price, are rejected.
A Note on OR & Price Oracles
Unlike ZK-Rollups, Optimistic Rollups require publishing all oracle prices related data on-chain. This means that in OR, gas spending grows linearly with the frequency of price updates and the number of off-chain price providers.
As L2 scalability solutions become more and more popular and sophisticated, it is exciting to see how they not only scale up existing L1 capabilities, but also unlock completely new features and business logic. We hope that in the near future more dApps will rely on StarkEx, and soon on StarkNet, to offer users more scalable, secure, reliable & real-time services.