Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code quality #382

Merged
merged 7 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ designs/deploy
tags
temp
release
.clj-kondo
.lsp
6 changes: 5 additions & 1 deletion bb.edn
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@
run-server {:doc "Start Node.js API server process"
:task (do
(println "Starting Ethlance API server")
(shell {:dir "server"} "node out/ethlance_server.js"))}
(shell {:dir "server"
:extra-env {"ETHLANCE_ENV" "dev"
"UI_CONFIG_PATH" "../config/ui-config-dev.edn"
"SERVER_CONFIG_PATH" "../config/server-config-dev.edn"}}
"node out/ethlance_server.js"))}
repl {:doc "Start REPL. Usage: bb repl [ui|server|test]"
:requires ([clojure.edn :as edn])
:task (let [build-arg (or (first *command-line-args*) "ui")
Expand Down
68 changes: 36 additions & 32 deletions server/src/district/server/async_db.clj
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
(ns district.server.async-db)

(defmacro with-async-resolver-tx [conn & body]

(defmacro with-async-resolver-tx
[conn & body]
`(js/Promise.
(fn [resolve# reject#]
(.then (district.server.async-db/get-connection)
(fn [~conn]
(district.server.async-db/begin-tx ~conn)
(cljs.core.async/take! (district.shared.async-helpers/safe-go ~@body)
(fn [v-or-err#]

(if (cljs.core/instance? js/Error v-or-err#)
(do
(district.server.async-db/rollback-tx ~conn)
(district.server.async-db/release-connection ~conn)
(reject# v-or-err#))

(do
(district.server.async-db/commit-tx ~conn)
(district.server.async-db/release-connection ~conn)
(resolve# v-or-err#))))))))))

(defmacro with-async-resolver-conn [conn & body]
(fn [resolve# reject#]
(.then (district.server.async-db/get-connection)
(fn [~conn]
(district.server.async-db/begin-tx ~conn)
(cljs.core.async/take! (district.shared.async-helpers/safe-go ~@body)
(fn [v-or-err#]

(if (cljs.core/instance? js/Error v-or-err#)
(do
(district.server.async-db/rollback-tx ~conn)
(district.server.async-db/release-connection ~conn)
(reject# v-or-err#))

(do
(district.server.async-db/commit-tx ~conn)
(district.server.async-db/release-connection ~conn)
(resolve# v-or-err#))))))))))


(defmacro with-async-resolver-conn
[conn & body]
`(js/Promise.
(fn [resolve# reject#]
(.then (district.server.async-db/get-connection)
(fn [~conn]
(cljs.core.async/take! (district.shared.async-helpers/safe-go ~@body)
(fn [v-or-err#]

;; here we have the result so it is safe to release the connection
(district.server.async-db/release-connection ~conn)

(if (cljs.core/instance? js/Error v-or-err#)
(reject# v-or-err#)
(resolve# v-or-err#)))))))))
(fn [resolve# reject#]
(.then (district.server.async-db/get-connection)
(fn [~conn]
(cljs.core.async/take! (district.shared.async-helpers/safe-go ~@body)
(fn [v-or-err#]

;; here we have the result so it is safe to release the connection
(district.server.async-db/release-connection ~conn)

(if (cljs.core/instance? js/Error v-or-err#)
(reject# v-or-err#)
(resolve# v-or-err#)))))))))
90 changes: 57 additions & 33 deletions server/src/district/server/async_db.cljs
Original file line number Diff line number Diff line change
@@ -1,66 +1,80 @@
(ns district.server.async-db
(:refer-clojure :exclude [get run!])
(:require ["pg" :as pg]
[clojure.string :as str]
[district.server.config :refer [config]]
[district.server.logging]
[district.shared.async-helpers :refer [safe-go <?]]
[cljs.core.async :refer [<! go]]
[honeysql-postgres.format]
[honeysql-postgres.helpers]
[honeysql.core :as sql]
[honeysql.format :as sql-format]
[honeysql.helpers]
[mount.core :as mount :refer [defstate]]
[taoensso.timbre :as log]))
(:require
["pg" :as pg]
[cljs.core.async :refer [<! go]]
[clojure.string :as str]
[district.server.config :refer [config]]
[district.server.logging]
[district.shared.async-helpers :refer [safe-go <?]]
[honeysql-postgres.format]
[honeysql-postgres.helpers]
[honeysql.core :as sql]
[honeysql.format :as sql-format]
[honeysql.helpers]
[mount.core :as mount :refer [defstate]]
[taoensso.timbre :as log]))


(def Pool (.-Pool ^js pg))

(declare db start stop)


(def mount-state-key
"Key defining our mount component within the district configuration"
:district/db)


(defstate ^{:on-reload :noop} db
:start (start (merge (clojure.core/get @config mount-state-key)
(mount-state-key (mount/args))))
:stop (stop))

(defn sql-name-transform-fn [n]

(defn sql-name-transform-fn
[n]
(-> n
munge
(str/replace "_STAR_" "*")
(str/replace "_PERCENT_" "%")))

(def transform-result-keys-fn (comp keyword
demunge
#(str/replace % #"_slash_" "_SLASH_")))

(defn- map-keys [f m]
(def transform-result-keys-fn
(comp keyword
demunge
#(str/replace % #"_slash_" "_SLASH_")))


(defn- map-keys
[f m]
(into {} (map (fn [[k v]] [(f k) v]) m)))


(defn get-connection
"Returns a db connection from the pool."
[]
(.connect (:connection-pool @db)))


(defn release-connection
"Returns a db connection to the pool."
[conn]
(.release conn))


(defn run!
"Given a db connection and a honey sql query runs it and returns its result."
[conn statement]
(safe-go
(let [[query-str & values] (binding [sql-format/*name-transform-fn* sql-name-transform-fn]
(sql/format statement
:parameterizer :postgresql
:allow-namespaced-names? true))
res (<? (.query conn query-str (clj->js (or values []))))]
(->> (js->clj (.-rows res))
(map #(map-keys transform-result-keys-fn %))))))
(let [[query-str & values] (binding [sql-format/*name-transform-fn* sql-name-transform-fn]
(sql/format statement
:parameterizer :postgresql
:allow-namespaced-names? true))
res (<? (.query conn query-str (clj->js (or values []))))]
(->> (js->clj (.-rows res))
(map #(map-keys transform-result-keys-fn %))))))


(defn run-raw!
"Given a db connection and raw SQL string run & return result"
Expand All @@ -75,34 +89,42 @@
(->> (js->clj (.-rows res))
(map #(map-keys transform-result-keys-fn %)))))))


(defn all
"Given a db connection and a honey sql query runs it and returns resultset rows."
[conn q]
(run! conn q))


(defn get
"Given a db connection and a honey sql query runs it and returns first resultset row."
[conn q]
(safe-go
(first (<? (all conn q)))))
(first (<? (all conn q)))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Transaction management ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;

(defn begin-tx [conn]
(defn begin-tx
[conn]
(.query conn "BEGIN"))

(defn commit-tx [conn]

(defn commit-tx
[conn]
(.query conn "COMMIT"))

(defn rollback-tx [conn]

(defn rollback-tx
[conn]
(.query conn "ROLLBACK"))

;;;;;;;;;;;;;;;;;;;;;

;;
;; Mount component ;;
;;;;;;;;;;;;;;;;;;;;;
;;

(defn start
"Start the ethlance-db mount component."
Expand All @@ -119,12 +141,14 @@
(log/info "DB component started")
{:connection-pool pool}))


(defn stop
"Stop the db mount component."
[]
(release-connection (:wildcard-connection @db))
::stopped)


#_(comment

(safe-go
Expand Down
34 changes: 18 additions & 16 deletions server/src/ethlance/server/contract.cljs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
(ns ethlance.server.contract
"Includes functions for working with ethereum contracts in an asynchronous manner."
(:require [clojure.core.async
:as
async
:refer
[>! chan close! go put!]
:include-macros
true]
[district.server.smart-contracts :as contracts]))
(:require
[clojure.core.async
:as
async
:refer
[>! chan close! go put!]
:include-macros
true]
[district.server.smart-contracts :as contracts]))


(defn call
"Call the given `contract-address` with the kebab-case formatted
Expand Down Expand Up @@ -36,14 +38,14 @@
(if (instance? js/Promise result)
(-> result
(.then
;; Success
(fn [result]
(put! success-channel result)
(close! error-channel))
;; Failure
(fn [error-object]
(put! error-channel error-object)
(close! success-channel))))
;; Success
(fn [result]
(put! success-channel result)
(close! error-channel))
;; Failure
(fn [error-object]
(put! error-channel error-object)
(close! success-channel))))
;; No promise, pass the result to the success channel.
(do (>! success-channel result)
(close! error-channel)))))
Expand Down
29 changes: 17 additions & 12 deletions server/src/ethlance/server/contract/ds_auth.cljs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
(ns ethlance.server.contract.ds-auth
(:require [ethlance.server.contract :refer [call]]))
(:require
[ethlance.server.contract :refer [call]]))


(defn owner
"Get the owner address from the DSAuth contract defined by
`contract-key`."
[contract-key]
(call
:contract-key contract-key
:method-name :owner []))
:contract-key contract-key
:method-name :owner []))


(defn set-owner!
"Set the owner address for the DSAuth contract defined by
Expand All @@ -23,25 +26,27 @@
"
[contract-key _ & [opts]]
(call
:contract-key contract-key
:method-name :set-owner
:contract-options (merge {:gas 100000} opts)))
:contract-key contract-key
:method-name :set-owner
:contract-options (merge {:gas 100000} opts)))


(defn authority
"Get the authority address from the DSAuth contract defined by
`contract-key`."
[contract-key]
(call
:contract-key contract-key
:method-name :authority))
:contract-key contract-key
:method-name :authority))


(defn set-authority!
"Set the DSAuthority implementation defined by the contract address
`new-authority` to the DSAuth defined by the smart-contract key
`contract-key`."
[contract-key new-authority & [opts]]
(call
:contract-key contract-key
:method-name :set-authority
:contract-arguments [new-authority]
:contract-options (merge {:gas 100000} opts)))
:contract-key contract-key
:method-name :set-authority
:contract-arguments [new-authority]
:contract-options (merge {:gas 100000} opts)))
16 changes: 10 additions & 6 deletions server/src/ethlance/server/contract/ethlance.cljs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
(ns ethlance.server.contract.ethlance
(:require [district.server.smart-contracts :as smart-contracts]))
(:require
[district.server.smart-contracts :as smart-contracts]))

(defn initialize [job-proxy-address]

(defn initialize
[job-proxy-address]
(smart-contracts/contract-send :ethlance :initialize [job-proxy-address] {:gas 6000000}))


(defn create-job
([creator offered-values arbiters ipfs-data]
(create-job creator offered-values arbiters ipfs-data {}))

([creator offered-values arbiters ipfs-data merged-opts]
(smart-contracts/contract-send
:ethlance :create-job
[creator offered-values arbiters ipfs-data]
(merge {:gas 6000000} merged-opts))))
(smart-contracts/contract-send
:ethlance :create-job
[creator offered-values arbiters ipfs-data]
(merge {:gas 6000000} merged-opts))))
Loading
Loading