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

feat: update pay verification interval on opened request #1410

Merged
merged 5 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
@@ -1,9 +1,12 @@
import 'dart:async';

import 'package:flutter/material.dart';

import '../../../di.dart';
import '../../../ui/loader.dart';
import '../data/repository.dart';
import '../models/payment_request.dart';
import '../services/payment_request_service.dart';
import '../widgets/request_success.dart';
import '../widgets/share_request.dart';

Expand Down Expand Up @@ -33,6 +36,21 @@ class _PaymentRequestScreenState extends State<PaymentRequestScreen> {
void initState() {
super.initState();
_stream = sl<PaymentRequestRepository>().watchById(widget.id);

_watcher();
}

Future<void> _watcher() async {
final request = await _stream.first;

sl<PaymentRequestService>().initWatcher(request);
}

@override
void dispose() {
sl<PaymentRequestService>().disposeWatcher();

super.dispose();
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:dfunc/dfunc.dart';
import 'package:espressocash_api/espressocash_api.dart';

import 'package:flutter/foundation.dart';
import 'package:get_it/get_it.dart';
import 'package:injectable/injectable.dart';
import 'package:rxdart/rxdart.dart';
import 'package:solana/solana.dart';
Expand All @@ -20,7 +21,7 @@ import '../data/repository.dart';
import '../models/payment_request.dart';

@Singleton(scope: authScope)
class PaymentRequestService {
class PaymentRequestService implements Disposable {
PaymentRequestService(
this._repository,
this._solanaClient,
Expand All @@ -38,6 +39,8 @@ class PaymentRequestService {
final Map<String, StreamSubscription<void>> _subscriptions = {};
final Map<String, Duration> _currentBackoffs = {};

StreamSubscription<void>? _watcher;

@PostConstruct(preResolve: true)
Future<void> init() async {
final pendingPayments = await _repository.getAllPending();
Expand All @@ -48,14 +51,32 @@ class PaymentRequestService {
}

void _subscribe(PaymentRequest request) {
_waitForTx(request);
if (!request.state.isInitial) return;

_subscriptions[request.id]?.cancel();
_subscriptions[request.id] = _createSubscription(request);
}

void _waitForTx(PaymentRequest request) {
void initWatcher(PaymentRequest request) {
if (!request.state.isInitial) return;

_watcher?.cancel();
_watcher = _createSubscription(request, interval: _focusedInterval);
}

void disposeWatcher() {
_watcher?.cancel();
}

StreamSubscription<void> _createSubscription(
PaymentRequest request, {
Duration interval = _backgroundInterval,
}) {
final reference = request.payRequest.reference?.firstOrNull;
if (reference == null) return;

if (reference == null) {
return const Stream<void>.empty().listen(null);
}

Stream<TransactionId> solanaPayTransaction() => _solanaClient
.findSolanaPayTransaction(
Expand All @@ -65,10 +86,9 @@ class PaymentRequestService {
.asStream()
.whereType<TransactionId>();

_subscriptions[request.id] =
Stream<void>.periodic(const Duration(seconds: 30))
.flatMap((a) => solanaPayTransaction())
.mergeWith([solanaPayTransaction()]).listen(
return Stream<void>.periodic(interval)
.flatMap((a) => solanaPayTransaction())
.mergeWith([solanaPayTransaction()]).listen(
(id) {
_verifyTx(id, request);
},
Expand All @@ -80,8 +100,8 @@ class PaymentRequestService {
_currentBackoffs[request.id] = _maxBackoff;
}
await Future<void>.delayed(_currentBackoffs[request.id]!);
// ignore: avoid-recursive-calls, called in async callback
_waitForTx(request);

_subscribe(request);
},
);
}
Expand Down Expand Up @@ -113,6 +133,7 @@ class PaymentRequestService {
_refreshBalance();

await _subscriptions[request.id]?.cancel();
await _watcher?.cancel();
} on Exception {
_currentBackoffs[request.id] =
(_currentBackoffs[request.id] ?? _minBackoff) * _backoffStep;
Expand Down Expand Up @@ -169,9 +190,21 @@ class PaymentRequestService {
return paymentRequest;
}

Future<void> cancel(String id) async {
await _repository.delete(id);

await _subscriptions[id]?.cancel();
}

Future<Uri> unshortenLink(String shortLink) => _ecClient
.unshortenLink(UnshortenLinkRequestDto(shortLink: shortLink))
.then((e) => Uri.parse(e.fullLink));

@override
Future<void> onDispose() async {
await _watcher?.cancel();
await Future.wait(_subscriptions.values.map((it) => it.cancel()));
}
}

Future<Ed25519HDPublicKey> _randomPublicKey([dynamic _]) async {
Expand All @@ -184,6 +217,9 @@ const _backoffStep = 2;
const _minBackoff = Duration(seconds: 2);
const _maxBackoff = Duration(minutes: 1);

const _backgroundInterval = Duration(seconds: 30);
const _focusedInterval = Duration(seconds: 1);

extension on PaymentRequestState {
bool get isInitial => this == PaymentRequestState.initial;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import '../../../ui/text_button.dart';
import '../../../ui/theme.dart';
import '../../conversion_rates/widgets/extensions.dart';
import '../../tokens/token_list.dart';
import '../data/repository.dart';
import '../models/payment_request.dart';
import '../services/payment_request_service.dart';

class ShareRequestPayment extends StatelessWidget {
const ShareRequestPayment({
Expand Down Expand Up @@ -94,7 +94,7 @@ class ShareRequestPayment extends StatelessWidget {
message: context
.l10n.paymentRequest_lblCancelConfirmationSubtitle,
onConfirm: () {
sl<PaymentRequestRepository>().delete(request.id);
sl<PaymentRequestService>().cancel(request.id);
Navigator.of(context).pop();
},
),
Expand Down
Loading