diff --git a/src/clj/commiteth/eth/core.clj b/src/clj/commiteth/eth/core.clj index b04a0e3..c4c8fa5 100644 --- a/src/clj/commiteth/eth/core.clj +++ b/src/clj/commiteth/eth/core.clj @@ -6,19 +6,40 @@ [clojure.string :refer [join]] [clojure.tools.logging :as log] [clojure.string :as str] - [pandect.core :as pandect])) + [pandect.core :as pandect] + [commiteth.util.util :refer [json-api-request]])) (defn eth-rpc-url [] (env :eth-rpc-url "http://localhost:8545")) (defn eth-account [] (:eth-account env)) (defn eth-password [] (:eth-password env)) (defn gas-estimate-factor [] (env :gas-estimate-factor 1.0)) +(defn auto-gas-price? [] (env :auto-gas-price? false)) -(defn gas-price +(defn eth-gasstation-gas-price [] + (let [data (json-api-request "https://ethgasstation.info/json/ethgasAPI.json") + avg-price (-> (get data "average") + bigint) + avg-price-gwei (/ avg-price (bigint 10))] + (->> (* (bigint (Math/pow 10 9)) avg-price-gwei) ;; for some reason the API returns 10x gwei price + .toBigInteger))) + + +(defn gas-price-from-config [] (-> (:gas-price env 20000000000) ;; 20 gwei default str BigInteger.)) +(defn gas-price + [] + (if (auto-gas-price?) + (try + (eth-gasstation-gas-price) + (catch Throwable t + (log/error "Failed to get gas price with ethgasstation API" t) + (gas-price-from-config))) + (gas-price-from-config))) + (defn eth-rpc [method params] (let [request-id (rand-int 4096) diff --git a/src/clj/commiteth/util/crypto_fiat_value.clj b/src/clj/commiteth/util/crypto_fiat_value.clj index de95b09..07bea89 100644 --- a/src/clj/commiteth/util/crypto_fiat_value.clj +++ b/src/clj/commiteth/util/crypto_fiat_value.clj @@ -1,19 +1,13 @@ (ns commiteth.util.crypto-fiat-value - (:require [clj-http.client :as http] - [mount.core :as mount] + (:require [mount.core :as mount] [clojure.tools.logging :as log] [commiteth.config :refer [env]] - [clojure.data.json :as json])) + [commiteth.util.util :refer [json-api-request]])) (defn fiat-api-provider [] (env :fiat-api-provider :coinmarketcap)) -(defn json-api-request [url] - (->> (http/get url) - (:body) - (json/read-str))) - (defn get-token-usd-price-cryptonator "Get current USD value for a token using cryptonator API" diff --git a/src/clj/commiteth/util/util.clj b/src/clj/commiteth/util/util.clj index 65634c3..f85211e 100644 --- a/src/clj/commiteth/util/util.clj +++ b/src/clj/commiteth/util/util.clj @@ -1,4 +1,7 @@ -(ns commiteth.util.util) +(ns commiteth.util.util + (:require + [clj-http.client :as http] + [clojure.data.json :as json])) (defn eth-decimal->str [n] @@ -6,3 +9,8 @@ (defn usd-decimal->str [n] (format "%.2f" n)) + +(defn json-api-request [url] + (->> (http/get url) + (:body) + (json/read-str)))