Wallet providers (Eip1193)
The EIP-1193 provider type, and the provider’s events as streams: account changes, chain
changes, connect and disconnect. To send calls through a provider, use
Transport.eip1193.
import { Effect, Stream } from "effect";import { Eip1193 } from "@ethect/core";
// The wallet's EIP-1193 provider: `window.ethereum`, or the one a wallet SDK returns.declare const provider: Eip1193.Provider;
const watchChain = Eip1193.chainChanged(provider).pipe( Stream.runForEach((chainId) => Effect.log(`now on chain ${chainId}`)),);| Export | Returns | Description |
|---|---|---|
Provider |
a type | An EIP-1193 provider. |
accountsChanged(provider) |
Stream<ReadonlyArray<Address>, Eip1193.Error> |
The accounts the wallet exposes, each time they change. |
chainChanged(provider) |
Stream<number, Eip1193.Error> |
The id of the chain the wallet is on, each time it changes. |
connect(provider) |
Stream<number, Eip1193.Error> |
The chain id, each time the provider becomes able to reach a chain. |
disconnect(provider) |
Stream<RpcError, Eip1193.Error> |
The reason, each time the provider loses every chain. |
listen(provider, event) |
Stream<unknown, TransportError> |
The raw payloads of any event. |
Error |
a type | What an event stream can fail with. |
The streams need no transport and no layer: they listen on the provider you pass.
Provider
Section titled “Provider”The object a wallet injects or a wallet SDK hands out.
import type { Eip1193 } from "@ethect/core";
const provider: Eip1193.Provider = { request: ({ method, params }) => Promise.resolve(null), on: (event, listener) => {}, removeListener: (event, listener) => {},};| Member | Type |
|---|---|
request |
(args: { method: string; params?: unknown }) => Promise<unknown> |
on |
(event: string, listener: (payload: any) => void) => unknown |
removeListener |
(event: string, listener: (payload: any) => void) => unknown |
TypeScript’s DOM types do not declare window.ethereum. The
Wallets guide shows how to type it.
accountsChanged
Section titled “accountsChanged”Emits the accounts the wallet exposes to the app, each time they change. The list is empty when the wallet locks or the user disconnects the app.
import { Effect, Stream } from "effect";import { Eip1193 } from "@ethect/core";
// The wallet's EIP-1193 provider: `window.ethereum`, or the one a wallet SDK returns.declare const provider: Eip1193.Provider;
const watchAccount = Eip1193.accountsChanged(provider).pipe( Stream.runForEach(([account]) => Effect.log(account === undefined ? "disconnected" : `account ${account}`), ),);- Event:
accountsChanged - Parameters:
provider(Provider). - Emits:
ReadonlyArray<Address>. The addresses are validated and lowercase.
chainChanged
Section titled “chainChanged”Emits the id of the chain the wallet is on, each time it changes.
import { Effect, Stream } from "effect";import { Eip1193 } from "@ethect/core";
// The wallet's EIP-1193 provider: `window.ethereum`, or the one a wallet SDK returns.declare const provider: Eip1193.Provider;
const nextChain = Eip1193.chainChanged(provider).pipe( Stream.runHead, // the next change as an Option, then stop listening);- Event:
chainChanged - Parameters:
provider(Provider). - Emits:
number. The provider sends a hex quantity ("0xa") and the stream emits10.
connect
Section titled “connect”Emits the chain id each time the provider becomes able to reach a chain.
import { Effect, Stream } from "effect";import { Eip1193 } from "@ethect/core";
// The wallet's EIP-1193 provider: `window.ethereum`, or the one a wallet SDK returns.declare const provider: Eip1193.Provider;
const online = Eip1193.connect(provider).pipe( Stream.runForEach((chainId) => Effect.log(`connected to chain ${chainId}`)),);- Event:
connect - Parameters:
provider(Provider). - Emits:
number, thechainIdof the event’s payload.
disconnect
Section titled “disconnect”Emits the reason each time the provider loses every chain.
import { Effect, Stream } from "effect";import { Eip1193 } from "@ethect/core";
// The wallet's EIP-1193 provider: `window.ethereum`, or the one a wallet SDK returns.declare const provider: Eip1193.Provider;
const offline = Eip1193.disconnect(provider).pipe( Stream.runForEach((error) => Effect.log(`disconnected: ${error.reason._tag}`)),);- Event:
disconnect - Parameters:
provider(Provider). - Emits:
RpcError. The payload is the provider’s error object, classified like the error of a call: code4900is the reasonDisconnected.
listen
Section titled “listen”Emits the raw payloads of any event of the provider, undecoded. The four streams above are built on it.
import { Stream } from "effect";import { Eip1193 } from "@ethect/core";
// The wallet's EIP-1193 provider: `window.ethereum`, or the one a wallet SDK returns.declare const provider: Eip1193.Provider;
const messages = Eip1193.listen(provider, "message").pipe(Stream.take(1));- Parameters:
provider(Provider), thenevent(string). - Emits:
unknown. It does not decode, so it only fails when the provider cannot be listened to.
Eip1193.Error is what an event stream can fail with: TransportError | CodecError.
| Error | When |
|---|---|
TransportError, reason Unreachable |
the provider cannot be listened to: its on is missing or throws |
CodecError |
the provider emitted a payload of another shape |
import { Effect, Stream } from "effect";import { Eip1193 } from "@ethect/core";
// The wallet's EIP-1193 provider: `window.ethereum`, or the one a wallet SDK returns.declare const provider: Eip1193.Provider;
const chains = Eip1193.chainChanged(provider).pipe( Stream.catchTag("TransportError", () => // a minimal provider without events: carry on with no changes to follow Stream.empty, ), Stream.runForEach((chainId) => Effect.log(`now on chain ${chainId}`)),);Behavior
Section titled “Behavior”- Listening starts when the stream runs and stops when it ends: the listener is removed from the provider. Nothing is registered by building the stream.
- Only changes. A stream emits what happens after it started. Read the current state with
Eth.accountsandEth.chainId. - A payload of the wrong shape fails the stream with a
CodecErrorwhosemethodis the event name.listendoes not decode, so it never fails that way. - A provider without events fails the stream with a
TransportError.onis the part of EIP-1193 a minimal provider leaves out. - Independent of the transport. The streams work without
Transport.eip1193, and the transport does not consume these events.