Skip to content

Instantly share code, notes, and snippets.

@mariorz
Last active December 14, 2020 02:41
Show Gist options
  • Save mariorz/4701f0ea1c242346251b1e0f47d32534 to your computer and use it in GitHub Desktop.
Save mariorz/4701f0ea1c242346251b1e0f47d32534 to your computer and use it in GitHub Desktop.
(ns lpdashboard.compliqs
(:require [cljs.core.async :refer [go]]
[cljs.core.async.interop :refer-macros [<p!]]
[cljs-http.client :as http]
["ethers" :as ethers]
["@compound-finance/compound-js" :as compound]
[bignumber.core]
[bignumber.js :as BigNumber]
[clojure.string :as s]
[reagent.core :as reagent :refer [atom]]))
(defn token-borrow-underlying
[a token]
(let [token-state (first (filter #(= (:symbol %) token) (:tokens a)))]
(if token-state
(js/parseFloat (:value (:borrow_balance_underlying token-state)))
0.0)))
(defn token-supply-underlying
[a token]
(let [token-state (first (filter #(= (:symbol %) token) (:tokens a)))]
(if token-state
(js/parseFloat (:value (:supply_balance_underlying token-state)))
0.0)))
(defn acc-state
[a]
(let [a' (clojure.walk/keywordize-keys a)
dai-borrowed (token-borrow-underlying a' "cDAI")
dai-supplied (token-supply-underlying a' "cDAI")
usdc-borrowed (token-borrow-underlying a' "cUSDC")
usdc-supplied (token-supply-underlying a' "cUSDC")
usdt-borrowed (token-borrow-underlying a' "cUSDT")
usdt-supplied (token-supply-underlying a' "cUSDT")
eth-borrowed (token-borrow-underlying a' "cETH")
eth-supplied (token-supply-underlying a' "cETH")
sai-borrowed (token-borrow-underlying a' "cSAI")
sai-supplied (token-supply-underlying a' "cSAI")
bat-borrowed (token-borrow-underlying a' "cBAT")
bat-supplied (token-supply-underlying a' "cBAT")
usd-value-borrowed (+ dai-borrowed usdc-borrowed usdt-borrowed)
usd-value-supplied (+ dai-supplied usdc-supplied usdt-supplied)]
{:account (acc-address a)
:health (acc-health a)
:health-computed (/ (total-collateral-eth a) (total-borrow-eth a))
:total-borrow-eth (total-borrow-eth a)
:total-collateral-eth (total-collateral-eth a)
:block-number (block-number a)
:dai-borrowed dai-borrowed
:dai-supplied dai-supplied
:eth-borrowed eth-borrowed
:eth-supplied eth-supplied
:sai-borrowed sai-borrowed
:sai-supplied sai-supplied
:bat-borrowed bat-borrowed
:bat-supplied bat-supplied
:usdc-borrowed usdc-borrowed
:usdc-supplied usdc-supplied
:usdt-borrowed usdt-borrowed
:usdt-supplied usdt-supplied
:usd-value-borrowed usd-value-borrowed
:usd-value-supplied usd-value-supplied}))
(defn <account-states
[accounts block-number]
(go (let [resp (js->clj
(<p! (. compound/api
account
(clj->js {:addresses accounts
:page_size 1000
:block_number block-number}))))
account-resps (get resp "accounts")
account-resps' (map #(into % {"block-number" block-number})
account-resps)]
(map acc-state account-resps'))))
(defn dai-loop-factor
[account-state]
(let [supplied-dai (:dai-supplied account-state)
borrowed-dai (:dai-borrowed account-state)
supplied-usdc (:usdc-supplied account-state)
borrowed-usdc (:usdc-borrowed account-state)
;; no usdt as collteral
borrowed-usdt (:usdc-borrowed account-state)
supplied-usd (+ supplied-dai supplied-usdc)
borrowed-usd (+ borrowed-dai borrowed-usdc borrowed-usdt)
account (:account account-state)
factor (if (not= supplied-usd 0)
(if (> borrowed-usd supplied-usd)
(/ supplied-usd borrowed-usd)
(/ borrowed-usd supplied-usd))
0)]
{:account account
:borrowed-usd borrowed-usd
:supplied-usd supplied-usd
:borrowed-dai borrowed-dai
:loop-factor factor}))
(comment
(go (def liquidated-accounts
(let [response (<! (http/get (str "https://gist.githubusercontent.com/"
"mariorz/d682fa88f8c6437df9429f926cf12da0/"
"raw/d232f37be93f3eca8a99d619cd49233566b9e8a8/"
"compound-liquidated-accounts")
{:with-credentials? false
:query-params {}}))]
(clojure.string/split
(:body response)
"\n"))))
(go (def nov24-states
(clojure.walk/keywordize-keys
(<! (<account-states liquidated-accounts 11321862)))))
(def accounts-usd-factors
(->> (map dai-loop-factor nov24-states)
(sort-by :loop-factor)
(reverse)))
(def data-csv
(map (fn [x] (str (:account x) ","
(:supplied-usd x) ","
(:borrowed-usd x) ","
(:borrowed-dai x) ","
(:loop-factor x)))
accounts-usd-factors)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment