Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Resolve Trade Republic crypto to shared crypto securities (#3806)

Niels Eulinkdeb441032e68
diff --git a/app/models/trade_republic_account/data_helpers.rb b/app/models/trade_republic_account/data_helpers.rb
index 9bcd4e264f..0f62c89d2b 100644
--- a/app/models/trade_republic_account/data_helpers.rb
+++ b/app/models/trade_republic_account/data_helpers.rb
@@ -95,13 +95,26 @@ module TradeRepublicAccount::DataHelpers
}.freeze
OFFLINE_ISIN_REASON = "trade_republic_isin"
+ # Trade Republic lists crypto under pseudo-ISINs that embed the coin
+ # symbol: XF000BTC0017 is Bitcoin, XF000ETH0019 Ether. Digits pad the
+ # symbol up to the check digit.
+ CRYPTO_ISIN_PATTERN = /\AXF000(?=[A-Z0-9]{7}\z)([A-Z][A-Z0-9]*[A-Z])\d+\z/
+
# Resolve (or create) a Security from a Trade Republic position/trade.
- # Prefer an exact exchange ticker when the client supplied one; otherwise
- # keep the ISIN as ticker but mark the security offline so market-data
- # importers skip it while snapshot prices still value the holding.
+ # Crypto maps onto the shared CRYPTO:<SYMBOL> security when a crypto price
+ # provider is enabled. Otherwise prefer an exact exchange ticker when the
+ # client supplied one, else keep the ISIN as ticker but mark the security
+ # offline so market-data importers skip it while snapshot prices still
+ # value the holding.
def resolve_security(isin, name, symbol: nil, exchange_slug: nil)
return nil if isin.blank?
+ crypto_security = resolve_crypto_security(isin, name)
+ if crypto_security
+ rematch_account_from_isin!(isin, crypto_security)
+ return crypto_security
+ end
+
usable_symbol, mic = exchange_listing_for(isin, symbol: symbol, exchange_slug: exchange_slug)
if usable_symbol
security = cached_exchange_security(usable_symbol, mic, name)
@@ -112,6 +125,23 @@ module TradeRepublicAccount::DataHelpers
resolve_offline_isin_security(isin, name)
end
+ # Holdings and trades share this path, so both land on the same security.
+ # Without the crypto price provider a CRYPTO: security could not be priced
+ # at all; the offline ISIN keeps Trade Republic's own snapshot prices, and
+ # the rematch moves it over once the provider is enabled.
+ def resolve_crypto_security(isin, name)
+ symbol = crypto_symbol_for_isin(isin)
+ return nil if symbol.blank?
+ return nil unless Onchain::SecurityResolver.price_provider_enabled?
+
+ @crypto_securities ||= {}
+ @crypto_securities[symbol] ||= Onchain::SecurityResolver.resolve(symbol: symbol, name: name)
+ end
+
+ def crypto_symbol_for_isin(isin)
+ isin.to_s.strip.upcase[CRYPTO_ISIN_PATTERN, 1]
+ end
+
# [symbol, mic] when Trade Republic supplied a usable exchange ticker.
def exchange_listing_for(isin, symbol: nil, exchange_slug: nil)
position = position_metadata_for(isin)
diff --git a/test/models/trade_republic_account_activities_processor_test.rb b/test/models/trade_republic_account_activities_processor_test.rb
index 2f73b4e53b..f1d0506916 100644
--- a/test/models/trade_republic_account_activities_processor_test.rb
+++ b/test/models/trade_republic_account_activities_processor_test.rb
@@ -32,6 +32,21 @@ class TradeRepublicAccountActivitiesProcessorTest < ActiveSupport::TestCase
assert_equal BigDecimal("-2472.14"), trade.amount
end
+ test "crypto trades resolve to the same crypto security as crypto holdings" do
+ Setting.stubs(:enabled_securities_providers).returns(%w[twelve_data binance_public])
+ @tr_account.update!(raw_positions_payload: [
+ { "isin" => "XF000BTC0017", "name" => "Bitcoin", "category" => "crypto_wallet", "quantity" => "0.000134", "price" => "72688.64" }
+ ])
+ TradeRepublicAccount::HoldingsProcessor.new(@tr_account.reload).process
+
+ import_event(order_execution_detail(quantity: "0.000134", isin: "XF000BTC0017", amount: "11.00")
+ .deep_merge(detail: { name: "Bitcoin", symbol: "BTCEUR", exchange_slug: "BHS" }))
+
+ trade = find_trade("trade_republic_event_evt_buy").entryable
+ assert_equal "CRYPTO:BTC", trade.security.ticker
+ assert_equal @account.holdings.sole.security, trade.security
+ end
+
test "fractional quantity retains exact precision" do
import_event(order_execution_detail(quantity: "13.439945", isin: "US0378331005", amount: "2472.14"))
diff --git a/test/models/trade_republic_account_holdings_processor_test.rb b/test/models/trade_republic_account_holdings_processor_test.rb
index 7169955887..d2f39a9aa6 100644
--- a/test/models/trade_republic_account_holdings_processor_test.rb
+++ b/test/models/trade_republic_account_holdings_processor_test.rb
@@ -511,6 +511,37 @@ class TradeRepublicAccountHoldingsProcessorTest < ActiveSupport::TestCase
assert_equal "TGAT", security.exchange_operating_mic
end
+ test "crypto positions resolve to the shared crypto security when crypto prices are enabled" do
+ Setting.stubs(:enabled_securities_providers).returns(%w[twelve_data binance_public])
+
+ import_position(isin: "XF000BTC0017", quantity: "0.000134", price: "72688.64")
+
+ security = @account.holdings.sole.security
+ assert_equal "CRYPTO:BTC", security.ticker
+ assert_equal "binance_public", security.price_provider
+ assert security.crypto?
+ end
+
+ test "crypto positions keep the offline ISIN security without a crypto price provider" do
+ Setting.stubs(:enabled_securities_providers).returns(%w[twelve_data])
+
+ import_position(isin: "XF000BTC0017", quantity: "0.000134", price: "72688.64")
+
+ security = @account.holdings.sole.security
+ assert_equal "XF000BTC0017", security.ticker
+ assert security.offline?
+ end
+
+ test "holdings move from the offline crypto ISIN once crypto prices are enabled" do
+ Setting.stubs(:enabled_securities_providers).returns(%w[twelve_data])
+ import_position(isin: "XF000BTC0017", quantity: "0.000134", price: "72688.64")
+
+ Setting.stubs(:enabled_securities_providers).returns(%w[twelve_data binance_public])
+ import_position(isin: "XF000BTC0017", quantity: "0.000134", price: "72688.64")
+
+ assert_equal [ "CRYPTO:BTC" ], @account.holdings.includes(:security).map { |holding| holding.security.ticker }
+ end
+
private
def import_position(isin:, quantity:, price:, average_cost: nil, symbol: nil, exchange_slug: nil)