# Wallet.watchAsset

Suggests a token for the wallet to track (EIP-747). It is the one method whose params are sent
by name, as an object and not as an array.

## 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 ADDRESS = Address.makeUnsafe("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");

const program = Effect.gen(function* () {
  // USDC on Ethereum mainnet
  return yield* Wallet.watchAsset({
    type: "ERC20",
    options: { address: ADDRESS, symbol: "USDC", decimals: 6 },
  });
});

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

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

### With an image

`image` is the URL of the token's logo, for the wallet to show.

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

const ADDRESS = Address.makeUnsafe("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");

const watch = Wallet.watchAsset({
  type: "ERC20",
  options: {
    address: ADDRESS,
    symbol: "USDC",
    decimals: 6,
    image: "https://app.example/usdc.svg",
  },
});
```

### An NFT

`type` names the standard the token implements, and decides which options the wallet reads. An
NFT is named by its `tokenId`.

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

const ADDRESS = Address.makeUnsafe("0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D");

const watch = Effect.gen(function* () {
  // the Bored Ape Yacht Club collection
  return yield* Wallet.watchAsset({
    type: "ERC721",
    options: { address: ADDRESS, tokenId: "1234" },
  });
});
```

### On another chain

`chainId` names the chain of the token. Left out, the wallet uses its current chain.

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

const ADDRESS = Address.makeUnsafe("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");

const watch = Wallet.watchAsset({
  type: "ERC20",
  options: { address: ADDRESS, chainId: 1, symbol: "USDC", decimals: 6 },
});
```

## Returns

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

`true` when the wallet took the request. It does not say whether the user accepted the token.

## Parameters

### asset

- **Type:** [`WatchAsset.WatchAsset`](https://ethect.dev/rpc/types/watch-asset/)

The token to watch, as one object.

| Field | Type | Description |
| --- | --- | --- |
| `type` | `string` | the standard the token implements: `"ERC20"`, `"ERC721"`, `"ERC1155"` |
| `options.address` | [`Address`](https://ethect.dev/reference/primitives/address/) | the token contract |
| `options.chainId` | `number` (optional) | the chain of the token. The wallet's current chain when left out |
| `options.symbol` | `string` (optional) | the ticker of a fungible token |
| `options.decimals` | `number` (optional) | the decimals of a fungible token |
| `options.image` | `string` (optional) | the URL of the token's logo |
| `options.tokenId` | `string` (optional) | the id of an NFT |

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

const ADDRESS = Address.makeUnsafe("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");

const watch = Wallet.watchAsset({
  // the standard, then what the wallet needs to show the token
  type: "ERC20",
  options: { address: ADDRESS, symbol: "USDC", decimals: 6 },
});
```

## Behavior

- **Params by name.** The method is defined with
  [`Method.makeByName`](https://ethect.dev/rpc/method/make-by-name/): the request's `params` is the object
  itself, not an array that holds it.
- **Checksummed on the wire.** You pass a lowercase `Address.Address`, and the address is sent
  in its EIP-55 form, which the EIP requires. Every method sends addresses in that form.
- **`chainId` is a JSON number.** It is sent as `1`, not as the hex quantity `"0x1"` the other
  wallet methods use.
- **`true` is not consent.** By the EIP, the wallet answers before it prompts the user.

The request of the first example, as the provider receives it:

```json
{
  "method": "wallet_watchAsset",
  "params": {
    "type": "ERC20",
    "options": {
      "address": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
      "symbol": "USDC",
      "decimals": 6
    }
  }
}
```

## 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 |
| `CodecError` | `on: "result"` | the wallet answers something that is not a boolean, such as `null` |

By the EIP, a wallet also answers with an error when it does not support the asset type or when
the request is not valid. The EIP names no code for these cases.

## JSON-RPC method

`wallet_watchAsset`
