From c58d859d07177c61b6538b6fcad0f05dfdecba5e Mon Sep 17 00:00:00 2001
From: poka <poka@p2p.legal>
Date: Tue, 28 Jun 2022 12:23:14 +0200
Subject: [PATCH] fix: Use _removableOn instead of _renewableOn for
 certification renewale delay (#13)

---
 config/gdev_endpoints.json       |  1 -
 lib/providers/substrate_sdk.dart | 30 +++++++++---------------------
 pubspec.yaml                     |  2 +-
 3 files changed, 10 insertions(+), 23 deletions(-)

diff --git a/config/gdev_endpoints.json b/config/gdev_endpoints.json
index 90a332fa..a64247a6 100644
--- a/config/gdev_endpoints.json
+++ b/config/gdev_endpoints.json
@@ -2,5 +2,4 @@
       "wss://gdev.librelois.fr/ws",
       "wss://gdev.1000i100.fr/ws",
       "wss://gdev.komun.org/ws"
-
 ]
diff --git a/lib/providers/substrate_sdk.dart b/lib/providers/substrate_sdk.dart
index d7a2a7eb..dcd6d55c 100644
--- a/lib/providers/substrate_sdk.dart
+++ b/lib/providers/substrate_sdk.dart
@@ -524,7 +524,7 @@ class SubstrateSdk with ChangeNotifier {
 
     if (idtyStatus != null) {
       final String _status = idtyStatus['status'];
-      log.d('Status $address: $_status');
+      // log.d('Status $address: $_status');
       return (_status);
     } else {
       return 'expired';
@@ -605,40 +605,28 @@ class SubstrateSdk with ChangeNotifier {
   }
 
   Future<Map<String, int>> certState(String from, String to) async {
-    // String from = await getMemberAddress();
+    Map<String, int> _result = {};
     if (from != to && await isMember(from)) {
-      Map<String, int> _result = {};
       final _certData = await getCertData(from, to);
       final _certMeta = await getCertMeta(from);
       final int _removableOn = _certData['removableOn'] ?? 0;
-      final int _renewableOn = _certData['renewableOn'] ?? 0;
       final int _nextIssuableOn = _certMeta['nextIssuableOn'] ?? 0;
-      //TODO: use _removableOn instead of _renewableOn
-      log.d(_renewableOn.toString() +
-          '\n' +
-          _removableOn.toString() +
-          '\n' +
-          _nextIssuableOn.toString());
-      if (_renewableOn != 0) {
-        final certRenewDuration = (_renewableOn - blocNumber) * 6;
+      final certRemovableDuration = (_removableOn - blocNumber) * 6;
+      const int renewDelay = 2 * 30 * 24 * 3600; // 2 months
+
+      if (certRemovableDuration >= renewDelay) {
+        final certRenewDuration = certRemovableDuration - renewDelay;
         _result.putIfAbsent('certRenewable', () => certRenewDuration);
-        return _result;
       } else if (_nextIssuableOn > blocNumber) {
         final certDelayDuration = (_nextIssuableOn - blocNumber) * 6;
         _result.putIfAbsent('certDelay', () => certDelayDuration);
-        return _result;
       } else {
         _result.putIfAbsent('canCert', () => 0);
-        return _result;
       }
     }
-    return {};
+    return _result;
   }
 
-  // Future<String> certState(String from, String to) async {
-  //   return '';
-  // }
-
   Future<Map> getCertMeta(String address) async {
     var idtyIndex = await sdk.webView!
         .evalJavascript('api.query.identity.identityIndexOf("$address")');
@@ -648,7 +636,7 @@ class SubstrateSdk with ChangeNotifier {
         '';
     // if (_certMeta['nextIssuableOn'] != 0) return {};
 
-    log.d(_certMeta);
+    // log.d(_certMeta);
     return _certMeta;
   }
 
diff --git a/pubspec.yaml b/pubspec.yaml
index 05b92cd8..58f68621 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -5,7 +5,7 @@ description: Pay with G1.
 # pub.dev using `pub publish`. This is preferred for private packages.
 publish_to: 'none' # Remove this line if you wish to publish to pub.dev
 
-version: 0.0.9+12
+version: 0.0.9+13
 
 environment:
   sdk: '>=2.12.0 <3.0.0'
-- 
GitLab