<commit_before>(ns district0x.ui.events
  (:require
    [ajax.core :as ajax]
    [akiroz.re-frame.storage :as re-frame-storage]
    [bignumber.core :as bn]
    [cemerick.url :as url]
    [cljs-time.coerce :as time-coerce]
    [cljs-time.core :as t]
    [cljs-web3.core :as web3]
    [cljs-web3.eth :as web3-eth]
    [cljs-web3.personal :as web3-personal]
    [cljs-web3.utils :as web3-utils]
    [cljs.spec.alpha :as s]
    [clojure.data :as data]
    [clojure.set :as set]
    [clojure.string :as string]
    [day8.re-frame.async-flow-fx]
    [day8.re-frame.http-fx]
    [district.encryption :as encryption]
    [district.shared.error-handling :refer [try-catch]]
    [district.ui.logging.events :as logging]
    [district0x.shared.utils :as d0x-shared-utils :refer [wei->eth]]
    [district0x.ui.db]
    [district0x.ui.dispatch-fx]
    [district0x.ui.history :as history]
    [district0x.ui.history-fx]
    [district0x.ui.interval-fx]
    [district0x.ui.location-fx]
    [district0x.ui.spec-interceptors :refer [validate-args conform-args validate-db validate-first-arg]]
    [district0x.ui.spec]
    [district0x.ui.utils :as d0x-ui-utils :refer [get-window-size to-locale-string current-location-hash namehash file-write]]
    [district0x.ui.web3-fx :as web3-fx.ethereum]
    [district0x.ui.window-fx]
    [goog.string :as gstring]
    [goog.string.format]
    [madvas.re-frame.google-analytics-fx]
    [madvas.re-frame.web3-fx]
    [medley.core :as medley]
    [print.foo :include-macros true]
    [print.foo :refer [look]]
    [re-frame.core :as re-frame :refer [reg-event-db reg-event-fx inject-cofx path trim-v after debug reg-fx console dispatch reg-cofx]]
    [re-promise.core]
    [taoensso.timbre :as log]
    ))

(re-frame-storage/reg-co-fx! :contribution {:fx :localstorage :cofx :localstorage})

(defn reg-empty-event-fx [id]
  (reg-event-fx
    id
    (constantly nil)))

(reg-cofx
  :current-url
  (fn [coeffects _]
    (assoc coeffects :current-url (d0x-ui-utils/current-url))))

(def interceptors [trim-v (validate-db :district0x.ui/db)])

(defn get-contract [db key]
  (get-in db [:smart-contracts key]))

(defn get-instance
  ([db key]
   (get-in db [:smart-contracts key :instance]))
  ([db key address]
   (let [abi (:abi (get-contract db key))]
     (web3-eth/contract-at (:web3 db) abi address))))

(defn all-contracts-loaded? [db]
  (every? #(and (:abi %) (if goog.DEBUG (:bin %) true)) (vals (:smart-contracts db))))

(defn contract-xhrio [contract-name code-type version on-success on-failure]
  {:method :get
   :uri (gstring/format "./contracts/build/%s.%s?v=%s" contract-name (name code-type) (if goog.DEBUG
                                                                                        (.getTime (js/Date.))
                                                                                        version))
   :timeout 6000
   :response-format (if (= code-type :abi) (ajax/json-response-format) (ajax/text-response-format))
   :on-success on-success
   :on-failure on-failure})

(defn create-wallet-connect-web3
  "Initiate web3 modal popup and allow user to choose a way how to
  connect to Namebazaar. If the modal is closed or there is an
  unexpected error it tries to create web3 from fallback url.

  The result of the web3 modal will be web3 provider which is used
  to create new web3 instance (which is later saved into DB). This
  handling is asynchronous and the function returns a promise."
  [infura-id fallback-web3-url]
  ;; Clear the wallet connect local storage, which persists previous connection
  ;; I've found the persisted connection to be extremely unreliable
  (.removeItem (.-localStorage js/window) "walletconnect")
  ;; https://lwhorton.github.io/2018/10/20/clojurescript-interop-with-javascript.html
  (let [web3-modal-class (.. js/window -Web3Modal -default)
        wallet-connect-provider (.. js/window -WalletConnectProvider -default)
        ;; Inspired by https://github.com/Web3Modal/web3modal-vanilla-js-example/blob/master/example.js#L52
        provider-options (clj->js {:walletconnect {:package wallet-connect-provider :options {:infuraId infura-id }}})
        web3-modal-options (clj->js {:cacheProvider false :providerOptions provider-options :disableInjectedProvider false})
        web3-modal (new web3-modal-class web3-modal-options)]
    (->
      (js-invoke web3-modal "connect")
      (.then (fn [provider] (new (aget js/window "Web3") provider)))
      (.catch (fn [err]
                (log/error "Unable to retrieve web3 provider via wallet connect. Creating fallback web3. Reason:" err)
                (web3/create-web3 fallback-web3-url))))))

(defn initialize-db
  "Update re-frame `db` with `localstorage` and `current-url` co-effects.

  Notes:
  - Returned `db` is used as a `:db` side-effect."
  [db localstorage current-url]
  (-> db
      (d0x-shared-utils/merge-in localstorage)
      (update :active-page merge
              {:query-params (medley/map-keys keyword (:query current-url))
               :path (:path current-url)})
      (update-in [:transaction-log :settings] merge
                 {:open? false
                  :highlighted-transaction nil})))
<commit_msg>Don't mangle walletconnect in optimized builds<commit_after>(ns district0x.ui.events
  (:require
    [ajax.core :as ajax]
    [akiroz.re-frame.storage :as re-frame-storage]
    [bignumber.core :as bn]
    [cemerick.url :as url]
    [cljs-time.coerce :as time-coerce]
    [cljs-time.core :as t]
    [cljs-web3.core :as web3]
    [cljs-web3.eth :as web3-eth]
    [cljs-web3.personal :as web3-personal]
    [cljs-web3.utils :as web3-utils]
    [cljs.spec.alpha :as s]
    [clojure.data :as data]
    [clojure.set :as set]
    [clojure.string :as string]
    [day8.re-frame.async-flow-fx]
    [day8.re-frame.http-fx]
    [district.encryption :as encryption]
    [district.shared.error-handling :refer [try-catch]]
    [district.ui.logging.events :as logging]
    [district0x.shared.utils :as d0x-shared-utils :refer [wei->eth]]
    [district0x.ui.db]
    [district0x.ui.dispatch-fx]
    [district0x.ui.history :as history]
    [district0x.ui.history-fx]
    [district0x.ui.interval-fx]
    [district0x.ui.location-fx]
    [district0x.ui.spec-interceptors :refer [validate-args conform-args validate-db validate-first-arg]]
    [district0x.ui.spec]
    [district0x.ui.utils :as d0x-ui-utils :refer [get-window-size to-locale-string current-location-hash namehash file-write]]
    [district0x.ui.web3-fx :as web3-fx.ethereum]
    [district0x.ui.window-fx]
    [goog.object]
    [goog.string :as gstring]
    [goog.string.format]
    [madvas.re-frame.google-analytics-fx]
    [madvas.re-frame.web3-fx]
    [medley.core :as medley]
    [print.foo :include-macros true]
    [print.foo :refer [look]]
    [re-frame.core :as re-frame :refer [reg-event-db reg-event-fx inject-cofx path trim-v after debug reg-fx console dispatch reg-cofx]]
    [re-promise.core]
    [taoensso.timbre :as log]
    ))

(re-frame-storage/reg-co-fx! :contribution {:fx :localstorage :cofx :localstorage})

(defn reg-empty-event-fx [id]
  (reg-event-fx
    id
    (constantly nil)))

(reg-cofx
  :current-url
  (fn [coeffects _]
    (assoc coeffects :current-url (d0x-ui-utils/current-url))))

(def interceptors [trim-v (validate-db :district0x.ui/db)])

(defn get-contract [db key]
  (get-in db [:smart-contracts key]))

(defn get-instance
  ([db key]
   (get-in db [:smart-contracts key :instance]))
  ([db key address]
   (let [abi (:abi (get-contract db key))]
     (web3-eth/contract-at (:web3 db) abi address))))

(defn all-contracts-loaded? [db]
  (every? #(and (:abi %) (if goog.DEBUG (:bin %) true)) (vals (:smart-contracts db))))

(defn contract-xhrio [contract-name code-type version on-success on-failure]
  {:method :get
   :uri (gstring/format "./contracts/build/%s.%s?v=%s" contract-name (name code-type) (if goog.DEBUG
                                                                                        (.getTime (js/Date.))
                                                                                        version))
   :timeout 6000
   :response-format (if (= code-type :abi) (ajax/json-response-format) (ajax/text-response-format))
   :on-success on-success
   :on-failure on-failure})

(defn create-wallet-connect-web3
  "Initiate web3 modal popup and allow user to choose a way how to
  connect to Namebazaar. If the modal is closed or there is an
  unexpected error it tries to create web3 from fallback url.

  The result of the web3 modal will be web3 provider which is used
  to create new web3 instance (which is later saved into DB). This
  handling is asynchronous and the function returns a promise."
  [infura-id fallback-web3-url]
  ;; Clear the wallet connect local storage, which persists previous connection
  ;; I've found the persisted connection to be extremely unreliable
  (.removeItem (.-localStorage js/window) "walletconnect")
  ;; https://lwhorton.github.io/2018/10/20/clojurescript-interop-with-javascript.html
  (let [web3-modal-class (goog.object/get (goog.object/get js/window "Web3Modal") "default")
        wallet-connect-provider (goog.object/get (goog.object/get js/window "WalletConnectProvider") "default")
        ;; Inspired by https://github.com/Web3Modal/web3modal-vanilla-js-example/blob/master/example.js#L52
        provider-options (clj->js {:walletconnect {:package wallet-connect-provider :options {:infuraId infura-id }}})
        web3-modal-options (clj->js {:cacheProvider false :providerOptions provider-options :disableInjectedProvider false})
        web3-modal (new web3-modal-class web3-modal-options)]
    (->
      (js-invoke web3-modal "connect")
      (.then (fn [provider] (new (aget js/window "Web3") provider)))
      (.catch (fn [err]
                (log/error "Unable to retrieve web3 provider via wallet connect. Creating fallback web3. Reason:" err)
                (web3/create-web3 fallback-web3-url))))))

(defn initialize-db
  "Update re-frame `db` with `localstorage` and `current-url` co-effects.

  Notes:
  - Returned `db` is used as a `:db` side-effect."
  [db localstorage current-url]
  (-> db
      (d0x-shared-utils/merge-in localstorage)
      (update :active-page merge
              {:query-params (medley/map-keys keyword (:query current-url))
               :path (:path current-url)})
      (update-in [:transaction-log :settings] merge
                 {:open? false
                  :highlighted-transaction nil})))

{'fraction_non_alphanumeric': 0.13689737470167065, 'fraction_numerical': 0.013174224343675418, 'mean_word_length': 2.834553440702782, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1529256', 'n_tokens_mistral': 3651, 'n_tokens_neox': 3503, 'n_words': 866}