The personal finance app for everyone (by everyone)
Ruby
fix(onchain-wallets): show linked wallets on the accounts page, and drop the tracking row on unlink (#3136)
buzzromain0d0003b032e9
diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rbindex 1be03dee22..727af4dce0 100644--- a/app/controllers/accounts_controller.rb+++ b/app/controllers/accounts_controller.rb@@ -28,11 +28,23 @@ class AccountsController < ApplicationController@ibkr_items = visible_provider_items(family.ibkr_items.ordered.with_attached_logo.includes(:ibkr_accounts))@indexa_capital_items = visible_provider_items(family.indexa_capital_items.ordered.with_attached_logo.includes(:indexa_capital_accounts))@sophtron_items = visible_provider_items(family.sophtron_items.ordered.with_attached_logo.includes(:sophtron_accounts))+ @onchain_wallet_items = visible_provider_items(+ family.onchain_wallet_items.ordered.includes(:accounts, onchain_wallet_accounts: { account_provider: :account })+ )@binance_items = visible_provider_items(family.binance_items.ordered.with_attached_logo.includes(:binance_accounts, :accounts))@kraken_items = visible_provider_items(family.kraken_items.ordered.with_attached_logo.includes(:kraken_accounts, :accounts))@questrade_items = visible_provider_items(family.questrade_items.ordered.with_attached_logo.includes(:accounts, questrade_accounts: :account_provider))@wise_items = visible_provider_items(family.wise_items.ordered.includes(:wise_accounts, :accounts))+ # An on-chain item is admitted as soon as ONE of its accounts is accessible,+ # so the card is told which of them this viewer may actually see. nil is the+ # admin case, which visible_provider_items already lets through whole.+ allowed_ids = Current.user&.admin? ? nil : @accessible_account_ids+ @onchain_wallet_cards = @onchain_wallet_items.to_h do |item|+ visible = item.accounts_visible_to(allowed_ids)+ [ item.id, { accounts: visible, address_count: item.address_count_for(visible) } ]+ end+preload_latest_sync_metadata_for_index!# Build sync stats maps for all providers@@ -333,7 +345,8 @@ class AccountsController < ApplicationController@binance_items,@kraken_items,@questrade_items,- @wise_items+ @wise_items,+ @onchain_wallet_items].flatten.compactaccounts = @manual_accounts.to_adiff --git a/app/models/account_provider.rb b/app/models/account_provider.rbindex 5ac8ef2cb2..691e9318ac 100644--- a/app/models/account_provider.rb+++ b/app/models/account_provider.rb@@ -12,6 +12,14 @@ class AccountProvider < ApplicationRecord# Other providers may legitimately enter a "needs setup" state.after_destroy :destroy_coinstats_provider_account, if: :coinstats_provider?+ # An on-chain tracking row IS the link: unlike a bank connection there is+ # nothing to reconnect to and nothing worth keeping. Left behind by a generic+ # unlink it stops syncing, because the syncer only reads linked rows, while+ # its partial unique index still holds the (item, chain, address, asset) slot+ # — so linking that same asset again would collide with a row nothing shows.+ # The Sure account and its holdings are untouched and carry on as manual.+ after_destroy :destroy_onchain_provider_account, if: :onchain_provider?+# Returns the provider adapter for this connectiondef adapterProvider::Factory.create_adapter(provider, account: account)@@ -32,4 +40,16 @@ class AccountProvider < ApplicationRecorddef destroy_coinstats_provider_accountprovider&.destroyend++ def onchain_provider?+ provider_type == "OnchainWalletAccount"+ end++ def destroy_onchain_provider_account+ # The row destroys this link itself through dependent: :destroy, so+ # answering that by destroying the row again would go round in circles.+ return if destroyed_by_association++ provider&.destroy+ endenddiff --git a/app/models/onchain_wallet_item.rb b/app/models/onchain_wallet_item.rbindex fe2d7f0a39..a873db359d 100644--- a/app/models/onchain_wallet_item.rb+++ b/app/models/onchain_wallet_item.rb@@ -129,6 +129,32 @@ class OnchainWalletItem < ApplicationRecordonchain_wallet_accounts.for_wallet(chain, address).exists?end+ # The accounts a viewer may see on this item, and how many addresses those+ # cover. An item is surfaced on the accounts page as soon as ONE of its+ # accounts is accessible, so a card rendering them all would show a+ # partially-authorised member the names and balances of accounts nobody+ # shared with them. Passing nil means "no restriction", which is what an+ # admin gets.+ #+ # Both read the preloaded associations rather than opening new relations: the+ # accounts page renders one card per item, and a scope here would cost a+ # query per row.+ def accounts_visible_to(allowed_account_ids)+ return accounts.to_a if allowed_account_ids.nil?++ accounts.select { |account| allowed_account_ids.include?(account.id) }+ end++ def address_count_for(visible_accounts)+ visible_ids = visible_accounts.map(&:id).to_set++ onchain_wallet_accounts+ .select { |row| visible_ids.include?(row.account_provider&.account_id) }+ .map { |row| [ row.chain, row.wallet_address ] }+ .uniq+ .size+ end+def institution_display_nameinstitution_name.presence || nameenddiff --git a/app/views/accounts/index.html.erb b/app/views/accounts/index.html.erbindex d038130490..6d13fae02d 100644--- a/app/views/accounts/index.html.erb+++ b/app/views/accounts/index.html.erb@@ -10,7 +10,7 @@) %><% end %>-<% if @manual_accounts.empty? && @plaid_items.empty? && @simplefin_items.empty? && @lunchflow_items.empty? && @redbark_items.empty? && @akahu_items.empty? && @up_items.empty? && @enable_banking_items.empty? && @coinstats_items.empty? && @coinbase_items.empty? && @mercury_items.empty? && @brex_items.empty? && @ibkr_items.empty? && @snaptrade_items.empty? && @indexa_capital_items.empty? && @sophtron_items.empty? && @binance_items.empty? && @kraken_items.empty? && @questrade_items.empty? && @wise_items.empty? %>+<% if @manual_accounts.empty? && @plaid_items.empty? && @simplefin_items.empty? && @lunchflow_items.empty? && @redbark_items.empty? && @akahu_items.empty? && @up_items.empty? && @enable_banking_items.empty? && @coinstats_items.empty? && @coinbase_items.empty? && @mercury_items.empty? && @brex_items.empty? && @ibkr_items.empty? && @snaptrade_items.empty? && @indexa_capital_items.empty? && @sophtron_items.empty? && @binance_items.empty? && @kraken_items.empty? && @questrade_items.empty? && @wise_items.empty? && @onchain_wallet_items.empty? %><%= render "empty" %><% else %><div class="space-y-2">@@ -46,6 +46,16 @@<%= render @coinstats_items.sort_by(&:created_at) %><% end %>+ <% if @onchain_wallet_items.any? %>+ <%# Rendered by name rather than as a collection: the model's default+ partial is the provider settings row, and this is the accounts card. %>+ <% @onchain_wallet_items.sort_by(&:created_at).each do |onchain_wallet_item| %>+ <%= render "onchain_wallet_items/wallet_card",+ onchain_wallet_item: onchain_wallet_item,+ card: @onchain_wallet_cards.fetch(onchain_wallet_item.id) %>+ <% end %>+ <% end %>+<% if @sophtron_items.any? %><%= render @sophtron_items.sort_by(&:created_at) %><% end %>diff --git a/app/views/onchain_wallet_items/_wallet_card.html.erb b/app/views/onchain_wallet_items/_wallet_card.html.erbnew file mode 100644index 0000000000..c91fa9d094--- /dev/null+++ b/app/views/onchain_wallet_items/_wallet_card.html.erb@@ -0,0 +1,76 @@+<%# locals: (onchain_wallet_item:, card:) %>++<%= tag.div id: dom_id(onchain_wallet_item, :accounts_index) do %>+ <%= render DS::Disclosure.new(variant: :card, open: true) do |disclosure| %>+ <% disclosure.with_summary_content do %>+ <div class="flex items-center justify-between gap-2 focus-visible:outline-hidden">+ <div class="flex items-center gap-2">+ <%= icon "chevron-right", class: "group-open:rotate-90 motion-safe:transition-transform motion-safe:duration-150" %>++ <div class="flex items-center justify-center h-8 w-8 rounded-full bg-surface-inset shrink-0">+ <%= icon "wallet", size: "sm", class: "text-primary" %>+ </div>++ <div class="pl-1 text-sm">+ <div class="flex items-center gap-2">+ <%= tag.p onchain_wallet_item.institution_display_name, class: "font-medium text-primary" %>+ <% if onchain_wallet_item.scheduled_for_deletion? %>+ <p class="text-destructive text-sm animate-pulse"><%= t(".deletion_in_progress") %></p>+ <% end %>+ </div>++ <p class="text-xs text-secondary">+ <%= t(".addresses_tracked", count: card[:address_count]) %>+ </p>++ <% if onchain_wallet_item.syncing? %>+ <div class="text-secondary flex items-center gap-1">+ <%= icon "loader", size: "sm", class: "animate-spin" %>+ <%= tag.span t(".syncing") %>+ </div>+ <% elsif onchain_wallet_item.requires_update? %>+ <div class="text-warning flex items-center gap-1">+ <%= icon "alert-triangle", size: "sm", color: "warning" %>+ <%= tag.span t(".needs_attention") %>+ </div>+ <% else %>+ <p class="text-secondary">+ <% if onchain_wallet_item.last_synced_at %>+ <%= t(".status", timestamp: time_ago_in_words(onchain_wallet_item.last_synced_at)) %>+ <% else %>+ <%= t(".status_never") %>+ <% end %>+ </p>+ <% end %>+ </div>+ </div>++ <%# Management lives in the provider settings panel, where linking,+ token review and disconnection already are — this card links to it+ rather than growing a second, partial copy of those controls. %>+ <% if Current.user&.admin? %>+ <%= render DS::Link.new(+ text: t(".manage"),+ icon: "settings",+ variant: "secondary",+ href: settings_providers_path,+ frame: "_top"+ ) %>+ <% end %>+ </div>+ <% end %>++ <% unless onchain_wallet_item.scheduled_for_deletion? %>+ <div class="space-y-4 mt-4">+ <% if card[:accounts].any? %>+ <%= render "accounts/index/account_groups", accounts: card[:accounts] %>+ <% else %>+ <div class="p-4 flex flex-col gap-3 items-center justify-center">+ <p class="text-primary font-medium text-sm"><%= t(".no_wallets_title") %></p>+ <p class="text-secondary text-sm"><%= t(".no_wallets_message") %></p>+ </div>+ <% end %>+ </div>+ <% end %>+ <% end %>+<% end %>diff --git a/config/locales/views/onchain_wallet_items/en.yml b/config/locales/views/onchain_wallet_items/en.ymlindex 77853fcf3b..699887ac03 100644--- a/config/locales/views/onchain_wallet_items/en.yml+++ b/config/locales/views/onchain_wallet_items/en.yml@@ -79,6 +79,18 @@ en:other: "%{count} assets tracked"sync: Sync nowsyncing: Syncing...+ wallet_card:+ addresses_tracked:+ one: 1 address tracked+ other: "%{count} addresses tracked"+ deletion_in_progress: Removing...+ manage: Manage+ needs_attention: Needs attention+ no_wallets_message: Link an address from the provider settings to see it here.+ no_wallets_title: No wallets linked yet+ status: Synced %{timestamp} ago+ status_never: Not synced yet+ syncing: Syncing...price_provider_warning:body: No enabled market data provider can price crypto, so these wallets track quantities but show a value of zero.enable: Enable crypto pricesdiff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rbindex 33a98f04f1..ff4307e715 100644--- a/test/controllers/accounts_controller_test.rb+++ b/test/controllers/accounts_controller_test.rb@@ -2,6 +2,7 @@ require "test_helper"class AccountsControllerTest < ActionDispatch::IntegrationTestinclude ActionView::RecordIdentifier+ include OnchainTestHelpersetup dosign_in @user = users(:family_admin)@@ -21,6 +22,55 @@ class AccountsControllerTest < ActionDispatch::IntegrationTestassert_select "##{dom_id(kraken_item)}"end+ test "index renders on-chain wallet items" do+ register_fake_chain!+ item = create_onchain_wallet_item(family: @user.family)+ onchain_account = create_onchain_wallet_account(item: item)+ onchain_account.ensure_account_provider!(accounts(:investment))++ get accounts_url++ assert_response :success+ # Without this the wallet is invisible here: its accounts carry a provider+ # link, so Account.manual excludes them, and no provider section claimed them.+ assert_select "##{dom_id(item, :accounts_index)}"+ ensure+ unregister_fake_chain!+ end++ test "index does not leak wallet accounts a member was never given" do+ register_fake_chain!+ admin = users(:family_admin)+ member = users(:family_member)+ item = create_onchain_wallet_item(family: admin.family)++ shared = accounts(:investment)+ unshared = accounts(:credit_card)+ [ shared, unshared ].each_with_index do |account, index|+ account.update!(owner: admin)+ account.account_shares.destroy_all+ row = create_onchain_wallet_account(+ item: item,+ address: "#{OnchainTestHelper::FAKE_ADDRESS}#{index}"+ )+ row.ensure_account_provider!(account)+ end+ shared.account_shares.create!(user: member, permission: "read_only")++ sign_in member+ get accounts_url++ assert_response :success+ assert_select "##{dom_id(item, :accounts_index)}"+ # The item is surfaced as soon as ONE of its accounts is accessible, so+ # rendering them all would hand a partially-authorised member the names and+ # balances of accounts nobody shared with them.+ assert_includes response.body, shared.name+ assert_not_includes response.body, unshared.name+ ensure+ unregister_fake_chain!+ end+test "should get show" doget account_url(@account)assert_response :successdiff --git a/test/models/onchain_wallet_account_test.rb b/test/models/onchain_wallet_account_test.rbindex bedb424eda..ad2a85390c 100644--- a/test/models/onchain_wallet_account_test.rb+++ b/test/models/onchain_wallet_account_test.rb@@ -93,6 +93,27 @@ class OnchainWalletAccountTest < ActiveSupport::TestCaseendend+ test "unlinking the account drops the tracking row and leaves the account manual" do+ onchain_account = create_onchain_wallet_account(item: @item)+ account = accounts(:investment)+ provider_link = onchain_account.ensure_account_provider!(account)+ holding = holdings(:one)+ holding.update!(account_provider: provider_link)++ assert_difference "OnchainWalletAccount.count", -1 do+ account.account_providers.destroy_all+ end++ # What the user asked for is to stop tracking the address, not to lose the+ # account: the Sure account and its holdings stay, as manual ones.+ assert Account.exists?(account.id)+ assert Holding.exists?(holding.id)+ assert_nil holding.reload.account_provider_id+ # Left behind, the row would stop syncing while its partial unique index+ # still held the slot, so linking the same asset again would collide.+ assert_not OnchainWalletAccount.exists?(onchain_account.id)+ end+test "a native row and token rows coexist for one address" docreate_onchain_wallet_account(item: @item)create_onchain_wallet_account(item: @item, asset: fake_token_asset(contract: "0xaaa"))