Skip to content

Commit

Permalink
Fix order book infinite loading (#125)
Browse files Browse the repository at this point in the history
* Add `orderbook_ticker` to Coin model and remove segwit pair

Remove

* Add segwit pair check before fetching orderbook
  • Loading branch information
takenagain authored Apr 29, 2024
1 parent e4b06c7 commit 66471b9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lib/model/coin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -166,6 +167,7 @@ class Coin {
String explorerTxUrl;
String explorerAddressUrl;
int decimals;
String orderbookTicker;

Map<String, dynamic> toJson() => <String, dynamic>{
'type': type.name ?? '',
Expand Down Expand Up @@ -195,6 +197,7 @@ class Coin {
if (lightWalletDServers != null)
'light_wallet_d_servers':
List<dynamic>.from(lightWalletDServers.map<String>((x) => x)),
if (orderbookTicker != null) 'orderbook_ticker': orderbookTicker
};

String getTxFeeSatoshi() {
Expand Down
35 changes: 32 additions & 3 deletions lib/model/order_book_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> _updateOrderBooks() async {
Expand Down Expand Up @@ -445,6 +452,10 @@ class SyncOrderbook {

List<Future> futures = pairs.map((pair) async {
final List<String> abbr = pair.split('/');
if (isCoinPairSegwitPair(abbr[0], abbr[1])) {
return;
}

final dynamic orderbook = await MM.getOrderbook(
mmSe.client,
GetOrderbook(
Expand All @@ -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();

Expand Down Expand Up @@ -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;
}

0 comments on commit 66471b9

Please sign in to comment.