Skip to content

Commit

Permalink
feat: Disable taking orders when the prices are not available
Browse files Browse the repository at this point in the history
If there's no bid/ask price, disable the buttons that would react to it.
  • Loading branch information
klochowicz committed Jul 11, 2023
1 parent 39343ef commit 062d498
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
12 changes: 6 additions & 6 deletions mobile/lib/features/trade/position_change_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PositionChangeNotifier extends ChangeNotifier implements Subscriber {

Map<ContractSymbol, Position> positions = {};

Price? _price;
Price? price;

Future<void> initialize() async {
List<Position> positions = await _positionService.fetchPositions();
Expand All @@ -34,8 +34,8 @@ class PositionChangeNotifier extends ChangeNotifier implements Subscriber {
if (event is bridge.Event_PositionUpdateNotification) {
Position position = Position.fromApi(event.field0);

if (_price != null) {
final pnl = _positionService.calculatePnl(position, _price!);
if (price != null) {
final pnl = _positionService.calculatePnl(position, price!);
position.unrealizedPnl = pnl != null ? Amount(pnl) : null;
} else {
position.unrealizedPnl = null;
Expand All @@ -45,11 +45,11 @@ class PositionChangeNotifier extends ChangeNotifier implements Subscriber {
ContractSymbol contractSymbol = ContractSymbol.fromApi(event.field0.contractSymbol);
positions.remove(contractSymbol);
} else if (event is bridge.Event_PriceUpdateNotification) {
_price = Price.fromApi(event.field0);
price = Price.fromApi(event.field0);
for (ContractSymbol symbol in positions.keys) {
if (_price != null) {
if (price != null) {
if (positions[symbol] != null) {
final pnl = _positionService.calculatePnl(positions[symbol]!, _price!);
final pnl = _positionService.calculatePnl(positions[symbol]!, price!);
positions[symbol]!.unrealizedPnl = pnl != null ? Amount(pnl) : null;
}
}
Expand Down
15 changes: 11 additions & 4 deletions mobile/lib/features/trade/trade_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ class TradeScreen extends StatelessWidget {
height: 60,
);

bool isBuyButtonEnabled = positionChangeNotifier.price?.ask != null;
bool isSellButtonEnabled = positionChangeNotifier.price?.bid != null;

return Scaffold(
body: Container(
padding: const EdgeInsets.only(left: 15, right: 15),
Expand Down Expand Up @@ -277,11 +280,13 @@ class TradeScreen extends StatelessWidget {
key: tradeScreenButtonBuy,
heroTag: "btnBuy",
onPressed: () {
tradeBottomSheet(context: context, direction: Direction.long);
if (isBuyButtonEnabled) {
tradeBottomSheet(context: context, direction: Direction.long);
}
},
label: const Text("Buy"),
shape: tradeButtonShape,
backgroundColor: tradeTheme.buy,
backgroundColor: isBuyButtonEnabled ? tradeTheme.buy : tradeTheme.disabled,
)),
const SizedBox(width: 20),
SizedBox(
Expand All @@ -290,11 +295,13 @@ class TradeScreen extends StatelessWidget {
key: tradeScreenButtonSell,
heroTag: "btnSell",
onPressed: () {
tradeBottomSheet(context: context, direction: Direction.short);
if (isSellButtonEnabled) {
tradeBottomSheet(context: context, direction: Direction.short);
}
},
label: const Text("Sell"),
shape: tradeButtonShape,
backgroundColor: tradeTheme.sell,
backgroundColor: isBuyButtonEnabled ? tradeTheme.sell : tradeTheme.disabled,
)),
],
),
Expand Down
2 changes: 2 additions & 0 deletions mobile/lib/features/trade/trade_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class TradeTheme extends ThemeExtension<TradeTheme> {
static const Color green600 = Color(0xFF43A047);
static const Color red600 = Color(0xFFE53935);

final Color disabled = Colors.grey;

final Color buy;
final Color sell;

Expand Down
8 changes: 7 additions & 1 deletion mobile/test/trade_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:get_10101/common/amount_denomination_change_notifier.dart';
import 'package:get_10101/common/domain/model.dart';
import 'package:get_10101/features/trade/candlestick_change_notifier.dart';
import 'package:get_10101/features/trade/domain/price.dart';
import 'package:get_10101/features/trade/order_change_notifier.dart';
import 'package:get_10101/features/trade/position_change_notifier.dart';
import 'package:get_10101/features/trade/submit_order_change_notifier.dart';
Expand Down Expand Up @@ -128,13 +129,18 @@ void main() {

WalletChangeNotifier walletChangeNotifier = WalletChangeNotifier(walletService);

PositionChangeNotifier positionChangeNotifier = PositionChangeNotifier(positionService);

// We have to have current price, otherwise we can't take order
positionChangeNotifier.price = Price(bid: 30000.0, ask: 30000.0);

await tester.pumpWidget(MultiProvider(providers: [
ChangeNotifierProvider(
create: (context) =>
TradeValuesChangeNotifier(tradeValueService, channelConstraintsService)),
ChangeNotifierProvider(create: (context) => submitOrderChangeNotifier),
ChangeNotifierProvider(create: (context) => OrderChangeNotifier(orderService)),
ChangeNotifierProvider(create: (context) => PositionChangeNotifier(positionService)),
ChangeNotifierProvider(create: (context) => positionChangeNotifier),
ChangeNotifierProvider(create: (context) => AmountDenominationChangeNotifier()),
ChangeNotifierProvider(create: (context) => walletChangeNotifier),
ChangeNotifierProvider(create: (context) => candlestickChangeNotifier)
Expand Down

0 comments on commit 062d498

Please sign in to comment.