Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

Commit 1a19388

Browse files
bors[bot]holzeis
andauthored
Merge #566
566: Replace periodic sync with events r=holzeis a=holzeis Replaces periodic syncs of ... - wallet - offers (Event::Offer) - balance (Event::WalletInfo) - history (Event::WalletInfo) - channel state (Event::ChannelState) The only sync that is still triggered from flutter is the sync of the cfd orders. It would be nice to have the stream available in the cfd module and only push a cfd if it has been opened or closed, instead of syncing in an interval, but that needs some more design. Also introduces a `Ready` event notifying the flutter app that the ldk node has successfully started. Co-authored-by: Richard Holzeis <richard@holzeis.me>
2 parents 12e0a95 + 7edff75 commit 1a19388

23 files changed

Lines changed: 479 additions & 474 deletions

integration_test/wallet_test.dart

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import 'package:integration_test/integration_test.dart';
55
import 'package:go_router/go_router.dart';
66
import 'package:provider/provider.dart';
77
import 'package:ten_ten_one/balance.dart';
8+
import 'package:ten_ten_one/bridge_generated/bridge_definitions.dart' as bridge;
9+
import 'package:ten_ten_one/wallet/wallet_change_notifier.dart';
810
import 'package:ten_ten_one/wallet/wallet_lightning.dart';
9-
import 'package:ten_ten_one/models/amount.model.dart';
10-
import 'package:ten_ten_one/models/balance_model.dart';
1111
import 'package:ten_ten_one/models/seed_backup_model.dart';
1212
import 'package:ten_ten_one/wallet/seed.dart';
1313

@@ -31,7 +31,7 @@ final GoRouter _router = GoRouter(
3131

3232
Widget createWallet(balanceModel, seedBackupModel) => MultiProvider(
3333
providers: [
34-
ChangeNotifierProvider<LightningBalance>(create: (context) => balanceModel),
34+
ChangeNotifierProvider<WalletChangeNotifier>(create: (context) => balanceModel),
3535
ChangeNotifierProvider<SeedBackupModel>(create: (context) => seedBackupModel)
3636
],
3737
child: MaterialApp.router(
@@ -43,19 +43,24 @@ void main() {
4343

4444
group('Wallet widget tests', () {
4545
testWidgets('test if balance is rendered', (tester) async {
46-
await tester.pumpWidget(createWallet(LightningBalance(), SeedBackupModel()));
46+
await tester.pumpWidget(createWallet(WalletChangeNotifier(), SeedBackupModel()));
4747

4848
expect(find.byType(Balance), findsOneWidget);
4949
});
5050

5151
testWidgets('test if Bitcoin balance gets updated', (tester) async {
52-
final balanceModel = LightningBalance();
52+
final balanceModel = WalletChangeNotifier();
5353
await tester.pumpWidget(createWallet(balanceModel, SeedBackupModel()));
5454

5555
Text balance = find.byKey(const Key('bitcoinBalance')).evaluate().first.widget as Text;
5656
// balance is empty on start
5757
expect(balance.data, '0');
58-
balanceModel.update(Amount(1001));
58+
balanceModel.update(bridge.WalletInfo(
59+
balance: bridge.Balance(
60+
onChain: bridge.OnChain(trustedPending: 0, untrustedPending: 0, confirmed: 1001),
61+
offChain: bridge.OffChain(available: 0, pendingClose: 0)),
62+
bitcoinHistory: List.empty(),
63+
lightningHistory: List.empty()));
5964
await tester.pumpAndSettle();
6065

6166
balance = find.byKey(const Key('bitcoinBalance')).evaluate().first.widget as Text;

lib/app_bar_with_balance.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class AppBarWithBalance extends StatelessWidget {
3838
icon: const Icon(Icons.wallet),
3939
tooltip: 'Wallet Dashboard',
4040
onPressed: () {
41-
walletChangeNotifier.update(0);
41+
walletChangeNotifier.set(0);
4242
GoRouter.of(context).go('/');
4343
},
4444
)

lib/balance.dart

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import 'package:flutter/material.dart';
22
import 'package:provider/provider.dart';
33
import 'package:ten_ten_one/models/amount.model.dart';
4-
5-
import 'models/balance_model.dart';
4+
import 'package:ten_ten_one/wallet/wallet_change_notifier.dart';
65

76
enum BalanceSelector { bitcoin, lightning, both }
87

@@ -13,54 +12,53 @@ class Balance extends StatelessWidget {
1312

1413
@override
1514
Widget build(BuildContext context) {
16-
return Consumer2<LightningBalance, BitcoinBalance>(
17-
builder: (context, lightningBalance, bitcoinBalance, child) {
18-
var bitcoinBalanceDisplay = bitcoinBalance.confirmed.display(currency: Currency.sat);
19-
var bitcoinBalanceConfirmedDisplay =
20-
bitcoinBalance.confirmed.display(currency: Currency.sat);
21-
var bitcoinBalancePendingDisplay = bitcoinBalance.pending().display(currency: Currency.sat);
15+
final wallet = context.watch<WalletChangeNotifier>();
2216

23-
var lightningBalanceDisplay = lightningBalance.amount.display(currency: Currency.sat);
17+
var bitcoinBalanceDisplay = Amount(wallet.onChain().confirmed).display(currency: Currency.sat);
18+
var bitcoinBalanceConfirmedDisplay =
19+
Amount(wallet.onChain().confirmed).display(currency: Currency.sat);
20+
var bitcoinBalancePendingDisplay = wallet.pending().display(currency: Currency.sat);
2421

25-
var bitcoinBalanceWidget = Tooltip(
26-
richMessage: TextSpan(
27-
text: 'Confirmed: ${bitcoinBalanceConfirmedDisplay.value} sats',
28-
style: const TextStyle(fontWeight: FontWeight.bold),
29-
children: [
30-
TextSpan(
31-
text: '\nPending: ${bitcoinBalancePendingDisplay.value} sats',
32-
style: const TextStyle(fontWeight: FontWeight.normal),
33-
)
34-
],
35-
),
36-
child: BalanceRow(
37-
value: bitcoinBalanceDisplay.value,
38-
unit: bitcoinBalanceDisplay.unit,
39-
icon: Icons.link,
40-
smaller: balanceSelector == BalanceSelector.both));
41-
var lightningBalanceWidget = BalanceRow(
42-
value: lightningBalanceDisplay.value,
43-
unit: lightningBalanceDisplay.unit,
44-
icon: Icons.bolt_outlined,
45-
smaller: balanceSelector == BalanceSelector.both);
22+
var lightningBalanceDisplay =
23+
Amount(wallet.offChain().available).display(currency: Currency.sat);
4624

47-
var balanceWidgets = Column();
25+
var bitcoinBalanceWidget = Tooltip(
26+
richMessage: TextSpan(
27+
text: 'Confirmed: ${bitcoinBalanceConfirmedDisplay.value} sats',
28+
style: const TextStyle(fontWeight: FontWeight.bold),
29+
children: [
30+
TextSpan(
31+
text: '\nPending: ${bitcoinBalancePendingDisplay.value} sats',
32+
style: const TextStyle(fontWeight: FontWeight.normal),
33+
)
34+
],
35+
),
36+
child: BalanceRow(
37+
value: bitcoinBalanceDisplay.value,
38+
unit: bitcoinBalanceDisplay.unit,
39+
icon: Icons.link,
40+
smaller: balanceSelector == BalanceSelector.both));
41+
var lightningBalanceWidget = BalanceRow(
42+
value: lightningBalanceDisplay.value,
43+
unit: lightningBalanceDisplay.unit,
44+
icon: Icons.bolt_outlined,
45+
smaller: balanceSelector == BalanceSelector.both);
4846

49-
switch (balanceSelector) {
50-
case BalanceSelector.bitcoin:
51-
balanceWidgets = Column(children: [bitcoinBalanceWidget]);
52-
break;
53-
case BalanceSelector.lightning:
54-
balanceWidgets = Column(children: [lightningBalanceWidget]);
55-
break;
56-
case BalanceSelector.both:
57-
balanceWidgets = Column(children: [lightningBalanceWidget, bitcoinBalanceWidget]);
58-
break;
59-
}
47+
var balanceWidgets = Column();
6048

61-
return balanceWidgets;
62-
},
63-
);
49+
switch (balanceSelector) {
50+
case BalanceSelector.bitcoin:
51+
balanceWidgets = Column(children: [bitcoinBalanceWidget]);
52+
break;
53+
case BalanceSelector.lightning:
54+
balanceWidgets = Column(children: [lightningBalanceWidget]);
55+
break;
56+
case BalanceSelector.both:
57+
balanceWidgets = Column(children: [lightningBalanceWidget, bitcoinBalanceWidget]);
58+
break;
59+
}
60+
61+
return balanceWidgets;
6462
}
6563
}
6664

lib/cfd_trading/cfd_offer.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import 'package:ten_ten_one/cfd_trading/cfd_trading.dart';
1010
import 'package:ten_ten_one/cfd_trading/position_selection.dart';
1111
import 'package:ten_ten_one/cfd_trading/validation_error.dart';
1212
import 'package:ten_ten_one/models/amount.model.dart';
13-
import 'package:ten_ten_one/models/balance_model.dart';
1413
import 'package:ten_ten_one/utilities/dropdown.dart';
1514
import 'package:ten_ten_one/utilities/tto_table.dart';
1615
import 'package:ten_ten_one/ffi.io.dart' if (dart.library.html) 'ffi.web.dart';
1716
import 'package:ten_ten_one/wallet/channel_change_notifier.dart';
17+
import 'package:ten_ten_one/wallet/wallet_change_notifier.dart';
1818

1919
class CfdOffer extends StatefulWidget {
2020
static const leverages = [1, 2, 3, 4, 5, 10];
@@ -69,7 +69,7 @@ class _CfdOfferState extends State<CfdOffer> {
6969
.format(DateTime.fromMillisecondsSinceEpoch((order.calculateExpiry() * 1000)));
7070
final margin = Amount.fromBtc(order.marginTaker()).display(currency: Currency.sat).value;
7171

72-
final balance = context.watch<LightningBalance>().amount.asSats;
72+
final balance = context.watch<WalletChangeNotifier>().offChain().available;
7373
final channel = context.watch<ChannelChangeNotifier>();
7474
final int takerAmount = Amount.fromBtc(order.marginTaker()).asSats;
7575

0 commit comments

Comments
 (0)