Skip to content

Commit

Permalink
feat: Send payment from webapp
Browse files Browse the repository at this point in the history
  • Loading branch information
holzeis committed Jan 20, 2024
1 parent c075507 commit 5332482
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 6 deletions.
32 changes: 28 additions & 4 deletions webapp/frontend/lib/wallet/send_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ class SendScreen extends StatefulWidget {

class _SendScreenState extends State<SendScreen> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

final TextEditingController _addressController = TextEditingController();
final TextEditingController _amountController = TextEditingController();
final TextEditingController _feeController = TextEditingController();

String? address;
Amount? amount;
Expand Down Expand Up @@ -72,6 +75,7 @@ class _SendScreenState extends State<SendScreen> {
AmountInputField(
value: amount != null ? amount! : Amount.zero(),
label: "Amount in sats",
controller: _amountController,
validator: (value) {
return null;
},
Expand All @@ -86,6 +90,7 @@ class _SendScreenState extends State<SendScreen> {
AmountInputField(
value: fee != null ? fee! : Amount.zero(),
label: "Sats/vb",
controller: _feeController,
validator: (value) {
if (value == null || value == "0") {
return "The fee rate must be greater than 0";
Expand All @@ -111,6 +116,20 @@ class _SendScreenState extends State<SendScreen> {
await context
.read<WalletService>()
.sendPayment(address!, amount!, fee!);

setState(() {
_formKey.currentState!.reset();
_addressController.clear();
address = null;
_amountController.clear();
amount = null;
_feeController.clear();
fee = null;

_formKey.currentState!.validate();
});

showSnackBar(messenger, "Payment has been sent.");
} catch (e) {
showSnackBar(messenger, "Failed to send payment. $e");
}
Expand Down Expand Up @@ -150,8 +169,13 @@ class _SendScreenState extends State<SendScreen> {
],
);
}
}

/*
,
*/
@override
void dispose() {
super.dispose();

_addressController.dispose();
_amountController.dispose();
_feeController.dispose();
}
}
28 changes: 26 additions & 2 deletions webapp/frontend/lib/wallet/wallet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,31 @@ class WalletService {
}

Future<void> sendPayment(String address, Amount amount, Amount fee) async {
// todo: send payment
throw UnimplementedError("todo");
// TODO(holzeis): this should come from the config
const port = "3001";
const host = "localhost";

try {
final response = await http.post(Uri.http('$host:$port', '/api/sendpayment'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(
<String, dynamic>{'address': address, 'amount': amount.sats, 'fee': fee.sats}));

if (response.statusCode != 200) {
throw FlutterError("Failed to send payment");
}
} catch (e) {
throw FlutterError("Failed to send payment. $e");
}
}
}

class Payment {
final String address;
final int amount;
final int fee;

const Payment({required this.address, required this.amount, required this.fee});
}
22 changes: 22 additions & 0 deletions webapp/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ use axum::response::IntoResponse;
use axum::response::Response;
use axum::Json;
use native::api;
use native::api::Fee;
use native::api::SendPayment;
use native::ln_dlc;
use serde::Deserialize;
use serde::Serialize;
use std::sync::Arc;

Expand Down Expand Up @@ -68,3 +72,21 @@ pub async fn get_balance(

Ok(Json(balance))
}

#[derive(Deserialize)]
pub struct Payment {
address: String,
amount: u64,
fee: u64,
}

pub async fn send_payment(params: Json<Payment>) -> Result<(), AppError> {
ln_dlc::send_payment(SendPayment::OnChain {
address: params.0.address,
amount: params.0.amount,
fee: Fee::FeeRate { sats: params.0.fee },
})
.await?;

Ok(())
}
3 changes: 3 additions & 0 deletions webapp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod subscribers;

use crate::api::get_balance;
use crate::api::get_unused_address;
use crate::api::send_payment;
use crate::api::version;
use crate::cli::Opts;
use crate::subscribers::AppSubscribers;
Expand All @@ -18,6 +19,7 @@ use axum::response::Html;
use axum::response::IntoResponse;
use axum::response::Response;
use axum::routing::get;
use axum::routing::post;
use axum::Router;
use rust_embed::RustEmbed;
use std::net::SocketAddr;
Expand Down Expand Up @@ -88,6 +90,7 @@ fn using_serve_dir(subscribers: Arc<AppSubscribers>) -> Router {
.route("/api/version", get(version))
.route("/api/balance", get(get_balance))
.route("/api/newaddress", get(get_unused_address))
.route("/api/sendpayment", post(send_payment))
.route("/main.dart.js", get(main_dart_handler))
.route("/flutter.js", get(flutter_js))
.route("/index.html", get(index_handler))
Expand Down

0 comments on commit 5332482

Please sign in to comment.