Fixes.

M committed Nov 8, 2020 at 22:44 UTC 5c4e1b4d013222001adf76b7ca6ccbbbbc3a5aab
5 files changed +83 -46
cw_monero/ios/Classes/monero_api.cpp
-2
@@ -351,13 +351,11 @@ extern "C"
351
352 uint64_t get_full_balance(uint32_t account_index)
353 {
354 -// return 0;
354 return get_current_wallet()->balance(account_index);
355 }
356
357 uint64_t get_unlocked_balance(uint32_t account_index)
358 {
360 -// return 0;
359 return get_current_wallet()->unlockedBalance(account_index);
360 }
361
lib/main.dart
+52 -40
@@ -29,47 +29,59 @@ import 'package:cake_wallet/src/screens/root/root.dart';
29 final navigatorKey = GlobalKey<NavigatorState>();
30
31 void main() async {
32 - WidgetsFlutterBinding.ensureInitialized();
32 + try {
33 + WidgetsFlutterBinding.ensureInitialized();
34
34 - final appDir = await getApplicationDocumentsDirectory();
35 - Hive.init(appDir.path);
36 - Hive.registerAdapter(ContactAdapter());
37 - Hive.registerAdapter(NodeAdapter());
38 - Hive.registerAdapter(TransactionDescriptionAdapter());
39 - Hive.registerAdapter(TradeAdapter());
40 - Hive.registerAdapter(WalletInfoAdapter());
41 - Hive.registerAdapter(WalletTypeAdapter());
42 - Hive.registerAdapter(TemplateAdapter());
43 - Hive.registerAdapter(ExchangeTemplateAdapter());
44 -
45 - final secureStorage = FlutterSecureStorage();
46 - final transactionDescriptionsBoxKey = await getEncryptionKey(
47 - secureStorage: secureStorage, forKey: TransactionDescription.boxKey);
48 - final tradesBoxKey = await getEncryptionKey(
49 - secureStorage: secureStorage, forKey: Trade.boxKey);
50 - final contacts = await Hive.openBox<Contact>(Contact.boxName);
51 - final nodes = await Hive.openBox<Node>(Node.boxName);
52 - final transactionDescriptions = await Hive.openBox<TransactionDescription>(
53 - TransactionDescription.boxName,
54 - encryptionKey: transactionDescriptionsBoxKey);
55 - final trades =
56 - await Hive.openBox<Trade>(Trade.boxName, encryptionKey: tradesBoxKey);
57 - final walletInfoSource = await Hive.openBox<WalletInfo>(WalletInfo.boxName);
58 - final templates = await Hive.openBox<Template>(Template.boxName);
59 - final exchangeTemplates =
60 - await Hive.openBox<ExchangeTemplate>(ExchangeTemplate.boxName);
61 - await initialSetup(
62 - sharedPreferences: await SharedPreferences.getInstance(),
63 - nodes: nodes,
64 - walletInfoSource: walletInfoSource,
65 - contactSource: contacts,
66 - tradesSource: trades,
67 - // fiatConvertationService: fiatConvertationService,
68 - templates: templates,
69 - exchangeTemplates: exchangeTemplates,
70 - transactionDescriptions: transactionDescriptions,
71 - initialMigrationVersion: 4);
72 - runApp(App());
35 + final appDir = await getApplicationDocumentsDirectory();
36 + Hive.init(appDir.path);
37 + Hive.registerAdapter(ContactAdapter());
38 + Hive.registerAdapter(NodeAdapter());
39 + Hive.registerAdapter(TransactionDescriptionAdapter());
40 + Hive.registerAdapter(TradeAdapter());
41 + Hive.registerAdapter(WalletInfoAdapter());
42 + Hive.registerAdapter(WalletTypeAdapter());
43 + Hive.registerAdapter(TemplateAdapter());
44 + Hive.registerAdapter(ExchangeTemplateAdapter());
45 + final secureStorage = FlutterSecureStorage();
46 + final transactionDescriptionsBoxKey = await getEncryptionKey(
47 + secureStorage: secureStorage, forKey: TransactionDescription.boxKey);
48 + final tradesBoxKey = await getEncryptionKey(
49 + secureStorage: secureStorage, forKey: Trade.boxKey);
50 + final contacts = await Hive.openBox<Contact>(Contact.boxName);
51 + final nodes = await Hive.openBox<Node>(Node.boxName);
52 + final transactionDescriptions = await Hive.openBox<TransactionDescription>(
53 + TransactionDescription.boxName,
54 + encryptionKey: transactionDescriptionsBoxKey);
55 + final trades =
56 + await Hive.openBox<Trade>(Trade.boxName, encryptionKey: tradesBoxKey);
57 + final walletInfoSource = await Hive.openBox<WalletInfo>(WalletInfo.boxName);
58 + final templates = await Hive.openBox<Template>(Template.boxName);
59 + final exchangeTemplates =
60 + await Hive.openBox<ExchangeTemplate>(ExchangeTemplate.boxName);
61 + await initialSetup(
62 + sharedPreferences: await SharedPreferences.getInstance(),
63 + nodes: nodes,
64 + walletInfoSource: walletInfoSource,
65 + contactSource: contacts,
66 + tradesSource: trades,
67 + // fiatConvertationService: fiatConvertationService,
68 + templates: templates,
69 + exchangeTemplates: exchangeTemplates,
70 + transactionDescriptions: transactionDescriptions,
71 + initialMigrationVersion: 4);
72 + runApp(App());
73 + } catch (e) {
74 + runApp(MaterialApp(
75 + debugShowCheckedModeBanner: true,
76 + home: Scaffold(
77 + body: Container(
78 + margin:
79 + EdgeInsets.only(top: 50, left: 20, right: 20, bottom: 20),
80 + child: Text(
81 + 'Error:\n${e.toString()}',
82 + style: TextStyle(fontSize: 22),
83 + )))));
84 + }
85 }
86
87 Future<void> initialSetup(
lib/monero/monero_amount_format.dart
+6 -4
@@ -7,9 +7,11 @@ final moneroAmountFormat = NumberFormat()
7 ..maximumFractionDigits = moneroAmountLength
8 ..minimumFractionDigits = 1;
9
10 -String moneroAmountToString({int amount}) =>
11 - moneroAmountFormat.format(cryptoAmountToDouble(amount: amount, divider: moneroAmountDivider));
10 +String moneroAmountToString({int amount}) => moneroAmountFormat
11 + .format(cryptoAmountToDouble(amount: amount, divider: moneroAmountDivider));
12
13 -double moneroAmountToDouble({int amount}) => cryptoAmountToDouble(amount: amount, divider: moneroAmountDivider);
13 +double moneroAmountToDouble({int amount}) =>
14 + cryptoAmountToDouble(amount: amount, divider: moneroAmountDivider);
15
15 -int moneroParseAmount({String amount}) => moneroAmountFormat.parse(amount).toInt();
\ No newline at end of file
16 +int moneroParseAmount({String amount}) =>
17 + (double.parse(amount) * moneroAmountDivider).toInt();
lib/monero/monero_transaction_creation_exception.dart new
+8
@@ -0,0 +1,8 @@
1 +class MoneroTransactionCreationException implements Exception {
2 + MoneroTransactionCreationException(this.message);
3 +
4 + final String message;
5 +
6 + @override
7 + String toString() => message;
8 +}
\ No newline at end of file
lib/monero/monero_wallet.dart
+17
@@ -1,3 +1,7 @@
1 +import 'dart:async';
2 +
3 +import 'package:cake_wallet/monero/monero_amount_format.dart';
4 +import 'package:cake_wallet/monero/monero_transaction_creation_exception.dart';
5 import 'package:flutter/foundation.dart';
6 import 'package:mobx/mobx.dart';
7 import 'package:cw_monero/wallet.dart';
@@ -170,6 +174,19 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance> with Store {
174 @override
175 Future<PendingTransaction> createTransaction(Object credentials) async {
176 final _credentials = credentials as MoneroTransactionCreationCredentials;
177 + final amount = moneroParseAmount(amount: _credentials.amount);
178 + final unlockedBalance =
179 + monero_wallet.getUnlockedBalance(accountIndex: account.id);
180 +
181 + if (unlockedBalance < amount) {
182 + throw MoneroTransactionCreationException(
183 + 'Incorrect unlocked balance. Unlocked: $unlockedBalance. Transaction amount: ${_credentials.amount}.');
184 + }
185 +
186 + if (!(syncStatus is SyncedSyncStatus)) {
187 + throw MoneroTransactionCreationException('The wallet is not synced.');
188 + }
189 +
190 final pendingTransactionDescription =
191 await transaction_history.createTransaction(
192 address: _credentials.address,