From ce9f61b0ed46be74d81063694d3980389ef64274 Mon Sep 17 00:00:00 2001 From: OleksandrSobol Date: Thu, 25 Mar 2021 19:45:21 +0200 Subject: [PATCH] CAKE-291 | fixed bug in the get_height_by_date.dart --- lib/monero/get_height_by_date.dart | 39 ++++++++++++++++++------------ 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/lib/monero/get_height_by_date.dart b/lib/monero/get_height_by_date.dart index 64ab7e05..11921a2a 100644 --- a/lib/monero/get_height_by_date.dart +++ b/lib/monero/get_height_by_date.dart @@ -85,28 +85,35 @@ final dates = { "2020-11": 2220000 }; -final heightCoefficient = 0.7; - int getHeigthByDate({DateTime date}) { final raw = '${date.year}' + '-' + '${date.month}'; final lastHeight = dates.values.last; int startHeight; int endHeight; - int height; + int height = 0; - if ((dates[raw] == null)||(dates[raw] == lastHeight)) { - startHeight = dates.values.toList()[dates.length - 2]; - endHeight = dates.values.toList()[dates.length - 1]; - final heightPerDay = (endHeight - startHeight) / 31; - 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).round(); - final daysHeight = (date.day - 1) * heightPerDay; - height = startHeight + daysHeight - heightPerDay; + try { + if ((dates[raw] == null)||(dates[raw] == lastHeight)) { + startHeight = dates.values.toList()[dates.length - 2]; + endHeight = dates.values.toList()[dates.length - 1]; + final heightPerDay = (endHeight - startHeight) / 31; + final endDateRaw = dates.keys.toList()[dates.length - 1].split('-'); + final endYear = int.parse(endDateRaw[0]); + final endMonth = int.parse(endDateRaw[1]); + final endDate = DateTime(endYear, endMonth); + final differenceInDays = date.difference(endDate).inDays; + final daysHeight = (differenceInDays * 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).round(); + final daysHeight = (date.day - 1) * heightPerDay; + height = startHeight + daysHeight - heightPerDay; + } + } catch (e) { + print(e.toString()); } return height;