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

Commit

Permalink
Merge #566
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
bors[bot] and holzeis authored Dec 12, 2022
2 parents 12e0a95 + 7edff75 commit 1a19388
Show file tree
Hide file tree
Showing 23 changed files with 479 additions and 474 deletions.
17 changes: 11 additions & 6 deletions integration_test/wallet_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import 'package:integration_test/integration_test.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import 'package:ten_ten_one/balance.dart';
import 'package:ten_ten_one/bridge_generated/bridge_definitions.dart' as bridge;
import 'package:ten_ten_one/wallet/wallet_change_notifier.dart';
import 'package:ten_ten_one/wallet/wallet_lightning.dart';
import 'package:ten_ten_one/models/amount.model.dart';
import 'package:ten_ten_one/models/balance_model.dart';
import 'package:ten_ten_one/models/seed_backup_model.dart';
import 'package:ten_ten_one/wallet/seed.dart';

Expand All @@ -31,7 +31,7 @@ final GoRouter _router = GoRouter(

Widget createWallet(balanceModel, seedBackupModel) => MultiProvider(
providers: [
ChangeNotifierProvider<LightningBalance>(create: (context) => balanceModel),
ChangeNotifierProvider<WalletChangeNotifier>(create: (context) => balanceModel),
ChangeNotifierProvider<SeedBackupModel>(create: (context) => seedBackupModel)
],
child: MaterialApp.router(
Expand All @@ -43,19 +43,24 @@ void main() {

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

expect(find.byType(Balance), findsOneWidget);
});

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

Text balance = find.byKey(const Key('bitcoinBalance')).evaluate().first.widget as Text;
// balance is empty on start
expect(balance.data, '0');
balanceModel.update(Amount(1001));
balanceModel.update(bridge.WalletInfo(
balance: bridge.Balance(
onChain: bridge.OnChain(trustedPending: 0, untrustedPending: 0, confirmed: 1001),
offChain: bridge.OffChain(available: 0, pendingClose: 0)),
bitcoinHistory: List.empty(),
lightningHistory: List.empty()));
await tester.pumpAndSettle();

balance = find.byKey(const Key('bitcoinBalance')).evaluate().first.widget as Text;
Expand Down
2 changes: 1 addition & 1 deletion lib/app_bar_with_balance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AppBarWithBalance extends StatelessWidget {
icon: const Icon(Icons.wallet),
tooltip: 'Wallet Dashboard',
onPressed: () {
walletChangeNotifier.update(0);
walletChangeNotifier.set(0);
GoRouter.of(context).go('/');
},
)
Expand Down
88 changes: 43 additions & 45 deletions lib/balance.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:ten_ten_one/models/amount.model.dart';

import 'models/balance_model.dart';
import 'package:ten_ten_one/wallet/wallet_change_notifier.dart';

enum BalanceSelector { bitcoin, lightning, both }

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

@override
Widget build(BuildContext context) {
return Consumer2<LightningBalance, BitcoinBalance>(
builder: (context, lightningBalance, bitcoinBalance, child) {
var bitcoinBalanceDisplay = bitcoinBalance.confirmed.display(currency: Currency.sat);
var bitcoinBalanceConfirmedDisplay =
bitcoinBalance.confirmed.display(currency: Currency.sat);
var bitcoinBalancePendingDisplay = bitcoinBalance.pending().display(currency: Currency.sat);
final wallet = context.watch<WalletChangeNotifier>();

var lightningBalanceDisplay = lightningBalance.amount.display(currency: Currency.sat);
var bitcoinBalanceDisplay = Amount(wallet.onChain().confirmed).display(currency: Currency.sat);
var bitcoinBalanceConfirmedDisplay =
Amount(wallet.onChain().confirmed).display(currency: Currency.sat);
var bitcoinBalancePendingDisplay = wallet.pending().display(currency: Currency.sat);

var bitcoinBalanceWidget = Tooltip(
richMessage: TextSpan(
text: 'Confirmed: ${bitcoinBalanceConfirmedDisplay.value} sats',
style: const TextStyle(fontWeight: FontWeight.bold),
children: [
TextSpan(
text: '\nPending: ${bitcoinBalancePendingDisplay.value} sats',
style: const TextStyle(fontWeight: FontWeight.normal),
)
],
),
child: BalanceRow(
value: bitcoinBalanceDisplay.value,
unit: bitcoinBalanceDisplay.unit,
icon: Icons.link,
smaller: balanceSelector == BalanceSelector.both));
var lightningBalanceWidget = BalanceRow(
value: lightningBalanceDisplay.value,
unit: lightningBalanceDisplay.unit,
icon: Icons.bolt_outlined,
smaller: balanceSelector == BalanceSelector.both);
var lightningBalanceDisplay =
Amount(wallet.offChain().available).display(currency: Currency.sat);

var balanceWidgets = Column();
var bitcoinBalanceWidget = Tooltip(
richMessage: TextSpan(
text: 'Confirmed: ${bitcoinBalanceConfirmedDisplay.value} sats',
style: const TextStyle(fontWeight: FontWeight.bold),
children: [
TextSpan(
text: '\nPending: ${bitcoinBalancePendingDisplay.value} sats',
style: const TextStyle(fontWeight: FontWeight.normal),
)
],
),
child: BalanceRow(
value: bitcoinBalanceDisplay.value,
unit: bitcoinBalanceDisplay.unit,
icon: Icons.link,
smaller: balanceSelector == BalanceSelector.both));
var lightningBalanceWidget = BalanceRow(
value: lightningBalanceDisplay.value,
unit: lightningBalanceDisplay.unit,
icon: Icons.bolt_outlined,
smaller: balanceSelector == BalanceSelector.both);

switch (balanceSelector) {
case BalanceSelector.bitcoin:
balanceWidgets = Column(children: [bitcoinBalanceWidget]);
break;
case BalanceSelector.lightning:
balanceWidgets = Column(children: [lightningBalanceWidget]);
break;
case BalanceSelector.both:
balanceWidgets = Column(children: [lightningBalanceWidget, bitcoinBalanceWidget]);
break;
}
var balanceWidgets = Column();

return balanceWidgets;
},
);
switch (balanceSelector) {
case BalanceSelector.bitcoin:
balanceWidgets = Column(children: [bitcoinBalanceWidget]);
break;
case BalanceSelector.lightning:
balanceWidgets = Column(children: [lightningBalanceWidget]);
break;
case BalanceSelector.both:
balanceWidgets = Column(children: [lightningBalanceWidget, bitcoinBalanceWidget]);
break;
}

return balanceWidgets;
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/cfd_trading/cfd_offer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import 'package:ten_ten_one/cfd_trading/cfd_trading.dart';
import 'package:ten_ten_one/cfd_trading/position_selection.dart';
import 'package:ten_ten_one/cfd_trading/validation_error.dart';
import 'package:ten_ten_one/models/amount.model.dart';
import 'package:ten_ten_one/models/balance_model.dart';
import 'package:ten_ten_one/utilities/dropdown.dart';
import 'package:ten_ten_one/utilities/tto_table.dart';
import 'package:ten_ten_one/ffi.io.dart' if (dart.library.html) 'ffi.web.dart';
import 'package:ten_ten_one/wallet/channel_change_notifier.dart';
import 'package:ten_ten_one/wallet/wallet_change_notifier.dart';

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

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

Expand Down
Loading

0 comments on commit 1a19388

Please sign in to comment.