Skip to content

Commit

Permalink
multi: misc improvements for swapping chains
Browse files Browse the repository at this point in the history
  • Loading branch information
octobocto committed Nov 9, 2023
1 parent 9dfce3a commit 90b6f1b
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 176 deletions.
69 changes: 40 additions & 29 deletions lib/config/dependencies.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:get_it/get_it.dart';
import 'package:sidesail/app.dart';
import 'package:sidesail/config/sidechains.dart';
import 'package:sidesail/providers/balance_provider.dart';
import 'package:sidesail/providers/transactions_provider.dart';
Expand All @@ -13,21 +12,15 @@ import 'package:sidesail/storage/secure_store.dart';

// register all global dependencies, for use in views, or in view models
// each dependency can only be registered once
Future<void> initGetitDependencies(Sidechain chain) async {
Future<void> initGetitDependencies(Sidechain initialChain) async {
final mainFuture = MainchainRPCLive.create();
final ethFuture = EthereumRPCLive.create();
await setSidechainRPC(chain);
await _initSidechainRPC(initialChain);
final mainRPC = await mainFuture;
final ethRPC = await ethFuture;

GetIt.I.registerLazySingleton<MainchainRPC>(
() => mainRPC,
);

GetIt.I.registerLazySingleton<EthereumRPC>(
() => ethRPC,
);

GetIt.I.registerLazySingleton<AppRouter>(
() => AppRouter(),
);
Expand All @@ -46,31 +39,51 @@ Future<void> initGetitDependencies(Sidechain chain) async {
);
}

// register all global dependencies, for use in views, or in view models
// each dependency can only be registered once
Future<void> setSidechainRPC(Sidechain chain) async {
// register all rpc connections. We attempt to create all
// rpcs in parallell, so they're ready instantly when swapping
// we can also query the balance
Future<void> _initSidechainRPC(Sidechain chain) async {
final ethFuture = EthereumRPCLive.create();
final testFuture = TestchainRPCLive.create();

final ethRPC = await ethFuture;
final testRPC = await testFuture;

GetIt.I.registerLazySingleton<TestchainRPC>(
() => testRPC,
);
GetIt.I.registerLazySingleton<EthereumRPC>(
() => ethRPC,
);

SidechainRPC sidechainRPC;
switch (chain.type) {
case SidechainType.testChain:
final testchainRPC = await TestchainRPCLive.create();
if (GetIt.I.isRegistered<TestchainRPC>()) {
GetIt.I.unregister<TestchainRPC>();
}
GetIt.I.registerLazySingleton<TestchainRPC>(
() => testchainRPC,
);
sidechainRPC = testchainRPC;
sidechainRPC = testRPC;
break;

case SidechainType.ethereum:
final ethRPC = await EthereumRPCLive.create();
if (GetIt.I.isRegistered<EthereumRPC>()) {
GetIt.I.unregister<EthereumRPC>();
}
GetIt.I.registerLazySingleton<EthereumRPC>(
() => ethRPC,
);
sidechainRPC = ethRPC;
break;
}

GetIt.I.registerLazySingleton<SidechainRPC>(
() => sidechainRPC,
);
}

// swap out the SidechainRPC type in GetIt with the appropriate
// rpc. All of them are already registered, so this is pretty quick
void swapSidechainRPC(Sidechain chain) async {
SidechainRPC sidechainRPC;
switch (chain.type) {
case SidechainType.testChain:
final testchainRPC = GetIt.I.get<TestchainRPC>();
sidechainRPC = testchainRPC;
break;

case SidechainType.ethereum:
final ethRPC = GetIt.I.get<EthereumRPC>();
sidechainRPC = ethRPC;
break;
}
Expand All @@ -81,6 +94,4 @@ Future<void> setSidechainRPC(Sidechain chain) async {
GetIt.I.registerLazySingleton<SidechainRPC>(
() => sidechainRPC,
);

SailApp.sailAppKey.currentState?.rebuildUI();
}
3 changes: 1 addition & 2 deletions lib/pages/tabs/ethereum/ethereum_rpc_tab_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ class EthereumRPCTabPage extends StatelessWidget {
return const SailPage(
scrollable: true,
title: 'Ethereum RPC',
subtitle:
'Here you can call eth rpcs directly to the eth-sidechain. Try typing in "eth_blockNumber" in the input below.',
subtitle: 'Call RPCs directly to the Ethereum sidechain. Try typing in "eth_blockNumber" in the input below.',
body: Padding(
padding: EdgeInsets.only(bottom: SailStyleValues.padding30),
child: Column(
Expand Down
60 changes: 19 additions & 41 deletions lib/pages/tabs/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,32 @@ import 'package:stacked/stacked.dart';

@RoutePage()
class HomePage extends StatelessWidget {
SidechainRPC get _sideRPC => GetIt.I.get<SidechainRPC>();

const HomePage({super.key});

@override
Widget build(BuildContext context) {
final routes = routesForChain(_sideRPC.chain);
final theme = SailTheme.of(context);
const routes = [
// common routes
SidechainExplorerTabRoute(),
// testchain routes
DashboardTabRoute(),
TransferMainchainTabRoute(),
WithdrawalBundleTabRoute(),
BlindMergedMiningTabRoute(),

// ethereum routes
EthereumRPCTabRoute(),

// trailing common routes
NodeSettingsTabRoute(),
ThemeSettingsTabRoute(),
];

return auto_router.AutoTabsRouter.builder(
homeIndex: 1,
routes: routes,
builder: (context, children, _) {
final tabsRouter = auto_router.AutoTabsRouter.of(context);
builder: (context, children, tabsRouter) {
return Scaffold(
backgroundColor: theme.colors.background,
body: SideNav(
Expand All @@ -41,40 +53,6 @@ class HomePage extends StatelessWidget {
},
);
}

List<auto_router.PageRouteInfo<dynamic>> routesForChain(Sidechain chain) {
final preRoutes = [
const SidechainExplorerTabRoute(),
];
final postRoutes = [
const NodeSettingsTabRoute(),
const ThemeSettingsTabRoute(),
];

List<auto_router.PageRouteInfo<dynamic>> chainRoutes = [];
switch (chain.type) {
case SidechainType.testChain:
chainRoutes = [
const DashboardTabRoute(),
const TransferMainchainTabRoute(),
const WithdrawalBundleTabRoute(),
const BlindMergedMiningTabRoute(),
];
break;

case SidechainType.ethereum:
chainRoutes = [
const EthereumRPCTabRoute(),
];
break;
}

return [
...preRoutes,
...chainRoutes,
...postRoutes,
];
}
}

class SideNav extends StatefulWidget {
Expand Down Expand Up @@ -222,9 +200,9 @@ class _SideNavState extends State<SideNav> {
NavEntry(
title: '${viewModel.chain.name} Dashboard',
icon: SailSVGAsset.iconDashboardTab,
selected: tabsRouter.activeIndex == 1,
selected: tabsRouter.activeIndex == 5,
onPressed: () {
tabsRouter.setActiveIndex(1);
tabsRouter.setActiveIndex(5);
},
),
];
Expand Down
14 changes: 8 additions & 6 deletions lib/pages/tabs/sidechain_explorer_tab_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import 'package:get_it/get_it.dart';
import 'package:sail_ui/sail_ui.dart';
import 'package:sidesail/config/sidechains.dart';
import 'package:sidesail/providers/balance_provider.dart';
import 'package:sidesail/rpc/rpc_ethereum.dart';
import 'package:sidesail/rpc/rpc_sidechain.dart';
import 'package:sidesail/rpc/rpc_testchain.dart';
import 'package:sidesail/widgets/containers/chain_overview_card.dart';
import 'package:sidesail/widgets/containers/tabs/dashboard_tab_widgets.dart';
import 'package:stacked/stacked.dart';

@RoutePage()
class SidechainExplorerTabPage extends StatelessWidget {
TestchainRPC get test => GetIt.I.get<TestchainRPC>();
EthereumRPC get eth => GetIt.I.get<EthereumRPC>();

const SidechainExplorerTabPage({super.key});

@override
Expand All @@ -36,19 +41,15 @@ class SidechainExplorerTabPage extends StatelessWidget {
unconfirmedBalance: viewModel.pendingBalance,
highlighted: false,
currentChain: viewModel.chain.type == SidechainType.testChain,
onPressed: () {
viewModel.setSidechainRPC(TestSidechain());
},
onPressed: () => viewModel.setSidechainRPC(TestSidechain()),
),
ChainOverviewCard(
chain: EthereumSidechain(),
confirmedBalance: viewModel.balance,
unconfirmedBalance: viewModel.pendingBalance,
highlighted: false,
currentChain: viewModel.chain.type == SidechainType.ethereum,
onPressed: () {
viewModel.setSidechainRPC(EthereumSidechain());
},
onPressed: () => viewModel.setSidechainRPC(EthereumSidechain()),
),
],
),
Expand Down Expand Up @@ -83,6 +84,7 @@ class SidechainExplorerTabViewModel extends BaseViewModel {

void setSidechainRPC(Sidechain chain) {
_sideRPC.setChain(chain);
notifyListeners();
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ class _UnbundledWithdrawalViewState extends State<UnbundledWithdrawalView> {
),
),
copyable: false,
label: '',
value:
'${satoshiToBTC(widget.withdrawal.amountSatoshi).toStringAsFixed(8)} BTC to ${widget.withdrawal.address}',
),
Expand Down
2 changes: 2 additions & 0 deletions lib/providers/balance_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class BalanceProvider extends ChangeNotifier {
BalanceProvider() {
fetch();
_startPolling();
_rpc.addListener(fetch);
}

// call this function from anywhere to refresh the balance
Expand All @@ -45,5 +46,6 @@ class BalanceProvider extends ChangeNotifier {
super.dispose();
// Cancel timer when provider is disposed (never?)
_timer.cancel();
_rpc.removeListener(notifyListeners);
}
}
2 changes: 1 addition & 1 deletion lib/routing/router.gr.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/rpc/rpc_ethereum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class EthereumRPCLive extends EthereumRPC {
return EthereumAddress.fromHex(accounts[0] as String);
}

// ignore: unused_element
Future<bool> _deposit(int amountSat, int feeSat) async {
final amount = sgweiPerSat * amountSat;
final fee = sgweiPerSat * feeSat;
Expand Down
2 changes: 1 addition & 1 deletion lib/rpc/rpc_sidechain.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ abstract class SidechainRPC extends RPCConnection {
late Sidechain chain;
void setChain(Sidechain newChain) {
chain = newChain;
setSidechainRPC(newChain);
swapSidechainRPC(newChain);
notifyListeners();
}
}
Expand Down
Loading

0 comments on commit 90b6f1b

Please sign in to comment.