-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdatescript.py
More file actions
241 lines (187 loc) · 9.02 KB
/
updatescript.py
File metadata and controls
241 lines (187 loc) · 9.02 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
from pymongo import MongoClient
import requests
from datetime import datetime, timedelta
import os
from dotenv import load_dotenv
from pprint import pprint
import time
print ("\nScript started\n")
load_dotenv()
ogclient = MongoClient(os.getenv("OG_CLIENT_URI"))
newclient = MongoClient(os.getenv("NEW_CLIENT_URI"))
dot_history_url = "https://api.coingecko.com/api/v3/coins/polkadot/history?date="
ksm_history_url = "https://api.coingecko.com/api/v3/coins/kusama/history?date="
dot_ticker_url = "https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=polkadot"
ksm_ticker_url = "https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=kusama"
ys_kusama_url = "https://api-yieldscan.onrender.com/api/kusama/transactions/stats"
ys_polkadot_url = "https://api-yieldscan.onrender.com/api/polkadot/transactions/stats"
ogdb = ogclient.test
newdb = newclient.suryansh
def update_coin_prices(lastUpdated, current_time):
datetoday = datetime(current_time.year, current_time.month, current_time.day)
date = datetime(lastUpdated.year, lastUpdated.month, lastUpdated.day)
while date <= current_time:
nextday = date + timedelta(days=1)
query = { "$and": [{ "date": { "$gte": date }}, { "date": { "$lt": nextday }}]}
alreadypresentdot = newdb.dotPriceInUSD.find_one(query)
if alreadypresentdot is None:
if date < datetoday:
dotdata = requests.get(f"{dot_history_url}{date.day}-{date.month}-{date.year}").json()
dotprice = dotdata["market_data"]["current_price"]["usd"]
else:
dotprice = requests.get(dot_ticker_url).json()[0]["current_price"]
result = newdb.dotPriceInUSD.insert_one({"date": date, "price": dotprice})
alreadypresentksm = newdb.ksmPriceInUSD.find_one(query)
if alreadypresentksm is None:
if date < datetoday:
ksmdata = requests.get(f"{ksm_history_url}{date.day}-{date.month}-{date.year}").json()
ksmprice = ksmdata["market_data"]["current_price"]["usd"]
else:
ksmprice = requests.get(ksm_ticker_url).json()[0]["current_price"]
result = newdb.ksmPriceInUSD.insert_one({"date": date, "price": ksmprice})
date = nextday
def update_transaction_data(lastUpdated):
ogpolkadottransactiondatas = ogdb.polkadottransactiondatas.find({"createdAt": {"$gt": lastUpdated}})
for tx in ogpolkadottransactiondatas:
if newdb.polkadottransactiondatas.find_one({"_id": tx["_id"]}) is None:
date = datetime(tx["createdAt"].year, tx["createdAt"].month, tx["createdAt"].day)
nextday = date + timedelta(days=1)
query = { "$and": [{ "date": { "$gte": date }}, { "date": { "$lt": nextday }}]}
dotprice = newdb.dotPriceInUSD.find_one(query)["price"]
if tx["successful"] == True:
stake = tx["stake"]
alreadyBonded = tx["alreadyBonded"]
if stake == alreadyBonded:
dollarvalue = stake*dotprice
else:
dollarvalue = abs(stake-alreadyBonded)*dotprice
else:
dollarvalue = 0
txdict = tx
txdict["dotPriceInUSD"] = dotprice
txdict["txDollarValue"] = dollarvalue
result=newdb.polkadottransactiondatas.insert_one(txdict)
ogkusamatransactiondatas = ogdb.kusamatransactiondatas.find({"createdAt": {"$gt": lastUpdated}})
for tx in ogkusamatransactiondatas:
if newdb.kusamatransactiondatas.find_one({"_id": tx["_id"]}) is None:
date = datetime(tx["createdAt"].year, tx["createdAt"].month, tx["createdAt"].day)
nextday = date + timedelta(days=1)
query = { "$and": [{ "date": { "$gte": date }}, { "date": { "$lt": nextday }}]}
ksmprice = newdb.ksmPriceInUSD.find_one(query)["price"]
if tx["successful"] == True:
stake = tx["stake"]
alreadyBonded = tx["alreadyBonded"]
if stake == alreadyBonded:
dollarvalue = stake*ksmprice
else:
dollarvalue = abs(stake-alreadyBonded)*ksmprice
else:
dollarvalue = 0
txdict = tx
txdict["ksmPriceInUSD"] = ksmprice
txdict["txDollarValue"] = dollarvalue
result=newdb.kusamatransactiondatas.insert_one(txdict)
def update_stats(current_time):
date = datetime(current_time.year, current_time.month, current_time.day)
nextday = date + timedelta(days=1)
query = { "$and": [{ "date": { "$gte": date }}, { "date": { "$lt": nextday }}]}
alreadypresentdotstat = newdb.polkadotstats.find_one(query)
if alreadypresentdotstat is None:
alreadypresentdotstat = newdb.polkadotstats.find()
ys_polka_stats = requests.get(ys_polkadot_url).json()
polkadotTotalAmountCurrentlyManaged = ys_polka_stats["totalAmountCurrentlyManaged"]
ogpolkastats = ogdb.polkadotnominatorstats.find()
polkadotPercentAmountCaptured = (polkadotTotalAmountCurrentlyManaged*100)/ogpolkastats[0]["totalAmountStaked"]
data = {
"_id": alreadypresentdotstat[0]["_id"],
"date": current_time,
"totalAmountCurrentlyManaged": polkadotTotalAmountCurrentlyManaged,
"totalAmountStaked": ogpolkastats[0]["totalAmountStaked"],
"percentMarketCaptured": polkadotPercentAmountCaptured
}
result = newdb.polkadotstats.update_one({"_id": alreadypresentdotstat[0]["_id"]}, {"$set": data})
alreadypresentksmstat = newdb.kusamastats.find_one(query)
if alreadypresentksmstat is None:
alreadypresentksmstat = newdb.kusamastats.find()
ys_kusama_stats = requests.get(ys_kusama_url).json()
kusamaTotalAmountCurrentlyManaged = ys_kusama_stats["totalAmountCurrentlyManaged"]
ogkusamastats = ogdb.kusamanominatorstats.find()
kusamaPercentAmountCaptured = (kusamaTotalAmountCurrentlyManaged*100)/ogkusamastats[0]["totalAmountStaked"]
data = {
"_id": alreadypresentksmstat[0]["_id"],
"date": current_time,
"totalAmountCurrentlyManaged": kusamaTotalAmountCurrentlyManaged,
"totalAmountStaked": ogkusamastats[0]["totalAmountStaked"],
"percentMarketCaptured": kusamaPercentAmountCaptured
}
result = newdb.kusamastats.update_one({"_id": alreadypresentksmstat[0]["_id"]}, {"$set": data})
def update_last_updated(current_time):
id_last_update = newdb.lastUpdated.find()[0]["_id"]
result = newdb.lastUpdated.update_one({"_id": id_last_update}, {"$set": {"lastUpdated": current_time}})
def update_nominations_updated_data(lastUpdated):
ogpolkadottransactiondatas = ogdb.polkadottransactiondatas.find({"createdAt": {"$gt": lastUpdated}})
for tx in ogpolkadottransactiondatas:
if tx["successful"] == True:
stake = tx["stake"]
alreadyBonded = tx["alreadyBonded"]
txdict=tx
if stake == alreadyBonded:
txdict["volume"] = stake
if newdb.polkadotnominationsupadteddata.find_one({"_id": txdict["_id"]}) is None:
result=newdb.polkadotnominationsupadteddata.insert_one(txdict)
elif stake > alreadyBonded:
txdict["volume"] = stake-alreadyBonded
if newdb.polkadotamountstakeddata.find_one({"_id": txdict["_id"]}) is None:
result=newdb.polkadotamountstakeddata.insert_one(txdict)
if stake > alreadyBonded or stake < alreadyBonded:
if newdb.polkadotAUMdata.find_one({"stashId": txdict["stashId"]}) is None:
txdict["volume"] = stake
else:
txdict["volume"] = stake-alreadyBonded
if newdb.polkadotAUMdata.find_one({"_id": txdict["_id"]}) is None:
result = newdb.polkadotAUMdata.insert_one(txdict)
elif stake == alreadyBonded:
if newdb.polkadotAUMdata.find_one({"stashId": tx["stashId"]}) is None:
txdict["volume"] = stake
if newdb.polkadotAUMdata.find_one({"_id": txdict["_id"]}) is None:
result = newdb.polkadotAUMdata.insert_one(txdict)
ogkusamatransactiondatas = ogdb.kusamatransactiondatas.find({"createdAt": {"$gt": lastUpdated}})
for tx in ogkusamatransactiondatas:
if tx["successful"] == True:
stake = tx["stake"]
alreadyBonded = tx["alreadyBonded"]
txdict=tx
if stake == alreadyBonded:
txdict["volume"] = stake
if newdb.kusamanominationsupadteddata.find_one({"_id": txdict["_id"]}) is None:
result=newdb.kusamanominationsupadteddata.insert_one(txdict)
elif stake > alreadyBonded:
txdict["volume"] = stake-alreadyBonded
if newdb.kusamanominationsupadteddata.find_one({"_id": txdict["_id"]}) is None:
result=newdb.kusamanominationsupadteddata.insert_one(txdict)
if stake > alreadyBonded or stake < alreadyBonded:
if newdb.kusamaAUMdata.find_one({"stashId": tx["stashId"]}) is None:
txdict["volume"] = stake
else:
txdict["volume"] = stake-alreadyBonded
if newdb.kusamaAUMdata.find_one({"_id": txdict["_id"]}) is None:
result = newdb.kusamaAUMdata.insert_one(txdict)
elif stake == alreadyBonded:
if newdb.kusamaAUMdata.find_one({"stashId": tx["stashId"]}) is None:
txdict["volume"] = stake
if newdb.kusamaAUMdata.find_one({"_id": txdict["_id"]}) is None:
result = newdb.kusamaAUMdata.insert_one(txdict)
#driver code
lastUpdated = newdb.lastUpdated.find()[0]["lastUpdated"]
current_time = datetime.now()
update_coin_prices(lastUpdated, current_time)
print("Step 1/5: update_coin_prices completed!")
update_transaction_data(lastUpdated)
print("Step 2/5: update_transaction_data completed!")
update_stats(current_time)
print("Step 3/5: update_stats completed!")
update_last_updated(current_time)
print("Step 4/5: update_last_updated completed!")
update_nominations_updated_data(lastUpdated)
print("Step 5/5: update_nominations_updated_data completed!")
print("\nScript completed at " + str(datetime.now()))