From cd8ad676f5e1f90268ed44c043bea541d7b8d3c7 Mon Sep 17 00:00:00 2001 From: fosse Date: Tue, 27 Feb 2024 16:50:26 -0500 Subject: [PATCH] sats --- cw_bitcoin/lib/bitcoin_amount_format.dart | 5 +++++ cw_core/lib/crypto_currency.dart | 2 ++ lib/src/screens/receive/lightning_receive_page.dart | 8 ++++---- .../receive/widgets/anonpay_currency_input_field.dart | 10 ++++++---- .../screens/receive/widgets/lightning_input_form.dart | 5 +++-- lib/view_model/lightning_invoice_page_view_model.dart | 6 +++--- lib/view_model/lightning_view_model.dart | 10 ++++++---- 7 files changed, 29 insertions(+), 17 deletions(-) diff --git a/cw_bitcoin/lib/bitcoin_amount_format.dart b/cw_bitcoin/lib/bitcoin_amount_format.dart index 3d6c4663f..1a0860dcb 100644 --- a/cw_bitcoin/lib/bitcoin_amount_format.dart +++ b/cw_bitcoin/lib/bitcoin_amount_format.dart @@ -30,3 +30,8 @@ String bitcoinAmountToLightningString({required int amount}) { String formattedAmount = bitcoinAmountFormat.format(cryptoAmountToDouble(amount: amount, divider: lightningAmountDivider)); return formattedAmount.substring(0, formattedAmount.length - 2); } + +String satsToLightningString(double sats) { + String formattedAmount = bitcoinAmountFormat.format(sats); + return formattedAmount.substring(0, formattedAmount.length - 2); +} \ No newline at end of file diff --git a/cw_core/lib/crypto_currency.dart b/cw_core/lib/crypto_currency.dart index 67581ecb8..93684ec0f 100644 --- a/cw_core/lib/crypto_currency.dart +++ b/cw_core/lib/crypto_currency.dart @@ -216,6 +216,8 @@ class CryptoCurrency extends EnumerableItem with Serializable implemen static const kaspa = CryptoCurrency(title: 'KAS', fullName: 'Kaspa', raw: 89, name: 'kas', iconPath: 'assets/images/kaspa_icon.png', decimals: 8); static const digibyte = CryptoCurrency(title: 'DGB', fullName: 'DigiByte', raw: 90, name: 'dgb', iconPath: 'assets/images/digibyte.png', decimals: 8); static const usdtSol = CryptoCurrency(title: 'USDT', tag: 'SOL', fullName: 'USDT Tether', raw: 90, name: 'usdtsol', iconPath: 'assets/images/usdt_icon.png', decimals: 6); + static const satoshis = CryptoCurrency(title: 'Sats', fullName: 'Satoshis', raw: 91, name: 'sats', iconPath: 'assets/images/btc.png', decimals: 0); + static final Map _rawCurrencyMap = diff --git a/lib/src/screens/receive/lightning_receive_page.dart b/lib/src/screens/receive/lightning_receive_page.dart index 15a11cae1..24984dcf5 100644 --- a/lib/src/screens/receive/lightning_receive_page.dart +++ b/lib/src/screens/receive/lightning_receive_page.dart @@ -209,13 +209,13 @@ class LightningReceiveOnchainPage extends BasePage { String min = (snapshot.data as List)[1]; String max = (snapshot.data as List)[2]; String fee = (snapshot.data as List)[3]; - min = bitcoinAmountToLightningString(amount: int.parse(min) ~/ 1000); - max = bitcoinAmountToLightningString(amount: int.parse(min) ~/ 1000); - fee = bitcoinAmountToLightningString(amount: int.parse(min) ~/ 1000); + min = satsToLightningString(double.parse(min)); + max = satsToLightningString(double.parse(max)); + fee = satsToLightningString(double.parse(fee)); return Expanded( child: Text( // S.of(context).lightning_receive_limits(min, max, fee), - "Needs fixing!: $min, $max, $fee", + "Needs fixing!: min: $min max: $max fee: $fee", maxLines: 10, style: TextStyle( fontSize: 14, diff --git a/lib/src/screens/receive/widgets/anonpay_currency_input_field.dart b/lib/src/screens/receive/widgets/anonpay_currency_input_field.dart index ebcc65870..f4fa8e292 100644 --- a/lib/src/screens/receive/widgets/anonpay_currency_input_field.dart +++ b/lib/src/screens/receive/widgets/anonpay_currency_input_field.dart @@ -23,6 +23,7 @@ class AnonpayCurrencyInputField extends StatelessWidget { final String maxAmount; @override Widget build(BuildContext context) { + bool hasDecimals = selectedCurrency.name != "Sats"; final arrowBottomPurple = Image.asset( 'assets/images/arrow_bottom_purple_icon.png', color: Colors.white, @@ -114,12 +115,13 @@ class AnonpayCurrencyInputField extends StatelessWidget { textInputAction: TextInputAction.next, enabled: true, textAlign: TextAlign.left, - keyboardType: - TextInputType.numberWithOptions(signed: false, decimal: true), + keyboardType: TextInputType.numberWithOptions( + signed: false, decimal: hasDecimals), inputFormatters: [ - FilteringTextInputFormatter.deny(RegExp('[\\-|\\ ]')) + FilteringTextInputFormatter.deny(RegExp('[\\-|\\ ]')), + if (!hasDecimals) FilteringTextInputFormatter.deny(RegExp('[\.,]')), ], - hintText: '0.0000', + hintText: hasDecimals ? '0.0000' : '0', borderColor: Colors.transparent, //widget.borderColor, textStyle: TextStyle( diff --git a/lib/src/screens/receive/widgets/lightning_input_form.dart b/lib/src/screens/receive/widgets/lightning_input_form.dart index 24cf794ea..dc176a6f8 100644 --- a/lib/src/screens/receive/widgets/lightning_input_form.dart +++ b/lib/src/screens/receive/widgets/lightning_input_form.dart @@ -4,6 +4,7 @@ import 'package:cake_wallet/src/widgets/base_text_form_field.dart'; import 'package:cake_wallet/themes/extensions/exchange_page_theme.dart'; import 'package:cake_wallet/typography.dart'; import 'package:cake_wallet/view_model/lightning_invoice_page_view_model.dart'; +import 'package:cw_bitcoin/bitcoin_amount_format.dart'; import 'package:cw_core/crypto_currency.dart'; import 'package:flutter/material.dart'; import 'package:flutter_mobx/flutter_mobx.dart'; @@ -44,8 +45,8 @@ class LightningInvoiceForm extends StatelessWidget { controller: amountController, focusNode: depositAmountFocus, maxAmount: '', - minAmount: lightningInvoicePageViewModel.minimum?.toString() ?? '...', - selectedCurrency: CryptoCurrency.btc, + minAmount: (lightningInvoicePageViewModel.minimum != null) ? satsToLightningString(lightningInvoicePageViewModel.minimum!) : '...', + selectedCurrency: CryptoCurrency.satoshis, ); }), SizedBox( diff --git a/lib/view_model/lightning_invoice_page_view_model.dart b/lib/view_model/lightning_invoice_page_view_model.dart index a9e886b18..cd108f30c 100644 --- a/lib/view_model/lightning_invoice_page_view_model.dart +++ b/lib/view_model/lightning_invoice_page_view_model.dart @@ -8,7 +8,6 @@ import 'package:cw_core/currency.dart'; import 'package:cw_core/receive_page_option.dart'; import 'package:cw_core/wallet_base.dart'; import 'package:cw_core/wallet_type.dart'; -import 'package:cw_lightning/lightning_receive_page_option.dart'; import 'package:mobx/mobx.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -114,8 +113,9 @@ abstract class LightningInvoicePageViewModelBase with Store { Future _fetchLimits() async { List limits = await lightningViewModel.invoiceLimitsSats(); - minimum = bitcoinAmountToDouble(amount: int.parse(limits[0]) ~/ 1000); - maximum = bitcoinAmountToDouble(amount: int.parse(limits[1]) ~/ 1000); + minimum = double.parse(limits[0]); + maximum = double.parse(limits[1]); + print(minimum); } @action diff --git a/lib/view_model/lightning_view_model.dart b/lib/view_model/lightning_view_model.dart index 7117b0da4..65045e7fc 100644 --- a/lib/view_model/lightning_view_model.dart +++ b/lib/view_model/lightning_view_model.dart @@ -17,18 +17,20 @@ abstract class LightningViewModelBase with Store { SwapInfo swapInfo = await sdk.receiveOnchain(req: req); print("Minimum amount allowed to deposit in sats: ${swapInfo.minAllowedDeposit}"); print("Maximum amount allowed to deposit in sats: ${swapInfo.maxAllowedDeposit}"); + int fee = swapInfo.channelOpeningFees?.minMsat ?? 2000; + fee = fee ~/ 1000; return [ swapInfo.bitcoinAddress, swapInfo.minAllowedDeposit.toString(), swapInfo.maxAllowedDeposit.toString(), - swapInfo.channelOpeningFees?.minMsat.toString() ?? "2000", + fee.toString(), ]; } Future createInvoice({required String amount, String? description}) async { final sdk = await BreezSDK(); final req = ReceivePaymentRequest( - amountMsat: (double.parse(amount) * 100000000).round(), + amountMsat: (double.parse(amount) * 1000).round(), description: description ?? '', ); final res = await sdk.receivePayment(req: req); @@ -38,11 +40,11 @@ abstract class LightningViewModelBase with Store { Future> invoiceLimitsSats() async { final sdk = await BreezSDK(); final req = ReceivePaymentRequest( - amountMsat: 3000 * 1000, + amountMsat: 3000 * 1000,// 3000 sats description: "limits", ); final res = await sdk.receivePayment(req: req); - int min = (res.openingFeeMsat ?? (2500 * 1000)); + int min = (res.openingFeeMsat ?? (2500 * 1000)) ~/ 1000; int max = 1000000000 * 1000 * 10;// 10 BTC return [min.toString(), max.toString()]; }