How to Build Real-Time Oracles with MagicBlock
Guides & Tutorials

How to Build Real-Time Oracles with MagicBlock

Andy (Developer Relations)
Andy (Developer Relations)
@fauxfire_

I like watching numbers wiggle. In college I’d sit in my lecture, Robinhood open, hypnotized by a single green (more often red) line. So I built a viewer reading Pyth Lazer data directly from MagicBlock’s Ephemeral Rollup at 50-200 ms per tick (asset-dependent). I used to trust a chart with cartoon confetti cannons, now I trust bytes.

my new favorite pastime

(demo here - watch for a little easter egg on the chart 🙂) 

What Are Onchain Oracles?

the oracle of MagicBlock, bringing truth to the world of Ephemeral Rollups

Oracles move facts from the outside world onto a blockchain in a way smart contracts can verify. This could be anything from the price of Bitcoin to the price of eggs, or even events like the winner of the US presidential election. 

On Solana, most oracles are just programs that keep Solana accounts updated. When a smart contract needs to know the price of Bitcoin, it doesn’t “call an api”, it simply reads the data of a known oracle account. We use Pyth, one of the most popular oracle networks across chains.

Why Oracles Matter:

  • Finance: things like liquidations, perps funding, or TWAPs. Bad quotes leak money or leave you open to exploits
  • Games: settle sports bets, or link your in-game weather to whatever’s happening in downtown London
  • Composability: once prices are just another on-chain input, other programs can build on them confidently.

If you can’t trust the number - or can’t get it fast enough - you’re flying blind.

Oracles on MagicBlock

At a high level, the oracle flow on MagicBlock is similar to most onchain oracles. A pusher writes into on-chain accounts that can be composed upon. MagicBlock supercharges this flow by updating at an industry leading speed - as low as 50ms per update (asset-dependent) as compared to Solana’s 400ms. This is critical for infrastructure like liquidations and copy-trading - where the faster you can react, the less money you lose + the more money you make.

The two pieces of an oracle are:

  • Data Source: This is the source of truth. We can also ingest arbitrary feeds - on or off chain - so that you can surface prices for assets Pyth doesn’t cover yet (e.g new PumpFun or Raydium Rtokens) into Ephemeral Rollups.
  • Chain Pusher: A component that takes the data source, processes it, and pushes it onchain. MagicBlock’s chain pusher is open source here.

Flow:

  1. MagicBlock receives updates on the price of Pyth Lazer assets at fixed intervals, 50ms or 200ms depending on the asset. The full asset list and intervals can be found here.
  2. The MagicBlock chain pusher pushes the price of the assets to predefined onchain accounts
  3. Any contract looking to use this information can read directly from the associated account.

Implementation

The implementation here is pretty straightforward, since the only thing we are doing is reading from an onchain account. 

Deriving the Price Feed Account

In order to derive the price feed account, we find the PDA of our price account, using the following seeds:

  • The buffer of ‘price feed’
  • The buffer of ‘pyth-lazer’
  • The buffer of the feedID (found here): this is the asset you want
  • The price program ID: ‘PriCems5tHihc6UDXDjzjeawomAwBduWMGAi8ZUjppd’

Parsing the Account

The account on chain is in the following format. In order to use it, we need to take the raw bytes of the account, and correctly decode it into the format we want.

In this case, we’re interested in the raw price, so by doing some math, we’re able to get the price offset as 73 bytes, which means that it is 73 bytes from the beginning of the raw account data. From there, we know that it is an int64 number, so we can directly decode it. Finally, we apply the exponent to the raw int64 number that we got, giving us the price.

You can find the open source implementation of this here.

Wrapping Up 

Oracles are a fundamental blockchain primitive. You shouldn’t need to worry about creating and managing your own to keep your app running - you can trust MagicBlock and Pyth to do it for you. 

If you’re building something on MagicBlock, Join our Discord - we’d love to see what you’re shipping.