Skip to content

Commit

Permalink
Migrate BestOrders to mmrpc 2.0 (#110)
Browse files Browse the repository at this point in the history
* Migrate BestOrders to mmrpc 2.0

Leaving the BestOrder and BestOrders attributes unchanged to minimise the impact

* Fix empty best orders list

ARRR has no `address_data` as it is a `Shielded` address, so we cannot use address to filter ARRR top orders.
  • Loading branch information
takenagain authored Jan 31, 2024
1 parent 31a393f commit 10a6a74
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
28 changes: 20 additions & 8 deletions lib/model/best_order.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ class BestOrders {
if (json['result'] == null) return bestOrders;

final Market action = bestOrders.request.action;
final Map<String, dynamic> result = json['result'];
final Map<String, dynamic> resultOrders =
result['orders'] as Map<String, dynamic>;

json['result'].forEach((String ticker, dynamic items) {
resultOrders.forEach((String ticker, dynamic items) {
bestOrders.result ??= {};
final List<BestOrder> list = [];
for (dynamic item in items) {
for (final Map<String, dynamic> item in items) {
item['action'] = action;
item['other_coin'] =
action == Market.SELL ? bestOrders.request.coin : ticker;
Expand Down Expand Up @@ -45,15 +48,24 @@ class BestOrder {
});

factory BestOrder.fromJson(Map<String, dynamic> json) {
final Map<String, dynamic> price = json['price'];
final Map<String, dynamic> address = json['address'];

// base_ max and min volume are used, as the base and rel coins are swapped
// for buy and sell orders, so the max volume is always the max volume of
// the base coin. The web wallet has a similar implementation.
final Map<String, dynamic> maxVolume = json['base_max_volume'];
final Map<String, dynamic> minVolume = json['base_min_volume'];

return BestOrder(
price: fract2rat(json['price_fraction']) ?? Rational.parse(json['price']),
maxVolume: fract2rat(json['max_volume_fraction']) ??
Rational.parse(json['maxvolume']),
minVolume: fract2rat(json['min_volume_fraction']) ??
Rational.parse(json['min_volume']),
price: fract2rat(price['fraction']) ?? Rational.parse(price['decimal']),
maxVolume: fract2rat(maxVolume['fraction']) ??
Rational.parse(maxVolume['decimal']),
minVolume: fract2rat(minVolume['fraction']) ??
Rational.parse(minVolume['decimal']),
coin: json['coin'],
otherCoin: json['other_coin'],
address: json['address'],
address: address['address_data'],
action: json['action'],
);
}
Expand Down
17 changes: 11 additions & 6 deletions lib/model/get_best_orders.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,30 @@ class GetBestOrders {
GetBestOrders({
this.userpass,
this.method = 'best_orders',
this.mmrpc = '2.0',
this.coin,
this.volume,
this.action,
});

String userpass;
String method;
String mmrpc;
String coin;
Rational volume;
Market action;

Map<String, dynamic> toJson() => <String, dynamic>{
'method': method,
'userpass': userpass,
'coin': coin,
'volume': {
'numer': volume.numerator.toString(),
'denom': volume.denominator.toString(),
},
'action': action == Market.BUY ? 'buy' : 'sell',
'mmrpc': mmrpc,
'params': {
'coin': coin,
'action': action == Market.BUY ? 'buy' : 'sell',
'request_by': {
'type': 'volume',
'value': volume.toDecimalString(),
}
}
};
}
9 changes: 8 additions & 1 deletion lib/model/swap_constructor_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,18 @@ class ConstructorProvider extends ChangeNotifier {

BestOrder getTickerTopOrder(List<BestOrder> tickerOrdersList, Market type) {
final List<BestOrder> sorted = List.from(tickerOrdersList);
// This code appears to remove orders placed by the current wallet
// so that the user doesn't trade with themselves
sorted.removeWhere((BestOrder order) {
final String coin =
order.action == Market.SELL ? order.coin : order.otherCoin;
final CoinBalance coinBalance = coinsBloc.getBalanceByAbbr(coin);
if (coinBalance == null) return false;

// ZHTLC address is null (Shielded), so we can't check it.
// This removes the protection against users taking their own maker orders
if (coinBalance == null || order.address == null) {
return false;
}
return coinBalance.balance.address.toLowerCase() ==
order.address.toLowerCase();
});
Expand Down

0 comments on commit 10a6a74

Please sign in to comment.