-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
155 lines (126 loc) · 5.25 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from flask import Flask,request,render_template, redirect, url_for
import requests
app = Flask(__name__, template_folder='templates')
RAINFI_PATH = "https://api-v2.rain.fi"
NON_LENDING_DROPLET = 1
LENDING_DROPLET = 2
BORROWING_DROPLET = 3
@app.route('/droplets', methods = ['GET'])
def fetch_droplets():
pubkey = request.args.get('pubkey')
if pubkey == None:
return "Send proper pubkey"
token_list = _fetch_price_feed()
borrow_droplets = borrow_droplets_count(pubkey=pubkey, token_list= token_list)
lender_droplets = lender_droplets_count(pubkey=pubkey, token_list= token_list)
_active_loans = active_loans(pubkey=pubkey, token_list= token_list)
droplets = borrow_droplets + lender_droplets
result = {}
result['droplets'] = droplets
result['borrow_droplets'] = borrow_droplets
result[' lender_droplets'] = lender_droplets
result[' _active_loans'] = _active_loans if _active_loans!=None else None
return render_template('results.html', result=result)
def borrow_droplets_count(pubkey, token_list):
_borrow_droplets = 0
api_route = "/users/user-stats?owner="
api_url = RAINFI_PATH + api_route+ pubkey
headers = {"Content-Type": "application/json"}
payload = {}
response = requests.request("GET", api_url, headers=headers , data = payload)
res = response.json()
stats = res["rain"]
for i in stats:
if i in token_list["prices"]:
price = token_list["prices"][i]
decimals = token_list["decimals"][i]
_curr_volume = stats[i]["currentBorrowedVolume"]
curr_volume = _curr_volume/10**decimals
value = curr_volume * price
_borrow_droplets += float(round(value*BORROWING_DROPLET,1))
return _borrow_droplets
def lender_droplets_count(pubkey, token_list):
_lender_droplets = 0
api_route = "/pools/pool?pubkey="
pool_pubkey = _fetch_pool_pubkey(pubkey)
if pool_pubkey == None:
return 0
api_url = RAINFI_PATH + api_route+ pool_pubkey
headers = {"Content-Type": "application/json"}
payload = {}
response = requests.request("GET", api_url, headers=headers , data = payload)
res = response.json()
token_address = res["pool"]["currency"]
borrowed_amount = int(res["pool"]["borrowedAmount"])
available_amount = int(res["pool"]["availableAmount"])
price = token_list["prices"][token_address]
decimals = token_list["decimals"][token_address]
lending_droplets = (borrowed_amount/10**decimals) * price * LENDING_DROPLET
non_lending_droplets = (available_amount/10**decimals) * price * NON_LENDING_DROPLET
_lender_droplets = float(round(lending_droplets + non_lending_droplets,1))
return _lender_droplets
def active_loans(pubkey,token_list):
_active_loans = _fetch_active_loans(pubkey)
lst = []
for i in _active_loans:
d = {}
d["collateral"] = i[0]
d["collateral_amount"] = i[1]
collateral_price = token_list["prices"][i[0]]
d["collateral_value"] = i[1]*collateral_price
d["borrowed"] = i[2]
_borrow_amount = int(i[3])
_borrow_decimals = token_list["decimals"][i[2]]
d["borrowed_amount"] = _borrow_amount/10**_borrow_decimals
borrow_price = token_list["prices"][i[2]]
d["borrowed_value"] = d["borrowed_amount"] *borrow_price
d["daily_droplets"] = d["borrowed_value"]*BORROWING_DROPLET
lst.append(d)
return lst if len(lst)>0 else None
def _fetch_price_feed():
api_route = "/loans/price-feeds"
api_url = RAINFI_PATH + api_route
headers = {"Content-Type": "application/json"}
payload = {}
response = requests.request("GET", api_url, headers=headers , data = payload)
res = response.json()
return res
def _fetch_pool_pubkey(pubkey):
pool_pubkey = None
api_route = "/loans/activity-by-user?pubkey="
api_url = RAINFI_PATH + api_route+ pubkey
headers = {"Content-Type": "application/json"}
payload = {}
response = requests.request("GET", api_url, headers=headers , data = payload)
res = response.json()
stats = res["loans"]
for i in stats:
if i["lender"] == pubkey:
pool_pubkey = i["pool"]
return pool_pubkey if pool_pubkey else None
def _fetch_active_loans(pubkey):
api_route = "/loans/user?pubkey="
api_params = "&platform=Rain&status=Ongoing"
url = RAINFI_PATH + api_route + pubkey + api_params
headers = {"Content-Type": "application/json"}
payload = {}
response = requests.request("GET", url , headers = headers, data = payload)
res = response.json()
_active_loans = res["loans"]
loan_lst = []
for i in _active_loans:
collateral = i["mint"]
collateral_amount = i["collateralAmount"]
collateral_decimals = i["collateralDecimals"]
collateral_value = float(round(int(collateral_amount)/10**collateral_decimals,2))
borrowed = i["currency"]
borrowed_amount = i["amount"]
loan_id = i["pubkey"]
created_time = i["createdAt"]
expiry_time = i["expiredAt"]
_loan_list = [collateral,collateral_value,borrowed,borrowed_amount,created_time,expiry_time,loan_id]
loan_lst.append(_loan_list)
return loan_lst
@app.route("/")
def index():
return render_template("droplets.html")