Skip to content

Let the user pick a wallet

A wallet picker in React: the list of installed wallets, a click that connects, and a state that follows the wallet. There is no React package: the hooks the picker uses are in one file, which you copy into your app. If you come from wagmi, this table names each piece.

WalletPicker.tsx
import { Address, Connection } from "@ethect/core";
import { run, useConnection, useWallets } from "./ethect";
export function WalletPicker() {
const wallets = useWallets(); // fills in as wallets announce themselves
const connection = useConnection({ status: "disconnected" }); // follows the wallet
if (connection.status === "connected") {
return (
<p>
{Address.checksum(connection.accounts[0])} on chain {connection.chainId}{" "}
<button onClick={() => void run(Connection.disconnect)}>Disconnect</button>
</p>
);
}
return (
<ul>
{wallets.map((wallet) => (
<li key={wallet.id}>
<button onClick={() => void run(Connection.connect(wallet))}>
{wallet.icon && <img src={wallet.icon} alt="" width={20} height={20} />}
{wallet.name}
{connection.status === "connecting" && connection.wallet === wallet.id && "…"}
</button>
</li>
))}
</ul>
);
}

Three pieces, one after the other. The picker reads the first two and never names the third.

Three layers, each over the one before. Wallets is the list of what is installed. Connection is the one wallet the user clicked, with its accounts and its chain. Signer.connected sends every signature to that wallet.

Each piece is a layer, built once. A runtime holds them and runs what the components ask for.

ethect.tsx, the second tab above, is the whole of it. If you have never seen Effect, three words are enough here:

Word Is In this file
effect a description of work. Nothing happens until it is run Connection.connect(wallet), run by run
stream values over time Wallets.changes and Connection.changes, read by useStream
layer a part of the app, built once and shared the numbered lines of App

useWallets() is a list of Wallets.Wallet: an id that is the same on every visit, a name, an icon to use as the src of an image, and a provider that nothing asks for until the user clicks.

  • Each installed wallet announces itself to the page (EIP-6963), so several are listed side by side.
  • The list starts empty and grows. Installed wallets answer at once, and one that loads later is added when it does. A component that mounts late gets the whole list.
  • Listing asks the wallets nothing. No request, no prompt, no permission.

A click runs Connection.connect(wallet): that wallet opens its prompt, and no other wallet is asked anything. The state says how it went, so the picker stores nothing.

The user The state becomes run resolves with
clicks a wallet connecting, with the id of that wallet nothing yet
approves connected, with the accounts and the chain a success: that state
refuses what it was before the click a failure: SignerRejected
clicks another wallet meanwhile connecting, with the new wallet an interruption, for the first click

run never throws, and its promise never rejects: it resolves with an Exit, a success or a failure. To show a refusal, turn each outcome into a sentence before you run it.

connect.tsx
import { Effect, Exit } from "effect";
import { Address, Connection, type Wallets } from "@ethect/core";
import { run } from "./ethect";
const connect = (wallet: Wallets.Wallet) =>
Connection.connect(wallet).pipe(
Effect.map((state) => `Connected as ${Address.checksum(state.accounts[0])}`),
Effect.catchTag("SignerRejected", () => Effect.succeed("You refused the connection")),
Effect.catch((error) => Effect.succeed(error.message)), // every failure has a message
);
// `show` is the `setState` of a line under the picker.
export const onPick = (wallet: Wallets.Wallet, show: (text: string) => void) =>
run(connect(wallet)).then((exit) => {
if (Exit.isSuccess(exit)) show(exit.value); // not a success: another click took over
});

The user changes account or chain in the wallet, not in your page. The wallet tells the page, the state changes, and every component that calls useConnection renders again.

In the wallet, the user The state becomes
picks another account connected, with that account first in accounts
picks another chain connected, with the new chainId
removes your site disconnected
  • connection.accounts[0] is the account to show. A connected state always has one. It is lowercase, as every Address in memory: show Address.checksum of it, its EIP-55 form.
  • chainId is where the wallet is. Your reads still go to your node. A button brings the wallet back: Connection.switchChain(1).
  • A reload forgets the connection. A cookie keeps it, server or not.
  • Signing names no wallet. run(Tx.send(call)) goes to the connected one: see A button with steps.
  1. your programthe page loads: listens to "eip6963:announceProvider", dispatches "eip6963:requestProvider"
  2. the walleteth_requestAccountsa click on a wallet: the prompt, to that wallet alone
  3. the walleteth_chainIdthen
  4. your programthen listens to accountsChanged, chainChanged and disconnect on it
  5. your programa change in a wallet: no request, the event carries the accounts or the chain
  6. your programDisconnect: no request, the listeners are removed, and the wallet keeps its permission

Your node is never asked. See Wallets, Connection.connect and Connection.State.