Import Trade Republic crypto into a separate Crypto account
Merged
NI
2mo ago+1292+1292+0123456789012345678901234567890123456789-132-132-012345678901234567890123456789
Depends on #3800 and #3806. This branch builds on both, so until they merge the diff also shows their commits. Only the last commit belongs to this PR.
Problem
Trade Republic returns crypto in the same portfolio snapshot and timeline as stocks and ETFs, so every crypto position and trade landed on the Investment "Portfolio" account. Sure has a Crypto account type, which gets its own net worth group, tax treatment and exact quantity display, but Trade Republic users could not use it.
Behavior
- New provider account. The importer adds a
cryptoTrade Republic account (crypto:<account id>) once the portfolio holds or traded crypto, detected by thecryptoscategory or theXF000pseudo-ISIN. It stores only its balance and reads its positions and trades from the portfolio payloads, so there is no second timeline to keep in sync. - Nothing changes until it is linked. The Crypto account shows up in account setup like any unlinked account. While it is unlinked, the portfolio keeps holding crypto exactly as before.
- Once linked:
- The account is created as
Cryptowith subtypeexchange, the Crypto subtype that supports trades. - Crypto holdings and trades import on the Crypto account. The portfolio leaves them out, and so does its balance (
TradeRepublicAccount#account_balance). - Crypto trades already imported on the portfolio move to the Crypto account. A copy the user edited stays on the portfolio, and the Crypto account skips that trade, so nothing is counted twice.
- Cash settlements (#3800) link to a counterpart on whichever account holds the trade. A counterpart left on the portfolio is removed at the start of the cash run, so the settlement relinks to the Crypto account in the same sync.
- The account is created as
- Other changes:
- Accounts process in the order portfolio, crypto, cash.
- Crypto accounts can be linked to existing manual Crypto accounts.
- Account setup labels the new account "Crypto account".
Private Markets and fixed-interest products stay on the portfolio: they remain Investment accounts in Sure, so a separate account would not add anything.
Validation
- New tests:
- The importer creates the Crypto account with the crypto value and creates none without crypto.
- Setup creates a Crypto/exchange account.
- Holdings split between the portfolio and the Crypto account, and balances split with them.
- Trades and settlement counterparts move to the Crypto account in one sync, and re-processing is idempotent.
- An edited portfolio trade stays put.
- Crypto stays on the portfolio while the Crypto account is unlinked.
- Ran the full chain against a real Trade Republic connection inside a rolled-back transaction:
- The Crypto account got the BTC holding, its trade and a counterpart linked to the cash settlement.
- The portfolio balance dropped by exactly the crypto value.
bin/rails test: 10107 runs, 0 failures, 0 errors.- RuboCop, erb_lint and Brakeman are clean.
- The only UI change is the account type label in Trade Republic account setup. No system tests cover that page.
Summary by CodeRabbit
-
New Features
- Trade Republic cryptocurrency holdings are shown and managed separately in Crypto accounts.
- Crypto holdings can match shared crypto assets when pricing data is available, or remain available as offline holdings.
- Account setup offers eligible accounts for linking, including Crypto accounts, with choices tailored to each Trade Republic account.
-
Bug Fixes
- Improved handling of trades, cash movements, and settlements across linked portfolio, Crypto, and cash accounts.
- Improved balance reporting and holdings reconciliation for split crypto and portfolio holdings.
- Standardized Trade Republic activity labels while preserving user-edited and locked entries.