Skip to content

Wallet.getAssets

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

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

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

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.

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

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

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] ?? [];
});

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

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

Effect<WalletAsset.ByChain, Method.Error, Transport>

A record from chain id to the list of WalletAsset the account holds on that chain.

The account, and which of its assets to report.

Field Type Description
account 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
import { Address } from "@ethect/core";
import { Wallet } from "@ethect/core/rpc";
const ACCOUNT = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const onBase = Wallet.getAssets({ account: ACCOUNT, chainFilter: [8453] });
  • 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.

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

wallet_getAssets