Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persistence - Wallet Addresses Storage #41

Draft
wants to merge 24 commits into
base: add_arrr
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c89d825
create WalletAddress
naezith Jun 27, 2023
b7d13c1
create AccountAddressesApiInterface
naezith Jun 27, 2023
4ee8952
create AccountAddressesApiHive
naezith Jun 27, 2023
0b25c30
validation and cleanup for AccountAddressesApiHive
naezith Jun 27, 2023
3cf3d9e
error handling for AccountAddressesApiHive
naezith Jun 27, 2023
01f5cab
field params instead of WalletAddress obj
naezith Jun 27, 2023
90b1541
updateOrCreate for AccountAddressesApiHive
naezith Jun 27, 2023
14748a4
factory for AccountAddressesApiHive
naezith Jun 27, 2023
cb92f78
HiveException for AccountAddressesApiHive
naezith Jun 27, 2023
562b899
FutureOr for AccountAddressesApiHive init
naezith Jun 27, 2023
dc8fa90
store original exception in HiveException
naezith Jun 27, 2023
448162a
remove comment
naezith Jun 27, 2023
16bb0ca
call Hive.registerAdapter only once
naezith Jun 28, 2023
8d7d6ee
add trailing commas
naezith Jun 28, 2023
b0b6fa7
added @required and named params
naezith Jun 28, 2023
f5809ab
implement AccountAddressesRepository
naezith Jun 28, 2023
1b3066b
atomic AccountAddressesRepository
naezith Jun 29, 2023
010fcc5
cancel AddressRepository subscription earlier
naezith Jun 29, 2023
3ea1e55
remove custom HiveException
naezith Jul 3, 2023
fac2231
Add a real-time stream for active wallet changes
CharlVS Jul 3, 2023
e1e8ba2
Add stream for wallet snapshot changes
CharlVS Jul 3, 2023
7197b76
Add wallet address serialisation metods
CharlVS Jul 4, 2023
8631c9b
Save wallet changes in DB class methods
CharlVS Jul 4, 2023
9d3548e
Add realtime address list helper method for bloc
CharlVS Jul 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import 'dart:async';
import 'dart:convert';
import 'package:komodo_dex/model/coin_balance.dart';
import 'package:komodo_dex/packages/account_addresses/api/account_addresses_api_interface.dart';
import 'package:komodo_dex/packages/account_addresses/models/wallet_address.dart';
import 'package:komodo_dex/services/db/database.dart';
import 'package:meta/meta.dart';

class AccountAddressesRepository {
final AccountAddressesApiInterface _accountAddressesApi;
String _currentWalletId;
StreamController<WalletAddress> _controller;
StreamSubscription<WalletAddress> _watchSubscription;
bool _isDisposed = false;

AccountAddressesRepository({
@required AccountAddressesApiInterface accountAddressesApi,
}) : _accountAddressesApi = accountAddressesApi {
_controller = StreamController<WalletAddress>();
_startPolling();
naezith marked this conversation as resolved.
Show resolved Hide resolved
}

Future<void> _startPolling() async {
while (!_isDisposed) {
try {
final currentWallet = await Db.getCurrentWallet();
if (currentWallet != null && currentWallet.id != _currentWalletId) {
_currentWalletId = currentWallet.id;

_watchSubscription?.cancel();
_watchSubscription = _accountAddressesApi
.watchAll(walletId: _currentWalletId)
.listen((WalletAddress walletAddress) async {
_controller.add(walletAddress);
});
naezith marked this conversation as resolved.
Show resolved Hide resolved

final String jsonStr = await Db.getWalletSnapshot();
if (jsonStr != null) {
List<dynamic> items;
try {
items = json.decode(jsonStr);
} catch (e) {
throw Exception('Failed to decode getWalletSnapshot JSON: $e');
}

if (items != null) {
for (final item in items) {
final coinBalance = CoinBalance.fromJson(item);

final walletAddress = WalletAddress(
walletId: _currentWalletId,
address: coinBalance.balance.address,
ticker: coinBalance.coin.abbr,
availableBalance: coinBalance.balance.balance.toDouble(),
accountId: 'iguana',
);

await _accountAddressesApi.updateOrCreate(
walletId: walletAddress.walletId,
address: walletAddress.address,
ticker: walletAddress.ticker,
availableBalance: walletAddress.availableBalance,
accountId: walletAddress.accountId,
);
}
}
}
}
} catch (e) {
_controller.addError(e);
}

await Future.delayed(Duration(seconds: 5));
}
}

Stream<WalletAddress> watchCurrentWalletAddresses() async* {
naezith marked this conversation as resolved.
Show resolved Hide resolved
yield* _controller.stream;
}

void dispose() {
_isDisposed = true;
_watchSubscription?.cancel();
_controller.close();
}
}