# Wallet.getAssets

Returns the assets the wallet holds for an account, by chain (ERC-7811, a draft).

## Usage

```ts
import { Effect } from "effect";
import { Address, 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 ACCOUNT = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");

const program = Wallet.getAssets({ account: ACCOUNT });

const assets = await program.pipe(
  Effect.provide(Transport.eip1193(provider)),
  Effect.runPromise,
);
```

A wallet method runs over [`Transport.eip1193`](https://ethect.dev/reference/transport/eip1193/). The examples
below leave the transport out.

### Read a balance

The result is keyed by chain id, and each asset carries its balance as a `bigint`.

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

const ACCOUNT = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");

const ether = Effect.gen(function* () {
  const assets = yield* Wallet.getAssets({ account: ACCOUNT });
  const onMainnet = assets[1] ?? [];
  return onMainnet.find((asset) => asset.address === "native")?.balance;
});
```

With the example answer of the ERC below, `ether` is `14604790206809659138n`.

```json
{
  "0x1": [
    {
      "address": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
      "balance": "0xcaaea35047fe5702",
      "type": "erc20",
      "metadata": { "name": "Token", "symbol": "TOK", "decimals": 18 }
    },
    { "address": "native", "balance": "0xcaaea35047fe5702", "type": "native" }
  ]
}
```

### On some chains, of some types

`chainFilter` keeps the chains you name, and `assetTypeFilter` the asset types.

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

const ACCOUNT = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");

const tokens = Effect.gen(function* () {
  const assets = yield* Wallet.getAssets({
    account: ACCOUNT,
    chainFilter: [10, 8453],
    assetTypeFilter: ["native", "erc20"],
  });
  return assets[8453] ?? [];
});
```

### Named assets

`assetFilter` names the very assets to report, per chain. By the ERC, a wallet that gets one
ignores the two other filters.

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

const ACCOUNT = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const USDC = Address.makeUnsafe("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");

const balances = Wallet.getAssets({
  account: ACCOUNT,
  assetFilter: {
    1: [
      { address: "native", type: "native" },
      { address: USDC, type: "erc20" },
    ],
  },
});
```

## Returns

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

A record from chain id to the list of [`WalletAsset`](https://ethect.dev/rpc/types/wallet-asset/) the
account holds on that chain.

## Parameters

### filter

- **Type:** [`WalletAsset.Filter`](https://ethect.dev/rpc/types/wallet-asset/)

The account, and which of its assets to report.

| Field | Type | Description |
| --- | --- | --- |
| `account` | [`Address`](https://ethect.dev/reference/primitives/address/) | the account to report on |
| `assetFilter` | record of chain id to `{ address, type }` list (optional) | the very assets to report, per chain |
| `assetTypeFilter` | `ReadonlyArray<string>` (optional) | the asset types to keep, such as `"native"` or `"erc20"` |
| `chainFilter` | `ReadonlyArray<number>` (optional) | the chains to keep |

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

const ACCOUNT = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");

const onBase = Wallet.getAssets({ account: ACCOUNT, chainFilter: [8453] });
```

## Behavior

- **A draft.** ERC-7811 is not final: its shapes can change, and a wallet may not serve the
  method.
- **Chain ids are numbers.** The key `"0x2105"` of the answer decodes to `8453`, and
  `chainFilter: [10, 8453]` is sent as `["0xa", "0x2105"]`. The keys of `assetFilter` are
  encoded the same way.
- **A missing chain is `undefined`.** `assets[8453]` is typed as a list, and is `undefined`
  when the answer has no key for the chain.
- **Every key must be a chain id.** A key of the answer that is not a hex quantity, such as
  `"1"` or `"eip155:1"`, fails the call with a `CodecError` on the result. A key of
  `assetFilter` that is not a number fails it on the params, and nothing is sent.
- **Balances are `bigint`.** `"0xcaaea35047fe5702"` decodes to `14604790206809659138n`.
- **`"native"` or an address.** The `address` of an asset is the literal `"native"` for the
  chain's own currency, and a lowercase `Address` for a token.
- **`metadata` is open.** It is typed `unknown`: what it holds depends on the asset type and
  on the wallet.

## Errors

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

| Error | Reason | When |
| --- | --- | --- |
| `RpcError` | `MethodNotFound` | the call goes to a node: anvil does not serve the method |
| `RpcError` | `UnsupportedMethod` | the provider rejects the method with code 4200, as a `MockProvider` without a handler for it does |
| `CodecError` | `on: "params"` | a chain id is negative or not an integer: nothing is sent |

## JSON-RPC method

`wallet_getAssets`
