Skip to main content

fallback()

Write web apps in Solidity. Serve HTTP over Ethereum.

fallback() is a Solidity web framework / a proof-of-concept implementation of HTTP over Ethereum. For more, see How it Works or the Live Demo (on Goerli Optimism).

Try It

Write a fallback()-based web app and run it in your browser (support for Web Workers API required).

1. Configure a route handler

The path to write a handler for
The name of the route handler function in the web app contract
The content of the HTTP response

2. View generated web app contract

MyApp.sol
import {HttpConstants} from "https://github.com/nathanhleung/fallback/blob/main/src/http/HttpConstants.sol";
import {HttpMessages} from "https://github.com/nathanhleung/fallback/blob/main/src/http/HttpMessages.sol";
import {WebApp} from "https://github.com/nathanhleung/fallback/blob/main/src/WebApp.sol";

contract MyApp is WebApp {
constructor() {
routes[HttpConstants.Method.GET]["/"] = "getMyPath";
}

function getMyPath(HttpMessages.Request calldata request)
external
pure
returns (HttpMessages.Response memory)
{
return text('Hello world!');
}
}

3. Compile and send request

request.js
const CONTRACT_ADDRESS = "";

const request = "GET / HTTP/1.1";
const result = await web3.eth.call({
to: CONTRACT_ADDRESS,
data: Buffer.from(request).toString("hex");
});
console.log(Buffer.from(result, "hex").toString());
Your browser must support the Web Workers API

4. Decode contract return value

Decoded Contract Return Value
// Compile and run to see response