Transport.eip1193
A transport over an EIP-1193 provider, such as the object a browser wallet injects. Calls go
to the provider’s request function, and subscription items come from its message events.
import { Effect } from "effect";import { type Eip1193, Transport } from "@ethect/core";import { Wallet } from "@ethect/core/rpc";
// The wallet's EIP-1193 provider: `window.ethereum`, or the one a wallet SDK returns.declare const provider: Eip1193.Provider;
const Injected = Transport.eip1193(provider);
const accounts = await Wallet.requestAccounts().pipe( Effect.provide(Injected), Effect.runPromise,);The layer needs nothing from the platform: the provider is the connection.
Every method, not only the wallet’s
Section titled “Every method, not only the wallet’s”A wallet forwards the calls it does not answer itself to its node, so the
Eth methods work over the same transport.
import { Effect } from "effect";import { type Eip1193, Transport } from "@ethect/core";import { Eth, Wallet } from "@ethect/core/rpc";
// The wallet's EIP-1193 provider: `window.ethereum`, or the one a wallet SDK returns.declare const provider: Eip1193.Provider;
const program = Effect.gen(function* () { const [account] = yield* Wallet.requestAccounts(); const chainId = yield* Eth.chainId(); return { account, chainId };}).pipe(Effect.provide(Transport.eip1193(provider)));In a test
Section titled “In a test”MockProvider is an EIP-1193 provider that answers from
handlers, so the same transport runs a program without a wallet.
import { Effect } from "effect";import { Transport } from "@ethect/core";import { Eth } from "@ethect/core/rpc";import { MockProvider } from "@ethect/core/testing";
const provider = MockProvider.make([ MockProvider.handler(Eth.chainId, () => Effect.succeed(1)),]);
const chainId = Eth.chainId().pipe(Effect.provide(Transport.eip1193(provider)));Seeing what the wallet answers
Section titled “Seeing what the wallet answers”A wallet’s requests never show in the Network tab of a browser. Each one is logged at the
Debug level once it is answered: set the minimum level to "Debug" to see them.
import { Effect, References } from "effect";import { type Eip1193, Transport } from "@ethect/core";import { Wallet } from "@ethect/core/rpc";
// The wallet's EIP-1193 provider: `window.ethereum`, or the one a wallet SDK returns.declare const provider: Eip1193.Provider;declare const id: string;
const status = Wallet.getCallsStatus(id).pipe( Effect.provide(Transport.eip1193(provider)), // timestamp=… level=DEBUG fiber=#1 message=wallet_getCallsStatus ms=12 params=["0x…"] answer={…} Effect.provideService(References.MinimumLogLevel, "Debug"),);| Annotation | Holds |
|---|---|
ms |
how long the provider took to answer |
params |
the params, as they were sent |
answer |
the answer, as the provider gave it, before anything reads it |
error |
what the provider rejected with, instead of answer |
interrupted |
true when the request was given up before an answer, such as by a timeout |
The method is the message. Signers and connections built on a wallet log the same way.
Returns
Section titled “Returns”Layer<Transport | Subscriptions>
A layer that provides Transport and
Subscriptions, with no requirement.
Parameters
Section titled “Parameters”provider
Section titled “provider”- Type:
Eip1193.Provider
An object with request, on and removeListener.
import { type Eip1193, Transport } from "@ethect/core";
// Any object of this shape is a provider.const provider: Eip1193.Provider = { request: ({ method }) => Promise.resolve(method === "eth_chainId" ? "0x1" : null), on: () => {}, removeListener: () => {},};
const Injected = Transport.eip1193(provider);Behavior
Section titled “Behavior”- The method and params are passed as they are on the wire:
provider.requestreceivesmethodand the encodedparams. The provider assigns ids and correlates answers itself. - A rejection with a JSON-RPC error object is an
RpcError. The object needs a numericcodeand a stringmessage. A user who refuses a prompt is the reasonUserRejectedRequest. - Any other rejection is a
TransportErrorwith the reasonMalformedResponse: the provider did not answer in JSON-RPC terms. - Subscriptions are the
messageevents of typeeth_subscription. The transport listens for as long as its layer lives. A provider that does not serveeth_subscribefails the stream with its own error. - A provider without events still answers calls. When
provider.onis missing or throws, calls work and every subscription fails with aTransportError, reasonUnreachable. - Each request is logged at the Debug level, once answered or given up. At the default level nothing is logged. A provider has no URL and its requests carry no key, so the log holds nothing secret.
- No reconnection. The provider owns its connection. Watch
Eip1193.disconnectandEip1193.connectto know its state. - Events are separate. Account and chain changes are not part of the transport: they are
the streams of the
Eip1193module.
See Wallets for a connect flow built on this transport.
Errors
Section titled “Errors”| Error | Reason | When |
|---|---|---|
RpcError |
the reason of the code | the provider rejected with a JSON-RPC error object |
TransportError |
MalformedResponse |
the provider rejected with anything else |
TransportError |
Unreachable |
a subscription, on a provider whose events cannot be listened to |