# WalletAsset

An asset a wallet holds for an account, as `wallet_getAssets` reports it (ERC-7811, a draft).
The module also holds the filter that selects assets, and the result by chain.

```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 byChain = yield* Wallet.getAssets({ account: ACCOUNT, chainFilter: [1] });
  // byChain: WalletAsset.ByChain
  const assets = byChain[1] ?? [];
  return assets.filter((asset) => asset.type === "erc20");
});
```

## Schemas

| Schema | Type | Describes | Used by |
| --- | --- | --- | --- |
| `WalletAsset.schema` | `WalletAsset.WalletAsset` | an asset and the account's balance of it | the lists of a `WalletAsset.ByChain` |
| `WalletAsset.filterSchema` | `WalletAsset.Filter` | which of an account's assets to report | passed to [`Wallet.getAssets`](https://ethect.dev/rpc/wallet/get-assets/) |
| `WalletAsset.byChainSchema` | `WalletAsset.ByChain` | assets by chain id | returned by [`Wallet.getAssets`](https://ethect.dev/rpc/wallet/get-assets/) |

## Asset fields

`WalletAsset.WalletAsset`.

| Field | Type | Notes |
| --- | --- | --- |
| `address` | `"native" \| Address` | `"native"` for the chain's own currency |
| `balance` | `bigint` | the account's balance of the asset |
| `type` | `string` | open: `"native"`, `"erc20"`, `"erc721"` and others |
| `metadata` | `unknown` (optional) | depends on `type` |

## Filter fields

`WalletAsset.Filter`.

| Field | Type | Notes |
| --- | --- | --- |
| `account` | [`Address`](https://ethect.dev/reference/primitives/address/) | the account to report on |
| `assetFilter` | `{ [chainId: number]: ReadonlyArray<{ address: "native" \| Address, type: string }> }` (optional) | the very assets to report, per chain |
| `assetTypeFilter` | `ReadonlyArray<string>` (optional) | the asset types to keep |
| `chainFilter` | `ReadonlyArray<number>` (optional) | the chains to keep |

Build one to name the assets you want.

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

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

const filter = Effect.gen(function* () {
  const named: WalletAsset.Filter = {
    account: ACCOUNT,
    assetFilter: {
      1: [{ address: "native", type: "native" }, { address: USDC, type: "erc20" }],
    },
  };
  return named;
});
```

## Behavior

- **`assetFilter` wins.** By the ERC, a wallet that gets an `assetFilter` reports those assets
  only and ignores `assetTypeFilter` and `chainFilter`.
- **`ByChain` is a record.** `{ [chainId: number]: ReadonlyArray<WalletAsset> }`. The key
  `"0x1"` of the answer decodes to `1`, and the chain ids of a filter are encoded the same way.
- **`metadata` is open.** By the ERC, an `erc20` asset carries `name`, `symbol` and `decimals`
  there. The type is `unknown`: check it before use.
- **Every key must be a chain id.** A key of the answer that is not a hex quantity, such as
  `"1"`, fails the decoding. So does a key of a filter's `assetFilter` that is not a number,
  when it is encoded.
- **Lowercase addresses.** The `address` of a token decodes to a lowercase
  [`Address`](https://ethect.dev/reference/primitives/address/), whatever case the wallet sent.
