mirror of
https://github.com/status-im/open-bounty.git
synced 2025-02-17 03:47:58 +00:00
Automatic gas price
Use ethgasstation API for gas price if :auto-gas-price? is true in config. Fixes: #189
This commit is contained in:
parent
c7e4224e3b
commit
bb200e8399
@ -6,19 +6,40 @@
|
|||||||
[clojure.string :refer [join]]
|
[clojure.string :refer [join]]
|
||||||
[clojure.tools.logging :as log]
|
[clojure.tools.logging :as log]
|
||||||
[clojure.string :as str]
|
[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-rpc-url [] (env :eth-rpc-url "http://localhost:8545"))
|
||||||
(defn eth-account [] (:eth-account env))
|
(defn eth-account [] (:eth-account env))
|
||||||
(defn eth-password [] (:eth-password env))
|
(defn eth-password [] (:eth-password env))
|
||||||
(defn gas-estimate-factor [] (env :gas-estimate-factor 1.0))
|
(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
|
(-> (:gas-price env 20000000000) ;; 20 gwei default
|
||||||
str
|
str
|
||||||
BigInteger.))
|
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
|
(defn eth-rpc
|
||||||
[method params]
|
[method params]
|
||||||
(let [request-id (rand-int 4096)
|
(let [request-id (rand-int 4096)
|
||||||
|
@ -1,19 +1,13 @@
|
|||||||
(ns commiteth.util.crypto-fiat-value
|
(ns commiteth.util.crypto-fiat-value
|
||||||
(:require [clj-http.client :as http]
|
(:require [mount.core :as mount]
|
||||||
[mount.core :as mount]
|
|
||||||
[clojure.tools.logging :as log]
|
[clojure.tools.logging :as log]
|
||||||
[commiteth.config :refer [env]]
|
[commiteth.config :refer [env]]
|
||||||
[clojure.data.json :as json]))
|
[commiteth.util.util :refer [json-api-request]]))
|
||||||
|
|
||||||
|
|
||||||
(defn fiat-api-provider []
|
(defn fiat-api-provider []
|
||||||
(env :fiat-api-provider :coinmarketcap))
|
(env :fiat-api-provider :coinmarketcap))
|
||||||
|
|
||||||
(defn json-api-request [url]
|
|
||||||
(->> (http/get url)
|
|
||||||
(:body)
|
|
||||||
(json/read-str)))
|
|
||||||
|
|
||||||
|
|
||||||
(defn get-token-usd-price-cryptonator
|
(defn get-token-usd-price-cryptonator
|
||||||
"Get current USD value for a token using cryptonator API"
|
"Get current USD value for a token using cryptonator API"
|
||||||
|
@ -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]
|
(defn eth-decimal->str [n]
|
||||||
@ -6,3 +9,8 @@
|
|||||||
|
|
||||||
(defn usd-decimal->str [n]
|
(defn usd-decimal->str [n]
|
||||||
(format "%.2f" n))
|
(format "%.2f" n))
|
||||||
|
|
||||||
|
(defn json-api-request [url]
|
||||||
|
(->> (http/get url)
|
||||||
|
(:body)
|
||||||
|
(json/read-str)))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user