gecko/lib/providers/duniter_indexer.dart

337 lines
10 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'dart:convert';
import 'dart:io';
2022-06-18 01:50:06 +02:00
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:gecko/globals.dart';
import 'package:gecko/models/queries_indexer.dart';
2022-06-15 01:14:23 +02:00
import 'package:gecko/providers/substrate_sdk.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
class DuniterIndexer with ChangeNotifier {
Map<String, String?> walletNameIndexer = {};
String? fetchMoreCursor;
Map? pageInfo;
List? transBC;
2022-07-16 21:03:40 +02:00
List listIndexerEndpoints = [];
bool isLoadingIndexer = false;
2022-12-06 03:35:35 +01:00
Map<String, String> idtyStatusCache = {};
void reload() {
notifyListeners();
}
2022-07-16 21:03:40 +02:00
Future<bool> checkIndexerEndpoint(String endpoint) async {
isLoadingIndexer = true;
notifyListeners();
final client = HttpClient();
client.connectionTimeout = const Duration(milliseconds: 4000);
2022-07-16 21:03:40 +02:00
try {
final request = await client.postUrl(Uri.parse('$endpoint/v1/graphql'));
2022-07-16 21:03:40 +02:00
final response = await request.close();
if (response.statusCode != 200) {
log.d('INDEXER IS OFFLINE');
indexerEndpoint = '';
2022-07-16 21:03:40 +02:00
isLoadingIndexer = false;
notifyListeners();
return false;
} else {
indexerEndpoint = endpoint;
await configBox.put('indexerEndpoint', endpoint);
// await configBox.put('customEndpoint', endpoint);
isLoadingIndexer = false;
notifyListeners();
final cache = HiveStore();
cache.reset();
return true;
}
2022-07-16 21:03:40 +02:00
} catch (e) {
log.d('INDEXER IS OFFLINE');
2022-07-16 21:03:40 +02:00
indexerEndpoint = '';
isLoadingIndexer = false;
notifyListeners();
return false;
}
}
2022-07-16 21:03:40 +02:00
// Future checkIndexerEndpointBackground() async {
// final oldEndpoint = indexerEndpoint;
// while (true) {
// await Future.delayed(const Duration(seconds: 30));
// final isValid = await checkIndexerEndpoint(oldEndpoint);
// if (!isValid) {
// log.d('INDEXER IS OFFILINE');
// indexerEndpoint = '';
// } else {
// // log.d('Indexer is online');
// indexerEndpoint = oldEndpoint;
// }
// }
// }
Future<String> getValidIndexerEndpoint() async {
2022-07-16 21:03:40 +02:00
// await configBox.delete('indexerEndpoint');
listIndexerEndpoints = await rootBundle
.loadString('config/indexer_endpoints.json')
.then((jsonStr) => jsonDecode(jsonStr));
// _listEndpoints.shuffle();
2022-07-16 21:03:40 +02:00
log.d(listIndexerEndpoints);
2022-07-16 21:22:12 +02:00
listIndexerEndpoints.add('Personnalisé');
2022-07-16 21:03:40 +02:00
if (configBox.containsKey('customIndexer')) {
2022-07-16 21:22:12 +02:00
return configBox.get('customIndexer');
2022-07-16 21:03:40 +02:00
}
if (configBox.containsKey('indexerEndpoint')) {
if (await checkIndexerEndpoint(configBox.get('indexerEndpoint'))) {
return configBox.get('indexerEndpoint');
}
}
int i = 0;
// String _endpoint = '';
int statusCode = 0;
final client = HttpClient();
client.connectionTimeout = const Duration(milliseconds: 3000);
do {
2022-08-15 17:31:17 +02:00
int listLenght = listIndexerEndpoints.length - 1;
if (i >= listLenght) {
log.e('NO VALID INDEXER ENDPOINT FOUND');
indexerEndpoint = '';
break;
}
log.d('${i + 1}n indexer endpoint try: ${listIndexerEndpoints[i]}');
if (i != 0) {
await Future.delayed(const Duration(milliseconds: 300));
}
try {
2022-07-16 21:03:40 +02:00
String endpointPath = '${listIndexerEndpoints[i]}/v1/graphql';
final request = await client.postUrl(Uri.parse(endpointPath));
final response = await request.close();
2022-07-16 21:03:40 +02:00
indexerEndpoint = listIndexerEndpoints[i];
await configBox.put('indexerEndpoint', listIndexerEndpoints[i]);
statusCode = response.statusCode;
i++;
} on TimeoutException catch (_) {
log.e('This endpoint is timeout, next');
statusCode = 50;
i++;
continue;
} on SocketException catch (_) {
log.e('This endpoint is a bad endpoint, next');
statusCode = 70;
i++;
continue;
} on Exception {
log.e('Unknown error');
statusCode = 60;
i++;
continue;
}
} while (statusCode != 200);
log.i('INDEXER: $indexerEndpoint');
return indexerEndpoint;
}
List parseHistory(blockchainTX, pubkey) {
2022-12-09 05:19:34 +01:00
List transBC = [];
int i = 0;
for (final trans in blockchainTX) {
final transaction = trans['node'];
final direction =
transaction['issuer_pubkey'] != pubkey ? 'RECEIVED' : 'SENT';
transBC.add(i);
transBC[i] = [];
transBC[i].add(DateTime.parse(transaction['created_at']));
final amountBrut = transaction['amount'];
final amount = removeDecimalZero(amountBrut / 100);
if (direction == "RECEIVED") {
transBC[i].add(transaction['issuer_pubkey']);
transBC[i].add(transaction['issuer']['identity']?['name'] ?? '');
} else if (direction == "SENT") {
transBC[i].add(transaction['receiver_pubkey']);
transBC[i].add(transaction['receiver']['identity']?['name'] ?? '');
}
2022-09-11 13:14:52 +02:00
transBC[i].add(amount);
transBC[i].add(direction);
i++;
}
return transBC;
}
2022-12-09 06:23:47 +01:00
FetchMoreOptions? mergeQueryResult(result, opts, pubkey, nRepositories) {
final List<dynamic>? blockchainTX =
(result.data['transaction_connection']['edges'] as List<dynamic>?);
pageInfo = result.data['transaction_connection']['pageInfo'];
fetchMoreCursor = pageInfo!['endCursor'];
2022-12-08 11:38:45 +01:00
final hasNextPage = pageInfo!['hasNextPage'];
final hasPreviousPage = pageInfo!['hasPreviousPage'];
log.d('endCursor: $fetchMoreCursor $hasNextPage $hasPreviousPage');
if (fetchMoreCursor != null) {
opts = FetchMoreOptions(
variables: {'cursor': fetchMoreCursor, 'number': nRepositories},
updateQuery: (previousResultData, fetchMoreResultData) {
final List<dynamic> repos = [
...previousResultData!['transaction_connection']['edges']
as List<dynamic>,
...fetchMoreResultData!['transaction_connection']['edges']
as List<dynamic>
];
fetchMoreResultData['transaction_connection']['edges'] = repos;
return fetchMoreResultData;
},
);
}
if (fetchMoreCursor != null) {
transBC = parseHistory(blockchainTX, pubkey);
} else {
log.i("###### DEBUG H - Début de l'historique");
}
return opts;
}
2022-09-11 13:14:52 +02:00
double removeDecimalZero(double n) {
String result = n.toStringAsFixed(n.truncateToDouble() == n ? 0 : 2);
2022-09-11 13:14:52 +02:00
return double.parse(result);
}
}
//// Manuals queries
Future<bool> isIdtyExist(String name) async {
final variables = <String, dynamic>{
'name': name,
};
final result = await _execQuery(isIdtyExistQ, variables);
return result.data!['identity']?.isEmpty ?? false ? false : true;
}
Future<DateTime> getBlockStart() async {
final result = await _execQuery(getBlockchainStartQ, {});
2023-02-03 11:47:55 +01:00
if (!result.hasException) {
startBlockchainTime =
DateTime.parse(result.data!['block'][0]['created_at']);
startBlockchainInitialized = true;
return startBlockchainTime;
}
2023-02-03 11:47:55 +01:00
return DateTime(0, 0, 0, 0, 0);
}
Future<QueryResult> _execQuery(
String query, Map<String, dynamic> variables) async {
final httpLink = HttpLink(
'$indexerEndpoint/v1/graphql',
);
final GraphQLClient client = GraphQLClient(
cache: GraphQLCache(),
link: httpLink,
);
final QueryOptions options =
QueryOptions(document: gql(query), variables: variables);
return await client.query(options);
}
2022-12-09 06:23:47 +01:00
2023-02-10 22:54:36 +01:00
Map computeHistoryView(repository) {
2022-12-09 06:23:47 +01:00
bool isTody = false;
bool isYesterday = false;
bool isThisWeek = false;
bool isMigrationTime = false;
String? dateDelimiter;
DateTime now = DateTime.now();
final bool isUdUnit = configBox.get('isUdUnit') ?? false;
late double amount;
late String finalAmount;
DateTime date = repository[0];
String dateForm;
bool isDelimiter = true;
if ({4, 10, 11, 12}.contains(date.month)) {
2022-12-10 06:09:05 +01:00
dateForm = "${date.day} ${monthsInYear[date.month]!.substring(0, 3)}";
2022-12-09 06:23:47 +01:00
} else if ({1, 2, 7, 9}.contains(date.month)) {
2022-12-10 06:09:05 +01:00
dateForm = "${date.day} ${monthsInYear[date.month]!.substring(0, 4)}";
2022-12-09 06:23:47 +01:00
} else {
dateForm = "${date.day} ${monthsInYear[date.month]}";
}
final transactionDate = DateTime(date.year, date.month, date.day);
final todayDate = DateTime(now.year, now.month, now.day);
final yesterdayDate = DateTime(now.year, now.month, now.day - 1);
if (transactionDate == todayDate && !isTody) {
2023-02-10 22:54:36 +01:00
dateDelimiter = "today".tr();
2022-12-09 06:23:47 +01:00
isTody = true;
} else if (transactionDate == yesterdayDate && !isYesterday) {
2023-02-10 22:54:36 +01:00
dateDelimiter = "yesterday".tr();
2022-12-09 06:23:47 +01:00
isYesterday = true;
} else if (weekNumber(date) == weekNumber(now) &&
date.year == now.year &&
transactionDate != yesterdayDate &&
transactionDate != todayDate &&
!isThisWeek) {
2023-02-10 22:54:36 +01:00
dateDelimiter = "thisWeek".tr();
2022-12-09 06:23:47 +01:00
isThisWeek = true;
2023-02-10 22:54:36 +01:00
} else if (dateDelimiter != "${monthsInYear[date.month]} ${date.year}" &&
2022-12-09 06:23:47 +01:00
transactionDate != todayDate &&
transactionDate != yesterdayDate &&
!(weekNumber(date) == weekNumber(now) && date.year == now.year)) {
if (date.year == now.year) {
2023-02-10 22:54:36 +01:00
dateDelimiter = monthsInYear[date.month];
2022-12-09 06:23:47 +01:00
} else {
2023-02-10 22:54:36 +01:00
dateDelimiter = "${monthsInYear[date.month]} ${date.year}";
2022-12-09 06:23:47 +01:00
}
} else {
isDelimiter = false;
}
amount = repository[4] == 'RECEIVED' ? repository[3] : repository[3] * -1;
if (isUdUnit) {
amount = round(amount / balanceRatio);
finalAmount = 'ud'.tr(args: ['$amount ']);
} else {
finalAmount = '$amount $currencyName';
}
if (startBlockchainInitialized && date.compareTo(startBlockchainTime) < 0) {
2022-12-09 06:23:47 +01:00
isMigrationTime = true;
} else {
isMigrationTime = false;
}
return {
'finalAmount': finalAmount,
'isMigrationTime': isMigrationTime,
'dateDelimiter': dateDelimiter ?? '',
'isDelimiter': isDelimiter,
'dateForm': dateForm,
};
}
int weekNumber(DateTime date) {
int dayOfYear = int.parse(DateFormat("D").format(date));
return ((dayOfYear - date.weekday + 10) / 7).floor();
}