EIPs Explained

ERC-4804 · Final · ERC · 2022-02-14

ERC-4804: Web3 URL to EVM Call Message Translation

Defines web3:// URLs that map directly to read-only EVM calls, so a browser-like client can fetch on-chain content such as HTML or SVG without a Web2 gateway.

What problem does it solve?

Reading data from a contract usually goes through someone's Web2 server: a dApp website, an RPC provider or a block explorer turns your request into an eth_call and shows the result. Users cannot control that layer, and it does not fit with web standards that expect a URL.

ERC-4804 defines a URL scheme, web3://, that turns a human-readable address straight into an EVM call message. A client that understands it can load content stored or rendered on-chain, such as HTML, SVG or JSON, the way a browser loads https://. SVG and HTML documents can also link to these URLs.

How it works

The general shape is:

web3://[userinfo@]contractName[:chainId]/method/arg0/arg1?returns=(types)
  • contractName is either a 0x address or a name-service name such as example.eth (ENS).
  • chainId is optional. It defaults to the name service's chain (1 for .eth), or 1 when there is no name service.
  • userinfo sets the from address of the call. The default is the zero address.
  • Only read-only (view) calls are defined.

The target contract can pick a resolve mode by implementing resolveMode():

  • Auto mode (the default, also used when resolveMode is missing). An empty path calls the contract with empty calldata. Otherwise the first path segment is the method name and the rest are its arguments, ABI-encoded. You can set a type with type!value. If you don't, it is inferred: a number becomes uint256, 32-byte hex becomes bytes32, 20-byte hex becomes address, other hex becomes bytes, and anything else is treated as a name to resolve to an address.
  • Manual mode. The raw path and query are passed as calldata, and the contract parses them itself. This is useful for serving a whole website from one contract.

The returns query parameter controls decoding. With no returns, the result is treated as bytes and the MIME type comes from the file extension of the last argument. returns=() gives raw bytes as JSON, and returns=(uint256) decodes to JSON values.

Sample code

Example URLs:

web3://0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48/balanceOf/vitalik.eth?returns=(uint256)
web3://example.eth/tokenSVG/uint256!42
web3://example.eth:5/

The first one resolves vitalik.eth through ENS, calls balanceOf(address) on the USDC contract, and returns something like ["10000000000000"].

What a client does for that first URL in auto mode (ethers v6):

import { Contract, JsonRpcProvider } from "ethers";

const provider = new JsonRpcProvider(process.env.RPC_URL); // chain 1
const who = await provider.resolveName("vitalik.eth");      // auto-detected as address
const c = new Contract("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
  ["function balanceOf(address) view returns (uint256)"], provider);
console.log(JSON.stringify([(await c.balanceOf(who)).toString()]));

Things to know

  • Status: Final, but a revised specification exists. ERC-6860 (Draft) is a newer version of the web3:// URL to EVM call translation, and ERC-6821 covers ENS name resolution for web3 URLs. New implementations should check those.
  • A few of the examples in the original text are inconsistent (mismatched method names and addresses), so rely on the grammar and rules rather than the examples.
  • Type auto-detection can guess wrong, for example a numeric string meant as a string. Use explicit type!value when it matters.
  • Calls are read-only. Writing state through web3:// is left for future extensions.

#url #web3-url #ens #frontend #read-only

Details

Authors
Qi Zhou (@qizhou), Chao Pi (@pichaoqkc), Sam Wilson (@SamWilsn)
Status
Final
Type
Standards Track / ERC
Created
2022-02-14
Requires
Required by

Machine-readable: Markdown · JSON