Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 41 ยท Smart Contracts

dApps, Oracles and Wallet Integration

How a decentralized app talks to a node over JSON-RPC, how wallets sign, why oracles are a trust problem, and how indexers and off-chain storage fit in.

A decentralized application (dApp) is an app whose important rules and data live in smart contracts instead of on a company's server. But a contract on its own is just code waiting for transactions. Users need a web page or mobile app to click, a wallet to approve actions, a way to read the current state, and often data from the outside world that the chain cannot see by itself.

This lesson maps the whole picture: front end, node, wallet, contract, oracle, indexer and off-chain storage. Real nodes and wallets cannot run inside this page, so the examples are honest stand-ins written in Python. They use the same message shapes and the same logic, with a tiny fake node instead of a real network.

The architecture of a dApp

flowchart LR
    U["User"] --> F["Front end<br/>(web page)"]
    F -->|"sign request"| W["Wallet<br/>(holds private key)"]
    F -->|"JSON-RPC reads"| N["Node / RPC provider"]
    W -->|"signed transaction"| N
    N --> C["Smart contracts<br/>on the chain"]
    O["Oracle network"] -->|"data feed"| C
    C -->|"event logs"| I["Indexer"]
    I -->|"fast queries"| F
    F -.-> S["Off-chain storage<br/>(IPFS, servers)"]

The front end is ordinary web code; trust-critical parts are the wallet, the contracts and the data feeds.

Notice what is not in the picture: a login server. The user's identity is their address, and permission comes from signatures.

Talking to a node with JSON-RPC

A node keeps a copy of the chain (see Peer-to-Peer Networking and Gossip). Apps rarely run their own node; they use an RPC provider that exposes the node's API over HTTPS or WebSocket. On Ethereum-style chains that API is JSON-RPC 2.0: every request is a small JSON object with a method and params, and the answer carries the same id.

Some common methods:

Method What it does
eth_chainId Which network am I talking to?
eth_blockNumber Height of the latest block
eth_getBalance Native coin balance of an address
eth_call Run a contract function read-only, without a transaction
eth_sendRawTransaction Submit an already signed transaction
eth_getTransactionReceipt Did it get included? What logs did it emit?
eth_getLogs Search event logs by address and topics

Numbers travel as hex strings such as "0x10d4f", because JSON numbers cannot safely hold 256-bit integers.

Python JSON-RPC against a fake node

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In a browser, libraries such as ethers.js or viem hide this plumbing. A read looks like an ordinary async call:

JavaScript
// read-only example, not runnable here
const provider = new ethers.JsonRpcProvider("https://rpc.example.org");
const balance = await provider.getBalance("0x...");
console.log(ethers.formatEther(balance));

Reads are free and need no signature. Only changing state needs a transaction.

Wallets: the thing that signs

A wallet (a browser extension, a phone app or a hardware device) holds the private key. The dApp never sees that key. Instead, the page asks the wallet to do things through a standard browser interface (EIP-1193), which wallets inject as window.ethereum:

JavaScript
// read-only example, not runnable here
const [account] = await window.ethereum.request({ method: "eth_requestAccounts" });
await window.ethereum.request({
  method: "eth_sendTransaction",
  params: [{ from: account, to: contractAddress, data: encodedCall }],
});

The wallet pops up a window that shows what is being signed and asks the user to approve. This approval step is the user's real security boundary, so a dApp should make requests that are readable and honest, and users should read them.

Besides transactions, a wallet can sign messages. That gives a login without passwords, commonly called "Sign-In with Ethereum": the server sends a random challenge, the wallet signs it, and the server checks that the signature belongs to the claimed address. The challenge must be fresh and bound to the site, otherwise a captured signature could be replayed (see Digital Signatures with a Toy Scheme).

The stand-in below uses an HMAC from Python's standard library as a toy "signature". Real wallets use elliptic-curve signatures, where the verifier needs only the public address. HMAC needs the same secret to verify, so it is only a stand-in for the flow: challenge, sign, verify, reject replays.

Python Wallet sign-in flow (toy signature)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Never ask users, and never type yourself, a seed phrase or private key into a web page. A legitimate dApp never needs it. Phishing sites imitate dApps to collect them, and also trick users into signing "approve" transactions that hand over token allowances (see Tokens, ERC-20 and NFTs).

The oracle problem

A blockchain is a closed world. Every node must reach the same result when it replays a transaction, so a contract cannot call a weather API, read a stock price or check who won a match: different nodes would get different answers at different times, and consensus would break.

An oracle is a service that brings outside facts on chain by posting them in a transaction. That solves the data problem but creates a trust problem: the contract is now only as honest as its data source. A flawless contract fed a wrong price still pays out wrongly. (This is how the manipulation in Smart Contract Security Pitfalls works.)

Common ways to reduce the trust:

  • Many independent reporters. Several nodes fetch the data from several sources; the contract takes the median, so a minority of liars or broken feeds cannot move it.
  • Freshness checks. The contract rejects data older than a limit (a heartbeat), so a stalled feed is noticed.
  • Deviation and bounds checks. Sudden jumps are suspicious.
  • Economic incentives. Reporters stake money that can be taken away for false data, and disputes can be raised during a challenge period (an "optimistic" oracle).
  • Pull oracles. The user supplies a signed data packet along with the transaction, and the contract verifies the signature.
Python Aggregating an oracle price feed

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The median ignores the liar, and the quorum rule refuses to answer when too few reporters are alive. Also notice the plain average, which a single bad report drags far from the truth. Still, no design removes trust completely; it only spreads it and makes cheating expensive.

Oracles also deliver randomness (a contract cannot generate secure random numbers, since everyone must compute the same result), automation triggers (call this function every day) and cross-chain messages.

Indexing: making chain data usable

Blockchains are built for agreement, not for queries. Asking "show every transfer involving this address, newest first" by scanning blocks would be painfully slow. So dApps use indexers: services that follow the chain, read each block's event logs (the Transfer events from the token lesson), and store them in an ordinary database that supports filtering and sorting. Block explorers work this way, and so do subgraph-style indexing services.

That is why contracts emit events even when no other contract reads them: logs are cheap, searchable and perfect for building a history.

Python An indexer building a balance table from events

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The indexer is a convenience layer. It can lag behind or be wrong, so valuable decisions should be verifiable against the chain itself.

Off-chain storage

Putting large data on chain costs a lot, and everyone replicates it forever. dApps keep big files elsewhere and store only a small fingerprint or pointer on chain:

  • Content-addressed storage (IPFS and similar): the address is the hash of the content, so the file cannot silently change. Someone still has to keep it available ("pinning").
  • Permanent storage networks that charge once for long retention.
  • Ordinary servers or cloud storage, which are simple but reintroduce a party who can edit or delete files.

Using the Merkle idea from Merkle Trees and Proofs of Inclusion, a contract can store a single root hash for thousands of records and verify any one record against it later.

Where the trust really sits

When you evaluate a dApp, ask these questions instead of just "is it on a blockchain?":

  1. Which parts are contracts, and can an admin upgrade or pause them?
  2. Where do prices and other facts come from?
  3. Is the front end hosted by a company that could change what you sign?
  4. Which indexer or RPC provider does it rely on, and what happens if that provider lies or disappears?
  5. Where are the files stored, and could they change?

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why do JSON-RPC responses for chain data use hex strings such as "0x10d4f"?

    Values like balances in wei can be up to 256 bits. Standard JSON numbers lose precision, so quantities are sent as hex strings.
  2. 2. What does the front end of a dApp send to the wallet?

    The wallet keeps the private key. The page only asks for signatures, and the wallet shows the request for the user to approve.
  3. 3. Why can a smart contract not simply fetch a price from a web API?

    Every node must replay the same transaction and get the identical result. External data therefore has to be put on chain by a transaction that everyone sees.
  4. 4. Why does an oracle design often take the median of several reports?

    A median is robust to outliers. As long as most reporters are honest, the result stays near the truth, unlike an average that one bad value can drag away.
  5. 5. What is the main job of an indexer?

    Chains are poor at queries such as "all transfers of this address". Indexers copy event data into searchable databases.

Summary

  • A dApp is a normal front end plus contracts; users are identified by addresses and approve actions with wallet signatures.
  • Apps read and write through a node's JSON-RPC API; reads are free, state changes need signed transactions.
  • Wallets keep private keys and show each request for approval; never share seed phrases or sign what you do not understand.
  • Contracts cannot reach the outside world, so oracles bring data on chain, which introduces a trust problem that is reduced with many reporters, medians, freshness checks and staking.
  • Indexers turn event logs into queryable databases; large files live off-chain with a hash or pointer on chain.
  • Judge a dApp by its trust assumptions: upgrade rights, data sources, front-end hosting and storage.

Next: Bitcoin in Practice, where we look at a real network's rules and numbers.