BTC…ETH…ETH gas…Fear & Greed…Market data · not advice

Academy School · Chapter 10 of 14

Web3 Infrastructure

The hidden services that make blockchains usable at scale.

10 minute lesson4 required questionsFull score to continue
Course progress0 / 14

← Academy School overview

An application depends on an RPC connection, an execution network and settlement.

Oracles and Offchain Data

A smart contract cannot load a web page or call a price API. Every node must re-run every transaction and reach exactly the same result, and an outside website might answer differently from one node to the next. So any external fact (an ETH price, a match result, a reserve figure) has to be written onto the chain as data inside a transaction. An oracle is the system that does that writing: it brings offchain data onchain, and every contract that reads it trusts whatever it reports.

How a price feed decides when to update

Many price feeds collect prices from several independent node operators and publish an aggregate, such as the median. Each write costs gas, so feeds do not update every second. They use two triggers, configured separately for each feed: a deviation threshold and a heartbeat. Suppose a feed is set to 0.5 percent and one hour, and the last published price is 2,000. A new value is written as soon as the market passes 2,010 or drops below 1,990, or when an hour has passed with no such move. Between updates the onchain price can sit up to 0.5 percent away from the market. A lending protocol has to budget for that gap with a liquidation buffer wider than the threshold, and should reject any price older than the heartbeat.

When the oracle reads a thin market

On 11 October 2022 an attacker on Mango Markets, a Solana trading and lending venue, drove up the price of its MNGO token on the thin spot markets the oracle relied on. The oracle reported that inflated price faithfully, the platform valued the attacker's position at it, and the attacker borrowed more than $100 million against it. Nothing in the oracle was hacked. It accurately reported a market that was cheap to move. The oracle manipulation entry covers other versions of this attack.

Core idea

For any contract that relies on a price, ask three things: which sources feed it, how stale it can get (threshold and heartbeat), and what it would cost to move those sources. If moving the source costs less than the protocol would lend against it, the design is fragile.

RPC Nodes and Access

Your wallet does not contain the blockchain. To show a balance or send a transaction it talks to a node using JSON-RPC, a simple request and response format. A node that answers these requests for applications is called an RPC node or endpoint. It provides access to blockchain data and is the route by which your signed transactions are submitted to the network.

What a balance request actually returns

To show your ETH, the wallet sends the method eth_getBalance with your address. The node replies with a hexadecimal count of wei, the smallest unit, where 1 ETH is 10^18 wei. A reply of 0x1bc16d674ec80000 converts to 2,000,000,000,000,000,000 wei, which is 2 ETH. A call to eth_chainId that returns 0x1 confirms the endpoint is serving Ethereum mainnet. When you send, the wallet signs the transaction locally and passes the signed bytes to eth_sendRawTransaction, and the node returns a transaction hash. The node never needs your private key, so any page asking for a seed phrase to "fix a connection" is a scam.

When the gateway fails

On 11 November 2020 a consensus bug split Ethereum. Nodes running older versions of the Geth client, including those of Infura, a large hosted RPC provider, followed a minority chain. Applications relying on Infura showed stale or wrong data, and several exchanges, Binance among them, paused ETH withdrawals, while the majority chain carried on producing blocks. The chain was healthy; the access layer was not.

If a balance ever looks wrong, confirm the network and chain ID, compare the latest block number with a second endpoint, and look up the address on an independent explorer before assuming funds are gone. Running your own node removes the dependency and keeps your queries private, at the cost of disk space and maintenance.

Core idea

An RPC endpoint can show you wrong data and can see which addresses you look up, but it cannot spend your coins. Check any surprising number through a second, independent source before acting on it.

Indexing and Data APIs

A node stores the chain in a form built for verifying blocks, not for answering questions such as "every USDC transfer this address has ever received". There is no RPC method that answers that directly. Indexers make chain data searchable and usable: they read every block, decode the events that contracts emit, and write them into an ordinary database that applications can query quickly.

Worked example: why the index exists

Every ERC-20 transfer emits a Transfer event whose first topic is the same identifier, 0xddf252ad...b3ef, the keccak-256 hash of Transfer(address,address,uint256). Without an indexer you would find them with eth_getLogs, one block range at a time. Ethereum has a 12-second slot, so at most 7,200 blocks a day and about 2.6 million a year. If an endpoint caps each log request at 10,000 blocks (limits differ by provider), one year of history takes 263 requests, and that covers only one token. An indexer does that scan once, keeps pace as new blocks arrive, and answers your question in milliseconds.

Where indexed data goes wrong

  • Reorganisations. A recent block can be replaced. Ethereum blocks are normally finalised after two epochs, about 13 minutes, so an indexer must roll back data from blocks that do not survive. See transaction finality.
  • Decoding errors. Wrong decimals or a misread event turn real transfers into wrong amounts.
  • Labels. Tags such as "exchange", "whale" or "fund" are the indexer's own inference, not onchain fact, and two providers can disagree.
Core idea

Indexed data is a derived copy. Anything you would act on (a large transfer, a balance, a label) should trace back to a transaction hash and block number you can check on the chain itself.

MEV and Mempool Dynamics

Before a transaction is included in a block it usually waits in the public mempool, where anyone running a node can see it. Whoever assembles the next block decides which transactions go in and in what order. Maximal extractable value (MEV) is the profit available from that transaction ordering, inclusion or exclusion. The 2019 paper Flash Boys 2.0 introduced the idea as "miner extractable value"; it was renamed after Ethereum moved to proof of stake on 15 September 2022, since validators now propose blocks. Under proposer-builder separation, much of that block building is now done by specialised builders who bid for the right to have their block proposed.

Worked example: a sandwich

A pool holds 200 ETH and 600,000 USDC (fees ignored for clarity). You submit a swap of 30,000 USDC. On its own it would return 9.524 ETH. You set a 2 percent slippage tolerance, so the contract will accept as little as 9.333 ETH.

  1. A searcher sees your swap in the mempool and places a 6,000 USDC buy just before it, receiving 1.980 ETH and pushing the price up.
  2. Your swap executes at the worse price and returns 9.341 ETH. That is above your minimum, so it does not revert.
  3. The searcher sells its 1.980 ETH straight after you and receives 6,605.53 USDC.

The searcher's gross profit is 605.53 USDC before gas and payments to the builder, and you received 0.183 ETH less than you would have alone. Your tolerance set the size of the opportunity: the wider it is, the larger the front-run that still passes. Not all MEV harms users, though. Arbitrage that pulls a pool back in line with other markets, and liquidations that keep lending protocols solvent, are MEV too. The MEV entry covers private submission routes, which hide a transaction from the public mempool but add trust in whoever runs the route.

Core idea

Your slippage tolerance is the most you are offering to anyone who can reorder your trade. Set it from the price impact you have calculated, not from an app's default.

Further reading

On Ledgers Academy

Primary sources