From ae6b9a11f7cfebbb9a85bfe8217759760a85d183 Mon Sep 17 00:00:00 2001
From: Diego Calero <dcalero@fiqus.coop>
Date: Sat, 6 Jan 2024 17:23:30 -0300
Subject: [PATCH] Adding pause support and updating donate addrs.

---
 README.md         | 11 ++++----
 config.gs         |  2 +-
 misc/scheduler.gs | 72 +++++++++++++++++++++++++++++++++++++++++++----
 ui/menu.gs        | 24 ++++++++++++----
 ui/setup.gs       |  8 +-----
 5 files changed, 92 insertions(+), 25 deletions(-)

diff --git a/README.md b/README.md
index 2c1a9b3..3eee845 100644
--- a/README.md
+++ b/README.md
@@ -295,12 +295,11 @@ If your love is strong enough, feel free to share it with me!  =D
 I will much appreciate any contribution and support to keep working on it.  
 I have several ideas for new features, so much more could come!
 
-You can send any token through the **Binance Smart Chain** (BSC/BEP20) to the address:  
-`0x1d047bc3e46ce0351fd0c44fc2a2029512e87a97`
-
-But you can also use these networks:
-* **BTC:** `bc1qanxn2ycp9em50hj5p7en6wxe962zj4umqvs7q9`
-* **ETH:** `0x1d047bc3e46ce0351fd0c44fc2a2029512e87a97`
+You can send any token to these network addresses:
+* **BTC (SegWit):** `bc1quxsufu73vy3d2ehpjddgxl9pjs2wygltmkryd0`
+* **ETH (ERC20) | BNB (BSC/BEP20) | MATIC (Polygon):** `0x25307eea23642c03e3e2a522624f8181870afb18`
+* **TRX (TRC20):** `THu2mwfkFXSs2jFuZDxuMBKd22Wjkp3zwv`
+* **SOL (Solana):** `3c7g2DP1cgth1rxrF5iis5RjkWWZKavACaoJ1JTvZpL2`
 
 ---
 
diff --git a/config.gs b/config.gs
index c5c9a23..b467ec0 100644
--- a/config.gs
+++ b/config.gs
@@ -17,7 +17,7 @@ const USE_PROXY = false;
 
 // Other settings
 let DEBUG = false; // If enabled, will output more logs to the AppScript console
-const VERSION = "v0.5.4";
+const VERSION = "v0.5.5";
 const REPO_URL = "https://github.com/diegomanuel/binance-to-google-sheets";
 const SPOT_API_URL = "https://api.binance.com";
 const FUTURES_API_URL = "https://fapi.binance.com";
diff --git a/misc/scheduler.gs b/misc/scheduler.gs
index 94809d0..785d49c 100644
--- a/misc/scheduler.gs
+++ b/misc/scheduler.gs
@@ -6,6 +6,7 @@ function BinScheduler(OPTIONS) {
   const SCHEDULES_PROP_NAME = "BIN_SCHEDULER_ENTRIES";
   const RESCHEDULES_PROP_NAME = "BIN_SCHEDULER_ENTRIES_RETRY";
   const LAST_RUN_PROP_NAME = "BIN_SCHEDULER_LAST_RUN";
+  const PAUSED_PROP_NAME = "BIN_SCHEDULER_PAUSED";
 
   return {
     init,
@@ -15,11 +16,14 @@ function BinScheduler(OPTIONS) {
     run15m,
     run30m,
     run60m,
+    runOrdersTable,
     getSchedule,
     setSchedule,
     cleanSchedules,
     rescheduleFailed,
     clearFailed,
+    isPaused,
+    setPaused,
     isStalled
   };
 
@@ -36,7 +40,11 @@ function BinScheduler(OPTIONS) {
   function run1m() {
     _updateLastRun();
     BinDoOrdersTable().init(); // Initialize orders table sheets (if any)
-    BinUtils().forceRefreshSheetFormulas("1m");
+    if (isPaused()) {
+      Logger.log("Scheduler is paused, skipping '1m' call..");
+    } else {
+      BinUtils().forceRefreshSheetFormulas("1m");
+    }
   }
 
   /**
@@ -44,7 +52,11 @@ function BinScheduler(OPTIONS) {
    */
   function run5m() {
     _updateLastRun();
-    BinUtils().forceRefreshSheetFormulas("5m");
+    if (isPaused()) {
+      Logger.log("Scheduler is paused, skipping '5m' call..");
+    } else {
+      BinUtils().forceRefreshSheetFormulas("5m");
+    }
   }
 
   /**
@@ -52,7 +64,11 @@ function BinScheduler(OPTIONS) {
    */
   function run10m() {
     _updateLastRun();
-    BinUtils().forceRefreshSheetFormulas("10m");
+    if (isPaused()) {
+      Logger.log("Scheduler is paused, skipping '10m' call..");
+    } else {
+      BinUtils().forceRefreshSheetFormulas("10m");
+    }
   }
 
   /**
@@ -60,7 +76,11 @@ function BinScheduler(OPTIONS) {
    */
   function run15m() {
     _updateLastRun();
-    BinUtils().forceRefreshSheetFormulas("15m");
+    if (isPaused()) {
+      Logger.log("Scheduler is paused, skipping '15m' call..");
+    } else {
+      BinUtils().forceRefreshSheetFormulas("15m");
+    }
   }
 
   /**
@@ -68,7 +88,11 @@ function BinScheduler(OPTIONS) {
    */
   function run30m() {
     _updateLastRun();
-    BinUtils().forceRefreshSheetFormulas("30m");
+    if (isPaused()) {
+      Logger.log("Scheduler is paused, skipping '30m' call..");
+    } else {
+      BinUtils().forceRefreshSheetFormulas("30m");
+    }
   }
 
   /**
@@ -76,7 +100,24 @@ function BinScheduler(OPTIONS) {
    */
   function run60m() {
     _updateLastRun();
-    BinUtils().forceRefreshSheetFormulas("1h");
+    if (isPaused()) {
+      Logger.log("Scheduler is paused, skipping '1h' call..");
+    } else {
+      BinUtils().forceRefreshSheetFormulas("1h");
+    }
+  }
+
+  /**
+   * Runs the scheduled functions for OrdersTable
+   */
+  function runOrdersTable() {
+    _updateLastRun();
+    if (isPaused()) {
+      Logger.log("Scheduler is paused, skipping 'orders table poll' call..");
+    }
+    else if (BinSetup().areAPIKeysConfigured()) {
+      BinDoOrdersTable().execute();
+    }
   }
 
   /**
@@ -137,6 +178,25 @@ function BinScheduler(OPTIONS) {
     return false;
   }
 
+  /**
+   * Returns true if the scheduler is paused
+   */
+  function isPaused() {
+    return _getDocPropService().getProperty(PAUSED_PROP_NAME) == "true";
+  }
+
+  /**
+   * Sets the paused status
+   */
+  function setPaused(paused) {
+    const doc = _getDocPropService();
+    doc.setProperty(PAUSED_PROP_NAME, paused ? "true" : "false");
+    // Also clean the re-schedules (no matter the pause status)
+    doc.setProperty(RESCHEDULES_PROP_NAME, JSON.stringify({}));
+    Logger.log("Scheduler paused status set to: "+isPaused());
+    return paused;
+  }
+
   /**
    * Returns true if the scheduler didn't run in the last 5 minutes
    */
diff --git a/ui/menu.gs b/ui/menu.gs
index 789a7d6..254813e 100644
--- a/ui/menu.gs
+++ b/ui/menu.gs
@@ -7,11 +7,13 @@ function BinMenu(ui) {
    */
   function addMenuItems(menu) {
     const is_ready = BinSetup().isReady();
+    const is_paused = BinScheduler().isPaused();
 
     if (!is_ready) { // Add-on is not ready (unauthorized or BinScheduler is stalled or never run)
       menu.addItem("Authorize add-on!", "authorizeMe");
     } else {
       menu.addItem("Refresh", "forceRefreshFormulas");
+      menu.addItem((is_paused?"Resume":"Pause")+" auto-refresh", "togglePaused");
     }
     menu.addSeparator()
         .addItem("Show API Last Update", "showAPILastUpdate")
@@ -108,6 +110,18 @@ function forceRefreshFormulas() {
   utils.forceRefreshSheetFormulas(); // Refresh'em all!
 }
 
+/**
+ * Toggles the pause state
+ */
+function togglePaused() {
+  const utils = BinUtils();
+  const scheduler = BinScheduler();
+  const is_paused = scheduler.isPaused();
+  scheduler.setPaused(!is_paused);
+  utils.toast("Auto-refresh was "+(is_paused?"resumed":"paused")+"!", "", 5);
+  utils.refreshMenu(); // Refresh main menu items
+}
+
 /**
  * Triggers the modal to enable/authorize the add-on.
  * If this function gets executed, it means that the user has authorized the add-on!
@@ -258,12 +272,12 @@ function showDonate() {
                "I have several ideas for new features, so much more could come!\n"+
                "\n"+
                "\n"+
-               "You can send any token through the Binance Smart Chain (BSC/BEP20) to:\n"+
-               "0x1d047bc3e46ce0351fd0c44fc2a2029512e87a97\n"+
+               "You can send any token to these network addresses:\n"+
                "\n"+
-               "But you can also use these networks:\n"+
-               "BTC: bc1qanxn2ycp9em50hj5p7en6wxe962zj4umqvs7q9\n"+
-               "ETH: 0x1d047bc3e46ce0351fd0c44fc2a2029512e87a97\n"+
+               "BTC (SegWit): bc1quxsufu73vy3d2ehpjddgxl9pjs2wygltmkryd0\n"+
+               "ETH (ERC20) | BNB (BSC/BEP20) | MATIC (Polygon): 0x25307eea23642c03e3e2a522624f8181870afb18\n"+
+               "TRX (TRC20): THu2mwfkFXSs2jFuZDxuMBKd22Wjkp3zwv\n"+
+               "SOL (Solana): 3c7g2DP1cgth1rxrF5iis5RjkWWZKavACaoJ1JTvZpL2\n"+
                "\n"+
                "------------------------------------------------\n"+
                "Don't you have a Binance account yet?\n"+
diff --git a/ui/setup.gs b/ui/setup.gs
index f06ce4b..b75669a 100644
--- a/ui/setup.gs
+++ b/ui/setup.gs
@@ -418,14 +418,8 @@ function doRefresh30m(event) {
 function doRefresh1h(event) {
   _callScheduler(event, "60m");
 }
-
 function doTablesPoll(event) {
-  if (DEBUG) {
-    Logger.log("EVENT: "+JSON.stringify(event));
-  }
-  if (BinSetup().areAPIKeysConfigured()) {
-    BinDoOrdersTable().execute();
-  }
+  _callScheduler(event, "OrdersTable")
 }
 
 function _callScheduler(event, every) {