From 3ca9f2f89ad8f2702511b72458e75110d376398b Mon Sep 17 00:00:00 2001
From: Jason <jayvdb1@gmail.com>
Date: Wed, 18 Oct 2023 15:37:47 +0200
Subject: [PATCH] fix: check rgs updates every hour

---
 .../src/main/java/com/reactnativeldk/Helpers.kt        |  2 +-
 .../src/main/java/com/reactnativeldk/LdkModule.kt      | 10 +++++-----
 lib/ios/Helpers.swift                                  | 10 ++++++----
 lib/ios/Ldk.swift                                      | 10 +++++-----
 lib/package.json                                       |  2 +-
 5 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/lib/android/src/main/java/com/reactnativeldk/Helpers.kt b/lib/android/src/main/java/com/reactnativeldk/Helpers.kt
index 9d9662d3..2bef483a 100644
--- a/lib/android/src/main/java/com/reactnativeldk/Helpers.kt
+++ b/lib/android/src/main/java/com/reactnativeldk/Helpers.kt
@@ -235,7 +235,7 @@ fun RapidGossipSync.downloadAndUpdateGraph(downloadUrl: String, tempStoragePath:
                 return@downloadFile
             }
 
-            val res = update_network_graph_no_std(File(destinationFile).readBytes(), Option_u64Z.some((System.currentTimeMillis() / 1000)))
+            val res = update_network_graph(File(destinationFile).readBytes())
             if (!res.is_ok()) {
                 val error = res as? Result_u32GraphSyncErrorZ.Result_u32GraphSyncErrorZ_Err
 
diff --git a/lib/android/src/main/java/com/reactnativeldk/LdkModule.kt b/lib/android/src/main/java/com/reactnativeldk/LdkModule.kt
index ce6d001b..57a634af 100644
--- a/lib/android/src/main/java/com/reactnativeldk/LdkModule.kt
+++ b/lib/android/src/main/java/com/reactnativeldk/LdkModule.kt
@@ -291,13 +291,13 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
 
         //If it's been more than 24 hours then we need to update RGS
         val timestamp = if (networkGraph!!._last_rapid_gossip_sync_timestamp is Option_u32Z.Some) (networkGraph!!._last_rapid_gossip_sync_timestamp as Option_u32Z.Some).some.toLong() else 0 // (networkGraph!!._last_rapid_gossip_sync_timestamp as Option_u32Z.Some).some
-        val hoursDiffSinceLastRGS = (System.currentTimeMillis() / 1000 - timestamp) / 60 / 60
-        if (hoursDiffSinceLastRGS < 24) {
-            LdkEventEmitter.send(EventTypes.native_log, "Skipping rapid gossip sync. Last updated $hoursDiffSinceLastRGS hours ago.")
+        val minutesDiffSinceLastRGS = (System.currentTimeMillis() / 1000 - timestamp) / 60
+        if (minutesDiffSinceLastRGS < 60) {
+            LdkEventEmitter.send(EventTypes.native_log, "Skipping rapid gossip sync. Last updated ${minutesDiffSinceLastRGS/60} hours ago.")
             return handleResolve(promise, LdkCallbackResponses.network_graph_init_success)
         }
 
-        LdkEventEmitter.send(EventTypes.native_log, "Rapid gossip sync applying update. Last updated $hoursDiffSinceLastRGS hours ago.")
+        LdkEventEmitter.send(EventTypes.native_log, "Rapid gossip sync applying update. Last updated ${minutesDiffSinceLastRGS/60} hours ago.")
 
         rapidGossipSync!!.downloadAndUpdateGraph(rapidGossipSyncUrl, "$accountStoragePath/rapid_gossip_sync/", timestamp) { error ->
             if (error != null) {
@@ -1091,7 +1091,7 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
             logDump.add("Claimable balances unavailable. Chain monitor not set yet")
         }
 
-        networkGraph?._last_rapid_gossip_sync_timestamp?.let { res ->
+        channelManagerConstructor?.net_graph?._last_rapid_gossip_sync_timestamp?.let { res ->
             val syncTimestamp = if (res is Option_u32Z.Some) (res as Option_u32Z.Some).some.toLong() else 0
             if (syncTimestamp == 0L) {
                 logDump.add("Last rapid gossip sync time: NEVER")
diff --git a/lib/ios/Helpers.swift b/lib/ios/Helpers.swift
index 264916a5..69e47a78 100644
--- a/lib/ios/Helpers.swift
+++ b/lib/ios/Helpers.swift
@@ -112,6 +112,8 @@ extension RouteHintHop {
 //Our own channels
 extension ChannelDetails {
     var asJson: [String: Any] {
+        let shortChannelId = getShortChannelId() != nil ? String(getShortChannelId()!) : ""
+        
         return [
             "channel_id": Data(getChannelId() ?? []).hexEncodedString(),
             "is_public": getIsPublic(),
@@ -124,9 +126,9 @@ extension ChannelDetails {
             "channel_type": Data(getChannelType()?.write() ?? []).hexEncodedString(),
             "user_channel_id": Data(getUserChannelId()).hexEncodedString(), //String
             "confirmations_required": getConfirmationsRequired() as Any, // Optional number
-            "short_channel_id": getShortChannelId() != nil ? String(getShortChannelId()!) : "", //String
-            "inbound_scid_alias": getInboundScidAlias() != nil ? String(getInboundScidAlias()!) : "", //String
-            "inbound_payment_scid": getInboundPaymentScid() != nil ? String(getInboundPaymentScid()!) : "", //String
+            "short_channel_id": shortChannelId,
+            "inbound_scid_alias": getInboundScidAlias() != nil ? String(getInboundScidAlias()!) : shortChannelId, //String
+            "inbound_payment_scid": getInboundPaymentScid() as Any, //Optional number,
             "inbound_capacity_sat": getInboundCapacityMsat() / 1000,
             "outbound_capacity_sat": getOutboundCapacityMsat() / 1000,
             "channel_value_satoshis": getChannelValueSatoshis(),
@@ -279,7 +281,7 @@ extension RapidGossipSync {
             
             guard let self = self else { return }
             
-            let res = self.updateNetworkGraphNoStd(updateData: [UInt8](try! Data(contentsOf: destinationFile)), currentTimeUnix: UInt64(Date().timeIntervalSince1970))
+            let res = self.updateNetworkGraph(updateData: [UInt8](try! Data(contentsOf: destinationFile)))
             guard res.isOk() else {
                 var errorMessage = "Failed to update network graph."
                 switch res.getError()?.getValueType() {
diff --git a/lib/ios/Ldk.swift b/lib/ios/Ldk.swift
index cab55c82..9f5053e9 100644
--- a/lib/ios/Ldk.swift
+++ b/lib/ios/Ldk.swift
@@ -272,14 +272,14 @@ class Ldk: NSObject {
             
             //If it's been more than 24 hours then we need to update RGS
             let timestamp = networkGraph?.getLastRapidGossipSyncTimestamp() ?? 0
-            let hoursDiffSinceLastRGS = (Calendar.current.dateComponents([.hour], from: Date.init(timeIntervalSince1970: TimeInterval(timestamp)), to: Date()).hour)!
+            let minutesDiffSinceLastRGS = (Calendar.current.dateComponents([.minute], from: Date.init(timeIntervalSince1970: TimeInterval(timestamp)), to: Date()).minute)!
             
-            guard hoursDiffSinceLastRGS > 24 else {
-                LdkEventEmitter.shared.send(withEvent: .native_log, body: "Skipping rapid gossip sync. Last updated \(hoursDiffSinceLastRGS) hours ago.")
+            guard minutesDiffSinceLastRGS > 60 else {
+                LdkEventEmitter.shared.send(withEvent: .native_log, body: "Skipping rapid gossip sync. Last updated \(minutesDiffSinceLastRGS/60) hours ago.")
                 return handleResolve(resolve, .network_graph_init_success)
             }
             
-            LdkEventEmitter.shared.send(withEvent: .native_log, body: "Rapid gossip sync applying update. Last updated \(hoursDiffSinceLastRGS) hours ago.")
+            LdkEventEmitter.shared.send(withEvent: .native_log, body: "Rapid gossip sync applying update. Last updated \(minutesDiffSinceLastRGS/60) hours ago.")
             
             rapidGossipSync!.downloadAndUpdateGraph(downloadUrl: String(rapidGossipSyncUrl), tempStoragePath: rapidGossipSyncStoragePath, timestamp: timestamp) { [weak self] error in
                 guard let self = self else { return }
@@ -1176,7 +1176,7 @@ class Ldk: NSObject {
             logDump.append("Claimable balances unavailable. Chain monitor not set yet")
         }
         
-        if let syncTimestamp = networkGraph?.getLastRapidGossipSyncTimestamp() {
+        if let syncTimestamp = channelManagerConstructor?.netGraph?.getLastRapidGossipSyncTimestamp() {
             let date = Date(timeIntervalSince1970: TimeInterval(syncTimestamp))
 
             let dateFormatter = DateFormatter()
diff --git a/lib/package.json b/lib/package.json
index 525422a1..811ee4b9 100644
--- a/lib/package.json
+++ b/lib/package.json
@@ -1,7 +1,7 @@
 {
   "name": "@synonymdev/react-native-ldk",
   "title": "React Native LDK",
-  "version": "0.0.118",
+  "version": "0.0.119",
   "description": "React Native wrapper for LDK",
   "main": "./dist/index.js",
   "types": "./dist/index.d.ts",