Wallet connection (Connection)
Which wallet an app is connected to, with which accounts, on which chain. It is one state, as
plain data: it can be stored in a cookie and rendered from on a server.
Signer.connected signs with the wallet of that state,
whichever it is at the time.
import { Effect, Layer } from "effect";import { Connection, Wallets } from "@ethect/core";
// The connection of an app, over the wallets of the browser.const App = Connection.layer().pipe(Layer.provideMerge(Wallets.browser));
const program = Effect.gen(function* () { const [wallet] = yield* Wallets.get; if (wallet === undefined) return yield* Connection.get; // no wallet is installed return yield* Connection.connect(wallet); // the wallet prompts // { status: "connected", wallet: "io.metamask", accounts: ["0x…"], chainId: 1 }}).pipe( // the user closed the prompt: the state is as it was Effect.catchTag("Rejected", () => Connection.get),);
const state = await program.pipe(Effect.provide(App), Effect.runPromise);| Export | Kind | Description |
|---|---|---|
Connection.layer(options?) |
layer | The connection of an app, over its Wallets. initial is what storage held. |
Connection.get |
effect | The state now. |
Connection.changes |
stream | The state now, and again each time the user changes account, chain or wallet. |
Connection.connect(wallet) |
function | Asks the wallet for its accounts. The user sees a prompt. |
Connection.reconnect |
effect | Asks the remembered wallet for its accounts, without a prompt. |
Connection.disconnect |
effect | Ends the connection. |
Connection.switchChain(chain) |
function | Asks the wallet to change chain. With a Chain, a wallet that does not know it is offered to add it. |
Connection.session |
effect | The wallet to sign with: its provider, for a request that is the wallet’s alone. |
Connection.toCookie(state, options?) |
function | The state as a cookie: the text to give to document.cookie, or to send as Set-Cookie. |
Connection.fromCookie(cookies, options?) |
function | Reads it back from document.cookie or a Cookie header. It never fails. |
Connection.toJson(state) |
function | The state as text, for local storage or a framework that holds the cookie. |
Connection.fromJson(text) |
function | Reads it back. It never fails. |
Connection.Connection |
service | The connection: what the functions above go to. |
Connection.State |
type | Any of the four states below. |
Connection.Session |
type | The connected wallet, for as long as it is: its provider and a scope. |
The three pieces
Section titled “The three pieces”Each is a layer, and each needs the one above it:
Wallets, Connection.layer and
Signer.connected.
The states
Section titled “The states”| Type | status |
Holds |
|---|---|---|
Connection.Disconnected |
"disconnected" |
nothing |
Connection.Connecting |
"connecting" |
wallet: the one that is asking the user |
Connection.Reconnecting |
"reconnecting" |
wallet, accounts, chainId: read from storage, not confirmed by the wallet yet |
Connection.Connected |
"connected" |
wallet, accounts with the active one first, chainId |
Connection.State says what moves the state from one to the
other.
Two chains
Section titled “Two chains”- The chain of the program is the chain of its
Transport: the one of a single-chain app, or the one named withTransport.on. Reads go to that node, and a transaction is for that chain. - The chain of the wallet is
chainIdin the state. The user changes it in the wallet.
Nothing here changes the chain of the program to follow the wallet. An app that wants that
writes Transport.on(state.chainId). When a send is for another chain than the wallet’s,
Signer.connected asks the wallet to switch, or fails.
The service
Section titled “The service”Connection.Connection is the service that Connection.layer
provides. Each function of the module is one of its members.
| Member | Is |
|---|---|
get, changes |
the state now, and the stream of it |
connect(wallet), reconnect, disconnect, switchChain(chain) |
the functions of the same name |
session |
the wallet to sign with: an Effect<Connection.Session, Signer.Error> |
session
Section titled “session”- Type:
Effect<Connection.Session, Signer.Error, Connection.Connection>
The wallet to sign with. It is what a signer that follows the connection uses, and what a
program uses for a request that is the wallet’s alone, such as
wallet_watchAsset.
import { Effect } from "effect";import { type Address, Connection, Transport } from "@ethect/core";import { Wallet } from "@ethect/core/rpc";
const watch = (address: Address.Address) => Effect.gen(function* () { const { provider } = yield* Connection.session; const asset = { type: "ERC20", options: { address, symbol: "USDC", decimals: 6 } } as const; return yield* Wallet.watchAsset(asset).pipe(Effect.provide(Transport.eip1193(provider))); });Field of Connection.Session |
Type | Is |
|---|---|---|
provider |
Eip1193.Provider |
the provider of the connected wallet |
scope |
Scope |
closed when that wallet is no longer the connected one: what listens to it listens in there |
- While the state is
reconnecting, it waits for a runningreconnectto end. - With no wallet connected, it fails with
SignerError"Disconnected".
Under the hood
Section titled “Under the hood”Only the wallet is asked, and only by what connects or changes chain.
Connection.connect(wallet)- the wallet
eth_requestAccountsthe prompt - the wallet
eth_chainId - your programthen listens to
accountsChanged,chainChangedanddisconnect
- the wallet
Connection.reconnect- the wallet
eth_accountsno prompt - the wallet
eth_chainId
- the wallet
Connection.switchChain(chain)- the wallet
wallet_switchEthereumChainthenwallet_addEthereumChain, for aChainthe wallet does not know - the wallet
eth_chainIdwhere the wallet is afterwards
- the wallet
Connection.get,Connection.changes,Connection.disconnect- your programno request: the state is kept, and the wallet’s events keep it true
See Connection.connect,
Connection.switchChain and
Connection.State.