Skip to content

Commit

Permalink
feat: use myWitWallet adapters for db
Browse files Browse the repository at this point in the history
  • Loading branch information
gabaldon committed Mar 1, 2024
1 parent beabe64 commit 3161036
Show file tree
Hide file tree
Showing 23 changed files with 210 additions and 43 deletions.
2 changes: 1 addition & 1 deletion lib/bloc/crypto/crypto_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:my_wit_wallet/util/storage/cache/implementations/vtt_get_through_block_explorer.dart';
import 'package:my_wit_wallet/util/storage/database/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/adapters/transaction_adapter.dart';
import 'package:witnet/data_structures.dart';
import 'package:witnet/constants.dart';
import 'package:witnet/crypto.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/bloc/explorer/api_explorer.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:my_wit_wallet/util/storage/database/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/adapters/transaction_adapter.dart';
import 'package:witnet/data_structures.dart';
import 'package:witnet/explorer.dart';
import 'package:witnet/schema.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/bloc/explorer/explorer_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:my_wit_wallet/shared/api_database.dart';
import 'package:my_wit_wallet/shared/locator.dart';
import 'package:my_wit_wallet/util/storage/database/account.dart';
import 'package:my_wit_wallet/util/storage/database/stats.dart';
import 'package:my_wit_wallet/util/storage/database/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/adapters/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/wallet.dart';
import 'package:my_wit_wallet/util/storage/database/wallet_storage.dart';
import 'package:witnet/data_structures.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:my_wit_wallet/constants.dart';
import 'package:my_wit_wallet/util/allow_biometrics.dart';
import 'package:my_wit_wallet/util/filter_utxos.dart';
import 'package:my_wit_wallet/util/get_utxos_match_inputs.dart';
import 'package:my_wit_wallet/util/storage/database/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/adapters/transaction_adapter.dart';
import 'package:witnet/constants.dart';
import 'package:witnet/data_structures.dart';
import 'package:witnet/explorer.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/dashboard/view/transactions_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:my_wit_wallet/bloc/explorer/explorer_bloc.dart';
import 'package:my_wit_wallet/screens/dashboard/view/dashboard_screen.dart';
import 'package:my_wit_wallet/theme/extended_theme.dart';
import 'package:my_wit_wallet/util/storage/database/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/adapters/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/wallet.dart';
import 'package:my_wit_wallet/widgets/transactions_list.dart';
import 'package:number_paginator/number_paginator.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/api_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'package:my_wit_wallet/util/storage/path_provider_interface.dart';

import 'package:my_wit_wallet/util/storage/database/account.dart';
import 'package:my_wit_wallet/util/storage/database/wallet_storage.dart';
import 'package:my_wit_wallet/util/storage/database/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/adapters/transaction_adapter.dart';
import 'locator.dart';

class DatabaseException {
Expand Down
2 changes: 1 addition & 1 deletion lib/util/storage/database/account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:core';

import 'package:my_wit_wallet/shared/api_database.dart';
import 'package:my_wit_wallet/shared/locator.dart';
import 'package:my_wit_wallet/util/storage/database/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/adapters/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/wallet.dart';
import 'package:my_wit_wallet/util/utxo_list_to_string.dart';
import 'package:quiver/core.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,184 @@ class MintEntry {
reverted: blockDetails.reverted,
);
}

extension InputUtxoAdapter on InputUtxo {
static InputUtxo fromDBJson(Map<String, dynamic> json) => InputUtxo(
address: json["pkh"],
inputUtxo: json["output_pointer"],
value: json["value"]);

static InputUtxo fromJson(Map<String, dynamic> json) {
bool dbJson = json["pkh"] != null;
if (dbJson) {
return fromDBJson(json);
} else {
return InputUtxo.fromJson(json);
}
}
}

// TODO: it is not used, delete if not necessary
extension AddressBlocksAdapter on AddressBlocks {
static AddressBlocks fromDBJson(List<dynamic> data) {
return AddressBlocks(
address: data[0]['address'],
blocks: List<BlockInfo>.from(
data.map((blockInfo) => BlockInfo.fromJson(blockInfo))),
);
}

static AddressBlocks fromJson(List<dynamic> data) {
bool dbJson = data[0]['miner'] == null;
if (dbJson) {
return fromDBJson(data);
} else {
return AddressBlocks.fromJson(data);
}
}
}

extension ValueTransferAdapter on ValueTransferInfo {
static ValueTransferInfo fromJson(Map<String, dynamic> data) {
bool dbJson = data["epoch"] == null;
if (dbJson) {
print('------FROM DB JSON--------');
return fromDBJson(data);
} else {
print('------FROM EXPLORER JSON------');
return ValueTransferInfo.fromJson(data);
}
}

static ValueTransferInfo fromDBJson(Map<String, dynamic> data) {
List<String> outputAddresses = getOrDefault(data).outputAddresses;
List<int> outputValues = getOrDefault(data).outputValues;
List<ValueTransferOutput> outputs = [];
if (data['outputs'] != null) {
data['outputs'].forEach((element) {
Address address = Address.fromAddress(element['pkh']);

outputs.add(ValueTransferOutput(
pkh: address.publicKeyHash!,
timeLock: element['time_lock'],
value: element['value']));
});
} else {
for (int i = 0; i < outputValues.length; i++) {
ValueTransferOutput vto = ValueTransferOutput(
value: outputValues[i],
pkh: Address.fromAddress(outputAddresses[i]).publicKeyHash!,
timeLock: 0,
);
outputs.add(vto);
}
}
return ValueTransferInfo(
epoch: data["txn_epoch"],
timestamp: data["txn_time"],
hash: data["txn_hash"],
block: data["block_hash"],
inputUtxos: List<InputUtxo>.from(
data["inputs"].map((x) => InputUtxoAdapter.fromJson(x))),
fee: data["fee"],
priority: data["priority"],
weight: data["weight"],
status: TransactionStatus.fromJson(data).status,
outputs: outputs,
value: getOrDefault(data).value,
confirmed: getOrDefault(data).confirmed,
reverted: getOrDefault(data).reverted,
inputAddresses: getOrDefault(data).inputAddresses,
inputsMerged: getOrDefault(data).inputsMerged,
outputAddresses: getOrDefault(data).outputAddresses,
outputValues: getOrDefault(data).outputValues,
timelocks: getOrDefault(data).timelocks,
utxos: getOrDefault(data).utxos,
utxosMerged: getOrDefault(data).utxosMerged,
trueOutputAddresses: getOrDefault(data).trueOutputAddresses,
changeOutputAddresses: getOrDefault(data).outputAddresses,
trueValue: getOrDefault(data).trueValue,
changeValue: getOrDefault(data).changeValue);
}
}

class NullableFields {
final int? value;
final bool confirmed;
final bool reverted;
final List<String> inputAddresses;
final List<InputMerged> inputsMerged;
final List<String> outputAddresses;
final List<int> outputValues;
final List<int> timelocks;
final List<TransactionUtxo> utxos;
final List<TransactionUtxo> utxosMerged;
final List<String> trueOutputAddresses;
final List<String> changeOutputAddresses;
final int? trueValue;
final int? changeValue;
NullableFields(
{required this.changeOutputAddresses,
required this.changeValue,
required this.confirmed,
required this.inputAddresses,
required this.inputsMerged,
required this.outputAddresses,
required this.outputValues,
required this.reverted,
required this.timelocks,
required this.trueOutputAddresses,
required this.trueValue,
required this.utxos,
required this.utxosMerged,
required this.value});
}

NullableFields getOrDefault(Map<String, dynamic> data) {
print('-----get or default-----');
return NullableFields(
value: data["value"] ?? null,
confirmed: data["confirmed"] ??
TransactionStatus.fromJson(data).status == TxStatusLabel.confirmed,
reverted: data["reverted"] ??
TransactionStatus.fromJson(data).status == TxStatusLabel.reverted,
inputAddresses: data["input_addresses"] != null
? List<String>.from(data["input_addresses"])
: List<String>.from(data["inputs"].map((input) {
print('inout address:: ${input}');
return input['pkh'];
}).toList()),
inputsMerged: data["inputs_merged"] != null
? List<InputMerged>.from(
data["inputs_merged"].map((x) => InputMerged.fromJson(x)))
: [],
outputAddresses: data["output_addresses"] != null
? List<String>.from(data["output_addresses"])
: List<String>.from(
data["outputs"].map((output) => output['pkh']).toList()),
outputValues: data["output_values"] != null
? List<int>.from(data["output_values"])
: List<int>.from(
data["outputs"].map((output) => output['value']).toList()),
timelocks: data["timelocks"] != null
? List<int>.from(data["timelocks"])
: List<int>.from(
data["outputs"].map((output) => output['time_lock']).toList()),
utxos: data["utxos"] != null
? List<TransactionUtxo>.from(data["utxos"]
.map((e) => TransactionUtxo.fromJson(Map<String, dynamic>.from(e))))
: [],
utxosMerged: data["utxos_merged"] != null
? List<TransactionUtxo>.from(data["utxos_merged"]
.map((e) => TransactionUtxo.fromJson(Map<String, dynamic>.from(e))))
: [],
trueOutputAddresses: data["true_output_addresses"] != null
? List<String>.from(data["true_output_addresses"])
: [],
changeOutputAddresses: data["change_output_addresses"] != null
? List<String>.from(data["change_output_addresses"])
: [],
trueValue: data["true_value"],
changeValue: data["change_value"],
);
}
13 changes: 7 additions & 6 deletions lib/util/storage/database/database_isolate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:convert';
import 'dart:isolate';

import 'package:my_wit_wallet/util/storage/database/stats.dart';
import 'package:my_wit_wallet/util/storage/database/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/adapters/transaction_adapter.dart';
import 'package:witnet/explorer.dart';
import 'package:my_wit_wallet/util/storage/database/wallet.dart';

Expand Down Expand Up @@ -145,7 +145,8 @@ Future<void> _addRecord(
value = await dbService.add(Wallet.fromJson(params['value']));
break;
case 'vtt':
value = await dbService.add(ValueTransferInfo.fromJson(params['value']));
value =
await dbService.add(ValueTransferAdapter.fromJson(params['value']));
break;
case 'account':
value = await dbService.add(Account.fromJson(params['value']));
Expand Down Expand Up @@ -174,8 +175,8 @@ Future<void> _deleteRecord(
value = await dbService.delete(Wallet.fromJson(params['value']));
break;
case 'vtt':
value =
await dbService.delete(ValueTransferInfo.fromJson(params['value']));
value = await dbService
.delete(ValueTransferAdapter.fromJson(params['value']));
break;
case 'account':
value = await dbService.delete(Account.fromJson(params['value']));
Expand Down Expand Up @@ -210,8 +211,8 @@ Future<void> _updateRecord(
value = await dbService.update(MintEntry.fromJson(params['value']));
break;
case 'vtt':
value =
await dbService.update(ValueTransferInfo.fromJson(params['value']));
value = await dbService
.update(ValueTransferAdapter.fromJson(params['value']));
break;
case 'account':
value = await dbService.update(Account.fromJson(params['value']));
Expand Down
2 changes: 1 addition & 1 deletion lib/util/storage/database/database_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:async';
import 'package:my_wit_wallet/util/storage/database/get_account_mints_map.dart';
import 'package:my_wit_wallet/util/storage/database/get_account_vtts_map.dart';
import 'package:my_wit_wallet/util/storage/database/stats.dart';
import 'package:my_wit_wallet/util/storage/database/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/adapters/transaction_adapter.dart';
import 'package:sembast/sembast_io.dart';
import 'package:sembast/sembast.dart';
import 'package:witnet/explorer.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/util/storage/database/get_account_mints_map.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:my_wit_wallet/util/storage/database/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/adapters/transaction_adapter.dart';
import 'package:witnet/schema.dart';

Map<String, List<MintEntry>> getAccountMintsMap(List<MintEntry> vttList) {
Expand Down
6 changes: 3 additions & 3 deletions lib/util/storage/database/transaction_repository.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:my_wit_wallet/util/storage/database/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/adapters/transaction_adapter.dart';
import 'package:sembast/sembast.dart';
import 'package:witnet/explorer.dart';
import 'package:witnet/schema.dart';
Expand Down Expand Up @@ -42,7 +42,7 @@ class VttRepository extends _TransactionRepository {
final snapshots = await _store.find(databaseClient);
try {
List<ValueTransferInfo> transactions = snapshots
.map((snapshot) => ValueTransferInfo.fromJson(
.map((snapshot) => ValueTransferAdapter.fromJson(
snapshot.value as Map<String, dynamic>))
.toList(growable: false);
return transactions;
Expand Down Expand Up @@ -88,7 +88,7 @@ class VttRepository extends _TransactionRepository {
await _store.record(txHash).get(databaseClient);

ValueTransferInfo valueTransferInfo =
ValueTransferInfo.fromJson(valueTransferInfoDbJson);
ValueTransferAdapter.fromJson(valueTransferInfoDbJson);

return valueTransferInfo;
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion lib/util/storage/database/wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:my_wit_wallet/screens/dashboard/view/dashboard_screen.dart';
import 'package:my_wit_wallet/shared/api_database.dart';
import 'package:my_wit_wallet/shared/locator.dart';
import 'package:my_wit_wallet/util/storage/database/stats.dart';
import 'package:my_wit_wallet/util/storage/database/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/adapters/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/wallet_storage.dart';
import 'package:witnet/constants.dart';
import 'package:witnet/crypto.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/util/storage/database/wallet_storage.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:my_wit_wallet/util/storage/database/stats.dart';
import 'package:my_wit_wallet/util/storage/database/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/adapters/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/wallet.dart';
import 'package:witnet/explorer.dart';

Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/speed_up_tx.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:my_wit_wallet/screens/dashboard/view/dashboard_screen.dart';
import 'package:my_wit_wallet/shared/api_database.dart';
import 'package:my_wit_wallet/shared/locator.dart';
import 'package:my_wit_wallet/util/get_localization.dart';
import 'package:my_wit_wallet/util/storage/database/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/adapters/transaction_adapter.dart';
import 'package:my_wit_wallet/widgets/PaddedButton.dart';
import 'package:my_wit_wallet/widgets/closable_view.dart';
import 'package:my_wit_wallet/widgets/witnet/transactions/value_transfer/create_dialog_box/vtt_builder/02_select_miner_fee.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/speedup_btn.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:my_wit_wallet/shared/api_database.dart';
import 'package:my_wit_wallet/shared/locator.dart';
import 'package:flutter/material.dart';
import 'package:my_wit_wallet/util/get_localization.dart';
import 'package:my_wit_wallet/util/storage/database/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/adapters/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/wallet.dart';
import 'package:my_wit_wallet/widgets/PaddedButton.dart';

Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/transaction_details.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'package:my_wit_wallet/util/storage/database/account.dart';
import 'package:my_wit_wallet/util/storage/database/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/adapters/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/wallet.dart';
import 'package:my_wit_wallet/util/transactions_list/get_transaction_label.dart';
import 'package:my_wit_wallet/widgets/closable_view.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/transaction_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:my_wit_wallet/util/extensions/string_extensions.dart';
import 'package:my_wit_wallet/util/extensions/num_extensions.dart';
import 'package:flutter/material.dart';
import 'package:my_wit_wallet/util/storage/database/account.dart';
import 'package:my_wit_wallet/util/storage/database/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/adapters/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/wallet.dart';
import 'package:my_wit_wallet/util/transactions_list/get_transaction_address.dart';
import 'package:my_wit_wallet/util/transactions_list/get_transaction_label.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/transactions_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:my_wit_wallet/screens/dashboard/view/dashboard_screen.dart';
import 'package:my_wit_wallet/shared/api_database.dart';
import 'package:my_wit_wallet/shared/locator.dart';
import 'package:my_wit_wallet/util/get_localization.dart';
import 'package:my_wit_wallet/util/storage/database/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/adapters/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/wallet.dart';
import 'package:my_wit_wallet/widgets/speed_up_tx.dart';
import 'package:my_wit_wallet/widgets/transaction_details.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:my_wit_wallet/util/allow_biometrics.dart';
import 'package:my_wit_wallet/util/storage/database/transaction_adapter.dart';
import 'package:my_wit_wallet/util/storage/database/adapters/transaction_adapter.dart';
import 'package:my_wit_wallet/widgets/witnet/transactions/value_transfer/modals/general_error_tx_modal.dart';
import 'package:my_wit_wallet/widgets/witnet/transactions/value_transfer/modals/sending_tx_modal.dart';
import 'package:my_wit_wallet/widgets/witnet/transactions/value_transfer/modals/signing_tx_modal.dart';
Expand Down
Loading

0 comments on commit 3161036

Please sign in to comment.