Automatic gas price

Use ethgasstation API for gas price if :auto-gas-price? is true in
config.

Fixes: #189
This commit is contained in:
Teemu Patja 2017-12-18 12:53:50 +02:00
parent a76748b8be
commit 62b4394c14
No known key found for this signature in database
GPG Key ID: F5B7035E6580FD4C
3 changed files with 34 additions and 11 deletions

View File

@ -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)

View File

@ -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"

View File

@ -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)))