Skip to content

Commit

Permalink
chore: 🐛 Fix Oracle subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
redDwarf03 committed Jul 22, 2024
1 parent 32b6ccb commit f872e4c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .dart_tool/extension_discovery/vs_code.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":2,"entries":[{"package":"archethic_dapp_framework_flutter","rootUri":"../","packageUri":"lib/"}]}
{"version":2,"entries":[{"package":"archethic_dapp_framework_flutter","rootUri":"../","packageUri":"lib/"},{"package":"flutter_gen","rootUri":"flutter_gen/","packageUri":"flutter_gen/"}]}
37 changes: 26 additions & 11 deletions lib/src/application/oracle/provider.dart
Original file line number Diff line number Diff line change
@@ -1,47 +1,62 @@
/// SPDX-License-Identifier: AGPL-3.0-or-later
import 'dart:async';

import 'package:archethic_dapp_framework_flutter/src/application/oracle/state.dart';
import 'package:archethic_dapp_framework_flutter/src/util/generic/get_it_instance.dart';
import 'package:archethic_lib_dart/archethic_lib_dart.dart';
import 'package:logging/logging.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'provider.g.dart';

@Riverpod(keepAlive: true)
class _ArchethicOracleUCONotifier extends Notifier<ArchethicOracleUCO> {
ArchethicOracle? archethicOracle;
ArchethicOracle? archethicOracleSubscription;

static final _logger = Logger('ArchethicOracleUCONotifier');

@override
ArchethicOracleUCO build() {
ref.onDispose(() {
if (archethicOracle != null) {
sl
.get<OracleService>()
.closeOracleUpdatesSubscription(archethicOracle!);
}
});
ref.onDispose(stopSubscription);

_getValue();

startSubscription();
return const ArchethicOracleUCO();
}

Future<void> init() async {
await _getValue();
Future<void> startSubscription() async {
if (archethicOracleSubscription != null) return;

_logger.info('Start listening to Oracle');
await _subscribe();
}

Future<void> stopSubscription() async {
_logger.info('Stop listening to Oracle');
if (archethicOracleSubscription == null) return;
sl
.get<OracleService>()
.closeOracleUpdatesSubscription(archethicOracleSubscription!);
archethicOracleSubscription = null;
}

Future<void> _getValue() async {
final oracleUcoPrice = await sl.get<OracleService>().getOracleData();
_fillInfo(oracleUcoPrice);
}

Future<void> _subscribe() async {
archethicOracle = await sl
archethicOracleSubscription = await sl
.get<OracleService>()
.subscribeToOracleUpdates((oracleUcoPrice) {
_fillInfo(oracleUcoPrice!);
});
}

void _fillInfo(OracleUcoPrice oracleUcoPrice) {
_logger.info('Oracle: ${oracleUcoPrice.timestamp}, ${oracleUcoPrice.uco}');
state = state.copyWith(
timestamp: oracleUcoPrice.timestamp ?? 0,
eur: oracleUcoPrice.uco!.eur ?? 0,
Expand Down
1 change: 1 addition & 0 deletions lib/src/application/oracle/state.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// SPDX-License-Identifier: AGPL-3.0-or-later
import 'package:freezed_annotation/freezed_annotation.dart';

part 'state.freezed.dart';
Expand Down
7 changes: 3 additions & 4 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ packages:
archethic_wallet_client:
dependency: "direct main"
description:
name: archethic_wallet_client
sha256: "7d9ef9f23b285d4d5fc95bec08b3e4c51e8e636e1755f863426c5706ada2c175"
url: "https://pub.dev"
source: hosted
path: "../archethic-wallet-client-dart"
relative: true
source: path
version: "2.0.4"
archive:
dependency: "direct main"
Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ dependencies:
# path: ../libdart-2

# A client dart library to interact with Archethic Wallet RPC API.
archethic_wallet_client: ^2.0.4
#archethic_wallet_client:
# path: ../archethic-wallet-client-dart
#archethic_wallet_client: ^2.0.4
archethic_wallet_client:
path: ../archethic-wallet-client-dart

# Compression
archive: ^3.4.10
Expand Down

0 comments on commit f872e4c

Please sign in to comment.