Skip to content

Commit

Permalink
feat: Add emergency kit screen to set pending orders to failed
Browse files Browse the repository at this point in the history
  • Loading branch information
holzeis committed Feb 3, 2024
1 parent 97799db commit 9f118c4
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 135 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Feat(webapp): Show order history
- Fix: Add reject dlc channel, settle and renew offer
- Chore: Change pending offer policy to reject on reconnect
- Feat(settings): Add emergency kit screen to set orders in filling to failed

## [1.8.4] - 2024-01-31

Expand Down
6 changes: 3 additions & 3 deletions mobile/lib/common/routes.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:get_10101/common/global_keys.dart';
import 'package:get_10101/common/settings/channel_screen.dart';
import 'package:get_10101/common/settings/delete_network_graph.dart';
import 'package:get_10101/common/settings/emergency_kit_screen.dart';
import 'package:get_10101/common/status_screen.dart';
import 'package:get_10101/features/wallet/domain/destination.dart';
import 'package:get_10101/features/wallet/send/send_onchain_screen.dart';
Expand Down Expand Up @@ -118,11 +118,11 @@ GoRouter createRoutes() {
},
),
GoRoute(
path: DeleteNetworkGraphScreen.subRouteName,
path: EmergencyKitScreen.subRouteName,
// Use root navigator so the screen overlays the application shell
parentNavigatorKey: rootNavigatorKey,
builder: (BuildContext context, GoRouterState state) {
return const DeleteNetworkGraphScreen();
return const EmergencyKitScreen();
},
),
GoRoute(
Expand Down
113 changes: 0 additions & 113 deletions mobile/lib/common/settings/delete_network_graph.dart

This file was deleted.

133 changes: 133 additions & 0 deletions mobile/lib/common/settings/emergency_kit_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:get_10101/common/color.dart';
import 'package:get_10101/common/custom_app_bar.dart';
import 'package:get_10101/common/settings/settings_screen.dart';
import 'package:get_10101/common/snack_bar.dart';
import 'package:get_10101/features/trade/order_change_notifier.dart';
import 'package:get_10101/ffi.dart' as rust;
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';

class EmergencyKitScreen extends StatefulWidget {
static const route = "${SettingsScreen.route}/$subRouteName";
static const subRouteName = "emergencykit";

const EmergencyKitScreen({super.key});

@override
State<EmergencyKitScreen> createState() => _EmergencyKitScreenState();
}

class _EmergencyKitScreenState extends State<EmergencyKitScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
padding: const EdgeInsets.only(top: 20, left: 10, right: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const TenTenOneAppBar(title: "Emergency Kit"),
Container(
margin: const EdgeInsets.all(10),
child: Column(
children: [
const SizedBox(
height: 20,
),
Container(
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
decoration: BoxDecoration(
border: Border.all(color: Colors.orange),
color: Colors.white,
borderRadius: BorderRadius.circular(10)),
child: const Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Icon(
Icons.warning_rounded,
color: Colors.orange,
size: 22,
),
SizedBox(width: 10),
Expanded(
child: Text(
"Only use these emergency kits if you know what you are doing and after consulting with the 10101 team.",
softWrap: true,
style: TextStyle(fontSize: 16),
),
)
],
)),
const SizedBox(height: 30),
OutlinedButton(
onPressed: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text("Are you sure?"),
content: const Text(
"Performing that action may break your app state and should only get executed after consulting with the 10101 Team."),
actions: [
TextButton(
onPressed: () => GoRouter.of(context).pop(),
child: const Text('No'),
),
TextButton(
onPressed: () async {
final messenger = ScaffoldMessenger.of(context);
final orderChangeNotifier =
context.read<OrderChangeNotifier>();
final goRouter = GoRouter.of(context);

try {
await rust.api.setFillingOrdersToFailed();
await orderChangeNotifier.initialize();
goRouter.pop();
showSnackBar(messenger,
"Successfully set filling orders to failed");
} catch (e) {
showSnackBar(messenger,
"Failed to set filling orders to failed. Error: $e");
}
},
child: const Text('Yes'),
),
]);
});
},
style: ButtonStyle(
fixedSize: MaterialStateProperty.all(const Size(double.infinity, 50)),
iconSize: MaterialStateProperty.all<double>(20.0),
elevation: MaterialStateProperty.all<double>(0),
// this reduces the shade
side: MaterialStateProperty.all(
const BorderSide(width: 1.0, color: tenTenOnePurple)),
padding: MaterialStateProperty.all<EdgeInsetsGeometry>(
const EdgeInsets.fromLTRB(20, 12, 20, 12),
),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
),
backgroundColor: MaterialStateProperty.all<Color>(Colors.transparent),
),
child: const Row(mainAxisAlignment: MainAxisAlignment.center, children: [
Icon(FontAwesomeIcons.broom),
SizedBox(width: 10),
Text("Cleanup filling orders", style: TextStyle(fontSize: 16))
]))
],
),
)
],
),
),
),
);
}
}
8 changes: 4 additions & 4 deletions mobile/lib/common/settings/settings_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:get_10101/common/service_status_notifier.dart';
import 'package:get_10101/common/settings/app_info_screen.dart';
import 'package:get_10101/common/settings/channel_screen.dart';
import 'package:get_10101/common/settings/collab_close_screen.dart';
import 'package:get_10101/common/settings/delete_network_graph.dart';
import 'package:get_10101/common/settings/emergency_kit_screen.dart';
import 'package:get_10101/common/settings/force_close_screen.dart';
import 'package:get_10101/common/settings/open_telegram.dart';
import 'package:get_10101/common/settings/share_logs_screen.dart';
Expand Down Expand Up @@ -241,10 +241,10 @@ class _SettingsScreenState extends State<SettingsScreen> {
child: Column(
children: [
SettingsClickable(
icon: Icons.delete,
title: "Delete Network Graph",
icon: FontAwesomeIcons.bandage,
title: "Emergency Kit",
callBackFunc: () =>
GoRouter.of(context).push(DeleteNetworkGraphScreen.route)),
GoRouter.of(context).push(EmergencyKitScreen.route)),
SettingsClickable(
icon: Icons.close,
title: "Close Channel",
Expand Down
21 changes: 6 additions & 15 deletions mobile/native/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::config::api::Config;
use crate::config::api::Directories;
use crate::config::get_network;
use crate::db;
use crate::db::connection;
use crate::destination;
use crate::event;
use crate::event::api::FlutterSubscriber;
Expand All @@ -22,7 +23,6 @@ use crate::trade::order::api::Order;
use crate::trade::position;
use crate::trade::position::api::Position;
use crate::trade::users;
use anyhow::anyhow;
use anyhow::ensure;
use anyhow::Context;
use anyhow::Result;
Expand All @@ -35,10 +35,6 @@ use flutter_rust_bridge::frb;
use flutter_rust_bridge::StreamSink;
use flutter_rust_bridge::SyncReturn;
use lightning::chain::chaininterface::ConfirmationTarget as LnConfirmationTarget;
use lightning::util::persist::KVStore;
use lightning::util::persist::NETWORK_GRAPH_PERSISTENCE_KEY;
use lightning::util::persist::NETWORK_GRAPH_PERSISTENCE_PRIMARY_NAMESPACE;
use lightning::util::persist::NETWORK_GRAPH_PERSISTENCE_SECONDARY_NAMESPACE;
use ln_dlc_node::channel::UserChannelId;
use rust_decimal::prelude::FromPrimitive;
use rust_decimal::Decimal;
Expand Down Expand Up @@ -277,16 +273,11 @@ pub async fn get_positions() -> Result<Vec<Position>> {
Ok(positions)
}

pub fn delete_network_graph() -> Result<()> {
crate::state::get_storage()
.ln_storage
.remove(
NETWORK_GRAPH_PERSISTENCE_PRIMARY_NAMESPACE,
NETWORK_GRAPH_PERSISTENCE_SECONDARY_NAMESPACE,
NETWORK_GRAPH_PERSISTENCE_KEY,
false,
)
.map_err(|e| anyhow!("{e:#}"))
pub fn set_filling_orders_to_failed() -> Result<()> {
tracing::warn!("Executing emergency kit! Setting orders in state Filling to Failed!");

let mut conn = connection()?;
db::models::Order::set_all_filling_orders_to_failed(&mut conn)
}

pub fn subscribe(stream: StreamSink<event::api::Event>) {
Expand Down
12 changes: 12 additions & 0 deletions mobile/native/src/db/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ impl Order {
}
}

/// Sets all filling orders to failed. Only be used for emergency recoveries!
pub fn set_all_filling_orders_to_failed(conn: &mut SqliteConnection) -> Result<()> {
let affected_rows = diesel::update(orders::table)
.filter(schema::orders::state.eq(OrderState::Filling))
.set(orders::state.eq(OrderState::Failed))
.execute(conn)?;

tracing::info!("Updated {affected_rows} orders from Filling to Failed");

Ok(())
}

/// updates the status of the given order in the db
pub fn update_state(
order_id: String,
Expand Down

0 comments on commit 9f118c4

Please sign in to comment.