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.
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
Section titled “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 |
WalletAsset.byChainSchema |
WalletAsset.ByChain |
assets by chain id | returned by Wallet.getAssets |
Asset fields
Section titled “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
Section titled “Filter fields”WalletAsset.Filter.
| Field | Type | Notes |
|---|---|---|
account |
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.
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
Section titled “Behavior”assetFilterwins. By the ERC, a wallet that gets anassetFilterreports those assets only and ignoresassetTypeFilterandchainFilter.ByChainis a record.{ [chainId: number]: ReadonlyArray<WalletAsset> }. The key"0x1"of the answer decodes to1, and the chain ids of a filter are encoded the same way.metadatais open. By the ERC, anerc20asset carriesname,symbolanddecimalsthere. The type isunknown: 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’sassetFilterthat is not a number, when it is encoded. - Lowercase addresses. The
addressof a token decodes to a lowercaseAddress, whatever case the wallet sent.