# Wallet.requestAccounts

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

## Usage

```ts
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`](https://ethect.dev/reference/transport/eip1193/). The examples
below leave the transport out.

### When the user declines

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

```ts
import { Effect } from "effect";
import { Wallet } from "@ethect/core/rpc";

const accounts = Wallet.requestAccounts().pipe(
  Effect.catchReason("RpcError", "UserRejectedRequest", () =>
    Effect.succeed([]),
  ),
);
```

### The first account

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

```ts
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

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

```ts
import { Effect } from "effect";
import { Wallet } from "@ethect/core/rpc";

const connected = Wallet.accounts().pipe(
  Effect.map((accounts) => accounts.length > 0),
);
```

See [`Eth.accounts`](https://ethect.dev/rpc/eth/accounts/).

## Returns

[`Effect<ReadonlyArray<Address.Address>, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method)

The accounts the user chose, as [addresses](https://ethect.dev/reference/primitives/address/) in lowercase.

## Parameters

None.

## Behavior

- **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`](https://ethect.dev/reference/eip1193/).

## Errors

The three [errors](https://ethect.dev/reference/errors/) every method can fail with. The cases specific to this
method, from EIP-1193 and checked with a
[`MockProvider`](https://ethect.dev/reference/testing/mock-provider/):

| 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

`eth_requestAccounts`
