Skip to content

Commit

Permalink
feat: add price depending on long vs short
Browse files Browse the repository at this point in the history
  • Loading branch information
bonomat committed Jan 19, 2024
1 parent 9ed0e0f commit 0721a91
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
11 changes: 6 additions & 5 deletions webapp/frontend/lib/common/model.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:decimal/decimal.dart';
import 'package:decimal/intl.dart';
import 'package:get_10101/common/amount_text.dart';
import 'package:intl/intl.dart';

Expand Down Expand Up @@ -151,20 +152,20 @@ class Price implements Formattable {

try {
final f = NumberFormat("#,###");
int amount =
double amount =
// remove any comma and dot from text formatting the users input.
int.parse(value.replaceAll(f.symbols.GROUP_SEP, ''));
double.parse(value.replaceAll(f.symbols.GROUP_SEP, ''));

_usd = Decimal.fromInt(amount);
_usd = Decimal.parse(value);
} on Exception {
_usd = Decimal.zero;
}
}

@override
String formatted() {
final formatter = NumberFormat("#,###,###,###,###", "en");
return formatter.format(_usd);
final formatter = NumberFormat("#,##0.00", "en_US");
return formatter.format(DecimalIntl(_usd));
}

@override
Expand Down
20 changes: 19 additions & 1 deletion webapp/frontend/lib/trade/trade_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TradeScreen extends StatefulWidget {
static const route = "/trade";

// TODO: get price from service
final Quote quote = Quote(Price(41129.0), Price(41129.5));
final Quote quote = Quote(Price(41129.0), Price(41130.5));

// for now just to avoid division by 0 errors, later we should introduce a maintenance margin
final double maintenanceMargin = 0.001;
Expand All @@ -28,6 +28,7 @@ class _TradeScreenState extends State<TradeScreen> {

final TextEditingController _marginController = TextEditingController();
final TextEditingController _liquidationPriceController = TextEditingController();
final TextEditingController _latestPriceController = TextEditingController();

@override
void initState() {
Expand Down Expand Up @@ -87,6 +88,17 @@ class _TradeScreenState extends State<TradeScreen> {
],
),
const SizedBox(height: 20),
Align(
alignment: AlignmentDirectional.centerEnd,
child: AmountInputField(
enabled: false,
label: isLong ? "Ask" : "Bid",
textAlign: TextAlign.right,
suffixIcon: const Icon(FontAwesomeIcons.dollarSign),
controller: _latestPriceController,
),
),
const SizedBox(height: 20),
Align(
alignment: AlignmentDirectional.centerEnd,
child: AmountInputField(
Expand Down Expand Up @@ -151,6 +163,12 @@ class _TradeScreenState extends State<TradeScreen> {
_quantity!, _quote!, _leverage, widget.maintenanceMargin, isLong)
.formatted();
}

if (isLong && _quote != null && _quote!.ask != null) {
_latestPriceController.text = _quote!.ask!.formatted();
} else if (!isLong && _quote != null && _quote!.bid != null) {
_latestPriceController.text = _quote!.bid!.formatted();
}
}
}

Expand Down

0 comments on commit 0721a91

Please sign in to comment.