Skip to content

Wallet.requestAccounts

Asks the user to connect accounts to the app, and returns the ones they chose (EIP-1102).

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 accounts = await Wallet.requestAccounts().pipe(
Effect.provide(Transport.eip1193(provider)),
Effect.runPromise,
);
// ["0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"]

A wallet method runs over Transport.eip1193. The examples below leave the transport out.

A wallet rejects the call when the user closes the prompt. Catch the reason to continue with no account.

import { Effect } from "effect";
import { Wallet } from "@ethect/core/rpc";
const accounts = Wallet.requestAccounts().pipe(
Effect.catchReason("RpcError", "UserRejectedRequest", () =>
Effect.succeed([]),
),
);

The result is a list. Take the account to act with from it, and handle the empty case.

import { Effect } from "effect";
import { Eth, Wallet } from "@ethect/core/rpc";
const balance = Effect.gen(function* () {
const accounts = yield* Wallet.requestAccounts();
const address = accounts.at(0);
if (address === undefined) return 0n;
return yield* Eth.getBalance(address, "latest");
});

Wallet.accounts is eth_accounts: it reads the accounts the app can already see, and opens no prompt.

import { Effect } from "effect";
import { Wallet } from "@ethect/core/rpc";
const connected = Wallet.accounts().pipe(
Effect.map((accounts) => accounts.length > 0),
);

See Eth.accounts.

Effect<ReadonlyArray<Address.Address>, Method.Error, Transport>

The accounts the user chose, as addresses in lowercase.

None.

  • Lowercase addresses. A wallet that answers 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 gives you 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266. Use Address.checksum to display one.
  • A node answers too. anvil 1.9 returns its ten development accounts, the same list as Wallet.accounts, with no prompt.
  • Later changes are events. The user can change accounts in the wallet afterwards. Eip1193.accountsChanged is the stream of those changes: see Eip1193.

The three errors every method can fail with. The cases specific to this method, from EIP-1193 and checked with a MockProvider:

Error Reason When
RpcError UserRejectedRequest the user rejected the request (code 4001)
CodecError on: "result" the wallet answers something that is not a list of addresses

eth_requestAccounts