Skip to content

Connection.layer

The connection of an app, as a layer over its wallets. It starts from what storage held, and asks no wallet anything until you connect.

import { Layer, ManagedRuntime } from "effect";
import { FetchHttpClient } from "effect/http";
import { Connection, Signer, Transport, Wallets } from "@ethect/core";
// The layers of an app, read from the bottom up.
const App = Signer.connected().pipe( // 4. signs with the connected wallet
Layer.provideMerge(Connection.layer()), // 3. which wallet is connected
Layer.provideMerge(Wallets.browser), // 2. which wallets are installed
Layer.provideMerge(Transport.http("https://ethereum-rpc.publicnode.com")), // 1. the node it reads from
Layer.provide(FetchHttpClient.layer),
);
// Built once, for the life of the page: the connection is the one of the runtime.
const runtime = ManagedRuntime.make(App);

An app on several chains gives Transport.chains in place of Transport.http. Nothing else changes.

initial is the state the last visit stored. A stored connection starts as reconnecting: enough to render with, and not yet confirmed by the wallet.

import { Effect, Layer } from "effect";
import { Connection, Wallets } from "@ethect/core";
// `cookies` is `document.cookie`, which the app wrote to with `Connection.toCookie`.
const App = (cookies: string | undefined) =>
Connection.layer({ initial: Connection.fromCookie(cookies) }).pipe(
Layer.provideMerge(Wallets.browser),
);
// In the browser, on load: ask the remembered wallet for its accounts, without a prompt.
const onLoad = Effect.gen(function* () {
const before = yield* Connection.get; // { status: "reconnecting", wallet: "io.metamask", … }
const after = yield* Connection.reconnect; // "connected", or "disconnected" if the wallet is gone
return { before, after };
});

See Connection.toCookie for the cookie, Connection.toJson for what it holds, and Connection.reconnect for what confirms it.

The same layers with no wallet. The state of the cookie is the state, and nothing can sign.

import { Layer } from "effect";
import { FetchHttpClient } from "effect/http";
import { Connection, Signer, Transport, Wallets } from "@ethect/core";
// `cookies` is the `Cookie` header of the request, or `null` when it has none.
const Server = (cookies: string | null) =>
Signer.connected().pipe(
Layer.provideMerge(Connection.layer({ initial: Connection.fromCookie(cookies) })),
Layer.provideMerge(Wallets.of([])),
Layer.provideMerge(Transport.http("https://ethereum-rpc.publicnode.com")),
Layer.provide(FetchHttpClient.layer),
);

Wallets.browser works there too: where there is no window it is an empty list. Building these layers on a server touches no browser API.

A browser wallet announces itself a moment after the page loads. reconnect waits for the remembered one, and gives up after reconnectTimeout. Once the wallet is there, its answer is waited for as long again.

import { Connection } from "@ethect/core";
declare const stored: Connection.State;
const Patient = Connection.layer({ initial: stored, reconnectTimeout: "5 seconds" });

Layer<Connection.Connection, never, Wallets.Wallets>

A layer that provides the Connection service. It needs the Wallets of the app, and building it cannot fail.

What storage held, usually the result of Connection.fromCookie or of Connection.fromJson.

initial is The connection starts as
reconnecting, or connected reconnecting, with the same wallet, accounts and chain
disconnected, connecting, or not given disconnected
  • Type: Duration.Input
  • Default: "2 seconds"

How long reconnect waits, twice over. A number is milliseconds.

It waits for Up to Past it
the remembered wallet to be among the Wallets reconnectTimeout disconnected
then that wallet’s provider and its answer, together reconnectTimeout again disconnected

So a reconnection takes twice reconnectTimeout at most: four seconds by default. It cannot wait without end, and neither can a send that waits for it.

The first wait is only for a list that may still grow. Over Wallets.of, or Wallets.browser where there is no window, a wallet that is not listed ends the reconnection at once.

  • One connection per layer. Build the layer once, in the runtime of the app. Each build has a state of its own.
  • No wallet is asked when it is built. No request, no listener, no prompt: a wallet is first asked by connect or by reconnect.
  • A stored connection is not a connection. It starts as reconnecting, and stays so until reconnect runs. What it holds comes from a cookie: it is good enough to render with, and it proves nothing.
  • One wallet at a time. Connecting another wallet lets go of the one before.
  • Everything goes when its scope closes. The listeners on the connected wallet are removed, a connect that waits for its prompt is interrupted, and a reconnect that waits for its wallet is too.
  1. building the layer

    1. your programthe state is initial, read as above. No wallet is asked
  2. while a wallet is connected

    1. your programlistens to accountsChanged, chainChanged and disconnect on its provider
    2. the walleteth_accountsa disconnect asks it, once: the connection stays if the wallet still has accounts
  3. the layer's scope closes

    1. your programthe listeners are removed

The requests are sent by connect and reconnect and by switchChain.