Track self-custody wallets natively: Bitcoin, EVM and Solana
Adds native self-custody wallet tracking: Bitcoin, six EVM networks (Ethereum, Base, Arbitrum, Optimism, Polygon, Gnosis) and Solana, read from public addresses only. No keys, no seed phrases, no signatures, and no API key required for any chain.
Note on #2191 — @Badbird5907 has an open PR adding on-chain wallet support for ETH/BTC. This is an alternative implementation rather than a follow-up: it shares the goal but not the code. The main differences are a chain-agnostic core (see below), three chain families instead of two, and a token review / manage flow. Maintainers should pick one; I'm happy to fold anything useful from either direction.
The design constraint worth reviewing first
What makes a multi-chain integration unreviewable is case chain spreading through the importer, processor, controller and views, so that every new chain multiplies branches.
So a chain adapter's only job is to turn an address into an Onchain::Snapshot — a list of Onchain::Assets and Onchain::Movements — and Onchain::Chains is the single source of truth for which chains exist, how their addresses are validated, what their native asset is, and which adapter to build. Everything downstream is written once and never branches on chain.
grep -rn 'case chain\|when "bitcoin"\|when "solana"' app/ # nothing outside chains.rb and the adapters
Adding a chain is one registry entry plus one adapter. The six EVM networks already share a single adapter and differ only by data.
What it does
One Sure account per asset, per address, per network (Crypto accounts, wallet subtype). For each asset: the quantity from the chain, a holding valued at the current price (or zero), and transfers materialised as signed investment trades where that day's price is known — so cost basis and the value chart reconstruct back to acquisition. Where it isn't known, the transfer still appears as an excluded, zero-amount entry, and is upgraded to a trade on the first sync after its price becomes available.
Uniqueness uses three partial unique indexes, one per asset kind, because a native coin is identified by its address alone while a token is identified by its contract/mint. A single index over all columns would treat two native rows with a NULL contract as distinct.
Decisions
- Bitcoin is a single address, not an extended key. Extended keys would need BIP32 derivation (a dependency this codebase avoids) or a descriptor-indexing backend. The limit is stated in the linking UI, not just the docs, and an xpub is rejected as an address rather than silently treated as one. The schema already allows the derived addresses an HD wallet would need, so adding it later needs no destructive change.
- The unit on EVM is the (chain, address) couple, carried by the unique index. A 0x address is valid on all six networks with different balances on each, so detection asks each candidate whether the address is worth tracking there — exactly one bounded request per network, never paginated history — and when several answer yes, the user chooses.
- Nothing is imported without being ticked. Real wallets are airdrop dumping grounds, so only assets the data source treats as notable are pre-ticked (a priced holding above dust on EVM, a place on Solana's verified token list). Unpriceable assets are still trackable by quantity, valued at zero.
Data sources
| Network | Source | Key | Override |
|---|---|---|---|
| Bitcoin | mempool.space | No | MEMPOOL_SPACE_URL |
| 6 EVM networks | Blockscout (per network) | No | BLOCKSCOUT_<CHAIN>_URL |
| Ethereum (optional) | Etherscan v2 — history only | Per-family, encrypted | — |
| Solana | public JSON-RPC | No | SOLANA_RPC_URL |
| Solana token names | Jupiter token search (verified mints only) | No | SOLANA_TOKEN_LIST_URL |
An Etherscan key moves only transfer history; balances and detection always come from the keyless indexer, which is the only backend that can answer them correctly.
What a user has to configure
No API key is required to read any chain. Paste a public address and it reads — that was a design goal, not a coincidence, and it is why every default above is a keyless public endpoint.
Valuation is a different matter, and needs two settings — neither of which requires a key either, but both of which have to be right. On-chain sources report quantities, not values, so prices come from Sure's own market data providers:
- A crypto-capable market data provider. The only provider that quotes bare crypto symbols is
binance_public, which takes no key — but the default issecurities_provider = "twelve_data", so it is not enabled out of the box. - An exchange rate provider, only if the family currency is not USD. Crypto is quoted in USD, and the default
exchange_rate_provider = "twelve_data"does require a key.frankfurter,yahoo_financeandmoex_publicdo not. A USD family needs nothing here.
With neither in place, wallets link and sync, quantities are tracked, and everything is valued at zero — the shape of the classic "the sync is broken" report. So the settings panel and the linking modal warn before linking, name which of the two applies (and the currency, for the FX one), offer a one-click fix for the first on a self-hosted instance, and the reason is recorded in the debug log when a holding lands at zero.
Everything else is optional and has a working default: MEMPOOL_SPACE_URL, BLOCKSCOUT_<CHAIN>_URL, SOLANA_RPC_URL, SOLANA_TOKEN_LIST_URL to point at your own instances, and ONCHAIN_HISTORY_MAX_PAGES / ONCHAIN_MAX_TOKENS_PER_ADDRESS for depth. One caveat worth knowing: the public Solana endpoint routinely throttles getTransaction, so balances arrive but transfers often do not — anyone who wants Solana history should point SOLANA_RPC_URL at a real node. docs/hosting/onchain-wallets.md covers all of it.
Reading order
The commits are the reading order: foundation → pipeline → pricing → one commit per chain → linking UI → manage UI → docs, then the fixes that came out of verification. Each commit builds and passes its own tests.
Verification
Every job in ci.yml has since run here and passed, including test_system. Because a fork's runs need a maintainer's Approve and run each time, they are also run locally with the workflow's own commands against the branch as pushed:
bin/brakeman --no-pager 0 security warnings (CI job scan_ruby)
bin/importmap audit no vulnerable packages (CI job scan_js)
bin/rubocop -f github no offenses (CI job lint)
npm install && npm run lint 109 files, biome clean (CI job lint_js)
bin/rails test 6857 runs, 27612 assertions (CI job test_unit)
DISABLE_PARALLELIZATION=true bin/rails test:system 98 runs, 403 assertions (CI job test_system)
Beyond the suite, because unit tests cannot catch a mistaken API contract:
-
Every data source was probed live and the field names checked against the real payloads. This caught a blocker:
token-balances?type=ERC-20returns HTTP 422 — the filter exists ontoken-transfers, not there — so every EVM read was failing in production while the tests passed against a stub encoding the same wrong assumption. It also showed that endpoint returns every token standard, so an NFT would have been imported as a balance of one fungible token. -
A real Bitcoin address was linked through the UI and synced by Sidekiq: 275 movements → 275 priced trades, 1,940 prices backfilled in one batched call, average cost and total return reconstructed on the holdings tab.
-
A real EVM address (7,924 ERC-20 balances, 3.2 MB) and a real Solana wallet were read end to end, in both a USD and a EUR family.
-
The browser flow was walked (linking and management), which found that the panel is reached two different ways depending on whether anything is linked yet — only one had been exercised.
-
The same probing was repeated after review, which is how the last commit exists at all. A change bounding detection latency had set a 5s budget that sat on one explorer's median (p50 4.79s), so about a fifth of probes timed out — and the timeout was being read as "this address is not here", which could link a Base wallet as Ethereum-only. Measured before and after: false negatives 4/18 → 0/10, and detecting a 0x address across six chains 9.4-10.7s → 4.3-4.8s, since probes now run concurrently under one wall-clock deadline. No amount of unit testing would have found it: the stubs encoded the assumption.
Those runs also produced most of the fixes at the end of this branch, including a crash that was latent from the second commit: a movement first seen unpriced could not later become a trade, so the first sync able to price it raised, taking the repair pass down with it. Regression tests for the non-obvious fixes were each checked against the pre-fix behaviour.
Known limitations
-
Tokens are priced by symbol, so long-tail coverage is poor — this is the biggest limitation here. Valuation goes through
CRYPTO:<SYMBOL>on the crypto provider, and a symbol is not a token's identity: its contract is. Measured on a real Ethereum address, of its ten highest-value token holdings the provider lists two (ENS, and WETH via its alias); the other eight — including positions worth ~$406k, ~$141k and ~$74k — would show a value of zero. The quantity is tracked and the UI does not claim otherwise, but showing zero on a six-figure position is worse than uninformative.Two things make this fixable rather than inherent, and worth flagging to maintainers as a follow-up rather than something to bolt onto this PR: the prices are already in our responses and we discard them — Blockscout returns
exchange_rateper token (this PR uses it only as a boolean-ish signal for which tokens to pre-tick, which is how the figures above were computed) and Jupiter returnsusdPriceper mint. And a keyless source indexed by contract address covers most of the gap: DefiLlama priced six of the eight tokens the symbol route cannot. Moving to contract-based pricing touches security resolution and the processor, and it is an architecture decision about how crypto enters Sure's price pipeline — not a change I wanted to make unilaterally inside a feature PR. -
DeFi positions are not seen at all: staked ETH, LP tokens, lending positions, Solana stake accounts. A wallet holding most of its value in Lido or Aave will report a fraction of it. Only directly-held native coins and fungible tokens are read.
-
Bitcoin is one address at a time (see above); an HD wallet is under-reported.
-
History is capped per sync (
ONCHAIN_HISTORY_MAX_PAGES), and at most 200 tokens per address are surfaced (ONCHAIN_MAX_TOKENS_PER_ADDRESS). Balances are never affected — they come from an address summary — and both caps are stated in the UI when they apply. -
History is best effort: if a source refuses it, balances are still recorded and the history is marked incomplete rather than the whole read failing. This matters on the free Solana endpoint, which throttles
getTransaction. -
Unverified Solana mints keep a label that cannot pass for a ticker, so they are never priced as an unrelated asset that shares a symbol.
-
Network fees are not itemised. NFTs are not tracked.
-
Not verified: the Etherscan-with-key path (no key available — shapes are from the docs), and Solana history against a real node.
What I'd most like reviewed
- The three partial unique indexes and the
native/erc20/splasset kinds. - The trade sign and amount convention in
OnchainWalletAccount::Processor, and whether the excluded zero-amount entry is the right shape for this ledger. - The thresholds I picked alone: 200 tokens per address, $1 of dust for pre-ticking, 25 transactions for Solana.
Provider::Blockscoutfield names against a self-hosted instance — verified against the public one only.
db/schema.rb is hand-edited to add only the two new tables: regenerating it with the Rails version in the Gemfile reorders every column in the file, which is out of scope here.
🤖 Generated with Claude Code
Summary by CodeRabbit
-
New Features
- Added self-custody wallet support for Bitcoin, EVM networks, and Solana.
- Users can detect chains, preview wallets, select assets, link wallets, sync balances, review tokens, and track transaction history.
- Added tools for changing wallet addresses and disconnecting assets or wallets.
- Added pricing warnings and optional crypto-price configuration.
- Added provider settings for wallet management, API keys, and sync dates.
-
Bug Fixes
- Improved duplicate-address handling, incomplete-history reporting, partial sync failures, provider errors, and unavailable pricing.
-
Documentation
- Added setup, supported-network, configuration, limitations, and troubleshooting guidance.