Created
May 19, 2018 18:01
-
-
Save jpmonettas/ba37c9903b843e9dd5331d2f50b689d3 to your computer and use it in GitHub Desktop.
Dirty Clojurescript script to retrieve ethereum wallets balances, for using with conky etc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/home/jmonetta/bin/lumo | |
(require '[clojure.string :as str]) | |
(require '[goog.string.format]) | |
(def http (js/require "http")) | |
(def addresses {"0xa662c033F936D0e950772aE3934D4f7EA5DCF934" "wage" | |
"0x07906afa8Af02c8739ef59999769533e2a07426A" "reimb"}) | |
;; Etherscan APIkey | |
(def api-key "") | |
(defn grab-price [accounts] | |
(.get http (str "http://api.etherscan.io/api?module=stats&action=ethprice&apikey=" | |
api-key) | |
(fn [response] | |
(let [body-ref (atom "")] | |
(.on response "data" (fn [chunk] (swap! body-ref str chunk))) | |
(.on response "end" (fn [] | |
(let [{:keys [ethusd]} (-> @body-ref | |
js/JSON.parse | |
(js->clj :keywordize-keys true) | |
:result)] | |
(println "Ether Price:" ethusd "US$\n") | |
(doseq [{:keys [account balance]} accounts] | |
(let [eth-balance (/ balance 1e18)] | |
(println "\t" (get addresses account) | |
(goog.string/format "%.3f" eth-balance) | |
"ETH (" | |
(goog.string/format "%.3f" (* eth-balance ethusd)) | |
" US$ )")))))))))) | |
(.get http (str "http://api.etherscan.io/api?module=account&action=balancemulti&address=" | |
(str/join "," (keys addresses)) | |
"&tag=latest&apikey=" | |
api-key) | |
(fn [response] | |
(let [body-ref (atom "")] | |
(.on response "data" (fn [chunk] (swap! body-ref str chunk))) | |
(.on response "end" (fn [] | |
(grab-price (-> @body-ref | |
js/JSON.parse | |
(js->clj :keywordize-keys true) | |
:result))))))) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Will output something like : ;; | |
;; ;; | |
;; Ether Price: 700.84 US$ ;; | |
;; ;; | |
;; wage 14.300 ETH ( 10022.012 US$ ) ;; | |
;; reimb 0.182 ETH ( 127.285 US$ ) ;; | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment