CAKE-334 | applied unspent coins control to the app; added unspent_coins_info.dart; reworked createTransaction(), calculateEstimatedFee() and updateUnspent() methods in the electrum_wallet.dart; fixed unspent_coins_list_view_model.dart, unspent_coins_details_view_model.dart, unspent_coins_list_item.dart, unspent_coins_list_page.dart and unspent_coins_details_page.dart; fixed bitcoin_transaction_wrong_balance_exception.dart; added properties to bitcoin_unspent.dart; applied localization to unspent coins pages

This commit is contained in:
OleksandrSobol
2021-07-05 16:52:24 +03:00
parent 3f81f708e5
commit 20e0c830cf
34 changed files with 525 additions and 238 deletions
+2 -4
View File
@@ -1,7 +1,6 @@
import 'package:cake_wallet/bitcoin/bitcoin_amount_format.dart';
import 'package:cake_wallet/bitcoin/bitcoin_transaction_priority.dart';
import 'package:cake_wallet/bitcoin/electrum_wallet.dart';
import 'package:cake_wallet/entities/balance_display_mode.dart';
import 'package:cake_wallet/entities/calculate_fiat_amount_raw.dart';
import 'package:cake_wallet/entities/transaction_description.dart';
import 'package:cake_wallet/entities/transaction_priority.dart';
@@ -20,7 +19,6 @@ import 'package:cake_wallet/core/pending_transaction.dart';
import 'package:cake_wallet/core/validator.dart';
import 'package:cake_wallet/core/wallet_base.dart';
import 'package:cake_wallet/core/execution_state.dart';
import 'package:cake_wallet/bitcoin/bitcoin_wallet.dart';
import 'package:cake_wallet/bitcoin/bitcoin_transaction_credentials.dart';
import 'package:cake_wallet/monero/monero_wallet.dart';
import 'package:cake_wallet/monero/monero_transaction_creation_credentials.dart';
@@ -55,7 +53,7 @@ abstract class SendViewModelBase with Store {
_settingsStore.priority[_wallet.type] = priorities.first;
}
isBitcoinWallet = _wallet is BitcoinWallet;
isElectrumWallet = _wallet is ElectrumWallet;
_setCryptoNumMaximumFractionDigits();
}
@@ -185,7 +183,7 @@ abstract class SendViewModelBase with Store {
PendingTransaction pendingTransaction;
@observable
bool isBitcoinWallet;
bool isElectrumWallet;
@computed
String get balance => _wallet.balance.formattedAvailableBalance ?? '0.0';
@@ -3,6 +3,7 @@ import 'package:cake_wallet/src/screens/transaction_details/textfield_list_item.
import 'package:cake_wallet/src/screens/transaction_details/transaction_details_list_item.dart';
import 'package:cake_wallet/view_model/unspent_coins/unspent_coins_item.dart';
import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/view_model/unspent_coins/unspent_coins_list_view_model.dart';
import 'package:cake_wallet/view_model/unspent_coins/unspent_coins_switch_item.dart';
import 'package:mobx/mobx.dart';
@@ -12,7 +13,9 @@ class UnspentCoinsDetailsViewModel = UnspentCoinsDetailsViewModelBase
with _$UnspentCoinsDetailsViewModel;
abstract class UnspentCoinsDetailsViewModelBase with Store {
UnspentCoinsDetailsViewModelBase({this.unspentCoinsItem}) {
UnspentCoinsDetailsViewModelBase({
this.unspentCoinsItem, this.unspentCoinsListViewModel}) {
final amount = unspentCoinsItem.amount ?? '';
final address = unspentCoinsItem.address ?? '';
isFrozen = unspentCoinsItem.isFrozen ?? false;
@@ -20,25 +23,31 @@ abstract class UnspentCoinsDetailsViewModelBase with Store {
items = [
StandartListItem(
title: 'Amount',
title: S.current.transaction_details_amount,
value: amount
),
StandartListItem(
title: 'Address',
title: S.current.widgets_address,
value: address
),
TextFieldListItem(
title: S.current.note_tap_to_change,
value: note,
onSubmitted: (value) {
note = value;
unspentCoinsItem.note = value;
unspentCoinsListViewModel.saveUnspentCoinInfo(unspentCoinsItem);
}),
UnspentCoinsSwitchItem(
title: 'Freeze',
title: S.current.freeze,
value: '',
switchValue: () => isFrozen,
onSwitchValueChange: (value) {
onSwitchValueChange: (value) async {
isFrozen = value;
unspentCoinsItem.isFrozen = value;
if (value) {
unspentCoinsItem.isSending = !value;
}
await unspentCoinsListViewModel.saveUnspentCoinInfo(unspentCoinsItem);
}
)
];
@@ -51,5 +60,6 @@ abstract class UnspentCoinsDetailsViewModelBase with Store {
String note;
final UnspentCoinsItem unspentCoinsItem;
final UnspentCoinsListViewModel unspentCoinsListViewModel;
List<TransactionDetailsListItem> items;
}
@@ -1,14 +1,33 @@
class UnspentCoinsItem {
UnspentCoinsItem({
import 'package:mobx/mobx.dart';
part 'unspent_coins_item.g.dart';
class UnspentCoinsItem = UnspentCoinsItemBase with _$UnspentCoinsItem;
abstract class UnspentCoinsItemBase with Store {
UnspentCoinsItemBase({
this.address,
this.amount,
this.hash,
this.isFrozen,
this.note,
this.isSending = true});
this.isSending});
final String address;
final String amount;
final bool isFrozen;
final String note;
final bool isSending;
@observable
String address;
@observable
String amount;
@observable
String hash;
@observable
bool isFrozen;
@observable
String note;
@observable
bool isSending;
}
@@ -1,61 +1,58 @@
import 'package:cake_wallet/bitcoin/bitcoin_amount_format.dart';
import 'package:cake_wallet/bitcoin/electrum_wallet.dart';
import 'package:cake_wallet/bitcoin/unspent_coins_info.dart';
import 'package:cake_wallet/core/wallet_base.dart';
import 'package:cake_wallet/view_model/unspent_coins/unspent_coins_item.dart';
import 'package:flutter/foundation.dart';
import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart';
part 'unspent_coins_list_view_model.g.dart';
const List<Map<String, dynamic>> unspentCoinsMap = [
<String, dynamic>{
"address" : "bc1qm80mu5p3mf04a7cj7teymasf04dwpc3av2fwtr",
"amount" : "0.00358 BTC",
"isFrozen" : true,
"note" : "333cvgf23132132132132131321321314rwrtdggfdddewq ewqasfdxgdhgfgfszczcxgbhhhbcgbc"},
<String, dynamic>{
"address" : "bc1qm80mu5p3mf04a7cj7teymasf04dwpc3av2fwtr",
"amount" : "0.00567894 BTC",
"note" : "sfjskf"},
<String, dynamic>{
"address" : "bc1qm80mu5p3mf04a7cj7teymasf04dwpc3av2fwtr",
"amount" : "0.00087 BTC",
"isFrozen" : false},
<String, dynamic>{
"address" : "bc1qm80mu5p3mf04a7cj7teymasf04dwpc3av2fwtr",
"amount" : "0.00012 BTC",
"isFrozen" : true,
"note" : "333cvgf23132132132132131321321314rwrtdggfdddewq ewqasfdxgdhgfgfszczcxgbhhhbcgbc"},
<String, dynamic>{
"address" : "bc1qm80mu5p3mf04a7cj7teymasf04dwpc3av2fwtr",
"amount" : "0.00574 BTC",
"note" : "sffsfsdsgs"},
<String, dynamic>{
"address" : "bc1qm80mu5p3mf04a7cj7teymasf04dwpc3av2fwtr",
"amount" : "0.000482 BTC",
"isFrozen" : false},
<String, dynamic>{},
<String, dynamic>{
"address" : "bc1qm80mu5p3mf04a7cj7teymasf04dwpc3av2fwtr",
"amount" : "0.00012 BTC",
"isFrozen" : true,
"note" : "333cvgf23132132132132131321321314rwrtdggfdddewq ewqasfdxgdhgfgfszczcxgbhhhbcgbc"},
<String, dynamic>{
"address" : "bc1qm80mu5p3mf04a7cj7teymasf04dwpc3av2fwtr",
"amount" : "0.00574 BTC",
"note" : "sffsfsdsgs"},
<String, dynamic>{
"address" : "bc1qm80mu5p3mf04a7cj7teymasf04dwpc3av2fwtr",
"amount" : "0.000482 BTC",
"isFrozen" : false},
];
class UnspentCoinsListViewModel = UnspentCoinsListViewModelBase with _$UnspentCoinsListViewModel;
abstract class UnspentCoinsListViewModelBase with Store {
UnspentCoinsListViewModelBase({
@required WalletBase wallet,
@required Box<UnspentCoinsInfo> unspentCoinsInfo}) {
_unspentCoinsInfo = unspentCoinsInfo;
_wallet = wallet as ElectrumWallet;
_wallet.updateUnspent();
}
ElectrumWallet _wallet;
Box<UnspentCoinsInfo> _unspentCoinsInfo;
@computed
ObservableList<UnspentCoinsItem> get items =>
ObservableList.of(unspentCoinsMap.map((elem) =>
UnspentCoinsItem(
address: elem["address"] as String,
amount: elem["amount"] as String,
isFrozen: elem["isFrozen"] as bool,
note: elem["note"] as String
)));
ObservableList.of(_wallet.unspentCoins.map((elem) {
final amount = bitcoinAmountToString(amount: elem.value) +
' ${_wallet.currency.title}';
return UnspentCoinsItem(
address: elem.address.address,
amount: amount,
hash: elem.hash,
isFrozen: elem.isFrozen,
note: elem.note,
isSending: elem.isSending
);
}));
Future<void> saveUnspentCoinInfo(UnspentCoinsItem item) async {
try {
final info = _unspentCoinsInfo.values
.firstWhere((element) => element.walletId.contains(_wallet.id) &&
element.hash.contains(item.hash));
info.isFrozen = item.isFrozen;
info.isSending = item.isSending;
info.note = item.note;
await info.save();
await _wallet.updateUnspent();
} catch (e) {
print(e.toString());
}
}
}