Skip to content

Commit

Permalink
Merge pull request #1158 from get10101/feat/stablesats-type-in-transa…
Browse files Browse the repository at this point in the history
…ctions

Add Synthetic USD balance into account overview
  • Loading branch information
klochowicz authored Aug 29, 2023
2 parents a783372 + 17531d4 commit 0d5edb4
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 28 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Display Synthetic USD balance in the account overview screen.

## [1.2.3] - 2023-08-28

- Add synthetic usd feature.
- Add synthetic USD feature.
- Fix delayed position update.
- Change contract duration to 7 days.
- Add settings in coordinator to make contract fee rate configurable during runtime.
Expand Down
3 changes: 1 addition & 2 deletions mobile/lib/features/stable/stable_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ class _StableScreenState extends State<StableScreen> {
mainAxisAlignment: MainAxisAlignment.center,
children: [
FiatText(
amount:
position != null && position.direction != Direction.long ? position.quantity : 0,
amount: positionChangeNotifier.getStableUSDAmountInFiat(),
textStyle: const TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
)
],
Expand Down
28 changes: 28 additions & 0 deletions mobile/lib/features/trade/position_change_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import 'package:get_10101/common/application/event_service.dart';
import 'package:get_10101/common/domain/model.dart';
import 'package:get_10101/features/trade/application/position_service.dart';
import 'package:get_10101/features/trade/domain/contract_symbol.dart';
import 'package:get_10101/features/trade/domain/direction.dart';
import 'package:get_10101/features/trade/domain/leverage.dart';

import 'domain/position.dart';
import 'domain/price.dart';
Expand All @@ -16,6 +18,32 @@ class PositionChangeNotifier extends ChangeNotifier implements Subscriber {

Price? price;

/// Amount of stabilised bitcoin in terms of USD (fiat)
double getStableUSDAmountInFiat() {
if (hasStableUSD()) {
final positionUsd = positions[ContractSymbol.btcusd];
return positionUsd!.quantity;
} else {
return 0.0;
}
}

Amount getStableUSDAmountInSats() {
if (hasStableUSD()) {
final positionUsd = positions[ContractSymbol.btcusd];
return positionUsd!.getAmountWithUnrealizedPnl();
} else {
return Amount(0);
}
}

bool hasStableUSD() {
final positionUsd = positions[ContractSymbol.btcusd];
return positionUsd != null &&
positionUsd.direction == Direction.short &&
positionUsd.leverage == Leverage(1);
}

Future<void> initialize() async {
List<Position> positions = await _positionService.fetchPositions();
for (Position position in positions) {
Expand Down
43 changes: 39 additions & 4 deletions mobile/lib/features/wallet/balance_row.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get_10101/common/amount_text.dart';
import 'package:get_10101/common/domain/model.dart';
import 'package:get_10101/common/fiat_text.dart';
import 'package:get_10101/features/stable/stable_screen.dart';
import 'package:get_10101/features/trade/position_change_notifier.dart';
import 'package:get_10101/features/wallet/create_invoice_screen.dart';
import 'package:get_10101/features/wallet/domain/wallet_history.dart';
import 'package:get_10101/features/wallet/send_screen.dart';
Expand Down Expand Up @@ -40,8 +43,10 @@ class _BalanceRowState extends State<BalanceRow> with SingleTickerProviderStateM
Widget build(BuildContext context) {
WalletTheme theme = Theme.of(context).extension<WalletTheme>()!;
WalletChangeNotifier walletChangeNotifier = context.watch<WalletChangeNotifier>();
TextStyle normal = const TextStyle(fontSize: 16.0);
TextStyle bold = const TextStyle(fontWeight: FontWeight.bold, fontSize: 16.0);
const normal = TextStyle(fontSize: 16.0);
const bold = TextStyle(fontWeight: FontWeight.bold, fontSize: 16.0);

PositionChangeNotifier positionChangeNotifier = context.watch<PositionChangeNotifier>();

Amount amount;
String name;
Expand All @@ -53,6 +58,12 @@ class _BalanceRowState extends State<BalanceRow> with SingleTickerProviderStateM
rowBgColor = theme.lightning;
icon = SvgPicture.asset("assets/Lightning_logo.svg");
amount = walletChangeNotifier.lightning();
} else if (widget.walletType == WalletHistoryItemDataType.stable) {
name = "Synthetic-USD";
rowBgColor = theme.lightning;
icon = SvgPicture.asset("assets/USD_logo.svg");
// this in unused for now, other than to initialise value (and satisfy the compiler)
amount = positionChangeNotifier.getStableUSDAmountInSats();
} else {
name = "On-chain";
rowBgColor = theme.onChain;
Expand All @@ -65,12 +76,14 @@ class _BalanceRowState extends State<BalanceRow> with SingleTickerProviderStateM
double buttonSpacing = 10;

BalanceRowButton send = BalanceRowButton(
type: widget.walletType,
flow: PaymentFlow.outbound,
enabled: _expanded,
buttonSize: buttonSize,
);

BalanceRowButton receive = BalanceRowButton(
type: widget.walletType,
flow: PaymentFlow.inbound,
enabled: _expanded,
buttonSize: buttonSize,
Expand Down Expand Up @@ -129,7 +142,14 @@ class _BalanceRowState extends State<BalanceRow> with SingleTickerProviderStateM
child: SizedBox(height: widget.iconSize, width: widget.iconSize, child: icon),
),
Expanded(child: Text(name, style: normal)),
AmountText(amount: amount, textStyle: bold),
if (widget.walletType == WalletHistoryItemDataType.stable)
// we need to use different widget as we display the value in dollar terms
FiatText(
amount: positionChangeNotifier.getStableUSDAmountInFiat(),
textStyle: bold,
)
else
AmountText(amount: amount, textStyle: bold),
]),
),
),
Expand All @@ -142,12 +162,17 @@ class _BalanceRowState extends State<BalanceRow> with SingleTickerProviderStateM
}

class BalanceRowButton extends StatelessWidget {
final WalletHistoryItemDataType type;
final PaymentFlow flow;
final bool enabled;
final double buttonSize;

const BalanceRowButton(
{super.key, required this.flow, required this.enabled, this.buttonSize = 40});
{super.key,
required this.type,
required this.flow,
required this.enabled,
this.buttonSize = 40});

double width() {
// 2x padding from around the icon, 2x padding from inside the icon
Expand All @@ -171,6 +196,16 @@ class BalanceRowButton extends StatelessWidget {
onPressed: !enabled
? null
: () {
if (type == WalletHistoryItemDataType.stable) {
if (flow == PaymentFlow.outbound) {
// eventually there should be a way to send stable sats directly
context.go(StableScreen.route);
} else {
// eventually there should be a way to receive stable sats directly
context.go(StableScreen.route);
}
return;
}
if (flow == PaymentFlow.outbound) {
context.go(SendScreen.route);
} else {
Expand Down
9 changes: 8 additions & 1 deletion mobile/lib/features/wallet/domain/wallet_history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import 'package:get_10101/common/domain/model.dart';
import 'payment_flow.dart';
import 'package:get_10101/bridge_generated/bridge_definitions.dart' as rust;

enum WalletHistoryItemDataType { lightning, onChain, trade, orderMatchingFee, jitChannelFee }
enum WalletHistoryItemDataType {
lightning,
onChain,
trade,
orderMatchingFee,
jitChannelFee,
stable
}

enum WalletHistoryStatus { pending, confirmed }

Expand Down
5 changes: 5 additions & 0 deletions mobile/lib/features/wallet/wallet_history_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class WalletHistoryItem extends StatelessWidget {
return "Matching fee";
case WalletHistoryItemDataType.jitChannelFee:
return "Channel opening fee";
case WalletHistoryItemDataType.stable:
return "Stable";
}
}();

Expand All @@ -81,6 +83,8 @@ class WalletHistoryItem extends StatelessWidget {
return "off-chain";
case WalletHistoryItemDataType.onChain:
return "on-chain";
case WalletHistoryItemDataType.stable:
return "stable sats";
}
}();

Expand Down Expand Up @@ -169,6 +173,7 @@ class WalletHistoryItem extends StatelessWidget {
case WalletHistoryItemDataType.onChain:
return [HistoryDetail(label: "Transaction id", value: data.txid ?? "")];
case WalletHistoryItemDataType.trade:
case WalletHistoryItemDataType.stable:
case WalletHistoryItemDataType.orderMatchingFee:
final orderId = data.orderId!.substring(0, 8);
return [HistoryDetail(label: "Order", value: orderId)];
Expand Down
40 changes: 33 additions & 7 deletions mobile/lib/features/wallet/wallet_screen.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
import 'package:get_10101/common/amount_text.dart';
import 'package:get_10101/common/fiat_text.dart';
import 'package:get_10101/common/submission_status_dialog.dart';
import 'package:get_10101/common/value_data_row.dart';
import 'package:get_10101/features/trade/position_change_notifier.dart';
import 'package:get_10101/features/wallet/seed_screen.dart';
import 'package:get_10101/features/wallet/send_payment_change_notifier.dart';
import 'package:get_10101/util/preferences.dart';
Expand Down Expand Up @@ -39,9 +41,11 @@ class _WalletScreenState extends State<WalletScreen> {

@override
Widget build(BuildContext context) {
WalletChangeNotifier walletChangeNotifier = context.watch<WalletChangeNotifier>();
SendPaymentChangeNotifier sendPaymentChangeNotifier =
context.watch<SendPaymentChangeNotifier>();
final walletChangeNotifier = context.watch<WalletChangeNotifier>();
final sendPaymentChangeNotifier = context.watch<SendPaymentChangeNotifier>();

// For displaying synthetic USD balance
final positionChangeNotifier = context.watch<PositionChangeNotifier>();

if (sendPaymentChangeNotifier.pendingPayment != null &&
!sendPaymentChangeNotifier.pendingPayment!.displayed) {
Expand Down Expand Up @@ -138,10 +142,31 @@ class _WalletScreenState extends State<WalletScreen> {
fontStyle: FontStyle.italic,
),
)
: AmountText(
amount: walletChangeNotifier.total(),
textStyle: const TextStyle(
fontSize: 20.0, fontWeight: FontWeight.bold))),
: Row(
children: [
AmountText(
amount: walletChangeNotifier.total(),
textStyle: const TextStyle(
fontSize: 20.0, fontWeight: FontWeight.bold)),
Visibility(
visible:
positionChangeNotifier.getStableUSDAmountInFiat() !=
0.0,
child: Row(
children: [
const SizedBox(width: 5),
const Text("+"),
const SizedBox(width: 5),
FiatText(
amount: positionChangeNotifier
.getStableUSDAmountInFiat(),
textStyle: const TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold)),
],
)),
],
)),
],
);
},
Expand All @@ -151,6 +176,7 @@ class _WalletScreenState extends State<WalletScreen> {
children: [
BalanceRow(walletType: WalletHistoryItemDataType.lightning),
BalanceRow(walletType: WalletHistoryItemDataType.onChain),
BalanceRow(walletType: WalletHistoryItemDataType.stable),
],
),
),
Expand Down
12 changes: 0 additions & 12 deletions mobile/macos/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
PODS:
- device_info_plus (0.0.1):
- FlutterMacOS
- Firebase/CoreOnly (10.12.0):
- FirebaseCore (= 10.12.0)
- Firebase/Messaging (10.12.0):
Expand Down Expand Up @@ -74,11 +72,8 @@ PODS:
- shared_preferences_foundation (0.0.1):
- Flutter
- FlutterMacOS
- url_launcher_macos (0.0.1):
- FlutterMacOS

DEPENDENCIES:
- device_info_plus (from `Flutter/ephemeral/.symlinks/plugins/device_info_plus/macos`)
- firebase_core (from `Flutter/ephemeral/.symlinks/plugins/firebase_core/macos`)
- firebase_messaging (from `Flutter/ephemeral/.symlinks/plugins/firebase_messaging/macos`)
- flutter_local_notifications (from `Flutter/ephemeral/.symlinks/plugins/flutter_local_notifications/macos`)
Expand All @@ -87,7 +82,6 @@ DEPENDENCIES:
- path_provider_foundation (from `Flutter/ephemeral/.symlinks/plugins/path_provider_foundation/darwin`)
- share_plus (from `Flutter/ephemeral/.symlinks/plugins/share_plus/macos`)
- shared_preferences_foundation (from `Flutter/ephemeral/.symlinks/plugins/shared_preferences_foundation/darwin`)
- url_launcher_macos (from `Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos`)

SPEC REPOS:
trunk:
Expand All @@ -102,8 +96,6 @@ SPEC REPOS:
- PromisesObjC

EXTERNAL SOURCES:
device_info_plus:
:path: Flutter/ephemeral/.symlinks/plugins/device_info_plus/macos
firebase_core:
:path: Flutter/ephemeral/.symlinks/plugins/firebase_core/macos
firebase_messaging:
Expand All @@ -120,11 +112,8 @@ EXTERNAL SOURCES:
:path: Flutter/ephemeral/.symlinks/plugins/share_plus/macos
shared_preferences_foundation:
:path: Flutter/ephemeral/.symlinks/plugins/shared_preferences_foundation/darwin
url_launcher_macos:
:path: Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos

SPEC CHECKSUMS:
device_info_plus: 5401765fde0b8d062a2f8eb65510fb17e77cf07f
Firebase: 07150e75d142fb9399f6777fa56a187b17f833a0
firebase_core: ff59797157ca9adda4440071643761b41fcd03b3
firebase_messaging: d489df2f5cf5eb4b1ffb0b920f1d8c6b911f6166
Expand All @@ -142,7 +131,6 @@ SPEC CHECKSUMS:
PromisesObjC: c50d2056b5253dadbd6c2bea79b0674bd5a52fa4
share_plus: 76dd39142738f7a68dd57b05093b5e8193f220f7
shared_preferences_foundation: 5b919d13b803cadd15ed2dc053125c68730e5126
url_launcher_macos: d2691c7dd33ed713bf3544850a623080ec693d95

PODFILE CHECKSUM: f429f80f5470aff39540e8333365097246b857cb

Expand Down
1 change: 0 additions & 1 deletion mobile/native/src/event/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ pub struct FlutterSubscriber {
/// Subscribes to event relevant for flutter and forwards them to the stream sink.
impl Subscriber for FlutterSubscriber {
fn notify(&self, event: &EventInternal) {
tracing::debug!(event = %event, "Forwarding event to flutter");
self.stream.add(event.clone().into());
}

Expand Down

0 comments on commit 0d5edb4

Please sign in to comment.