Skip to content

Commit

Permalink
Cw 498 improve wallet accounts (#1152)
Browse files Browse the repository at this point in the history
* CW-498 Call updateTransactions on account change

* CW-498 Code Cleanup

* CW-498 Add Wallet Address to trade to match transactions to trades

* CW-498 Fix containsAddress to include only addresses of account

* Handle nullability for fromWalletAddress

---------

Co-authored-by: OmarHatem <[email protected]>
  • Loading branch information
konstantinullrich and OmarHatem28 authored Nov 2, 2023
1 parent 5bd382f commit e5d9931
Show file tree
Hide file tree
Showing 13 changed files with 206 additions and 230 deletions.
4 changes: 3 additions & 1 deletion cw_core/lib/wallet_addresses.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ abstract class WalletAddresses {
print(e.toString());
}
}
}

bool containsAddress(String address) => addressesMap.containsKey(address);
}
10 changes: 7 additions & 3 deletions cw_haven/lib/haven_wallet_addresses.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ abstract class HavenWalletAddressesBase extends WalletAddressesWithAccount<Accou

bool validate() {
accountList.update();
final accountListLength = accountList.accounts.length ?? 0;
final accountListLength = accountList.accounts.length;

if (accountListLength <= 0) {
return false;
}

subaddressList.update(accountIndex: accountList.accounts.first.id);
final subaddressListLength = subaddressList.subaddresses.length ?? 0;
final subaddressListLength = subaddressList.subaddresses.length;

if (subaddressListLength <= 0) {
return false;
Expand All @@ -84,4 +84,8 @@ abstract class HavenWalletAddressesBase extends WalletAddressesWithAccount<Accou
subaddress = subaddressList.subaddresses.first;
address = subaddress!.address;
}
}

@override
bool containsAddress(String address) =>
addressInfos[account?.id ?? 0]?.any((it) => it.address == address) ?? false;
}
14 changes: 5 additions & 9 deletions cw_monero/lib/monero_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,15 @@ abstract class MoneroWalletBase
walletAddresses = MoneroWalletAddresses(walletInfo, transactionHistory);

_onAccountChangeReaction = reaction((_) => walletAddresses.account, (Account? account) {
if (account == null) {
return;
}
if (account == null) return;

balance = ObservableMap<CryptoCurrency, MoneroBalance>.of(<CryptoCurrency, MoneroBalance>{
currency: MoneroBalance(
fullBalance: monero_wallet.getFullBalance(accountIndex: account.id),
unlockedBalance: monero_wallet.getUnlockedBalance(accountIndex: account.id))
});
_updateSubAddress(isEnabledAutoGenerateSubaddress, account: account);
_askForUpdateTransactionHistory();
});

reaction((_) => isEnabledAutoGenerateSubaddress, (bool enabled) {
Expand Down Expand Up @@ -362,9 +361,7 @@ abstract class MoneroWalletBase
}

@override
Future<void> changePassword(String password) async {
monero_wallet.setPasswordSync(password);
}
Future<void> changePassword(String password) async => monero_wallet.setPasswordSync(password);

Future<int> getNodeHeight() async => monero_wallet.getNodeHeight();

Expand Down Expand Up @@ -499,9 +496,8 @@ abstract class MoneroWalletBase
}
}

String getSubaddressLabel(int accountIndex, int addressIndex) {
return monero_wallet.getSubaddressLabel(accountIndex, addressIndex);
}
String getSubaddressLabel(int accountIndex, int addressIndex) =>
monero_wallet.getSubaddressLabel(accountIndex, addressIndex);

List<MoneroTransactionInfo> _getAllTransactionsOfAccount(int? accountIndex) => transaction_history
.getAllTransactions()
Expand Down
8 changes: 6 additions & 2 deletions cw_monero/lib/monero_wallet_addresses.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'package:cw_core/account.dart';
import 'package:cw_core/address_info.dart';
import 'package:cw_core/subaddress.dart';
import 'package:cw_core/wallet_addresses.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/account.dart';
import 'package:cw_monero/api/wallet.dart';
import 'package:cw_monero/monero_account_list.dart';
import 'package:cw_monero/monero_subaddress_list.dart';
import 'package:cw_core/subaddress.dart';
import 'package:cw_monero/monero_transaction_history.dart';
import 'package:mobx/mobx.dart';

Expand Down Expand Up @@ -112,4 +112,8 @@ abstract class MoneroWalletAddressesBase extends WalletAddresses with Store {
subaddress = subaddressList.subaddresses.last;
address = subaddress!.address;
}

@override
bool containsAddress(String address) =>
addressInfos[account?.id ?? 0]?.any((it) => it.address == address) ?? false;
}
11 changes: 9 additions & 2 deletions lib/exchange/trade.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Trade extends HiveObject {
this.password,
this.providerId,
this.providerName,
this.fromWalletAddress
}) {
if (provider != null) providerRaw = provider.raw;

Expand Down Expand Up @@ -101,6 +102,9 @@ class Trade extends HiveObject {
@HiveField(16)
String? providerName;

@HiveField(17)
String? fromWalletAddress;

static Trade fromMap(Map<String, Object?> map) {
return Trade(
id: map['id'] as String,
Expand All @@ -110,7 +114,9 @@ class Trade extends HiveObject {
createdAt:
map['date'] != null ? DateTime.fromMillisecondsSinceEpoch(map['date'] as int) : null,
amount: map['amount'] as String,
walletId: map['wallet_id'] as String);
walletId: map['wallet_id'] as String,
fromWalletAddress: map['from_wallet_address'] as String?
);
}

Map<String, dynamic> toMap() {
Expand All @@ -121,7 +127,8 @@ class Trade extends HiveObject {
'output': to.serialize(),
'date': createdAt != null ? createdAt!.millisecondsSinceEpoch : null,
'amount': amount,
'wallet_id': walletId
'wallet_id': walletId,
'from_wallet_address': fromWalletAddress
};
}

Expand Down
140 changes: 56 additions & 84 deletions lib/monero/cw_monero.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ class CWMoneroWalletDetails extends MoneroWalletDetails {
@computed
@override
MoneroBalance get balance {
final moneroWallet = _wallet as MoneroWallet;
final balance = moneroWallet.balance;
throw Exception('Unimplemented');
// return MoneroBalance();
//return MoneroBalance(
Expand All @@ -132,14 +130,10 @@ class CWMoneroWalletDetails extends MoneroWalletDetails {

class CWMonero extends Monero {
@override
MoneroAccountList getAccountList(Object wallet) {
return CWMoneroAccountList(wallet);
}
MoneroAccountList getAccountList(Object wallet) => CWMoneroAccountList(wallet);

@override
MoneroSubaddressList getSubaddressList(Object wallet) {
return CWMoneroSubaddressList(wallet);
}
MoneroSubaddressList getSubaddressList(Object wallet) => CWMoneroSubaddressList(wallet);

@override
TransactionHistoryBase getTransactionHistory(Object wallet) {
Expand All @@ -148,19 +142,13 @@ class CWMonero extends Monero {
}

@override
MoneroWalletDetails getMoneroWalletDetails(Object wallet) {
return CWMoneroWalletDetails(wallet);
}
MoneroWalletDetails getMoneroWalletDetails(Object wallet) => CWMoneroWalletDetails(wallet);

@override
int getHeigthByDate({required DateTime date}) {
return getMoneroHeigthByDate(date: date);
}
int getHeightByDate({required DateTime date}) => getMoneroHeigthByDate(date: date);

@override
TransactionPriority getDefaultTransactionPriority() {
return MoneroTransactionPriority.automatic;
}
TransactionPriority getDefaultTransactionPriority() => MoneroTransactionPriority.automatic;

@override
TransactionPriority getMoneroTransactionPrioritySlow() => MoneroTransactionPriority.slow;
Expand All @@ -170,14 +158,11 @@ class CWMonero extends Monero {
MoneroTransactionPriority.automatic;

@override
TransactionPriority deserializeMoneroTransactionPriority({required int raw}) {
return MoneroTransactionPriority.deserialize(raw: raw);
}
TransactionPriority deserializeMoneroTransactionPriority({required int raw}) =>
MoneroTransactionPriority.deserialize(raw: raw);

@override
List<TransactionPriority> getTransactionPriorities() {
return MoneroTransactionPriority.all;
}
List<TransactionPriority> getTransactionPriorities() => MoneroTransactionPriority.all;

@override
List<String> getMoneroWordList(String language) {
Expand Down Expand Up @@ -209,41 +194,37 @@ class CWMonero extends Monero {

@override
WalletCredentials createMoneroRestoreWalletFromKeysCredentials(
{required String name,
required String spendKey,
required String viewKey,
required String address,
required String password,
required String language,
required int height}) {
return MoneroRestoreWalletFromKeysCredentials(
name: name,
spendKey: spendKey,
viewKey: viewKey,
address: address,
password: password,
language: language,
height: height);
}
{required String name,
required String spendKey,
required String viewKey,
required String address,
required String password,
required String language,
required int height}) =>
MoneroRestoreWalletFromKeysCredentials(
name: name,
spendKey: spendKey,
viewKey: viewKey,
address: address,
password: password,
language: language,
height: height);

@override
WalletCredentials createMoneroRestoreWalletFromSeedCredentials(
{required String name,
required String password,
required int height,
required String mnemonic}) {
return MoneroRestoreWalletFromSeedCredentials(
name: name, password: password, height: height, mnemonic: mnemonic);
}
{required String name,
required String password,
required int height,
required String mnemonic}) =>
MoneroRestoreWalletFromSeedCredentials(
name: name, password: password, height: height, mnemonic: mnemonic);

@override
WalletCredentials createMoneroNewWalletCredentials({
required String name,
required String language,
String? password,
}) {
return MoneroNewWalletCredentials(name: name, password: password, language: language);
}
String? password}) =>
MoneroNewWalletCredentials(name: name, password: password, language: language);

@override
Map<String, String> getKeys(Object wallet) {
Expand All @@ -259,43 +240,37 @@ class CWMonero extends Monero {

@override
Object createMoneroTransactionCreationCredentials(
{required List<Output> outputs, required TransactionPriority priority}) {
return MoneroTransactionCreationCredentials(
outputs: outputs
.map((out) => OutputInfo(
fiatAmount: out.fiatAmount,
cryptoAmount: out.cryptoAmount,
address: out.address,
note: out.note,
sendAll: out.sendAll,
extractedAddress: out.extractedAddress,
isParsedAddress: out.isParsedAddress,
formattedCryptoAmount: out.formattedCryptoAmount))
.toList(),
priority: priority as MoneroTransactionPriority);
}
{required List<Output> outputs, required TransactionPriority priority}) =>
MoneroTransactionCreationCredentials(
outputs: outputs
.map((out) => OutputInfo(
fiatAmount: out.fiatAmount,
cryptoAmount: out.cryptoAmount,
address: out.address,
note: out.note,
sendAll: out.sendAll,
extractedAddress: out.extractedAddress,
isParsedAddress: out.isParsedAddress,
formattedCryptoAmount: out.formattedCryptoAmount))
.toList(),
priority: priority as MoneroTransactionPriority);

@override
Object createMoneroTransactionCreationCredentialsRaw(
{required List<OutputInfo> outputs, required TransactionPriority priority}) {
return MoneroTransactionCreationCredentials(
outputs: outputs, priority: priority as MoneroTransactionPriority);
}
{required List<OutputInfo> outputs, required TransactionPriority priority}) =>
MoneroTransactionCreationCredentials(
outputs: outputs, priority: priority as MoneroTransactionPriority);

@override
String formatterMoneroAmountToString({required int amount}) {
return moneroAmountToString(amount: amount);
}
String formatterMoneroAmountToString({required int amount}) =>
moneroAmountToString(amount: amount);

@override
double formatterMoneroAmountToDouble({required int amount}) {
return moneroAmountToDouble(amount: amount);
}
double formatterMoneroAmountToDouble({required int amount}) =>
moneroAmountToDouble(amount: amount);

@override
int formatterMoneroParseAmount({required String amount}) {
return moneroParseAmount(amount: amount);
}
int formatterMoneroParseAmount({required String amount}) => moneroParseAmount(amount: amount);

@override
Account getCurrentAccount(Object wallet) {
Expand All @@ -312,9 +287,7 @@ class CWMonero extends Monero {
}

@override
void onStartup() {
monero_wallet_api.onStartup();
}
void onStartup() => monero_wallet_api.onStartup();

@override
int getTransactionInfoAccountId(TransactionInfo tx) {
Expand All @@ -324,9 +297,8 @@ class CWMonero extends Monero {

@override
WalletService createMoneroWalletService(
Box<WalletInfo> walletInfoSource, Box<UnspentCoinsInfo> unspentCoinSource) {
return MoneroWalletService(walletInfoSource, unspentCoinSource);
}
Box<WalletInfo> walletInfoSource, Box<UnspentCoinsInfo> unspentCoinSource) =>
MoneroWalletService(walletInfoSource, unspentCoinSource);

@override
String getTransactionAddress(Object wallet, int accountIndex, int addressIndex) {
Expand Down
13 changes: 5 additions & 8 deletions lib/src/widgets/blockchain_height_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,13 @@ class BlockchainHeightState extends State<BlockchainHeightWidget> {
restoreHeightController.addListener(() {
if (restoreHeightController.text.isNotEmpty) {
widget.onHeightOrDateEntered?.call(true);
}
else {
} else {
widget.onHeightOrDateEntered?.call(false);
dateController.text = '';
}
try {
_changeHeight(restoreHeightController.text != null &&
restoreHeightController.text.isNotEmpty
? int.parse(restoreHeightController.text)
: 0);
_changeHeight(
restoreHeightController.text.isNotEmpty ? int.parse(restoreHeightController.text) : 0);
} catch (_) {
_changeHeight(0);
}
Expand Down Expand Up @@ -117,7 +114,7 @@ class BlockchainHeightState extends State<BlockchainHeightWidget> {
);
}

Future _selectDate(BuildContext context) async {
Future<void> _selectDate(BuildContext context) async {
final now = DateTime.now();
final date = await getDate(
context: context,
Expand All @@ -126,7 +123,7 @@ class BlockchainHeightState extends State<BlockchainHeightWidget> {
lastDate: now);

if (date != null) {
final height = monero!.getHeigthByDate(date: date);
final height = monero!.getHeightByDate(date: date);
setState(() {
dateController.text = DateFormat('yyyy-MM-dd').format(date);
restoreHeightController.text = '$height';
Expand Down
Loading

0 comments on commit e5d9931

Please sign in to comment.