Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions assets/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@
"nothingFound": "Nothing found",
"half": "Half",
"max": "Max",
"exact": "Exact",
"reactivating": "Reactivating",
"weFailedCoinActivate": "We failed to activate {} :(",
"failedActivate": "Failed to activate",
Expand Down
5 changes: 5 additions & 0 deletions lib/bloc/taker_form/taker_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ class TakerBloc extends Bloc<TakerEvent, TakerState> {
autovalidate: switchingCoin ? () => false : null,
));

// Auto-fill the exact maker amount when an order is selected
if (event.order != null) {
add(TakerSetSellAmount(event.order!.maxVolume));
}

if (!state.autovalidate) add(TakerVerifyOrderVolume());

await _autoActivateCoin(state.selectedOrder?.coin);
Expand Down
1 change: 1 addition & 0 deletions lib/generated/codegen_loader.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ abstract class LocaleKeys {
static const nothingFound = 'nothingFound';
static const half = 'half';
static const max = 'max';
static const exact = 'exact';
static const reactivating = 'reactivating';
static const weFailedCoinActivate = 'weFailedCoinActivate';
static const failedActivate = 'failedActivate';
Expand Down
6 changes: 4 additions & 2 deletions lib/shared/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,11 @@ Rational? fract2rat(Map<String, dynamic>? fract, [bool willLog = true]) {
if (fract == null) return null;

try {
final String numerStr = fract['numer'].toString();
final String denomStr = fract['denom'].toString();
final rat = Rational(
BigInt.from(double.parse(fract['numer'])),
BigInt.from(double.parse(fract['denom'])),
BigInt.parse(numerStr),
BigInt.parse(denomStr),
);
return rat;
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class _SellHeader extends StatelessWidget {
actions: [
Flexible(child: _AvailableGroup()),
const SizedBox(width: 8),
_ExactButton(),
const SizedBox(width: 3),
_MaxButton(),
const SizedBox(width: 3),
_HalfButton(),
Expand Down Expand Up @@ -85,3 +87,13 @@ class _MaxButton extends DexSmallButton {
context.read<TakerBloc>().add(TakerAmountButtonClick(1));
});
}

class _ExactButton extends DexSmallButton {
_ExactButton()
: super(LocaleKeys.exact.tr(), (context) {
final state = context.read<TakerBloc>().state;
final order = state.selectedOrder;
if (order == null) return;
context.read<TakerBloc>().add(TakerSetSellAmount(order.maxVolume));
});
}
21 changes: 21 additions & 0 deletions test_units/tests/utils/convert_fract_rat_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,25 @@ void testRatToFracAndViseVersa() {
Rational? result = fract2rat(invalidFract, false);
expect(result, isNull);
});

test('fract2rat handles very large integers without precision loss', () {
// 10^50 / 10^20 = 10^30
final numer = '100000000000000000000000000000000000000000000000000';
final denom = '100000000000000000000';
final rat = fract2rat({'numer': numer, 'denom': denom}, false)!;
expect(rat.numerator, BigInt.parse(numer));
expect(rat.denominator, BigInt.parse(denom));
// Ensure round-trip
final back = rat2fract(rat, false)!;
expect(back['numer'], numer);
expect(back['denom'], denom);
});

test('fract2rat correctly parses strings that would overflow double', () {
final numer = '340282366920938463463374607431768211457'; // > 2^128
final denom = '1';
final rat = fract2rat({'numer': numer, 'denom': denom}, false)!;
expect(rat.numerator, BigInt.parse(numer));
expect(rat.denominator, BigInt.one);
});
}
Loading