From 66471b968c701451711a214be950fe6c114e8f6b Mon Sep 17 00:00:00 2001 From: Francois Date: Mon, 29 Apr 2024 21:34:59 +0200 Subject: [PATCH] Fix order book infinite loading (#125) * Add `orderbook_ticker` to Coin model and remove segwit pair Remove * Add segwit pair check before fetching orderbook --- lib/model/coin.dart | 3 +++ lib/model/order_book_provider.dart | 35 +++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/lib/model/coin.dart b/lib/model/coin.dart index 8a67a50b9..aeb6eb84b 100644 --- a/lib/model/coin.dart +++ b/lib/model/coin.dart @@ -117,6 +117,7 @@ class Coin { explorerTxUrl = config['explorer_tx_url'] ?? ''; explorerAddressUrl = config['explorer_address_url'] ?? ''; decimals = init['decimals']; + orderbookTicker = config['orderbook_ticker']; } // Coin suspended if was activated by user earlier, @@ -166,6 +167,7 @@ class Coin { String explorerTxUrl; String explorerAddressUrl; int decimals; + String orderbookTicker; Map toJson() => { 'type': type.name ?? '', @@ -195,6 +197,7 @@ class Coin { if (lightWalletDServers != null) 'light_wallet_d_servers': List.from(lightWalletDServers.map((x) => x)), + if (orderbookTicker != null) 'orderbook_ticker': orderbookTicker }; String getTxFeeSatoshi() { diff --git a/lib/model/order_book_provider.dart b/lib/model/order_book_provider.dart index 942574370..0e64e1b72 100644 --- a/lib/model/order_book_provider.dart +++ b/lib/model/order_book_provider.dart @@ -269,7 +269,14 @@ class SyncOrderbook { }); list.sort((a, b) => a.pair.rel.compareTo(b.pair.rel)); - return list; + + return list.where((OrderbookDepth item) { + final bool sameAsOrderbookTicker = item.pair.base == coin.orderbookTicker; + final bool sameAsSegwitPair = + isCoinPairSegwitPair(item.pair.base, item.pair.rel); + + return !sameAsOrderbookTicker && !sameAsSegwitPair; + }).toList(); } Future _updateOrderBooks() async { @@ -445,6 +452,10 @@ class SyncOrderbook { List futures = pairs.map((pair) async { final List abbr = pair.split('/'); + if (isCoinPairSegwitPair(abbr[0], abbr[1])) { + return; + } + final dynamic orderbook = await MM.getOrderbook( mmSe.client, GetOrderbook( @@ -455,8 +466,10 @@ class SyncOrderbook { if (orderbook is Orderbook) { orderbooks[pair] = orderbook; } else if (orderbook is ErrorString) { - Log('order_book_provider] _updateOrderBooksAsync', - '$pair: ${orderbook.error}'); + Log( + 'order_book_provider] _updateOrderBooksAsync', + '$pair: ${orderbook.error}', + ); } }).toList(); @@ -518,3 +531,19 @@ class SyncOrderbook { // await syncOrderbook._saveOrderbookSnapshot(); } } + +/// Returns true if [base] and [rel] are a segwit pair. +/// Example: isCoinPairSegwitPair('BTC-segwit', 'BTC') == true +bool isCoinPairSegwitPair(String base, String rel) { + final bool isBaseSegwit = base.toLowerCase().contains('segwit'); + if (isBaseSegwit) { + return base.replaceAll('segwit', '').replaceAll('-', '').contains(rel); + } + + final bool isRelSegwit = rel.toLowerCase().contains('segwit'); + if (isRelSegwit) { + return rel.replaceAll('segwit', '').replaceAll('-', '').contains(base); + } + + return false; +}