Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';
import 'dart:io';

import 'package:decimal/decimal.dart';
import 'package:rational/rational.dart';
import 'package:komodo_defi_rpc_methods/komodo_defi_rpc_methods.dart';
import 'package:test/test.dart';

Expand Down Expand Up @@ -85,24 +86,87 @@ void main() {

group('TradePreimageResponse', () {
test('parses fixture and round trips', () {
final fixture = File('test/fixtures/swaps/trade_preimage.json').readAsStringSync();
final fixture =
File('test/fixtures/swaps/trade_preimage.json').readAsStringSync();
final json = jsonDecode(fixture) as Map<String, dynamic>;

final response = TradePreimageResponse.fromJson(json);

expect(response.mmrpc, '2.0');
expect(response.result.totalFees.length, 2);

// Adjust expected JSON to match serialization format
// Use a robust comparison for rational/decimal values
final expected = Map<String, dynamic>.from(json);
final fees = List<Map<String, dynamic>>.from((json['result'] as Map<String, dynamic>)['total_fees'] as List);
fees[1]['required_balance_rat'][0] = [
1,
[0],
];
expected['result'] = Map<String, dynamic>.from(json['result'] as Map)..['total_fees'] = fees;

expect(response.toJson(), expected);
expect(
deepEqualsWithRationalTolerance(response.toJson(), expected),
isTrue,
reason:
'Serialized response does not match expected fixture (with rational/decimal tolerance)',
);
});
});
}

/// Recursively compares two objects, treating rational/decimal representations as equal if numerically equivalent.
Copy link

Copilot AI Aug 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation should clarify the expected format of rational representations (e.g., [numerator, [denominator1, denominator2, ...]] for chained division) and provide examples of what constitutes a rational list.

Suggested change
/// Recursively compares two objects, treating rational/decimal representations as equal if numerically equivalent.
/// Recursively compares two objects, treating rational/decimal representations as equal if numerically equivalent.
///
/// Rational representations are expected to be lists of the form:
/// [numerator, [denominator1, denominator2, ...]]
/// This format represents chained division, i.e.:
/// numerator / denominator1 / denominator2 / ...
///
/// Examples of rational lists:
/// [1, [2]] // represents 1/2
/// [3, [4, 5]] // represents 3/4/5 = 3/20
/// [7, [1]] // represents 7/1 = 7
///
/// Both compared objects are considered equal if their rational/decimal values are numerically equivalent.

Copilot uses AI. Check for mistakes.
///
/// Rational representations are expected to be lists of the form:
/// `[numerator, [denominator1, denominator2, ...]]`
/// This format represents chained division, i.e.:
/// `numerator / denominator1 / denominator2 / ...`
///
/// Examples of rational lists:
/// `[1, [2]]` // represents 1/2
/// `[3, [4, 5]]` // represents 3/4/5 = 3/20
/// `[7, [1]]` // represents 7/1 = 7
///
/// Both compared objects are considered equal if their rational/decimal values are
/// numerically equivalent.
bool deepEqualsWithRationalTolerance(dynamic a, dynamic b) {
if (a is Map && b is Map) {
if (a.length != b.length) return false;
for (final key in a.keys) {
if (!b.containsKey(key)) return false;
if (!deepEqualsWithRationalTolerance(a[key], b[key])) return false;
}
return true;
}
// Handle rational/decimal representations before generic list comparison
if (_isRationalList(a) && _isRationalList(b)) {
final ra = _rationalListToRational(a as List<dynamic>);
final rb = _rationalListToRational(b as List<dynamic>);
return ra == rb;
}
if (a is List && b is List) {
if (a.length != b.length) return false;
for (int i = 0; i < a.length; i++) {
if (!deepEqualsWithRationalTolerance(a[i], b[i])) return false;
}
return true;
}
return a == b;
}

bool _isRationalList(dynamic x) {
return x is List &&
x.length == 2 &&
x[0] is int &&
x[1] is List &&
(x[1] as List).every((e) => e is int);
}

Rational _rationalListToRational(List<dynamic> rat) {
final int numerator = rat[0] as int;
final List<int> denominators = List<int>.from(rat[1] as List);
if (denominators.any((d) => d == 0)) {
if (numerator == 0 || numerator == 1) {
return Rational.zero;
}
throw ArgumentError('Denominator cannot be zero in rational list: $rat');
}
Rational value = Rational.fromInt(numerator);
for (final d in denominators) {
Copy link

Copilot AI Aug 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Division by zero is not handled. If any denominator is 0, this will cause a runtime error. Add validation to check for zero denominators before division.

Suggested change
for (final d in denominators) {
for (final d in denominators) {
if (d == 0) {
throw ArgumentError('Denominator cannot be zero in rational list: $rat');
}

Copilot uses AI. Check for mistakes.
value = value / Rational.fromInt(d);
}
return value;
}
Loading