From 70467b1fe7ebca93b7ab1a5c381ef31af1ae91f7 Mon Sep 17 00:00:00 2001
From: OleksandrSobol <dr.alexander.sobol@gmail.com>
Date: Thu, 12 Nov 2020 17:46:05 +0200
Subject: [PATCH 1/3] CAKE-168 | fixed blockheight calculation by date; changed
 getHeightByDate method; changed first date in the date picker to 1 of May,
 2014

---
 lib/monero/get_height_by_date.dart            | 39 ++++++++-----------
 lib/src/widgets/blockchain_height_widget.dart |  2 +-
 2 files changed, 18 insertions(+), 23 deletions(-)

diff --git a/lib/monero/get_height_by_date.dart b/lib/monero/get_height_by_date.dart
index 3f5f87623..5393d2a32 100644
--- a/lib/monero/get_height_by_date.dart
+++ b/lib/monero/get_height_by_date.dart
@@ -85,34 +85,29 @@ final dates = {
   "2020-11": 2221803
 };
 
+final heightCoefficient = 0.7;
+
 int getHeigthByDate({DateTime date}) {
   final raw = '${date.year}' + '-' + '${date.month}';
-  var endHeight = dates[raw] ?? 0;
-  int preLastYear = date.year;
-  int preLastMonth = date.month - 1;
+  final lastHeight = dates.values.last;
+  int startHeight;
+  int endHeight;
+  int height;
 
-  if (endHeight <= 0) {
+  if ((dates[raw] == null)||(dates[raw] == lastHeight)) {
+    startHeight = dates.values.toList()[dates.length - 2];
     endHeight = dates.values.toList()[dates.length - 1];
-    final preLastDate =
-        dateFormat.parse(dates.keys.elementAt(dates.keys.length - 2));
-    preLastYear = preLastDate.year;
-    preLastMonth = preLastDate.month;
+    final heightPerDay = (endHeight - startHeight) / 31;
+    final daysHeight = (heightCoefficient * date.day * heightPerDay).round();
+    height = endHeight + daysHeight;
   } else {
-    preLastYear = date.year;
-    preLastMonth = date.month - 1;
+    startHeight = dates[raw];
+    final index = dates.values.toList().indexOf(startHeight);
+    endHeight = dates.values.toList()[index + 1];
+    final heightPerDay = (endHeight - startHeight) / 31;
+    final daysHeight = date.day * heightPerDay.round();
+    height = startHeight + daysHeight;
   }
 
-  if (preLastMonth <= 0) {
-    preLastMonth = 12;
-    preLastYear -= 1;
-  }
-
-  final startRaw = '$preLastYear' + '-' + '$preLastMonth';
-  final startHeight = dates[startRaw];
-  final diff = endHeight - startHeight;
-  final heightPerDay = diff / 30;
-  final daysHeight = date.day * heightPerDay.round();
-  final height = endHeight + daysHeight;
-
   return height;
 }
diff --git a/lib/src/widgets/blockchain_height_widget.dart b/lib/src/widgets/blockchain_height_widget.dart
index 384700839..f8b22b5c4 100644
--- a/lib/src/widgets/blockchain_height_widget.dart
+++ b/lib/src/widgets/blockchain_height_widget.dart
@@ -92,7 +92,7 @@ class BlockchainHeightState extends State<BlockchainHeightWidget> {
     final date = await getDate(
         context: context,
         initialDate: now.subtract(Duration(days: 1)),
-        firstDate: DateTime(2014, DateTime.april),
+        firstDate: DateTime(2014, DateTime.may),
         lastDate: now);
 
     if (date != null) {

From d682f6504b14438c8f20bbc20f24cd766c33e796 Mon Sep 17 00:00:00 2001
From: OleksandrSobol <dr.alexander.sobol@gmail.com>
Date: Thu, 12 Nov 2020 21:12:58 +0200
Subject: [PATCH 2/3] CAKE-168 | fixed getHeightByDate method

---
 lib/monero/get_height_by_date.dart | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/monero/get_height_by_date.dart b/lib/monero/get_height_by_date.dart
index 5393d2a32..ed3d19cac 100644
--- a/lib/monero/get_height_by_date.dart
+++ b/lib/monero/get_height_by_date.dart
@@ -98,14 +98,14 @@ int getHeigthByDate({DateTime date}) {
     startHeight = dates.values.toList()[dates.length - 2];
     endHeight = dates.values.toList()[dates.length - 1];
     final heightPerDay = (endHeight - startHeight) / 31;
-    final daysHeight = (heightCoefficient * date.day * heightPerDay).round();
+    final daysHeight = (heightCoefficient * (date.day - 1) * heightPerDay).round();
     height = endHeight + daysHeight;
   } else {
     startHeight = dates[raw];
     final index = dates.values.toList().indexOf(startHeight);
     endHeight = dates.values.toList()[index + 1];
     final heightPerDay = (endHeight - startHeight) / 31;
-    final daysHeight = date.day * heightPerDay.round();
+    final daysHeight = (date.day - 1) * heightPerDay.round();
     height = startHeight + daysHeight;
   }
 

From 29247967e7102d9f795ad242bceda77f7f07ee35 Mon Sep 17 00:00:00 2001
From: M <m@cakewallet.com>
Date: Thu, 12 Nov 2020 22:20:20 +0200
Subject: [PATCH 3/3] Fixes

---
 lib/monero/get_height_by_date.dart | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/monero/get_height_by_date.dart b/lib/monero/get_height_by_date.dart
index ed3d19cac..64ab7e05b 100644
--- a/lib/monero/get_height_by_date.dart
+++ b/lib/monero/get_height_by_date.dart
@@ -82,7 +82,7 @@ final dates = {
   "2020-8": 2153983,
   "2020-9": 2176466,
   "2020-10": 2198453,
-  "2020-11": 2221803
+  "2020-11": 2220000
 };
 
 final heightCoefficient = 0.7;
@@ -104,9 +104,9 @@ int getHeigthByDate({DateTime date}) {
     startHeight = dates[raw];
     final index = dates.values.toList().indexOf(startHeight);
     endHeight = dates.values.toList()[index + 1];
-    final heightPerDay = (endHeight - startHeight) / 31;
-    final daysHeight = (date.day - 1) * heightPerDay.round();
-    height = startHeight + daysHeight;
+    final heightPerDay = ((endHeight - startHeight) / 31).round();
+    final daysHeight = (date.day - 1) * heightPerDay;
+    height = startHeight + daysHeight - heightPerDay;
   }
 
   return height;