diff --git a/mobile/lib/common/domain/model.dart b/mobile/lib/common/domain/model.dart index 236403988..e83d5be4b 100644 --- a/mobile/lib/common/domain/model.dart +++ b/mobile/lib/common/domain/model.dart @@ -108,7 +108,7 @@ class Usd { String formatted() { final formatter = NumberFormat("#,###,###,###,###", "en"); - return formatter.format(_usd); + return formatter.format(_usd.toDouble()); } @override diff --git a/mobile/lib/features/trade/application/order_service.dart b/mobile/lib/features/trade/application/order_service.dart index 2d887212b..90ee90570 100644 --- a/mobile/lib/features/trade/application/order_service.dart +++ b/mobile/lib/features/trade/application/order_service.dart @@ -6,8 +6,8 @@ import 'package:get_10101/features/trade/domain/order.dart'; import 'package:get_10101/ffi.dart' as rust; class OrderService { - Future submitMarketOrder(Leverage leverage, Amount quantity, - ContractSymbol contractSymbol, Direction direction, bool stable) async { + Future submitMarketOrder(Leverage leverage, Usd quantity, ContractSymbol contractSymbol, + Direction direction, bool stable) async { rust.NewOrder order = rust.NewOrder( leverage: leverage.leverage, quantity: quantity.asDouble(), @@ -21,7 +21,7 @@ class OrderService { Future submitChannelOpeningMarketOrder( Leverage leverage, - Amount quantity, + Usd quantity, ContractSymbol contractSymbol, Direction direction, bool stable, diff --git a/mobile/lib/features/trade/application/trade_values_service.dart b/mobile/lib/features/trade/application/trade_values_service.dart index 995276cee..5e98e7f24 100644 --- a/mobile/lib/features/trade/application/trade_values_service.dart +++ b/mobile/lib/features/trade/application/trade_values_service.dart @@ -5,10 +5,7 @@ import 'package:get_10101/ffi.dart' as rust; class TradeValuesService { Amount? calculateMargin( - {required double? price, - required Amount? quantity, - required Leverage leverage, - dynamic hint}) { + {required double? price, required Usd? quantity, required Leverage leverage, dynamic hint}) { if (price == null || quantity == null) { return null; } else { @@ -17,14 +14,14 @@ class TradeValuesService { } } - Amount? calculateQuantity( + Usd? calculateQuantity( {required double? price, required Amount? margin, required Leverage leverage, dynamic hint}) { if (price == null || margin == null) { return null; } else { final quantity = rust.api .calculateQuantity(price: price, margin: margin.sats, leverage: leverage.leverage); - return Amount(quantity.ceil()); + return Usd(quantity.ceil()); } } @@ -41,7 +38,7 @@ class TradeValuesService { } } - Amount? orderMatchingFee({required Amount? quantity, required double? price}) { + Amount? orderMatchingFee({required Usd? quantity, required double? price}) { return quantity != null && price != null ? Amount(rust.api.orderMatchingFee(quantity: quantity.asDouble(), price: price)) : null; diff --git a/mobile/lib/features/trade/domain/position.dart b/mobile/lib/features/trade/domain/position.dart index 186871f75..09277fb34 100644 --- a/mobile/lib/features/trade/domain/position.dart +++ b/mobile/lib/features/trade/domain/position.dart @@ -26,7 +26,7 @@ enum PositionState { class Position { final Leverage leverage; - final Amount quantity; + final Usd quantity; final ContractSymbol contractSymbol; final Direction direction; final double averageEntryPrice; @@ -65,7 +65,7 @@ class Position { static Position fromApi(bridge.Position position) { return Position( leverage: Leverage(position.leverage), - quantity: Amount(position.quantity.ceil()), + quantity: Usd(position.quantity.ceil()), contractSymbol: ContractSymbol.fromApi(position.contractSymbol), direction: Direction.fromApi(position.direction), positionState: PositionState.fromApi(position.positionState), diff --git a/mobile/lib/features/trade/domain/trade_values.dart b/mobile/lib/features/trade/domain/trade_values.dart index 31bccd5aa..d7c0d4da8 100644 --- a/mobile/lib/features/trade/domain/trade_values.dart +++ b/mobile/lib/features/trade/domain/trade_values.dart @@ -9,7 +9,7 @@ class TradeValues { Direction direction; // These values can be null if coordinator is down - Amount? quantity; + Usd? quantity; double? price; double? liquidationPrice; Amount? fee; // This fee is an estimate of the order-matching fee. @@ -33,7 +33,7 @@ class TradeValues { required this.tradeValuesService}); factory TradeValues.fromQuantity( - {required Amount quantity, + {required Usd quantity, required Leverage leverage, required double? price, required double fundingRate, @@ -70,7 +70,7 @@ class TradeValues { required double fundingRate, required Direction direction, required TradeValuesService tradeValuesService}) { - Amount? quantity = + Usd? quantity = tradeValuesService.calculateQuantity(price: price, margin: margin, leverage: leverage); double? liquidationPrice = price != null ? tradeValuesService.calculateLiquidationPrice( @@ -94,7 +94,7 @@ class TradeValues { tradeValuesService: tradeValuesService); } - updateQuantity(Amount quantity) { + updateQuantity(Usd quantity) { this.quantity = quantity; _recalculateMargin(); _recalculateFee(); @@ -139,7 +139,7 @@ class TradeValues { } _recalculateQuantity() { - Amount? quantity = + Usd? quantity = tradeValuesService.calculateQuantity(price: price, margin: margin, leverage: leverage); this.quantity = quantity; } diff --git a/mobile/lib/features/trade/trade_bottom_sheet_tab.dart b/mobile/lib/features/trade/trade_bottom_sheet_tab.dart index e855955aa..342eca04f 100644 --- a/mobile/lib/features/trade/trade_bottom_sheet_tab.dart +++ b/mobile/lib/features/trade/trade_bottom_sheet_tab.dart @@ -49,6 +49,11 @@ class _TradeBottomSheetTabState extends State { bool showCapacityInfo = false; + bool marginInputFieldEnabled = false; + bool quantityInputFieldEnabled = true; + + // Amount quantity = Amount(sats); + @override void initState() { provider = context.read(); @@ -224,21 +229,21 @@ class _TradeBottomSheetTabState extends State { children: [ Flexible( child: AmountInputField( - initialValue: tradeValues.quantity ?? Amount.zero(), + initialValue: Amount(tradeValues.quantity?.toInt ?? 0), hint: "e.g. 100 USD", label: "Quantity (USD)", onChanged: (value) { - Amount quantity = Amount.zero(); + Usd quantity = Usd.zero(); try { if (value.isNotEmpty) { - quantity = Amount.parseAmount(value); + quantity = Usd.parseString(value); } context.read().updateQuantity(direction, quantity); } on Exception { context .read() - .updateQuantity(direction, Amount.zero()); + .updateQuantity(direction, Usd.zero()); } _formKey.currentState?.validate(); }, diff --git a/mobile/lib/features/trade/trade_value_change_notifier.dart b/mobile/lib/features/trade/trade_value_change_notifier.dart index 5c21a8da3..b7afc9444 100644 --- a/mobile/lib/features/trade/trade_value_change_notifier.dart +++ b/mobile/lib/features/trade/trade_value_change_notifier.dart @@ -24,7 +24,7 @@ class TradeValuesChangeNotifier extends ChangeNotifier implements Subscriber { } TradeValues _initOrder(Direction direction) { - Amount defaultQuantity = Amount(500); + Usd defaultQuantity = Usd(500); Leverage defaultLeverage = Leverage(2); switch (direction) { @@ -71,7 +71,7 @@ class TradeValuesChangeNotifier extends ChangeNotifier implements Subscriber { return fromDirection(direction).fee; } - void updateQuantity(Direction direction, Amount quantity) { + void updateQuantity(Direction direction, Usd quantity) { fromDirection(direction).updateQuantity(quantity); notifyListeners(); } diff --git a/mobile/lib/features/wallet/receive/receive_usdp_dialog.dart b/mobile/lib/features/wallet/receive/receive_usdp_dialog.dart index 377f2e5ef..5e30dce2b 100644 --- a/mobile/lib/features/wallet/receive/receive_usdp_dialog.dart +++ b/mobile/lib/features/wallet/receive/receive_usdp_dialog.dart @@ -66,7 +66,7 @@ Widget createSubmitWidget( bottomText = "Sorry, we couldn't match your order. Please try again later."; break; case PendingOrderState.orderFilled: - var amount = pendingOrder.tradeValues?.quantity?.sats ?? "0"; + var amount = pendingOrder.tradeValues?.quantity?.toInt ?? "0"; bottomText = "Congratulations! You received $amount USDP."; break; } @@ -80,9 +80,7 @@ Widget createSubmitWidget( runSpacing: 10, children: [ ValueDataRow( - type: ValueType.fiat, - value: pendingOrderValues?.quantity?.sats.toDouble(), - label: "USDP"), + type: ValueType.fiat, value: pendingOrderValues?.quantity?.toInt, label: "USDP"), ValueDataRow( type: ValueType.amount, value: pendingOrderValues?.margin, label: "Margin"), ValueDataRow( diff --git a/mobile/lib/features/wallet/send/confirm_payment_modal.dart b/mobile/lib/features/wallet/send/confirm_payment_modal.dart index c6342fca4..a9166afac 100644 --- a/mobile/lib/features/wallet/send/confirm_payment_modal.dart +++ b/mobile/lib/features/wallet/send/confirm_payment_modal.dart @@ -17,7 +17,7 @@ import 'package:provider/provider.dart'; import 'package:slide_to_confirm/slide_to_confirm.dart'; void showConfirmPaymentModal( - BuildContext context, Destination destination, bool payWithUsdp, Amount sats, Amount usdp, + BuildContext context, Destination destination, bool payWithUsdp, Amount sats, Usd usdp, {Fee? fee}) { logger.i(fee); showModalBottomSheet( @@ -45,7 +45,7 @@ class ConfirmPayment extends StatelessWidget { final Destination destination; final bool payWithUsdp; final Amount sats; - final Amount usdp; + final Usd usdp; final Fee? fee; const ConfirmPayment( diff --git a/mobile/lib/features/wallet/send/send_onchain_screen.dart b/mobile/lib/features/wallet/send/send_onchain_screen.dart index 4e337b585..d0cabcb96 100644 --- a/mobile/lib/features/wallet/send/send_onchain_screen.dart +++ b/mobile/lib/features/wallet/send/send_onchain_screen.dart @@ -320,8 +320,15 @@ class _SendOnChainScreenState extends State { width: MediaQuery.of(context).size.width * 0.9, child: ElevatedButton( onPressed: (_formKey.currentState?.validate() ?? false) - ? () => showConfirmPaymentModal(context, widget.destination, false, - _amount ?? Amount.zero(), _amount ?? Amount.zero(), fee: _fee) + ? () => showConfirmPaymentModal( + context, + widget.destination, + false, + _amount ?? Amount.zero(), + // this value doesn't matter at the moment because we do not support USDP sending + // TODO: remove USDP leftovers + Usd.zero(), + fee: _fee) : null, style: ButtonStyle( padding: diff --git a/mobile/test/trade_test.dart b/mobile/test/trade_test.dart index 7710f9d9a..2b6d1326b 100644 --- a/mobile/test/trade_test.dart +++ b/mobile/test/trade_test.dart @@ -102,7 +102,7 @@ void main() { .thenReturn(10000); when(tradeValueService.calculateQuantity( price: anyNamed('price'), leverage: anyNamed('leverage'), margin: anyNamed('margin'))) - .thenReturn(Amount(1)); + .thenReturn(Usd(1)); when(tradeValueService.getExpiryTimestamp()).thenReturn(DateTime.now()); when(tradeValueService.orderMatchingFee( quantity: anyNamed('quantity'), price: anyNamed('price'))) @@ -217,7 +217,7 @@ void main() { .thenReturn(10000); when(tradeValueService.calculateQuantity( price: anyNamed('price'), leverage: anyNamed('leverage'), margin: anyNamed('margin'))) - .thenReturn(Amount(1)); + .thenReturn(Usd(1)); when(tradeValueService.getExpiryTimestamp()).thenReturn(DateTime.now()); when(tradeValueService.orderMatchingFee( quantity: anyNamed('quantity'), price: anyNamed('price')))