Skip to content

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.

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. The examples below leave the transport out.

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

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",
},
});

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

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" },
});
});

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

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 },
});

Effect<boolean, Method.Error, Transport>

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

The token to watch, as one object.

Field Type Description
type string the standard the token implements: "ERC20", "ERC721", "ERC1155"
options.address 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
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 },
});
  • Params by name. The method is defined with Method.makeByName: 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:

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

The three errors every method can fail with. The cases specific to this method, checked with a MockProvider 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.

wallet_watchAsset