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.
When the user declines
Section titled “When the user declines”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 first account
Section titled “The first account”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");});Without asking
Section titled “Without asking”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.
Returns
Section titled “Returns”Effect<ReadonlyArray<Address.Address>, Method.Error, Transport>
The accounts the user chose, as addresses in lowercase.
Parameters
Section titled “Parameters”None.
Behavior
Section titled “Behavior”- Lowercase addresses. A wallet that answers
0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266gives you0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266. UseAddress.checksumto 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.accountsChangedis the stream of those changes: seeEip1193.
Errors
Section titled “Errors”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 |
JSON-RPC method
Section titled “JSON-RPC method”eth_requestAccounts