Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(prices): resolve dashed crypto tickers (BTC-USD, TRX-USD) via BinancePublic (#2372)

Artem Danilovfffcaf23458e
diff --git a/app/models/provider/binance_public.rb b/app/models/provider/binance_public.rb
index cc4a71f7c5..6dc5b5c205 100644
--- a/app/models/provider/binance_public.rb
+++ b/app/models/provider/binance_public.rb
@@ -41,6 +41,11 @@ class Provider::BinancePublic < Provider
# enough and avoids surfacing transient depeg ticks from market data.
USD_STABLECOINS = %w[USDT USDC BUSD DAI FDUSD TUSD USDP PYUSD].freeze
+ # Trailing separator between base and quote in a stored pair ticker
+ # ("BTC-USD", "BTC/USD", "BTC_USD"), stripped so parse_ticker builds a valid
+ # Binance pair.
+ QUOTE_SEPARATOR = /[-\/_ ]\z/
+
# Symbol prefix applied by holdings processors (CoinStats, Coinbase, Kraken,
# Binance, SimpleFIN, Lunchflow) to distinguish crypto from stock tickers.
CRYPTO_PREFIX = "CRYPTO:".freeze
@@ -68,12 +73,23 @@ class Provider::BinancePublic < Provider
def search_securities(symbol, country_code: nil, exchange_operating_mic: nil)
with_provider_response do
- query = symbol.to_s.strip.upcase.delete_prefix(CRYPTO_PREFIX)
+ # Collapse separators so the canonical "BTC-USD" / "TRX/USDT" form is
+ # treated like Binance's own unseparated "BTCUSD" — the existing narrow
+ # matching then resolves it instead of falling through to an offline row.
+ query = symbol.to_s.strip.upcase.delete_prefix(CRYPTO_PREFIX).gsub(/[\s\-\/_]/, "")
next [] if query.empty?
- if USD_STABLECOINS.include?(query)
- next [ stablecoin_search_result(query) ]
+ # Stablecoins have no Binance self-pair, so a USD-quoted query gets a
+ # synthetic 1.0 USD result: the bare coin ("USDT", or coins that end in
+ # USD like PYUSD/FDUSD), or the "<coin>USD" form ("USDTUSD", from a pasted
+ # "USDT-USD"). A non-USD quote like "USDTEUR" has a real Binance pair and
+ # must fall through to normal matching, so only the USD case short-circuits.
+ stablecoin = if USD_STABLECOINS.include?(query)
+ query
+ elsif query.end_with?("USD") && USD_STABLECOINS.include?(query.delete_suffix("USD"))
+ query.delete_suffix("USD")
end
+ next [ stablecoin_search_result(stablecoin) ] if stablecoin
symbols = exchange_info_symbols
@@ -86,8 +102,8 @@ class Provider::BinancePublic < Provider
# Match on either the base asset (so "BTC" surfaces every BTC pair) or
# the full Binance pair symbol (so users pasting their own portfolio
- # ticker like "BTCEUR" or "BTCUSD" — which prefixes Binance's raw
- # "BTCUSDT" — also hit a result).
+ # ticker like "BTCEUR", "BTCUSD", or "BTC-USD" — which prefix Binance's
+ # raw "BTCUSDT" once separators are collapsed — also hit a result).
base.include?(query) || symbol == query || symbol.start_with?(query)
end
@@ -319,7 +335,9 @@ class Provider::BinancePublic < Provider
display_currency = QUOTE_TO_CURRENCY[quote]
next unless ticker_up.end_with?(display_currency)
- base = ticker_up.delete_suffix(display_currency)
+ # Strip the quote plus any separator so the canonical "BTC-USD" form
+ # yields "BTC" (not "BTC-") and builds a valid "BTCUSDT" pair.
+ base = ticker_up.delete_suffix(display_currency).sub(QUOTE_SEPARATOR, "")
next if base.empty?
# "{stablecoin}USD" form (e.g. "USDTUSD" produced by search_securities)
diff --git a/test/models/provider/binance_public_test.rb b/test/models/provider/binance_public_test.rb
index bf04760431..5fad8dc1e1 100644
--- a/test/models/provider/binance_public_test.rb
+++ b/test/models/provider/binance_public_test.rb
@@ -100,6 +100,22 @@ class Provider::BinancePublicTest < ActiveSupport::TestCase
assert_equal "USD", parsed[:display_currency]
end
+ test "parse_ticker strips a separator before the quote (TRX-USD)" do
+ parsed = @provider.send(:parse_ticker, "TRX-USD")
+ refute parsed[:stablecoin]
+ # Without separator handling this would build the invalid pair "TRX-USDT".
+ assert_equal "TRXUSDT", parsed[:binance_pair]
+ assert_equal "TRX", parsed[:base]
+ assert_equal "USD", parsed[:display_currency]
+ end
+
+ test "parse_ticker treats a separated stablecoin (USDT-USD) as stablecoin" do
+ parsed = @provider.send(:parse_ticker, "USDT-USD")
+ assert parsed[:stablecoin]
+ assert_nil parsed[:binance_pair]
+ assert_equal "USDT", parsed[:base]
+ end
+
test "search_securities returns empty array when query does not match" do
@provider.stubs(:exchange_info_symbols).returns(sample_exchange_info)
@@ -146,6 +162,62 @@ class Provider::BinancePublicTest < ActiveSupport::TestCase
assert_equal [ "BTCUSD" ], tickers
end
+ test "search_securities resolves the canonical dashed BASE-USD form" do
+ @provider.stubs(:exchange_info_symbols).returns(sample_exchange_info)
+
+ # "BTC-USD" (Yahoo's crypto format, and what users paste) must resolve to
+ # the same single USD variant as "BTCUSD" — not fall through to offline.
+ response = @provider.search_securities("BTC-USD")
+
+ assert response.success?
+ assert_equal [ "BTCUSD" ], response.data.map(&:symbol)
+ end
+
+ test "search_securities resolves a dashed crypto the stock provider may miss (TRX-USD)" do
+ info = [ info_row("TRX", "USDT"), info_row("BTC", "USDT") ]
+ @provider.stubs(:exchange_info_symbols).returns(info)
+
+ response = @provider.search_securities("TRX-USD")
+
+ assert response.success?
+ assert_equal [ "TRXUSD" ], response.data.map(&:symbol)
+ end
+
+ test "search_securities accepts a slash separator (BTC/USD)" do
+ @provider.stubs(:exchange_info_symbols).returns(sample_exchange_info)
+
+ response = @provider.search_securities("BTC/USD")
+
+ assert response.success?
+ assert_equal [ "BTCUSD" ], response.data.map(&:symbol)
+ end
+
+ test "search_securities treats a dashed stablecoin (USDT-USD) as synthetic without hitting exchangeInfo" do
+ @provider.expects(:exchange_info_symbols).never
+
+ response = @provider.search_securities("USDT-USD")
+
+ assert response.success?
+ assert_equal 1, response.data.size
+ row = response.data.first
+ assert_equal "USDTUSD", row.symbol
+ assert_equal "USDT", row.name
+ assert_equal "USD", row.currency
+ end
+
+ test "search_securities keeps a native non-USD stablecoin pair (USDT-EUR) over the USD synthetic" do
+ info = [ info_row("USDT", "EUR"), info_row("BTC", "USDT") ]
+ @provider.stubs(:exchange_info_symbols).returns(info)
+
+ # Only USD-quoted stablecoins synthesize; a real EUR pair must survive.
+ response = @provider.search_securities("USDT-EUR")
+
+ assert response.success?
+ row = response.data.find { |s| s.symbol == "USDTEUR" }
+ assert_not_nil row, "expected native USDT/EUR pair, got #{response.data.map(&:symbol).inspect}"
+ assert_equal "EUR", row.currency
+ end
+
test "search_securities ranks exact symbol match above base prefix match" do
info = [
info_row("BTC", "USDT"), # base="BTC", symbol="BTCUSDT"