Skip to content

Commit

Permalink
chore: change quantity type to USD
Browse files Browse the repository at this point in the history
We should probably introduce a `Quantity` type if we want to support different contracts. For now, a contract equals $1, hence, USD is correct.
  • Loading branch information
bonomat committed Mar 26, 2024
1 parent cf2d7e4 commit c934880
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 36 deletions.
2 changes: 1 addition & 1 deletion mobile/lib/common/domain/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class Usd {

String formatted() {
final formatter = NumberFormat("#,###,###,###,###", "en");
return formatter.format(_usd);
return formatter.format(_usd.toDouble());
}

@override
Expand Down
6 changes: 3 additions & 3 deletions mobile/lib/features/trade/application/order_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import 'package:get_10101/features/trade/domain/order.dart';
import 'package:get_10101/ffi.dart' as rust;

class OrderService {
Future<String> submitMarketOrder(Leverage leverage, Amount quantity,
ContractSymbol contractSymbol, Direction direction, bool stable) async {
Future<String> submitMarketOrder(Leverage leverage, Usd quantity, ContractSymbol contractSymbol,
Direction direction, bool stable) async {
rust.NewOrder order = rust.NewOrder(
leverage: leverage.leverage,
quantity: quantity.asDouble(),
Expand All @@ -21,7 +21,7 @@ class OrderService {

Future<String> submitChannelOpeningMarketOrder(
Leverage leverage,
Amount quantity,
Usd quantity,
ContractSymbol contractSymbol,
Direction direction,
bool stable,
Expand Down
11 changes: 4 additions & 7 deletions mobile/lib/features/trade/application/trade_values_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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());
}
}

Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions mobile/lib/features/trade/domain/position.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
10 changes: 5 additions & 5 deletions mobile/lib/features/trade/domain/trade_values.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -94,7 +94,7 @@ class TradeValues {
tradeValuesService: tradeValuesService);
}

updateQuantity(Amount quantity) {
updateQuantity(Usd quantity) {
this.quantity = quantity;
_recalculateMargin();
_recalculateFee();
Expand Down Expand Up @@ -139,7 +139,7 @@ class TradeValues {
}

_recalculateQuantity() {
Amount? quantity =
Usd? quantity =
tradeValuesService.calculateQuantity(price: price, margin: margin, leverage: leverage);
this.quantity = quantity;
}
Expand Down
13 changes: 7 additions & 6 deletions mobile/lib/features/trade/trade_bottom_sheet_tab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class _TradeBottomSheetTabState extends State<TradeBottomSheetTab> {

bool showCapacityInfo = false;

bool marginInputFieldEnabled = false;
bool quantityInputFieldEnabled = true;

@override
void initState() {
provider = context.read<TradeValuesChangeNotifier>();
Expand Down Expand Up @@ -224,21 +227,19 @@ class _TradeBottomSheetTabState extends State<TradeBottomSheetTab> {
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<TradeValuesChangeNotifier>().updateQuantity(direction, quantity);
} on Exception {
context
.read<TradeValuesChangeNotifier>()
.updateQuantity(direction, Amount.zero());
context.read<TradeValuesChangeNotifier>().updateQuantity(direction, Usd.zero());
}
_formKey.currentState?.validate();
},
Expand Down
4 changes: 2 additions & 2 deletions mobile/lib/features/trade/trade_value_change_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
Expand Down
6 changes: 2 additions & 4 deletions mobile/lib/features/wallet/receive/receive_usdp_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions mobile/lib/features/wallet/send/confirm_payment_modal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>(
Expand Down Expand Up @@ -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(
Expand Down
11 changes: 9 additions & 2 deletions mobile/lib/features/wallet/send/send_onchain_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,15 @@ class _SendOnChainScreenState extends State<SendOnChainScreen> {
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:
Expand Down
4 changes: 2 additions & 2 deletions mobile/test/trade_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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')))
Expand Down Expand Up @@ -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')))
Expand Down

0 comments on commit c934880

Please sign in to comment.