From a88034b206f36de5b313ccccaa2e4b611636e7da Mon Sep 17 00:00:00 2001
From: poka <poka@p2p.legal>
Date: Mon, 8 Aug 2022 17:03:34 +0200
Subject: [PATCH] Use certsByReceiver to gets certValidityPeriod instead of
 obsolete certsByReceiver call

---
 lib/providers/substrate_sdk.dart | 24 +++++++++++++-----------
 lib/screens/wallet_view.dart     |  3 +++
 2 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/lib/providers/substrate_sdk.dart b/lib/providers/substrate_sdk.dart
index 1a8236cc..d56efd72 100644
--- a/lib/providers/substrate_sdk.dart
+++ b/lib/providers/substrate_sdk.dart
@@ -257,21 +257,24 @@ class SubstrateSdk with ChangeNotifier {
     return [certsReceiver['receivedCount'], certsReceiver['issuedCount']];
   }
 
-  Future<Map> getCertData(String from, String to) async {
+  Future<int> getCertValidityPeriod(String from, String to) async {
     final idtyIndexFrom = await getIdentityIndexOf(from);
-
     final idtyIndexTo = await getIdentityIndexOf(to);
 
-    if (idtyIndexFrom == 0 || idtyIndexTo == 0) return {};
+    if (idtyIndexFrom == 0 || idtyIndexTo == 0) return 0;
 
-    final certData = await sdk.webView!.evalJavascript(
-            'api.query.cert.storageCertsByIssuer($idtyIndexFrom, $idtyIndexTo)') ??
-        '';
+    final List certData = await sdk.webView!
+            .evalJavascript('api.query.cert.certsByReceiver($idtyIndexTo)') ??
+        [];
 
-    if (certData == '') return {};
+    if (certData.isEmpty) return 0;
+    for (List certInfo in certData) {
+      if (certInfo[0] == idtyIndexFrom) {
+        return certInfo[1];
+      }
+    }
 
-    // log.d(_certData);
-    return certData;
+    return 0;
   }
 
   Future<Map<String, dynamic>> getParameters() async {
@@ -666,9 +669,8 @@ class SubstrateSdk with ChangeNotifier {
   Future<Map<String, int>> certState(String from, String to) async {
     Map<String, int> result = {};
     if (from != to && await isMemberGet(from)) {
-      final certData = await getCertData(from, to);
+      final removableOn = await getCertValidityPeriod(from, to);
       final certMeta = await getCertMeta(from);
-      final int removableOn = certData['removableOn'] ?? 0;
       final int nextIssuableOn = certMeta['nextIssuableOn'] ?? 0;
       final certRemovableDuration = (removableOn - blocNumber) * 6;
       const int renewDelay = 2 * 30 * 24 * 3600; // 2 months
diff --git a/lib/screens/wallet_view.dart b/lib/screens/wallet_view.dart
index 84a0d748..e2cebd97 100644
--- a/lib/screens/wallet_view.dart
+++ b/lib/screens/wallet_view.dart
@@ -129,6 +129,9 @@ class WalletViewScreen extends StatelessWidget {
                   builder: (context, AsyncSnapshot<Map<String, int>> snapshot) {
                     if (snapshot.data == null) return const SizedBox();
                     String duration = '';
+                    log.d(snapshot.data!['certDelay']);
+                    log.d(snapshot.data!['certRenewable']);
+
                     if (snapshot.data!['certDelay'] != null ||
                         snapshot.data!['certRenewable'] != null) {
                       final Duration durationSeconds = Duration(
-- 
GitLab