Skip to content

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.

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)));

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)));

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.

Layer<Transport | Subscriptions>

A layer that provides Transport and Subscriptions, with no requirement.

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);
  • The method and params are passed as they are on the wire: provider.request receives method and the encoded params. The provider assigns ids and correlates answers itself.
  • A rejection with a JSON-RPC error object is an RpcError. The object needs a numeric code and a string message. A user who refuses a prompt is the reason UserRejectedRequest.
  • Any other rejection is a TransportError with the reason MalformedResponse: the provider did not answer in JSON-RPC terms.
  • Subscriptions are the message events of type eth_subscription. The transport listens for as long as its layer lives. A provider that does not serve eth_subscribe fails the stream with its own error.
  • A provider without events still answers calls. When provider.on is missing or throws, calls work and every subscription fails with a TransportError, reason Unreachable.
  • 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.disconnect and Eip1193.connect to know its state.
  • Events are separate. Account and chain changes are not part of the transport: they are the streams of the Eip1193 module.

See Wallets for a connect flow built on this transport.

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